Skip to content

Commit 62ca84d

Browse files
Refactor pointer utilities into internal/utils/ptr subpackage
Signed-off-by: Karthik Vetrivel <[email protected]>
1 parent f27737e commit 62ca84d

File tree

6 files changed

+101
-80
lines changed

6 files changed

+101
-80
lines changed

controllers/object_controls.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import (
5252
gpuv1 "github.com/NVIDIA/gpu-operator/api/nvidia/v1"
5353
"github.com/NVIDIA/gpu-operator/internal/consts"
5454
"github.com/NVIDIA/gpu-operator/internal/utils"
55+
"github.com/NVIDIA/gpu-operator/internal/utils/ptr"
5556
)
5657

5758
const (
@@ -201,7 +202,7 @@ const (
201202
)
202203

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

206207
// RepoConfigPathMap indicates standard OS specific paths for repository configuration files
207208
var RepoConfigPathMap = map[string]string{
@@ -234,67 +235,67 @@ var SubscriptionPathMap = map[string](MountPathToVolumeSource){
234235
"/run/secrets/etc-pki-entitlement": corev1.VolumeSource{
235236
HostPath: &corev1.HostPathVolumeSource{
236237
Path: "/etc/pki/entitlement",
237-
Type: utils.HostPathTypePtr(corev1.HostPathDirectory),
238+
Type: ptr.HostPathType(corev1.HostPathDirectory),
238239
},
239240
},
240241
"/run/secrets/redhat.repo": corev1.VolumeSource{
241242
HostPath: &corev1.HostPathVolumeSource{
242243
Path: "/etc/yum.repos.d/redhat.repo",
243-
Type: utils.HostPathTypePtr(corev1.HostPathFile),
244+
Type: ptr.HostPathType(corev1.HostPathFile),
244245
},
245246
},
246247
"/run/secrets/rhsm": corev1.VolumeSource{
247248
HostPath: &corev1.HostPathVolumeSource{
248249
Path: "/etc/rhsm",
249-
Type: utils.HostPathTypePtr(corev1.HostPathDirectory),
250+
Type: ptr.HostPathType(corev1.HostPathDirectory),
250251
},
251252
},
252253
},
253254
"rhcos": {
254255
"/run/secrets/etc-pki-entitlement": corev1.VolumeSource{
255256
HostPath: &corev1.HostPathVolumeSource{
256257
Path: "/etc/pki/entitlement",
257-
Type: utils.HostPathTypePtr(corev1.HostPathDirectory),
258+
Type: ptr.HostPathType(corev1.HostPathDirectory),
258259
},
259260
},
260261
"/run/secrets/redhat.repo": corev1.VolumeSource{
261262
HostPath: &corev1.HostPathVolumeSource{
262263
Path: "/etc/yum.repos.d/redhat.repo",
263-
Type: utils.HostPathTypePtr(corev1.HostPathFile),
264+
Type: ptr.HostPathType(corev1.HostPathFile),
264265
},
265266
},
266267
"/run/secrets/rhsm": corev1.VolumeSource{
267268
HostPath: &corev1.HostPathVolumeSource{
268269
Path: "/etc/rhsm",
269-
Type: utils.HostPathTypePtr(corev1.HostPathDirectory),
270+
Type: ptr.HostPathType(corev1.HostPathDirectory),
270271
},
271272
},
272273
},
273274
"sles": {
274275
"/etc/zypp/credentials.d": corev1.VolumeSource{
275276
HostPath: &corev1.HostPathVolumeSource{
276277
Path: "/etc/zypp/credentials.d",
277-
Type: utils.HostPathTypePtr(corev1.HostPathDirectory),
278+
Type: ptr.HostPathType(corev1.HostPathDirectory),
278279
},
279280
},
280281
"/etc/SUSEConnect": corev1.VolumeSource{
281282
HostPath: &corev1.HostPathVolumeSource{
282283
Path: "/etc/SUSEConnect",
283-
Type: utils.HostPathTypePtr(corev1.HostPathFileOrCreate),
284+
Type: ptr.HostPathType(corev1.HostPathFileOrCreate),
284285
},
285286
},
286287
},
287288
"sl-micro": {
288289
"/etc/zypp/credentials.d": corev1.VolumeSource{
289290
HostPath: &corev1.HostPathVolumeSource{
290291
Path: "/etc/zypp/credentials.d",
291-
Type: utils.HostPathTypePtr(corev1.HostPathDirectory),
292+
Type: ptr.HostPathType(corev1.HostPathDirectory),
292293
},
293294
},
294295
"/etc/SUSEConnect": corev1.VolumeSource{
295296
HostPath: &corev1.HostPathVolumeSource{
296297
Path: "/etc/SUSEConnect",
297-
Type: utils.HostPathTypePtr(corev1.HostPathFileOrCreate),
298+
Type: ptr.HostPathType(corev1.HostPathFileOrCreate),
298299
},
299300
},
300301
},
@@ -1379,7 +1380,7 @@ func transformForRuntime(obj *appsv1.DaemonSet, config *gpuv1.ClusterPolicySpec,
13791380
volMountConfig := corev1.VolumeMount{Name: volMountConfigName, MountPath: containerConfigDir}
13801381
container.VolumeMounts = append(container.VolumeMounts, volMountConfig)
13811382

1382-
configVol := corev1.Volume{Name: volMountConfigName, VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: sourceConfigDir, Type: utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)}}}
1383+
configVol := corev1.Volume{Name: volMountConfigName, VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: sourceConfigDir, Type: ptr.HostPathType(corev1.HostPathDirectoryOrCreate)}}}
13831384
obj.Spec.Template.Spec.Volumes = append(obj.Spec.Template.Spec.Volumes, configVol)
13841385
}
13851386

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

1404-
configVol := corev1.Volume{Name: volMountConfigName, VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: sourceConfigDir, Type: utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)}}}
1405+
configVol := corev1.Volume{Name: volMountConfigName, VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: sourceConfigDir, Type: ptr.HostPathType(corev1.HostPathDirectoryOrCreate)}}}
14051406
obj.Spec.Template.Spec.Volumes = append(obj.Spec.Template.Spec.Volumes, configVol)
14061407
}
14071408

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

1961-
artifactsVol := corev1.Volume{Name: "kata-artifacts", VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: artifactsDir, Type: utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)}}}
1962+
artifactsVol := corev1.Volume{Name: "kata-artifacts", VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: artifactsDir, Type: ptr.HostPathType(corev1.HostPathDirectoryOrCreate)}}}
19621963
obj.Spec.Template.Spec.Volumes = append(obj.Spec.Template.Spec.Volumes, artifactsVol)
19631964

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

controllers/transforms_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +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"
36+
internalptr "github.com/NVIDIA/gpu-operator/internal/utils/ptr"
3737
)
3838

3939
var mockClientMap map[string]client.Client
@@ -378,8 +378,8 @@ func TestTransformForRuntime(t *testing.T) {
378378
input: NewDaemonset().
379379
WithContainer(corev1.Container{Name: "test-ctr"}),
380380
expectedOutput: NewDaemonset().
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)).
381+
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
382+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
383383
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
384384
WithContainer(corev1.Container{
385385
Name: "test-ctr",
@@ -411,8 +411,8 @@ func TestTransformForRuntime(t *testing.T) {
411411
},
412412
}),
413413
expectedOutput: NewDaemonset().
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)).
414+
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
415+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
416416
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
417417
WithContainer(corev1.Container{
418418
Name: "test-ctr",
@@ -445,8 +445,8 @@ func TestTransformForRuntime(t *testing.T) {
445445
},
446446
}),
447447
expectedOutput: NewDaemonset().
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)).
448+
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
449+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
450450
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
451451
WithContainer(corev1.Container{
452452
Name: "test-ctr",
@@ -473,8 +473,8 @@ func TestTransformForRuntime(t *testing.T) {
473473
runtime: gpuv1.CRIO,
474474
input: NewDaemonset().WithContainer(corev1.Container{Name: "test-ctr"}),
475475
expectedOutput: NewDaemonset().
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)).
476+
WithHostPathVolume("crio-config", "/etc/crio", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
477+
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
478478
WithContainer(corev1.Container{
479479
Name: "test-ctr",
480480
Env: []corev1.EnvVar{
@@ -497,7 +497,7 @@ func TestTransformForRuntime(t *testing.T) {
497497
input: NewDaemonset().
498498
WithContainer(corev1.Container{Name: "nvidia-kata-manager"}),
499499
expectedOutput: NewDaemonset().
500-
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
500+
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
501501
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
502502
WithContainer(corev1.Container{
503503
Name: "nvidia-kata-manager",
@@ -520,7 +520,7 @@ func TestTransformForRuntime(t *testing.T) {
520520
runtime: gpuv1.Docker,
521521
input: NewDaemonset().WithContainer(corev1.Container{Name: "test-ctr"}),
522522
expectedOutput: NewDaemonset().
523-
WithHostPathVolume("docker-config", filepath.Dir(DefaultDockerConfigFile), utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
523+
WithHostPathVolume("docker-config", filepath.Dir(DefaultDockerConfigFile), internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
524524
WithHostPathVolume("docker-socket", filepath.Dir(DefaultDockerSocketFile), nil).
525525
WithContainer(corev1.Container{
526526
Name: "test-ctr",
@@ -841,8 +841,8 @@ func TestTransformToolkit(t *testing.T) {
841841
{Name: "containerd-socket", MountPath: "/runtime/sock-dir/"},
842842
},
843843
}).
844-
WithHostPathVolume("containerd-config", "/etc/containerd", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
845-
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
844+
WithHostPathVolume("containerd-config", "/etc/containerd", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
845+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
846846
WithHostPathVolume("containerd-socket", "/run/containerd", nil).
847847
WithPullSecret("pull-secret"),
848848
},
@@ -920,8 +920,8 @@ func TestTransformToolkit(t *testing.T) {
920920
{Name: "containerd-socket", MountPath: "/runtime/sock-dir/"},
921921
},
922922
}).
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)).
923+
WithHostPathVolume("containerd-config", "/var/lib/rancher/k3s/agent/etc/containerd", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
924+
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
925925
WithHostPathVolume("containerd-socket", "/run/k3s/containerd", nil).
926926
WithPullSecret("pull-secret"),
927927
},
@@ -958,8 +958,8 @@ func TestTransformToolkit(t *testing.T) {
958958
{Name: "crio-drop-in-config", MountPath: "/runtime/config-dir.d/"},
959959
},
960960
}).
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)),
961+
WithHostPathVolume("crio-config", "/etc/crio", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
962+
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)),
963963
},
964964
{
965965
description: "transform nvidia-container-toolkit-ctr container, cri-o runtime, cdi disabled",
@@ -994,8 +994,8 @@ func TestTransformToolkit(t *testing.T) {
994994
{Name: "crio-drop-in-config", MountPath: "/runtime/config-dir.d/"},
995995
},
996996
}).
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)),
997+
WithHostPathVolume("crio-config", "/etc/crio", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
998+
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)),
999999
},
10001000
}
10011001

@@ -1121,8 +1121,8 @@ func TestTransformMPSControlDaemon(t *testing.T) {
11211121
daemonset: NewDaemonset().
11221122
WithInitContainer(corev1.Container{Name: "mps-control-daemon-mounts"}).
11231123
WithContainer(corev1.Container{Name: "mps-control-daemon-ctr"}).
1124-
WithHostPathVolume("mps-root", "/run/nvidia/mps", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
1125-
WithHostPathVolume("mps-shm", "/run/nvidia/mps/shm", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)),
1124+
WithHostPathVolume("mps-root", "/run/nvidia/mps", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
1125+
WithHostPathVolume("mps-shm", "/run/nvidia/mps/shm", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)),
11261126
clusterPolicySpec: &gpuv1.ClusterPolicySpec{
11271127
DevicePlugin: gpuv1.DevicePluginSpec{
11281128
Repository: "nvcr.io",
@@ -1147,8 +1147,8 @@ func TestTransformMPSControlDaemon(t *testing.T) {
11471147
{Name: "NVIDIA_MIG_MONITOR_DEVICES", Value: "all"},
11481148
},
11491149
}).
1150-
WithHostPathVolume("mps-root", "/var/mps", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
1151-
WithHostPathVolume("mps-shm", "/var/mps/shm", utils.HostPathTypePtr(corev1.HostPathDirectoryOrCreate)).
1150+
WithHostPathVolume("mps-root", "/var/mps", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
1151+
WithHostPathVolume("mps-shm", "/var/mps/shm", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
11521152
WithPullSecret("secret").
11531153
WithRuntimeClassName("nvidia"),
11541154
},
@@ -1541,7 +1541,7 @@ func TestTransformKataManager(t *testing.T) {
15411541
{Name: "containerd-config", MountPath: "/runtime/config-dir/"},
15421542
{Name: "containerd-socket", MountPath: "/runtime/sock-dir/"},
15431543
},
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),
1544+
}).WithPullSecret("pull-secret").WithPodAnnotations(map[string]string{"nvidia.com/kata-manager.last-applied-hash": "1929911998"}).WithHostPathVolume("kata-artifacts", "/var/lib/kata", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).WithHostPathVolume("containerd-config", "/etc/containerd", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).WithHostPathVolume("containerd-socket", "/run/containerd", nil),
15451545
},
15461546
{
15471547
description: "transform kata manager with custom container runtime socket",
@@ -1595,8 +1595,8 @@ func TestTransformKataManager(t *testing.T) {
15951595
},
15961596
}).WithPullSecret("pull-secret").
15971597
WithPodAnnotations(map[string]string{"nvidia.com/kata-manager.last-applied-hash": "1929911998"}).
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)).
1598+
WithHostPathVolume("kata-artifacts", "/var/lib/kata", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
1599+
WithHostPathVolume("containerd-config", "/var/lib/rancher/k3s/agent/etc/containerd", internalptr.HostPathType(corev1.HostPathDirectoryOrCreate)).
16001600
WithHostPathVolume("containerd-socket", "/run/k3s/containerd", nil),
16011601
},
16021602
}

0 commit comments

Comments
 (0)