@@ -50,10 +50,13 @@ const (
50
50
gpuWorkloadConfigContainer = "container"
51
51
gpuWorkloadConfigVMPassthrough = "vm-passthrough"
52
52
gpuWorkloadConfigVMVgpu = "vm-vgpu"
53
+ podSecurityLabelPrefix = "pod-security.kubernetes.io/"
54
+ podSecurityLevelPrivileged = "privileged"
53
55
)
54
56
55
57
var (
56
58
defaultGPUWorkloadConfig = gpuWorkloadConfigContainer
59
+ podSecurityModes = []string {"enforce" , "audit" , "warn" }
57
60
)
58
61
59
62
var gpuStateLabels = map [string ]map [string ]string {
@@ -498,6 +501,48 @@ func getRuntimeString(node corev1.Node) (gpuv1.Runtime, error) {
498
501
return runtime , nil
499
502
}
500
503
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
+
501
546
func (n * ClusterPolicyController ) ocpEnsureNamespaceMonitoring () error {
502
547
namespaceName := clusterPolicyCtrl .operatorNamespace
503
548
@@ -692,6 +737,16 @@ func (n *ClusterPolicyController) init(reconciler *ClusterPolicyReconciler, clus
692
737
n .operatorMetrics .openshiftDriverToolkitEnabled .Set (openshiftDriverToolkitDisabled )
693
738
}
694
739
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
+
695
750
// fetch all nodes and label gpu nodes
696
751
hasNFDLabels , gpuNodeCount , err := n .labelGPUNodes ()
697
752
if err != nil {
0 commit comments