Skip to content

Commit f27737e

Browse files
Extract newHostPathType to public HostPathTypePtr utility function
Signed-off-by: Karthik Vetrivel <[email protected]>
1 parent c5d513d commit f27737e

File tree

5 files changed

+58
-63
lines changed

5 files changed

+58
-63
lines changed

controllers/object_controls.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,6 @@ var CertConfigPathMap = map[string]string{
222222
"rhel": "/etc/pki/ca-trust/extracted/pem",
223223
}
224224

225-
func newHostPathType(pathType corev1.HostPathType) *corev1.HostPathType {
226-
hostPathType := new(corev1.HostPathType)
227-
*hostPathType = pathType
228-
return hostPathType
229-
}
230-
231225
// MountPathToVolumeSource maps a container mount path to a VolumeSource
232226
type MountPathToVolumeSource map[string]corev1.VolumeSource
233227

@@ -240,67 +234,67 @@ var SubscriptionPathMap = map[string](MountPathToVolumeSource){
240234
"/run/secrets/etc-pki-entitlement": corev1.VolumeSource{
241235
HostPath: &corev1.HostPathVolumeSource{
242236
Path: "/etc/pki/entitlement",
243-
Type: newHostPathType(corev1.HostPathDirectory),
237+
Type: utils.HostPathTypePtr(corev1.HostPathDirectory),
244238
},
245239
},
246240
"/run/secrets/redhat.repo": corev1.VolumeSource{
247241
HostPath: &corev1.HostPathVolumeSource{
248242
Path: "/etc/yum.repos.d/redhat.repo",
249-
Type: newHostPathType(corev1.HostPathFile),
243+
Type: utils.HostPathTypePtr(corev1.HostPathFile),
250244
},
251245
},
252246
"/run/secrets/rhsm": corev1.VolumeSource{
253247
HostPath: &corev1.HostPathVolumeSource{
254248
Path: "/etc/rhsm",
255-
Type: newHostPathType(corev1.HostPathDirectory),
249+
Type: utils.HostPathTypePtr(corev1.HostPathDirectory),
256250
},
257251
},
258252
},
259253
"rhcos": {
260254
"/run/secrets/etc-pki-entitlement": corev1.VolumeSource{
261255
HostPath: &corev1.HostPathVolumeSource{
262256
Path: "/etc/pki/entitlement",
263-
Type: newHostPathType(corev1.HostPathDirectory),
257+
Type: utils.HostPathTypePtr(corev1.HostPathDirectory),
264258
},
265259
},
266260
"/run/secrets/redhat.repo": corev1.VolumeSource{
267261
HostPath: &corev1.HostPathVolumeSource{
268262
Path: "/etc/yum.repos.d/redhat.repo",
269-
Type: newHostPathType(corev1.HostPathFile),
263+
Type: utils.HostPathTypePtr(corev1.HostPathFile),
270264
},
271265
},
272266
"/run/secrets/rhsm": corev1.VolumeSource{
273267
HostPath: &corev1.HostPathVolumeSource{
274268
Path: "/etc/rhsm",
275-
Type: newHostPathType(corev1.HostPathDirectory),
269+
Type: utils.HostPathTypePtr(corev1.HostPathDirectory),
276270
},
277271
},
278272
},
279273
"sles": {
280274
"/etc/zypp/credentials.d": corev1.VolumeSource{
281275
HostPath: &corev1.HostPathVolumeSource{
282276
Path: "/etc/zypp/credentials.d",
283-
Type: newHostPathType(corev1.HostPathDirectory),
277+
Type: utils.HostPathTypePtr(corev1.HostPathDirectory),
284278
},
285279
},
286280
"/etc/SUSEConnect": corev1.VolumeSource{
287281
HostPath: &corev1.HostPathVolumeSource{
288282
Path: "/etc/SUSEConnect",
289-
Type: newHostPathType(corev1.HostPathFileOrCreate),
283+
Type: utils.HostPathTypePtr(corev1.HostPathFileOrCreate),
290284
},
291285
},
292286
},
293287
"sl-micro": {
294288
"/etc/zypp/credentials.d": corev1.VolumeSource{
295289
HostPath: &corev1.HostPathVolumeSource{
296290
Path: "/etc/zypp/credentials.d",
297-
Type: newHostPathType(corev1.HostPathDirectory),
291+
Type: utils.HostPathTypePtr(corev1.HostPathDirectory),
298292
},
299293
},
300294
"/etc/SUSEConnect": corev1.VolumeSource{
301295
HostPath: &corev1.HostPathVolumeSource{
302296
Path: "/etc/SUSEConnect",
303-
Type: newHostPathType(corev1.HostPathFileOrCreate),
297+
Type: utils.HostPathTypePtr(corev1.HostPathFileOrCreate),
304298
},
305299
},
306300
},
@@ -1385,7 +1379,7 @@ func transformForRuntime(obj *appsv1.DaemonSet, config *gpuv1.ClusterPolicySpec,
13851379
volMountConfig := corev1.VolumeMount{Name: volMountConfigName, MountPath: containerConfigDir}
13861380
container.VolumeMounts = append(container.VolumeMounts, volMountConfig)
13871381

1388-
configVol := corev1.Volume{Name: volMountConfigName, VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: sourceConfigDir, Type: newHostPathType(corev1.HostPathDirectoryOrCreate)}}}
1382+
configVol := corev1.Volume{Name: volMountConfigName, VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: sourceConfigDir, Type: utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)}}}
13891383
obj.Spec.Template.Spec.Volumes = append(obj.Spec.Template.Spec.Volumes, configVol)
13901384
}
13911385

@@ -1407,7 +1401,7 @@ func transformForRuntime(obj *appsv1.DaemonSet, config *gpuv1.ClusterPolicySpec,
14071401
volMountConfig := corev1.VolumeMount{Name: volMountConfigName, MountPath: containerConfigDir}
14081402
container.VolumeMounts = append(container.VolumeMounts, volMountConfig)
14091403

1410-
configVol := corev1.Volume{Name: volMountConfigName, VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: sourceConfigDir, Type: newHostPathType(corev1.HostPathDirectoryOrCreate)}}}
1404+
configVol := corev1.Volume{Name: volMountConfigName, VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: sourceConfigDir, Type: utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)}}}
14111405
obj.Spec.Template.Spec.Volumes = append(obj.Spec.Template.Spec.Volumes, configVol)
14121406
}
14131407

@@ -1964,7 +1958,7 @@ func TransformKataManager(obj *appsv1.DaemonSet, config *gpuv1.ClusterPolicySpec
19641958
artifactsVolMount := corev1.VolumeMount{Name: "kata-artifacts", MountPath: artifactsDir}
19651959
obj.Spec.Template.Spec.Containers[0].VolumeMounts = append(obj.Spec.Template.Spec.Containers[0].VolumeMounts, artifactsVolMount)
19661960

1967-
artifactsVol := corev1.Volume{Name: "kata-artifacts", VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: artifactsDir, Type: newHostPathType(corev1.HostPathDirectoryOrCreate)}}}
1961+
artifactsVol := corev1.Volume{Name: "kata-artifacts", VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: artifactsDir, Type: utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)}}}
19681962
obj.Spec.Template.Spec.Volumes = append(obj.Spec.Template.Spec.Volumes, artifactsVol)
19691963

19701964
// Compute hash of kata manager config and add an annotation with the value.

controllers/transforms_test.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3434

3535
gpuv1 "github.com/NVIDIA/gpu-operator/api/nvidia/v1"
36+
"github.com/NVIDIA/gpu-operator/internal/utils"
3637
)
3738

3839
var mockClientMap map[string]client.Client
@@ -377,8 +378,8 @@ func TestTransformForRuntime(t *testing.T) {
377378
input: NewDaemonset().
378379
WithContainer(corev1.Container{Name: "test-ctr"}),
379380
expectedOutput: NewDaemonset().
380-
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), newHostPathType(corev1.HostPathDirectoryOrCreate)).
381-
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)).
381+
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
382+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
382383
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
383384
WithContainer(corev1.Container{
384385
Name: "test-ctr",
@@ -410,8 +411,8 @@ func TestTransformForRuntime(t *testing.T) {
410411
},
411412
}),
412413
expectedOutput: NewDaemonset().
413-
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), newHostPathType(corev1.HostPathDirectoryOrCreate)).
414-
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)).
414+
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
415+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
415416
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
416417
WithContainer(corev1.Container{
417418
Name: "test-ctr",
@@ -444,8 +445,8 @@ func TestTransformForRuntime(t *testing.T) {
444445
},
445446
}),
446447
expectedOutput: NewDaemonset().
447-
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), newHostPathType(corev1.HostPathDirectoryOrCreate)).
448-
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)).
448+
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
449+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
449450
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
450451
WithContainer(corev1.Container{
451452
Name: "test-ctr",
@@ -472,8 +473,8 @@ func TestTransformForRuntime(t *testing.T) {
472473
runtime: gpuv1.CRIO,
473474
input: NewDaemonset().WithContainer(corev1.Container{Name: "test-ctr"}),
474475
expectedOutput: NewDaemonset().
475-
WithHostPathVolume("crio-config", "/etc/crio", newHostPathType(corev1.HostPathDirectoryOrCreate)).
476-
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)).
476+
WithHostPathVolume("crio-config", "/etc/crio", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
477+
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
477478
WithContainer(corev1.Container{
478479
Name: "test-ctr",
479480
Env: []corev1.EnvVar{
@@ -496,7 +497,7 @@ func TestTransformForRuntime(t *testing.T) {
496497
input: NewDaemonset().
497498
WithContainer(corev1.Container{Name: "nvidia-kata-manager"}),
498499
expectedOutput: NewDaemonset().
499-
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), newHostPathType(corev1.HostPathDirectoryOrCreate)).
500+
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
500501
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
501502
WithContainer(corev1.Container{
502503
Name: "nvidia-kata-manager",
@@ -519,7 +520,7 @@ func TestTransformForRuntime(t *testing.T) {
519520
runtime: gpuv1.Docker,
520521
input: NewDaemonset().WithContainer(corev1.Container{Name: "test-ctr"}),
521522
expectedOutput: NewDaemonset().
522-
WithHostPathVolume("docker-config", filepath.Dir(DefaultDockerConfigFile), newHostPathType(corev1.HostPathDirectoryOrCreate)).
523+
WithHostPathVolume("docker-config", filepath.Dir(DefaultDockerConfigFile), utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
523524
WithHostPathVolume("docker-socket", filepath.Dir(DefaultDockerSocketFile), nil).
524525
WithContainer(corev1.Container{
525526
Name: "test-ctr",
@@ -840,8 +841,8 @@ func TestTransformToolkit(t *testing.T) {
840841
{Name: "containerd-socket", MountPath: "/runtime/sock-dir/"},
841842
},
842843
}).
843-
WithHostPathVolume("containerd-config", "/etc/containerd", newHostPathType(corev1.HostPathDirectoryOrCreate)).
844-
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)).
844+
WithHostPathVolume("containerd-config", "/etc/containerd", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
845+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
845846
WithHostPathVolume("containerd-socket", "/run/containerd", nil).
846847
WithPullSecret("pull-secret"),
847848
},
@@ -919,8 +920,8 @@ func TestTransformToolkit(t *testing.T) {
919920
{Name: "containerd-socket", MountPath: "/runtime/sock-dir/"},
920921
},
921922
}).
922-
WithHostPathVolume("containerd-config", "/var/lib/rancher/k3s/agent/etc/containerd", newHostPathType(corev1.HostPathDirectoryOrCreate)).
923-
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)).
923+
WithHostPathVolume("containerd-config", "/var/lib/rancher/k3s/agent/etc/containerd", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
924+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
924925
WithHostPathVolume("containerd-socket", "/run/k3s/containerd", nil).
925926
WithPullSecret("pull-secret"),
926927
},
@@ -957,8 +958,8 @@ func TestTransformToolkit(t *testing.T) {
957958
{Name: "crio-drop-in-config", MountPath: "/runtime/config-dir.d/"},
958959
},
959960
}).
960-
WithHostPathVolume("crio-config", "/etc/crio", newHostPathType(corev1.HostPathDirectoryOrCreate)).
961-
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)),
961+
WithHostPathVolume("crio-config", "/etc/crio", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
962+
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)),
962963
},
963964
{
964965
description: "transform nvidia-container-toolkit-ctr container, cri-o runtime, cdi disabled",
@@ -993,8 +994,8 @@ func TestTransformToolkit(t *testing.T) {
993994
{Name: "crio-drop-in-config", MountPath: "/runtime/config-dir.d/"},
994995
},
995996
}).
996-
WithHostPathVolume("crio-config", "/etc/crio", newHostPathType(corev1.HostPathDirectoryOrCreate)).
997-
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)),
997+
WithHostPathVolume("crio-config", "/etc/crio", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
998+
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)),
998999
},
9991000
}
10001001

@@ -1120,8 +1121,8 @@ func TestTransformMPSControlDaemon(t *testing.T) {
11201121
daemonset: NewDaemonset().
11211122
WithInitContainer(corev1.Container{Name: "mps-control-daemon-mounts"}).
11221123
WithContainer(corev1.Container{Name: "mps-control-daemon-ctr"}).
1123-
WithHostPathVolume("mps-root", "/run/nvidia/mps", newHostPathType(corev1.HostPathDirectoryOrCreate)).
1124-
WithHostPathVolume("mps-shm", "/run/nvidia/mps/shm", newHostPathType(corev1.HostPathDirectoryOrCreate)),
1124+
WithHostPathVolume("mps-root", "/run/nvidia/mps", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
1125+
WithHostPathVolume("mps-shm", "/run/nvidia/mps/shm", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)),
11251126
clusterPolicySpec: &gpuv1.ClusterPolicySpec{
11261127
DevicePlugin: gpuv1.DevicePluginSpec{
11271128
Repository: "nvcr.io",
@@ -1146,8 +1147,8 @@ func TestTransformMPSControlDaemon(t *testing.T) {
11461147
{Name: "NVIDIA_MIG_MONITOR_DEVICES", Value: "all"},
11471148
},
11481149
}).
1149-
WithHostPathVolume("mps-root", "/var/mps", newHostPathType(corev1.HostPathDirectoryOrCreate)).
1150-
WithHostPathVolume("mps-shm", "/var/mps/shm", newHostPathType(corev1.HostPathDirectoryOrCreate)).
1150+
WithHostPathVolume("mps-root", "/var/mps", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
1151+
WithHostPathVolume("mps-shm", "/var/mps/shm", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
11511152
WithPullSecret("secret").
11521153
WithRuntimeClassName("nvidia"),
11531154
},
@@ -1540,7 +1541,7 @@ func TestTransformKataManager(t *testing.T) {
15401541
{Name: "containerd-config", MountPath: "/runtime/config-dir/"},
15411542
{Name: "containerd-socket", MountPath: "/runtime/sock-dir/"},
15421543
},
1543-
}).WithPullSecret("pull-secret").WithPodAnnotations(map[string]string{"nvidia.com/kata-manager.last-applied-hash": "1929911998"}).WithHostPathVolume("kata-artifacts", "/var/lib/kata", newHostPathType(corev1.HostPathDirectoryOrCreate)).WithHostPathVolume("containerd-config", "/etc/containerd", newHostPathType(corev1.HostPathDirectoryOrCreate)).WithHostPathVolume("containerd-socket", "/run/containerd", nil),
1544+
}).WithPullSecret("pull-secret").WithPodAnnotations(map[string]string{"nvidia.com/kata-manager.last-applied-hash": "1929911998"}).WithHostPathVolume("kata-artifacts", "/var/lib/kata", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).WithHostPathVolume("containerd-config", "/etc/containerd", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).WithHostPathVolume("containerd-socket", "/run/containerd", nil),
15441545
},
15451546
{
15461547
description: "transform kata manager with custom container runtime socket",
@@ -1594,8 +1595,8 @@ func TestTransformKataManager(t *testing.T) {
15941595
},
15951596
}).WithPullSecret("pull-secret").
15961597
WithPodAnnotations(map[string]string{"nvidia.com/kata-manager.last-applied-hash": "1929911998"}).
1597-
WithHostPathVolume("kata-artifacts", "/var/lib/kata", newHostPathType(corev1.HostPathDirectoryOrCreate)).
1598-
WithHostPathVolume("containerd-config", "/var/lib/rancher/k3s/agent/etc/containerd", newHostPathType(corev1.HostPathDirectoryOrCreate)).
1598+
WithHostPathVolume("kata-artifacts", "/var/lib/kata", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
1599+
WithHostPathVolume("containerd-config", "/var/lib/rancher/k3s/agent/etc/containerd", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
15991600
WithHostPathVolume("containerd-socket", "/run/k3s/containerd", nil),
16001601
},
16011602
}

internal/state/driver_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ func getSampleAdditionalConfigs() *additionalConfigs {
703703
VolumeSource: corev1.VolumeSource{
704704
HostPath: &corev1.HostPathVolumeSource{
705705
Path: "/opt/config/test-host-path",
706-
Type: newHostPathType(corev1.HostPathDirectoryOrCreate),
706+
Type: utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate),
707707
},
708708
},
709709
},
@@ -712,7 +712,7 @@ func getSampleAdditionalConfigs() *additionalConfigs {
712712
VolumeSource: corev1.VolumeSource{
713713
HostPath: &corev1.HostPathVolumeSource{
714714
Path: "/opt/config/test-host-path-ro",
715-
Type: newHostPathType(corev1.HostPathDirectoryOrCreate),
715+
Type: utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate),
716716
},
717717
},
718718
},

0 commit comments

Comments
 (0)