@@ -2,7 +2,6 @@ package podsecurityreadinesscontroller
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
7
6
securityv1 "github.com/openshift/api/security/v1"
8
7
corev1 "k8s.io/api/core/v1"
@@ -21,17 +20,16 @@ var (
21
20
alertLabels = sets .New (psapi .WarnLevelLabel , psapi .AuditLevelLabel )
22
21
)
23
22
24
- func (c * PodSecurityReadinessController ) isNamespaceViolating (ctx context.Context , ns * corev1.Namespace ) (bool , error ) {
23
+ // isNamespaceViolating checks if a namespace is ready for Pod Security Admission enforcement.
24
+ // It returns true if the namespace is violating the Pod Security Admission policy, along with
25
+ // the enforce label it was tested against.
26
+ func (c * PodSecurityReadinessController ) isNamespaceViolating (ctx context.Context , ns * corev1.Namespace ) (bool , string , error ) {
25
27
nsApplyConfig , err := applyconfiguration .ExtractNamespace (ns , syncerControllerName )
26
28
if err != nil {
27
- return false , err
28
- }
29
-
30
- enforceLabel , err := determineEnforceLabelForNamespace (nsApplyConfig )
31
- if err != nil {
32
- return false , err
29
+ return false , "" , err
33
30
}
34
31
32
+ enforceLabel := determineEnforceLabelForNamespace (nsApplyConfig )
35
33
nsApply := applyconfiguration .Namespace (ns .Name ).WithLabels (map [string ]string {
36
34
psapi .EnforceLevelLabel : enforceLabel ,
37
35
})
@@ -43,41 +41,34 @@ func (c *PodSecurityReadinessController) isNamespaceViolating(ctx context.Contex
43
41
FieldManager : "pod-security-readiness-controller" ,
44
42
})
45
43
if err != nil {
46
- return false , err
44
+ return false , "" , err
47
45
}
48
46
49
47
// If there are warnings, the namespace is violating.
50
- return len (c .warningsHandler .PopAll ()) > 0 , nil
48
+ warnings := c .warningsHandler .PopAll ()
49
+ if len (warnings ) > 0 {
50
+ return true , enforceLabel , nil
51
+ }
52
+
53
+ return false , "" , nil
51
54
}
52
55
53
- func determineEnforceLabelForNamespace (ns * applyconfiguration.NamespaceApplyConfiguration ) ( string , error ) {
54
- if label , ok := ns .Annotations [securityv1 .MinimallySufficientPodSecurityStandard ]; ok {
56
+ func determineEnforceLabelForNamespace (ns * applyconfiguration.NamespaceApplyConfiguration ) string {
57
+ if _ , ok := ns .Annotations [securityv1 .MinimallySufficientPodSecurityStandard ]; ok {
55
58
// This should generally exist and will be the only supported method of determining
56
59
// the enforce level going forward - however, we're keeping the label fallback for
57
60
// now to account for any workloads not yet annotated using a new enough version of
58
61
// the syncer, such as during upgrade scenarios.
59
- return label , nil
62
+ return ns . Annotations [ securityv1 . MinimallySufficientPodSecurityStandard ]
60
63
}
61
64
62
- viableLabels := map [ string ] string {}
63
-
64
- for alertLabel := range alertLabels {
65
- if value , ok := ns . Labels [ alertLabel ]; ok {
66
- viableLabels [ alertLabel ] = value
65
+ targetLevel := ""
66
+ for label := range alertLabels {
67
+ value , ok := ns . Labels [ label ]
68
+ if ! ok {
69
+ continue
67
70
}
68
- }
69
-
70
- if len (viableLabels ) == 0 {
71
- // If there are no labels/annotations managed by the syncer, we can't make a decision.
72
- return "" , fmt .Errorf ("unable to determine if the namespace is violating because no appropriate labels or annotations were found" )
73
- }
74
71
75
- return pickStrictest (viableLabels ), nil
76
- }
77
-
78
- func pickStrictest (viableLabels map [string ]string ) string {
79
- targetLevel := ""
80
- for label , value := range viableLabels {
81
72
level , err := psapi .ParseLevel (value )
82
73
if err != nil {
83
74
klog .V (4 ).InfoS ("invalid level" , "label" , label , "value" , value )
0 commit comments