Skip to content

Commit 0758246

Browse files
committed
feat(dcgm-exporter): naive implementation of adding extra annotations to the daemonset and it's underlying pods
1 parent d8b0120 commit 0758246

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

controllers/object_controls.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,6 +1755,12 @@ func TransformDCGMExporter(obj *appsv1.DaemonSet, config *gpuv1.ClusterPolicySpe
17551755
if len(config.DCGMExporter.ImagePullSecrets) > 0 {
17561756
addPullSecrets(&obj.Spec.Template.Spec, config.DCGMExporter.ImagePullSecrets)
17571757
}
1758+
1759+
// set extra annotations for the DS
1760+
if len(config.DCGMExporter.Annotations) > 0 {
1761+
addExtraAnnotations(obj, config.DCGMExporter.Annotations)
1762+
}
1763+
17581764
// set resource limits
17591765
if config.DCGMExporter.Resources != nil {
17601766
// apply resource limits to all containers
@@ -1851,6 +1857,19 @@ func TransformDCGMExporter(obj *appsv1.DaemonSet, config *gpuv1.ClusterPolicySpe
18511857
return nil
18521858
}
18531859

1860+
func addExtraAnnotations(obj *appsv1.DaemonSet, annotations map[string]string) {
1861+
if obj.Annotations == nil {
1862+
obj.Annotations = make(map[string]string)
1863+
}
1864+
if obj.Spec.Template.Annotations == nil {
1865+
obj.Spec.Template.Annotations = make(map[string]string)
1866+
}
1867+
for k, v := range annotations {
1868+
obj.Annotations[k] = v
1869+
obj.Spec.Template.Annotations[k] = v
1870+
}
1871+
}
1872+
18541873
// TransformDCGM transforms dcgm daemonset with required config as per ClusterPolicy
18551874
func TransformDCGM(obj *appsv1.DaemonSet, config *gpuv1.ClusterPolicySpec, n ClusterPolicyController) error {
18561875
// update validation container

controllers/transforms_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ func NewDaemonset() Daemonset {
8888
return Daemonset{ds}
8989
}
9090

91+
func (d Daemonset) WithAnnotations(annotations map[string]string) Daemonset {
92+
d.Annotations = annotations
93+
return d
94+
}
95+
9196
func (d Daemonset) WithHostPathVolume(name string, path string, hostPathType *corev1.HostPathType) Daemonset {
9297
volume := corev1.Volume{
9398
Name: name,
@@ -1512,6 +1517,78 @@ func TestTransformDCGMExporter(t *testing.T) {
15121517
WithRuntimeClassName("nvidia").
15131518
WithHostPathVolume("hpc-job-mapping", "/run/nvidia/dcgm-job-mapping", ptr.To(corev1.HostPathDirectoryOrCreate)),
15141519
},
1520+
{
1521+
description: "transform dcgm exporter with extra annotations",
1522+
ds: NewDaemonset().
1523+
WithContainer(corev1.Container{Name: "dcgm-exporter"}),
1524+
cpSpec: &gpuv1.ClusterPolicySpec{
1525+
DCGMExporter: gpuv1.DCGMExporterSpec{
1526+
Repository: "nvcr.io/nvidia/k8s",
1527+
Image: "dcgm-exporter",
1528+
Version: "v1.0.0",
1529+
Annotations: map[string]string{
1530+
"prometheus.io/scrape": "true",
1531+
"prometheus.io/port": "8080",
1532+
"prometheus.io/path": "/metrics",
1533+
},
1534+
},
1535+
},
1536+
expectedDs: NewDaemonset().
1537+
WithAnnotations(map[string]string{
1538+
"prometheus.io/scrape": "true",
1539+
"prometheus.io/port": "8080",
1540+
"prometheus.io/path": "/metrics",
1541+
}).
1542+
WithPodAnnotations(map[string]string{
1543+
"prometheus.io/scrape": "true",
1544+
"prometheus.io/port": "8080",
1545+
"prometheus.io/path": "/metrics",
1546+
}).
1547+
WithContainer(corev1.Container{
1548+
Name: "dcgm-exporter",
1549+
Image: "nvcr.io/nvidia/k8s/dcgm-exporter:v1.0.0",
1550+
ImagePullPolicy: corev1.PullIfNotPresent,
1551+
Env: []corev1.EnvVar{
1552+
{Name: "DCGM_REMOTE_HOSTENGINE_INFO", Value: "nvidia-dcgm:5555"},
1553+
},
1554+
}).
1555+
WithRuntimeClassName("nvidia"),
1556+
},
1557+
{
1558+
description: "transform dcgm exporter appends extra annotations to existing ones",
1559+
ds: NewDaemonset().
1560+
WithAnnotations(map[string]string{
1561+
"existing-key": "existing-value",
1562+
}).
1563+
WithContainer(corev1.Container{Name: "dcgm-exporter"}),
1564+
cpSpec: &gpuv1.ClusterPolicySpec{
1565+
DCGMExporter: gpuv1.DCGMExporterSpec{
1566+
Repository: "nvcr.io/nvidia/k8s",
1567+
Image: "dcgm-exporter",
1568+
Version: "v1.0.0",
1569+
Annotations: map[string]string{
1570+
"prometheus.io/scrape": "true",
1571+
},
1572+
},
1573+
},
1574+
expectedDs: NewDaemonset().
1575+
WithAnnotations(map[string]string{
1576+
"existing-key": "existing-value",
1577+
"prometheus.io/scrape": "true",
1578+
}).
1579+
WithPodAnnotations(map[string]string{
1580+
"prometheus.io/scrape": "true",
1581+
}).
1582+
WithContainer(corev1.Container{
1583+
Name: "dcgm-exporter",
1584+
Image: "nvcr.io/nvidia/k8s/dcgm-exporter:v1.0.0",
1585+
ImagePullPolicy: corev1.PullIfNotPresent,
1586+
Env: []corev1.EnvVar{
1587+
{Name: "DCGM_REMOTE_HOSTENGINE_INFO", Value: "nvidia-dcgm:5555"},
1588+
},
1589+
}).
1590+
WithRuntimeClassName("nvidia"),
1591+
},
15151592
}
15161593

15171594
for _, tc := range testCases {

0 commit comments

Comments
 (0)