forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.go
59 lines (48 loc) · 2.14 KB
/
cluster.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
package podsecuritypolicy
import (
v12 "github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/extensions/v1beta1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
)
type clusterManager struct {
clusterLister v3.ClusterLister
clusters v3.ClusterInterface
templateLister v3.PodSecurityPolicyTemplateLister
policyLister v1beta1.PodSecurityPolicyLister
policies v1beta1.PodSecurityPolicyInterface
serviceAccountLister v12.ServiceAccountLister
serviceAccountsController v12.ServiceAccountController
}
// RegisterCluster updates the pod security policy if the pod security policy template default for this cluster has been
// updated, then resyncs all service accounts in this namespace.
func RegisterCluster(context *config.UserContext) {
logrus.Infof("registering podsecuritypolicy cluster handler for cluster %v", context.ClusterName)
m := &clusterManager{
clusters: context.Management.Management.Clusters(""),
policies: context.Extensions.PodSecurityPolicies(""),
clusterLister: context.Management.Management.Clusters("").Controller().Lister(),
templateLister: context.Management.Management.PodSecurityPolicyTemplates("").Controller().Lister(),
policyLister: context.Extensions.PodSecurityPolicies("").Controller().Lister(),
serviceAccountLister: context.Core.ServiceAccounts("").Controller().Lister(),
serviceAccountsController: context.Core.ServiceAccounts("").Controller(),
}
context.Management.Management.Clusters("").AddHandler("ClusterSyncHandler", m.sync)
}
func (m *clusterManager) sync(key string, obj *v3.Cluster) error {
if obj == nil {
// Nothing to do
return nil
}
id := obj.Spec.DefaultPodSecurityPolicyTemplateName
if id == "" {
logrus.Debugf("No pod security policy template found for cluster %v", obj.Name)
return nil
}
err := updatePolicyIfOutdated(m.templateLister, m.policies, m.policyLister, id)
if err != nil {
return err
}
return resyncServiceAccounts(m.serviceAccountLister, m.serviceAccountsController, "")
}