@@ -22,6 +22,8 @@ import (
2222
2323 "github.com/stretchr/testify/require"
2424 appsv1 "k8s.io/api/apps/v1"
25+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+ "k8s.io/apimachinery/pkg/labels"
2527 "k8s.io/apimachinery/pkg/runtime"
2628 "sigs.k8s.io/controller-runtime/pkg/client/fake"
2729)
@@ -62,3 +64,57 @@ func TestDRAValidationRender(t *testing.T) {
6264 require .NotEmpty (t , ds .Spec .Template .Spec .Containers )
6365 require .Equal (t , "nvcr.io/nvidia/gpu-operator-validator:test" , ds .Spec .Template .Spec .Containers [0 ].Image )
6466}
67+
68+ func TestDRAValidationRenderCommonLabels (t * testing.T ) {
69+ tests := map [string ]map [string ]string {
70+ "default" : nil ,
71+ "custom label" : {
72+ "team" : "platform" ,
73+ },
74+ "selector label collision" : {
75+ "app.kubernetes.io/name" : "gpu-platform" ,
76+ "team" : "platform" ,
77+ },
78+ "upgrade controller label collision" : {
79+ "app" : "gpu-platform" ,
80+ },
81+ "part-of label" : {
82+ "app.kubernetes.io/part-of" : "gpu-platform" ,
83+ },
84+ "combined reserved labels" : {
85+ "app" : "gpu-platform" ,
86+ "app.kubernetes.io/name" : "gpu-platform" ,
87+ "app.kubernetes.io/part-of" : "gpu-platform" ,
88+ "team" : "platform" ,
89+ },
90+ }
91+ for name , commonLabels := range tests {
92+ t .Run (name , func (t * testing.T ) {
93+ s := newTestDRAValidationState (t )
94+ cr := sampleGPUCluster ()
95+ cr .Spec .Daemonsets .Labels = commonLabels
96+
97+ objs , err := s .getManifestObjects (context .Background (), cr , draSupportedCatalog ())
98+ require .NoError (t , err )
99+ ds := findDaemonSet (t , objs )
100+
101+ // Preserve the immutable selector, not just agreement with the pod labels.
102+ require .Equal (t , & metav1.LabelSelector {MatchLabels : map [string ]string {
103+ "app.kubernetes.io/name" : "nvidia-dra-validator" ,
104+ }}, ds .Spec .Selector )
105+ selector , err := metav1 .LabelSelectorAsSelector (ds .Spec .Selector )
106+ require .NoError (t , err )
107+ require .True (t , selector .Matches (labels .Set (ds .Spec .Template .Labels )),
108+ "DaemonSet selector must match pod-template labels" )
109+ require .Equal (t , "nvidia-operator-validator" , ds .Spec .Template .Labels ["app" ])
110+ require .NotContains (t , ds .Spec .Template .Labels , "app.kubernetes.io/part-of" )
111+ if team , ok := commonLabels ["team" ]; ok {
112+ require .Equal (t , team , ds .Spec .Template .Labels ["team" ])
113+ }
114+ // Common labels remain customizable on the DaemonSet itself.
115+ for key , value := range commonLabels {
116+ require .Equal (t , value , ds .Labels [key ])
117+ }
118+ })
119+ }
120+ }
0 commit comments