Skip to content

Commit cc17759

Browse files
Add pod labels and annotations for DCGMExporter
Signed-off-by: maciej-tatarski <mta@corti.ai>
1 parent 94520bc commit cc17759

5 files changed

Lines changed: 75 additions & 3 deletions

File tree

api/nvidia/v1/clusterpolicy_types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,16 @@ type DCGMExporterSpec struct {
884884
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors.x-descriptors="urn:alm:descriptor:io.kubernetes:Secret"
885885
ImagePullSecrets []string `json:"imagePullSecrets,omitempty"`
886886

887+
// Optional Pod Annotations for the DCGM Exporter deployment
888+
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
889+
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors.displayName="Annotations for the DCGM Exporter deployment"
890+
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
891+
892+
// Optional Pod Labels for the DCGM Exporter deployment
893+
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
894+
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors.displayName="Labels for the DCGM Exporter deployment"
895+
PodLabels map[string]string `json:"podLabels,omitempty"`
896+
887897
// Optional: Define resources requests and limits for each pod
888898
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
889899
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors.displayName="Resource Requirements"

controllers/object_controls.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ import (
2323
"fmt"
2424
"os"
2525
"path"
26+
"path/filepath"
2627
"regexp"
2728
"sort"
2829
"strconv"
2930
"strings"
3031

31-
"path/filepath"
32-
3332
apiconfigv1 "github.com/openshift/api/config/v1"
3433
apiimagev1 "github.com/openshift/api/image/v1"
3534
secv1 "github.com/openshift/api/security/v1"
@@ -1621,6 +1620,26 @@ func TransformDCGMExporter(obj *appsv1.DaemonSet, config *gpuv1.ClusterPolicySpe
16211620
obj.Spec.Template.Spec.Containers[0].Args = config.DCGMExporter.Args
16221621
}
16231622

1623+
// set labels if specified for exporter container
1624+
if obj.Spec.Template.ObjectMeta.Labels == nil {
1625+
obj.Spec.Template.ObjectMeta.Labels = make(map[string]string)
1626+
}
1627+
for labelKey, labelValue := range config.DCGMExporter.PodLabels {
1628+
// disallow setting app label
1629+
if labelKey == "app" {
1630+
continue
1631+
}
1632+
obj.Spec.Template.ObjectMeta.Labels[labelKey] = labelValue
1633+
}
1634+
1635+
// set annotations if specified for exporter container
1636+
if obj.Spec.Template.ObjectMeta.Annotations == nil {
1637+
obj.Spec.Template.ObjectMeta.Annotations = make(map[string]string)
1638+
}
1639+
for annKey, annValue := range config.DCGMExporter.PodAnnotations {
1640+
obj.Spec.Template.ObjectMeta.Annotations[annKey] = annValue
1641+
}
1642+
16241643
// check if DCGM hostengine is enabled as a separate Pod and setup env accordingly
16251644
if config.DCGM.IsEnabled() {
16261645
setContainerEnv(&(obj.Spec.Template.Spec.Containers[0]), DCGMRemoteEngineEnvName, fmt.Sprintf("nvidia-dcgm:%d", DCGMDefaultPort))

controllers/object_controls_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,14 @@ func getDCGMExporterTestInput(testCase string) *gpuv1.ClusterPolicy {
974974
case "standalone-dcgm":
975975
dcgmEnabled := true
976976
cp.Spec.DCGM.Enabled = &dcgmEnabled
977+
case "podLabelsAndAnnotations":
978+
cp.Spec.DCGMExporter.PodAnnotations = map[string]string{
979+
"prometheus.io/scrape": "true",
980+
"prometheus.io/port": "9400",
981+
}
982+
cp.Spec.DCGMExporter.PodLabels = map[string]string{
983+
"team": "test",
984+
}
977985
default:
978986
return nil
979987
}
@@ -989,15 +997,25 @@ func getDCGMExporterTestOutput(testCase string) map[string]interface{} {
989997
"numDaemonsets": 1,
990998
"dcgmExporterImage": "nvcr.io/nvidia/k8s/dcgm-exporter:3.3.0-3.2.0-ubuntu22.04",
991999
"imagePullSecret": "ngc-secret",
1000+
"env": map[string]string{},
1001+
"podLabels": map[string]string{},
1002+
"podAnnotations": map[string]string{},
9921003
}
9931004

9941005
switch testCase {
9951006
case "default":
996-
output["env"] = map[string]string{}
9971007
case "standalone-dcgm":
9981008
output["env"] = map[string]string{
9991009
"DCGM_REMOTE_HOSTENGINE_INFO": "nvidia-dcgm:5555",
10001010
}
1011+
case "podLabelsAndAnnotations":
1012+
output["podLabels"] = map[string]string{
1013+
"team": "test",
1014+
}
1015+
output["podAnnotations"] = map[string]string{
1016+
"prometheus.io/scrape": "true",
1017+
"prometheus.io/port": "9400",
1018+
}
10011019
default:
10021020
return nil
10031021
}
@@ -1023,6 +1041,11 @@ func TestDCGMExporter(t *testing.T) {
10231041
getDCGMExporterTestInput("standalone-dcgm"),
10241042
getDCGMExporterTestOutput("standalone-dcgm"),
10251043
},
1044+
{
1045+
"podLabelsAndAnnotations",
1046+
getDCGMExporterTestInput("podLabelsAndAnnotations"),
1047+
getDCGMExporterTestOutput("podLabelsAndAnnotations"),
1048+
},
10261049
}
10271050

10281051
for _, tc := range testCases {
@@ -1053,6 +1076,18 @@ func TestDCGMExporter(t *testing.T) {
10531076
t.Fatalf("Expected env is not set for daemonset nvidia-dcgm-exporter %s->%s", key, value)
10541077
}
10551078
}
1079+
for key, value := range tc.output["podLabels"].(map[string]string) {
1080+
objLabelvalue, ok := ds.Spec.Template.Labels[key]
1081+
if !ok || objLabelvalue != value {
1082+
t.Fatalf("Expected label is not set for daemonset nvidia-dcgm-exporter %s->%s", key, value)
1083+
}
1084+
}
1085+
for key, value := range tc.output["podAnnotations"].(map[string]string) {
1086+
objAnnotationValue, ok := ds.Spec.Template.Annotations[key]
1087+
if !ok || objAnnotationValue != value {
1088+
t.Fatalf("Expected annotation is not set for daemonset nvidia-dcgm-exporter %s->%s", key, value)
1089+
}
1090+
}
10561091

10571092
require.Equal(t, tc.output["dcgmExporterImage"], dcgmExporterImage, "Unexpected configuration for dcgm-exporter image")
10581093

deployments/gpu-operator/templates/clusterpolicy.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,12 @@ spec:
502502
{{- if .Values.dcgmExporter.resources }}
503503
resources: {{ toYaml .Values.dcgmExporter.resources | nindent 6 }}
504504
{{- end }}
505+
{{- if .Values.dcgmExporter.podAnnotations }}
506+
podAnnotations: {{ toYaml .Values.dcgmExporter.podAnnotations | nindent 6 }}
507+
{{- end }}
508+
{{- if .Values.dcgmExporter.podLabels }}
509+
podLabels: {{ toYaml .Values.dcgmExporter.podLabels | nindent 6 }}
510+
{{- end }}
505511
{{- if .Values.dcgmExporter.env }}
506512
env: {{ toYaml .Values.dcgmExporter.env | nindent 6 }}
507513
{{- end }}

deployments/gpu-operator/values.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ dcgmExporter:
315315
image: dcgm-exporter
316316
version: 4.2.3-4.1.3-ubuntu22.04
317317
imagePullPolicy: IfNotPresent
318+
podAnnotations: {}
319+
podLabels: {}
318320
env:
319321
- name: DCGM_EXPORTER_LISTEN
320322
value: ":9400"

0 commit comments

Comments
 (0)