Skip to content

Commit 1d8f71f

Browse files
committed
Merge branch 'cherry_pick_9459e4184c9e168c73df5714c4398036f85c2062' into 'release-22.09'
Cherry-pick: Add Pod Security Admissions labels to namespace for helm based deployments See merge request nvidia/kubernetes/gpu-operator!548
2 parents 465072b + 6b3717c commit 1d8f71f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

controllers/state_manager.go

+55
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,13 @@ const (
5050
gpuWorkloadConfigContainer = "container"
5151
gpuWorkloadConfigVMPassthrough = "vm-passthrough"
5252
gpuWorkloadConfigVMVgpu = "vm-vgpu"
53+
podSecurityLabelPrefix = "pod-security.kubernetes.io/"
54+
podSecurityLevelPrivileged = "privileged"
5355
)
5456

5557
var (
5658
defaultGPUWorkloadConfig = gpuWorkloadConfigContainer
59+
podSecurityModes = []string{"enforce", "audit", "warn"}
5760
)
5861

5962
var gpuStateLabels = map[string]map[string]string{
@@ -498,6 +501,48 @@ func getRuntimeString(node corev1.Node) (gpuv1.Runtime, error) {
498501
return runtime, nil
499502
}
500503

504+
func (n *ClusterPolicyController) setPodSecurityLabelsForNamespace() error {
505+
namespaceName := clusterPolicyCtrl.operatorNamespace
506+
507+
if n.openshift != "" && namespaceName != ocpSuggestedNamespace {
508+
// The GPU Operator is not installed in the suggested
509+
// namespace, so the namespace may be shared with other
510+
// untrusted operators. Do not set Pod Security Admission labels.
511+
n.rec.Log.Info("GPU Operator is not installed in the suggested namespace. Not setting Pod Security Admission labels for namespace",
512+
"namespace", namespaceName,
513+
"suggested namespace", ocpSuggestedNamespace)
514+
return nil
515+
}
516+
517+
ns := &corev1.Namespace{}
518+
opts := client.ObjectKey{Name: namespaceName}
519+
err := n.rec.Client.Get(context.TODO(), opts, ns)
520+
if err != nil {
521+
return fmt.Errorf("ERROR: could not get Namespace %s from client: %v", namespaceName, err)
522+
}
523+
524+
patch := client.MergeFrom(ns.DeepCopy())
525+
modified := false
526+
for _, mode := range podSecurityModes {
527+
key := podSecurityLabelPrefix + mode
528+
if val, ok := ns.ObjectMeta.Labels[key]; !ok || (val != podSecurityLevelPrivileged) {
529+
ns.ObjectMeta.Labels[key] = podSecurityLevelPrivileged
530+
modified = true
531+
}
532+
}
533+
534+
if !modified {
535+
return nil
536+
}
537+
538+
err = n.rec.Client.Patch(context.TODO(), ns, patch)
539+
if err != nil {
540+
return fmt.Errorf("unable to label namespace %s with pod security levels: %v", namespaceName, err)
541+
}
542+
543+
return nil
544+
}
545+
501546
func (n *ClusterPolicyController) ocpEnsureNamespaceMonitoring() error {
502547
namespaceName := clusterPolicyCtrl.operatorNamespace
503548

@@ -692,6 +737,16 @@ func (n *ClusterPolicyController) init(reconciler *ClusterPolicyReconciler, clus
692737
n.operatorMetrics.openshiftDriverToolkitEnabled.Set(openshiftDriverToolkitDisabled)
693738
}
694739

740+
if clusterPolicy.Spec.PSP.IsEnabled() {
741+
// label namespace with Pod Security Admission levels
742+
n.rec.Log.Info("Pod Security is enabled. Adding labels to GPU Operator namespace", "namespace", n.operatorNamespace)
743+
err := n.setPodSecurityLabelsForNamespace()
744+
if err != nil {
745+
return err
746+
}
747+
n.rec.Log.Info("Pod Security Admission labels added to GPU Operator namespace", "namespace", n.operatorNamespace)
748+
}
749+
695750
// fetch all nodes and label gpu nodes
696751
hasNFDLabels, gpuNodeCount, err := n.labelGPUNodes()
697752
if err != nil {

deployments/gpu-operator/templates/role.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ rules:
4646
- create
4747
- watch
4848
- update
49+
- patch
4950
- apiGroups:
5051
- apps
5152
resources:

0 commit comments

Comments
 (0)