@@ -17,6 +17,7 @@ import (
17
17
corev1listers "k8s.io/client-go/listers/core/v1"
18
18
rbacv1listers "k8s.io/client-go/listers/rbac/v1"
19
19
"k8s.io/client-go/tools/cache"
20
+ "k8s.io/client-go/util/workqueue"
20
21
"k8s.io/klog/v2"
21
22
psapi "k8s.io/pod-security-admission/api"
22
23
@@ -50,6 +51,7 @@ type PodSecurityAdmissionLabelSynchronizationController struct {
50
51
51
52
nsLabelSelector labels.Selector
52
53
54
+ workQueue workqueue.RateLimitingInterface
53
55
saToSCCsCache * SAToSCCCache
54
56
}
55
57
@@ -80,6 +82,7 @@ func NewPodSecurityAdmissionLabelSynchronizationController(
80
82
return nil , err
81
83
}
82
84
85
+ syncCtx := factory .NewSyncContext (controllerName , eventRecorder .WithComponentSuffix (controllerName ))
83
86
c := & PodSecurityAdmissionLabelSynchronizationController {
84
87
namespaceClient : namespaceClient ,
85
88
@@ -93,25 +96,22 @@ func NewPodSecurityAdmissionLabelSynchronizationController(
93
96
94
97
nsLabelSelector : controlledNamespacesLabelSelector ,
95
98
96
- saToSCCsCache : NewSAToSCCCache ( rbacInformers , sccInformer ),
99
+ workQueue : syncCtx . Queue ( ),
97
100
}
98
101
102
+ saToSCCCache := NewSAToSCCCache (rbacInformers , sccInformer ).WithExternalQueueEnqueue (c .saToSCCCAcheEnqueueFunc )
103
+
104
+ c .saToSCCsCache = saToSCCCache
105
+
99
106
return factory .New ().
100
107
WithSync (c .sync ).
108
+ WithSyncContext (syncCtx ).
101
109
WithFilteredEventsInformersQueueKeysFunc (
102
110
c .queueKeysForObj ,
103
111
c .saToSCCsCache .IsRoleBindingRelevant ,
104
112
rbacInformers .RoleBindings ().Informer (),
105
113
rbacInformers .ClusterRoleBindings ().Informer (),
106
114
).
107
- WithFilteredEventsInformersQueueKeysFunc (
108
- c .queueKeysForObj ,
109
- func (obj interface {}) bool {
110
- return c .saToSCCsCache .IsRoleInvolvesSCCs (obj , true )
111
- },
112
- rbacInformers .Roles ().Informer (),
113
- rbacInformers .ClusterRoles ().Informer (),
114
- ).
115
115
WithFilteredEventsInformersQueueKeysFunc (
116
116
nameToKey ,
117
117
func (obj interface {}) bool {
@@ -133,19 +133,6 @@ func NewPodSecurityAdmissionLabelSynchronizationController(
133
133
c .queueKeysForObj ,
134
134
serviceAccountInformer .Informer (),
135
135
).
136
- WithInformersQueueKeysFunc (
137
- func (o runtime.Object ) []string {
138
- // we need to reinitialize the SCC cache as the roles might be now granting
139
- // access to new SCCs, or won't grant access to removed SCCs
140
- err := c .saToSCCsCache .ReinitializeRoleCache ()
141
- if err != nil {
142
- klog .Errorf ("failed to reinitialize role cache: %v" , err )
143
- return nil
144
- }
145
- return c .allWatchedNamespacesAsQueueKeys (o )
146
- },
147
- sccInformer .Informer (),
148
- ).
149
136
ToController (
150
137
controllerName ,
151
138
eventRecorder .WithComponentSuffix (controllerName ),
@@ -273,6 +260,39 @@ func (c *PodSecurityAdmissionLabelSynchronizationController) allWatchedNamespace
273
260
return qKeys
274
261
}
275
262
263
+ func (c * PodSecurityAdmissionLabelSynchronizationController ) saToSCCCAcheEnqueueFunc (obj interface {}) {
264
+ enqeueAllNS := func () {
265
+ // NS was zero-len, we need to enqueue all controlled namespaces
266
+ nsList , err := c .namespaceLister .List (c .nsLabelSelector )
267
+ if err != nil {
268
+ klog .Errorf ("failed to enqueue all namespaces: %v" , err )
269
+ return
270
+ }
271
+ for _ , ns := range nsList {
272
+ c .workQueue .Add (ns .Name )
273
+ }
274
+ }
275
+
276
+ // TODO: maybe just allow setting 2 separate enqueuers for roles and SCCs?
277
+ switch t := obj .(type ) {
278
+ case roleInterface : // TODO: should be exported
279
+ if nsName := t .Namespace (); len (nsName ) > 0 {
280
+ ns , err := c .namespaceLister .Get (nsName )
281
+ if err != nil {
282
+ klog .Errorf ("failed to enqueue namespace %q: %v" , nsName , err )
283
+ return
284
+ }
285
+ if ns .Labels [labelSyncControlLabel ] != "false" {
286
+ c .workQueue .Add (nsName )
287
+ }
288
+ return
289
+ }
290
+ enqeueAllNS ()
291
+ case * securityv1.SecurityContextConstraints :
292
+ enqeueAllNS ()
293
+ }
294
+ }
295
+
276
296
// controlledNamespacesLabelSelector returns label selector to be used with the
277
297
// PodSecurityAdmissionLabelSynchronizationController.
278
298
func controlledNamespacesLabelSelector () (labels.Selector , error ) {
0 commit comments