Skip to content

Commit f69c842

Browse files
committed
wip: address comments
1 parent eef8d30 commit f69c842

File tree

2 files changed

+221
-66
lines changed

2 files changed

+221
-66
lines changed

pkg/psalabelsyncer/podsecurity_label_sync_controller.go

+42-22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
corev1listers "k8s.io/client-go/listers/core/v1"
1818
rbacv1listers "k8s.io/client-go/listers/rbac/v1"
1919
"k8s.io/client-go/tools/cache"
20+
"k8s.io/client-go/util/workqueue"
2021
"k8s.io/klog/v2"
2122
psapi "k8s.io/pod-security-admission/api"
2223

@@ -50,6 +51,7 @@ type PodSecurityAdmissionLabelSynchronizationController struct {
5051

5152
nsLabelSelector labels.Selector
5253

54+
workQueue workqueue.RateLimitingInterface
5355
saToSCCsCache *SAToSCCCache
5456
}
5557

@@ -80,6 +82,7 @@ func NewPodSecurityAdmissionLabelSynchronizationController(
8082
return nil, err
8183
}
8284

85+
syncCtx := factory.NewSyncContext(controllerName, eventRecorder.WithComponentSuffix(controllerName))
8386
c := &PodSecurityAdmissionLabelSynchronizationController{
8487
namespaceClient: namespaceClient,
8588

@@ -93,25 +96,22 @@ func NewPodSecurityAdmissionLabelSynchronizationController(
9396

9497
nsLabelSelector: controlledNamespacesLabelSelector,
9598

96-
saToSCCsCache: NewSAToSCCCache(rbacInformers, sccInformer),
99+
workQueue: syncCtx.Queue(),
97100
}
98101

102+
saToSCCCache := NewSAToSCCCache(rbacInformers, sccInformer).WithExternalQueueEnqueue(c.saToSCCCAcheEnqueueFunc)
103+
104+
c.saToSCCsCache = saToSCCCache
105+
99106
return factory.New().
100107
WithSync(c.sync).
108+
WithSyncContext(syncCtx).
101109
WithFilteredEventsInformersQueueKeysFunc(
102110
c.queueKeysForObj,
103111
c.saToSCCsCache.IsRoleBindingRelevant,
104112
rbacInformers.RoleBindings().Informer(),
105113
rbacInformers.ClusterRoleBindings().Informer(),
106114
).
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-
).
115115
WithFilteredEventsInformersQueueKeysFunc(
116116
nameToKey,
117117
func(obj interface{}) bool {
@@ -133,19 +133,6 @@ func NewPodSecurityAdmissionLabelSynchronizationController(
133133
c.queueKeysForObj,
134134
serviceAccountInformer.Informer(),
135135
).
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-
).
149136
ToController(
150137
controllerName,
151138
eventRecorder.WithComponentSuffix(controllerName),
@@ -273,6 +260,39 @@ func (c *PodSecurityAdmissionLabelSynchronizationController) allWatchedNamespace
273260
return qKeys
274261
}
275262

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+
276296
// controlledNamespacesLabelSelector returns label selector to be used with the
277297
// PodSecurityAdmissionLabelSynchronizationController.
278298
func controlledNamespacesLabelSelector() (labels.Selector, error) {

0 commit comments

Comments
 (0)