Skip to content

Commit 893dc07

Browse files
committed
podsecurityreadinesscontroller: refactor, return test PSS, fix err ret
The refactoring is necessary to split the condition into condition and classification as the classification is quite tricky. If we can't determine the PSS, we shouldn' error out, but default to global config.
1 parent 222cd59 commit 893dc07

File tree

3 files changed

+24
-33
lines changed

3 files changed

+24
-33
lines changed

pkg/operator/podsecurityreadinesscontroller/podsecurityreadinesscontroller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (c *PodSecurityReadinessController) sync(ctx context.Context, syncCtx facto
7171
conditions := podSecurityOperatorConditions{}
7272
for _, ns := range nsList.Items {
7373
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
74-
isViolating, err := c.isNamespaceViolating(ctx, &ns)
74+
isViolating, _, err := c.isNamespaceViolating(ctx, &ns)
7575
if apierrors.IsNotFound(err) {
7676
return nil
7777
}

pkg/operator/podsecurityreadinesscontroller/violation.go

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package podsecurityreadinesscontroller
22

33
import (
44
"context"
5-
"fmt"
65

76
securityv1 "github.com/openshift/api/security/v1"
87
corev1 "k8s.io/api/core/v1"
@@ -21,17 +20,16 @@ var (
2120
alertLabels = sets.New(psapi.WarnLevelLabel, psapi.AuditLevelLabel)
2221
)
2322

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) {
2527
nsApplyConfig, err := applyconfiguration.ExtractNamespace(ns, syncerControllerName)
2628
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
3330
}
3431

32+
enforceLabel := determineEnforceLabelForNamespace(nsApplyConfig)
3533
nsApply := applyconfiguration.Namespace(ns.Name).WithLabels(map[string]string{
3634
psapi.EnforceLevelLabel: enforceLabel,
3735
})
@@ -43,41 +41,34 @@ func (c *PodSecurityReadinessController) isNamespaceViolating(ctx context.Contex
4341
FieldManager: "pod-security-readiness-controller",
4442
})
4543
if err != nil {
46-
return false, err
44+
return false, "", err
4745
}
4846

4947
// 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
5154
}
5255

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 {
5558
// This should generally exist and will be the only supported method of determining
5659
// the enforce level going forward - however, we're keeping the label fallback for
5760
// now to account for any workloads not yet annotated using a new enough version of
5861
// the syncer, such as during upgrade scenarios.
59-
return label, nil
62+
return ns.Annotations[securityv1.MinimallySufficientPodSecurityStandard]
6063
}
6164

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
6770
}
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-
}
7471

75-
return pickStrictest(viableLabels), nil
76-
}
77-
78-
func pickStrictest(viableLabels map[string]string) string {
79-
targetLevel := ""
80-
for label, value := range viableLabels {
8172
level, err := psapi.ParseLevel(value)
8273
if err != nil {
8374
klog.V(4).InfoS("invalid level", "label", label, "value", value)

pkg/operator/podsecurityreadinesscontroller/violation_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestIsNamespaceViolating(t *testing.T) {
8888
return &mockKubeClientWithResponse{}
8989
},
9090
expectViolating: false,
91-
expectError: true,
91+
expectError: false,
9292
},
9393
{
9494
name: "Apply returns error",
@@ -124,7 +124,7 @@ func TestIsNamespaceViolating(t *testing.T) {
124124

125125
tc.namespace.ManagedFields = managedFields
126126

127-
violating, err := controller.isNamespaceViolating(context.Background(), tc.namespace)
127+
violating, _, err := controller.isNamespaceViolating(context.Background(), tc.namespace)
128128

129129
if (err != nil) != tc.expectError {
130130
t.Errorf("isNamespaceViolating() error = %v, expectError %v", err, tc.expectError)

0 commit comments

Comments
 (0)