Skip to content

Commit 83d95d4

Browse files
committed
don't sync on terminating and system namespaces
1 parent 175ac1b commit 83d95d4

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

pkg/psalabelsyncer/podsecurity_label_sync_controller.go

+23-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package psalabelsyncer
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
corev1 "k8s.io/api/core/v1"
89
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -125,7 +126,7 @@ func NewPodSecurityAdmissionLabelSynchronizationController(
125126
if ns.Annotations == nil || len(ns.Annotations[securityv1.UIDRangeAnnotation]) == 0 {
126127
return false
127128
}
128-
return true
129+
return checkNSControlled(ns)
129130
},
130131
namespaceInformer.Informer(),
131132
).
@@ -148,6 +149,11 @@ func (c *PodSecurityAdmissionLabelSynchronizationController) sync(ctx context.Co
148149
return fmt.Errorf(errFmt, qKey, err)
149150
}
150151

152+
if ns.Status.Phase == corev1.NamespaceTerminating {
153+
klog.Infof("skipping synchronizing namespace %q because it is terminating", ns.Name)
154+
return nil
155+
}
156+
151157
if err := c.syncNamespace(ctx, controllerContext, ns); err != nil {
152158
return fmt.Errorf(errFmt, qKey, err)
153159
}
@@ -304,17 +310,28 @@ func (c *PodSecurityAdmissionLabelSynchronizationController) saToSCCCAcheEnqueue
304310
}
305311
}
306312

307-
func (c *PodSecurityAdmissionLabelSynchronizationController) checkNSControlled(ns string) (bool, error) {
308-
nsObj, err := c.namespaceLister.Get(ns)
313+
func (c *PodSecurityAdmissionLabelSynchronizationController) checkNSControlled(nsName string) (bool, error) {
314+
ns, err := c.namespaceLister.Get(nsName)
309315
if err != nil {
310316
return false, err
311317
}
312318

313-
if nsObj.Labels[labelSyncControlLabel] != "false" {
314-
return true, nil
319+
return checkNSControlled(ns), nil
320+
321+
}
322+
323+
func checkNSControlled(ns *corev1.Namespace) bool {
324+
nsName := ns.Name
325+
isSystemNS := strings.HasPrefix(nsName, "openshift-") || nsName == "openshift" || nsName == "kube-system"
326+
if isSystemNS {
327+
return false
328+
}
329+
330+
if ns.Labels[labelSyncControlLabel] != "false" {
331+
return true
315332
}
316333

317-
return false, nil
334+
return false
318335
}
319336

320337
// controlledNamespacesLabelSelector returns label selector to be used with the

0 commit comments

Comments
 (0)