Skip to content

Commit 109f67e

Browse files
authored
Merge pull request #1832 from karthikvetrivel/refactor/extract-hostpathtype-util
2 parents 41f3501 + 22d3263 commit 109f67e

File tree

5 files changed

+64
-85
lines changed

5 files changed

+64
-85
lines changed

controllers/object_controls.go

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
"k8s.io/apimachinery/pkg/runtime/schema"
4646
"k8s.io/apimachinery/pkg/types"
4747
"k8s.io/apimachinery/pkg/util/intstr"
48+
"k8s.io/utils/ptr"
4849
"sigs.k8s.io/controller-runtime/pkg/client"
4950
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
5051
"sigs.k8s.io/yaml"
@@ -201,7 +202,7 @@ const (
201202
)
202203

203204
// rootUID represents user 0
204-
var rootUID = utils.Int64Ptr(0)
205+
var rootUID = ptr.To(int64(0))
205206

206207
// RepoConfigPathMap indicates standard OS specific paths for repository configuration files
207208
var RepoConfigPathMap = map[string]string{
@@ -222,12 +223,6 @@ var CertConfigPathMap = map[string]string{
222223
"rhel": "/etc/pki/ca-trust/extracted/pem",
223224
}
224225

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

@@ -240,67 +235,67 @@ var SubscriptionPathMap = map[string](MountPathToVolumeSource){
240235
"/run/secrets/etc-pki-entitlement": corev1.VolumeSource{
241236
HostPath: &corev1.HostPathVolumeSource{
242237
Path: "/etc/pki/entitlement",
243-
Type: newHostPathType(corev1.HostPathDirectory),
238+
Type: ptr.To(corev1.HostPathDirectory),
244239
},
245240
},
246241
"/run/secrets/redhat.repo": corev1.VolumeSource{
247242
HostPath: &corev1.HostPathVolumeSource{
248243
Path: "/etc/yum.repos.d/redhat.repo",
249-
Type: newHostPathType(corev1.HostPathFile),
244+
Type: ptr.To(corev1.HostPathFile),
250245
},
251246
},
252247
"/run/secrets/rhsm": corev1.VolumeSource{
253248
HostPath: &corev1.HostPathVolumeSource{
254249
Path: "/etc/rhsm",
255-
Type: newHostPathType(corev1.HostPathDirectory),
250+
Type: ptr.To(corev1.HostPathDirectory),
256251
},
257252
},
258253
},
259254
"rhcos": {
260255
"/run/secrets/etc-pki-entitlement": corev1.VolumeSource{
261256
HostPath: &corev1.HostPathVolumeSource{
262257
Path: "/etc/pki/entitlement",
263-
Type: newHostPathType(corev1.HostPathDirectory),
258+
Type: ptr.To(corev1.HostPathDirectory),
264259
},
265260
},
266261
"/run/secrets/redhat.repo": corev1.VolumeSource{
267262
HostPath: &corev1.HostPathVolumeSource{
268263
Path: "/etc/yum.repos.d/redhat.repo",
269-
Type: newHostPathType(corev1.HostPathFile),
264+
Type: ptr.To(corev1.HostPathFile),
270265
},
271266
},
272267
"/run/secrets/rhsm": corev1.VolumeSource{
273268
HostPath: &corev1.HostPathVolumeSource{
274269
Path: "/etc/rhsm",
275-
Type: newHostPathType(corev1.HostPathDirectory),
270+
Type: ptr.To(corev1.HostPathDirectory),
276271
},
277272
},
278273
},
279274
"sles": {
280275
"/etc/zypp/credentials.d": corev1.VolumeSource{
281276
HostPath: &corev1.HostPathVolumeSource{
282277
Path: "/etc/zypp/credentials.d",
283-
Type: newHostPathType(corev1.HostPathDirectory),
278+
Type: ptr.To(corev1.HostPathDirectory),
284279
},
285280
},
286281
"/etc/SUSEConnect": corev1.VolumeSource{
287282
HostPath: &corev1.HostPathVolumeSource{
288283
Path: "/etc/SUSEConnect",
289-
Type: newHostPathType(corev1.HostPathFileOrCreate),
284+
Type: ptr.To(corev1.HostPathFileOrCreate),
290285
},
291286
},
292287
},
293288
"sl-micro": {
294289
"/etc/zypp/credentials.d": corev1.VolumeSource{
295290
HostPath: &corev1.HostPathVolumeSource{
296291
Path: "/etc/zypp/credentials.d",
297-
Type: newHostPathType(corev1.HostPathDirectory),
292+
Type: ptr.To(corev1.HostPathDirectory),
298293
},
299294
},
300295
"/etc/SUSEConnect": corev1.VolumeSource{
301296
HostPath: &corev1.HostPathVolumeSource{
302297
Path: "/etc/SUSEConnect",
303-
Type: newHostPathType(corev1.HostPathFileOrCreate),
298+
Type: ptr.To(corev1.HostPathFileOrCreate),
304299
},
305300
},
306301
},
@@ -1385,7 +1380,7 @@ func transformForRuntime(obj *appsv1.DaemonSet, config *gpuv1.ClusterPolicySpec,
13851380
volMountConfig := corev1.VolumeMount{Name: volMountConfigName, MountPath: containerConfigDir}
13861381
container.VolumeMounts = append(container.VolumeMounts, volMountConfig)
13871382

1388-
configVol := corev1.Volume{Name: volMountConfigName, VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: sourceConfigDir, Type: newHostPathType(corev1.HostPathDirectoryOrCreate)}}}
1383+
configVol := corev1.Volume{Name: volMountConfigName, VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: sourceConfigDir, Type: ptr.To(corev1.HostPathDirectoryOrCreate)}}}
13891384
obj.Spec.Template.Spec.Volumes = append(obj.Spec.Template.Spec.Volumes, configVol)
13901385
}
13911386

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

1410-
configVol := corev1.Volume{Name: volMountConfigName, VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: sourceConfigDir, Type: newHostPathType(corev1.HostPathDirectoryOrCreate)}}}
1405+
configVol := corev1.Volume{Name: volMountConfigName, VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: sourceConfigDir, Type: ptr.To(corev1.HostPathDirectoryOrCreate)}}}
14111406
obj.Spec.Template.Spec.Volumes = append(obj.Spec.Template.Spec.Volumes, configVol)
14121407
}
14131408

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

1967-
artifactsVol := corev1.Volume{Name: "kata-artifacts", VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: artifactsDir, Type: newHostPathType(corev1.HostPathDirectoryOrCreate)}}}
1962+
artifactsVol := corev1.Volume{Name: "kata-artifacts", VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: artifactsDir, Type: ptr.To(corev1.HostPathDirectoryOrCreate)}}}
19681963
obj.Spec.Template.Spec.Volumes = append(obj.Spec.Template.Spec.Volumes, artifactsVol)
19691964

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

controllers/transforms_test.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ func TestTransformForRuntime(t *testing.T) {
377377
input: NewDaemonset().
378378
WithContainer(corev1.Container{Name: "test-ctr"}),
379379
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)).
380+
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), ptr.To(corev1.HostPathDirectoryOrCreate)).
381+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)).
382382
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
383383
WithContainer(corev1.Container{
384384
Name: "test-ctr",
@@ -410,8 +410,8 @@ func TestTransformForRuntime(t *testing.T) {
410410
},
411411
}),
412412
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)).
413+
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), ptr.To(corev1.HostPathDirectoryOrCreate)).
414+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)).
415415
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
416416
WithContainer(corev1.Container{
417417
Name: "test-ctr",
@@ -444,8 +444,8 @@ func TestTransformForRuntime(t *testing.T) {
444444
},
445445
}),
446446
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)).
447+
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), ptr.To(corev1.HostPathDirectoryOrCreate)).
448+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)).
449449
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
450450
WithContainer(corev1.Container{
451451
Name: "test-ctr",
@@ -472,8 +472,8 @@ func TestTransformForRuntime(t *testing.T) {
472472
runtime: gpuv1.CRIO,
473473
input: NewDaemonset().WithContainer(corev1.Container{Name: "test-ctr"}),
474474
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)).
475+
WithHostPathVolume("crio-config", "/etc/crio", ptr.To(corev1.HostPathDirectoryOrCreate)).
476+
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)).
477477
WithContainer(corev1.Container{
478478
Name: "test-ctr",
479479
Env: []corev1.EnvVar{
@@ -496,7 +496,7 @@ func TestTransformForRuntime(t *testing.T) {
496496
input: NewDaemonset().
497497
WithContainer(corev1.Container{Name: "nvidia-kata-manager"}),
498498
expectedOutput: NewDaemonset().
499-
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), newHostPathType(corev1.HostPathDirectoryOrCreate)).
499+
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), ptr.To(corev1.HostPathDirectoryOrCreate)).
500500
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
501501
WithContainer(corev1.Container{
502502
Name: "nvidia-kata-manager",
@@ -519,7 +519,7 @@ func TestTransformForRuntime(t *testing.T) {
519519
runtime: gpuv1.Docker,
520520
input: NewDaemonset().WithContainer(corev1.Container{Name: "test-ctr"}),
521521
expectedOutput: NewDaemonset().
522-
WithHostPathVolume("docker-config", filepath.Dir(DefaultDockerConfigFile), newHostPathType(corev1.HostPathDirectoryOrCreate)).
522+
WithHostPathVolume("docker-config", filepath.Dir(DefaultDockerConfigFile), ptr.To(corev1.HostPathDirectoryOrCreate)).
523523
WithHostPathVolume("docker-socket", filepath.Dir(DefaultDockerSocketFile), nil).
524524
WithContainer(corev1.Container{
525525
Name: "test-ctr",
@@ -840,8 +840,8 @@ func TestTransformToolkit(t *testing.T) {
840840
{Name: "containerd-socket", MountPath: "/runtime/sock-dir/"},
841841
},
842842
}).
843-
WithHostPathVolume("containerd-config", "/etc/containerd", newHostPathType(corev1.HostPathDirectoryOrCreate)).
844-
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)).
843+
WithHostPathVolume("containerd-config", "/etc/containerd", ptr.To(corev1.HostPathDirectoryOrCreate)).
844+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)).
845845
WithHostPathVolume("containerd-socket", "/run/containerd", nil).
846846
WithPullSecret("pull-secret"),
847847
},
@@ -919,8 +919,8 @@ func TestTransformToolkit(t *testing.T) {
919919
{Name: "containerd-socket", MountPath: "/runtime/sock-dir/"},
920920
},
921921
}).
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)).
922+
WithHostPathVolume("containerd-config", "/var/lib/rancher/k3s/agent/etc/containerd", ptr.To(corev1.HostPathDirectoryOrCreate)).
923+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)).
924924
WithHostPathVolume("containerd-socket", "/run/k3s/containerd", nil).
925925
WithPullSecret("pull-secret"),
926926
},
@@ -957,8 +957,8 @@ func TestTransformToolkit(t *testing.T) {
957957
{Name: "crio-drop-in-config", MountPath: "/runtime/config-dir.d/"},
958958
},
959959
}).
960-
WithHostPathVolume("crio-config", "/etc/crio", newHostPathType(corev1.HostPathDirectoryOrCreate)).
961-
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)),
960+
WithHostPathVolume("crio-config", "/etc/crio", ptr.To(corev1.HostPathDirectoryOrCreate)).
961+
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)),
962962
},
963963
{
964964
description: "transform nvidia-container-toolkit-ctr container, cri-o runtime, cdi disabled",
@@ -993,8 +993,8 @@ func TestTransformToolkit(t *testing.T) {
993993
{Name: "crio-drop-in-config", MountPath: "/runtime/config-dir.d/"},
994994
},
995995
}).
996-
WithHostPathVolume("crio-config", "/etc/crio", newHostPathType(corev1.HostPathDirectoryOrCreate)).
997-
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)),
996+
WithHostPathVolume("crio-config", "/etc/crio", ptr.To(corev1.HostPathDirectoryOrCreate)).
997+
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)),
998998
},
999999
}
10001000

@@ -1120,8 +1120,8 @@ func TestTransformMPSControlDaemon(t *testing.T) {
11201120
daemonset: NewDaemonset().
11211121
WithInitContainer(corev1.Container{Name: "mps-control-daemon-mounts"}).
11221122
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)),
1123+
WithHostPathVolume("mps-root", "/run/nvidia/mps", ptr.To(corev1.HostPathDirectoryOrCreate)).
1124+
WithHostPathVolume("mps-shm", "/run/nvidia/mps/shm", ptr.To(corev1.HostPathDirectoryOrCreate)),
11251125
clusterPolicySpec: &gpuv1.ClusterPolicySpec{
11261126
DevicePlugin: gpuv1.DevicePluginSpec{
11271127
Repository: "nvcr.io",
@@ -1146,8 +1146,8 @@ func TestTransformMPSControlDaemon(t *testing.T) {
11461146
{Name: "NVIDIA_MIG_MONITOR_DEVICES", Value: "all"},
11471147
},
11481148
}).
1149-
WithHostPathVolume("mps-root", "/var/mps", newHostPathType(corev1.HostPathDirectoryOrCreate)).
1150-
WithHostPathVolume("mps-shm", "/var/mps/shm", newHostPathType(corev1.HostPathDirectoryOrCreate)).
1149+
WithHostPathVolume("mps-root", "/var/mps", ptr.To(corev1.HostPathDirectoryOrCreate)).
1150+
WithHostPathVolume("mps-shm", "/var/mps/shm", ptr.To(corev1.HostPathDirectoryOrCreate)).
11511151
WithPullSecret("secret").
11521152
WithRuntimeClassName("nvidia"),
11531153
},
@@ -1540,7 +1540,7 @@ func TestTransformKataManager(t *testing.T) {
15401540
{Name: "containerd-config", MountPath: "/runtime/config-dir/"},
15411541
{Name: "containerd-socket", MountPath: "/runtime/sock-dir/"},
15421542
},
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),
1543+
}).WithPullSecret("pull-secret").WithPodAnnotations(map[string]string{"nvidia.com/kata-manager.last-applied-hash": "1929911998"}).WithHostPathVolume("kata-artifacts", "/var/lib/kata", ptr.To(corev1.HostPathDirectoryOrCreate)).WithHostPathVolume("containerd-config", "/etc/containerd", ptr.To(corev1.HostPathDirectoryOrCreate)).WithHostPathVolume("containerd-socket", "/run/containerd", nil),
15441544
},
15451545
{
15461546
description: "transform kata manager with custom container runtime socket",
@@ -1594,8 +1594,8 @@ func TestTransformKataManager(t *testing.T) {
15941594
},
15951595
}).WithPullSecret("pull-secret").
15961596
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)).
1597+
WithHostPathVolume("kata-artifacts", "/var/lib/kata", ptr.To(corev1.HostPathDirectoryOrCreate)).
1598+
WithHostPathVolume("containerd-config", "/var/lib/rancher/k3s/agent/etc/containerd", ptr.To(corev1.HostPathDirectoryOrCreate)).
15991599
WithHostPathVolume("containerd-socket", "/run/k3s/containerd", nil),
16001600
},
16011601
}

0 commit comments

Comments
 (0)