Skip to content

Commit 2f9ad36

Browse files
Merge pull request #404 from tnierman/SREP-1770
SREP-1770 - Allow nvidia-gpu-operator to label unprotected namespaces with `openshift.io/cluster-monitoring: "true"`
2 parents a6212aa + 65d220f commit 2f9ad36

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

pkg/webhooks/namespace/namespace.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ var (
4949
removableProtectedLabels = []string{
5050
"openshift.io/cluster-monitoring",
5151
}
52+
// https://issues.redhat.com/browse/SREP-1770 - nvidia-gpu-operator should be allowed to label namespaces
53+
labelUserExceptions = []string{"system:serviceaccount:nvidia-gpu-operator:gpu-operator"}
5254

5355
log = logf.Log.WithName(WebhookName)
5456

@@ -230,6 +232,14 @@ func (s *NamespaceWebhook) authorized(request admissionctl.Request) admissionctl
230232
ret.UID = request.AdmissionRequest.UID
231233
return ret
232234
}
235+
236+
// If the user making the request has a specific exception, allow them to change labels on non-platform and non-protected namespaces
237+
if allowLabelChanges(request) {
238+
ret = admissionctl.Allowed("User allowed to modify namespace labels")
239+
ret.UID = request.AdmissionRequest.UID
240+
return ret
241+
}
242+
233243
// Unprivileged users cannot modify certain labels on unprivileged namespaces
234244
unauthorized, err := s.unauthorizedLabelChanges(request)
235245
if unauthorized {
@@ -363,3 +373,10 @@ func amIAdmin(request admissionctl.Request) bool {
363373

364374
return false
365375
}
376+
377+
func allowLabelChanges(request admissionctl.Request) bool {
378+
if slices.Contains(labelUserExceptions, request.UserInfo.Username) {
379+
return true
380+
}
381+
return false
382+
}

pkg/webhooks/namespace/namespace_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,41 @@ func TestLabellingUpdates(t *testing.T) {
11021102
labels: map[string]string{},
11031103
shouldBeAllowed: true,
11041104
},
1105+
// https://issues.redhat.com/browse/SREP-1770 - test explicit exception for nvidia-gpu-operator
1106+
{
1107+
testID: "nvidia-gpu-operator-can-add-label-to-unprotected-ns",
1108+
targetNamespace: "nvidia-gpu-operator",
1109+
username: "system:serviceaccount:nvidia-gpu-operator:gpu-operator",
1110+
userGroups: []string{"system:authenticated", "system:authenticated:oauth"},
1111+
operation: admissionv1.Update,
1112+
oldObject: createOldObject("nvidia-gpu-operator", "nvidia-gpu-operato-can-add-label-to-unprotected-ns", map[string]string{}),
1113+
labels: map[string]string{"openshift.io/cluster-monitoring": "true"},
1114+
shouldBeAllowed: true,
1115+
},
1116+
{
1117+
testID: "nvidia-gpu-operator-can-remove-label-from-unprotected-ns",
1118+
targetNamespace: "nvidia-gpu-operator",
1119+
username: "system:serviceaccount:nvidia-gpu-operator:gpu-operator",
1120+
userGroups: []string{"system:authenticated", "system:authenticated:oauth"},
1121+
operation: admissionv1.Update,
1122+
oldObject: createOldObject("nvidia-gpu-operator", "nvidia-gpu-operato-can-remove-label-from-unprotected-ns", map[string]string{
1123+
"openshift.io/cluster-monitoring": "true",
1124+
}),
1125+
labels: map[string]string{},
1126+
shouldBeAllowed: true,
1127+
},
1128+
{
1129+
testID: "nvidia-gpu-operator-cannot-remove-label-from-protected-ns",
1130+
targetNamespace: "nvidia-gpu-operator",
1131+
username: "system:serviceaccount:nvidia-gpu-operator:gpu-operator",
1132+
userGroups: []string{"system:authenticated", "system:authenticated:oauth"},
1133+
operation: admissionv1.Update,
1134+
oldObject: createOldObject("openshift-kube-apiserver", "nvidia-gpu-operato-cannot-remove-label-from-protected-ns", map[string]string{
1135+
"openshift.io/cluster-monitoring": "true",
1136+
}),
1137+
labels: map[string]string{},
1138+
shouldBeAllowed: false,
1139+
},
11051140
}
11061141
runNamespaceTests(t, tests)
11071142
}

0 commit comments

Comments
 (0)