-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
62 lines (56 loc) · 1.83 KB
/
controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package cloudcredential
import (
"context"
"fmt"
"github.com/rancher/rancher/pkg/controllers/management/globalnamespacerbac"
corev1 "github.com/rancher/types/apis/core/v1"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"strings"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
)
type Controller struct {
cloudCredentials corev1.SecretInterface
managementContext *config.ManagementContext
}
func Register(ctx context.Context, management *config.ManagementContext) {
cloudCredentials := management.Core.Secrets("")
m := Controller{
cloudCredentials: cloudCredentials,
managementContext: management,
}
m.cloudCredentials.AddHandler(ctx, "management-cloudcredential-controller", m.ccSync)
}
func (n *Controller) ccSync(key string, cloudCredential *v1.Secret) (runtime.Object, error) {
if cloudCredential == nil || cloudCredential.DeletionTimestamp != nil {
return cloudCredential, nil
}
if !configExists(cloudCredential.Data) {
return cloudCredential, nil
}
metaAccessor, err := meta.Accessor(cloudCredential)
if err != nil {
return cloudCredential, err
}
creatorID, ok := metaAccessor.GetAnnotations()[globalnamespacerbac.CreatorIDAnn]
if !ok {
return cloudCredential, fmt.Errorf("cloud credential %v has no creatorId annotation", cloudCredential.Name)
}
if err := globalnamespacerbac.CreateRoleAndRoleBinding(
globalnamespacerbac.CloudCredentialResource, cloudCredential.Name, cloudCredential.UID, []v3.Member{}, creatorID,
n.managementContext, "*"); err != nil {
return nil, err
}
return cloudCredential, nil
}
func configExists(data map[string][]byte) bool {
for key := range data {
splitKey := strings.Split(key, "-")
if len(splitKey) == 2 && strings.HasSuffix(splitKey[0], "Config") {
return true
}
}
return false
}