Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions controllers/object_controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -201,7 +202,7 @@ const (
)

// rootUID represents user 0
var rootUID = utils.Int64Ptr(0)
var rootUID = ptr.To(int64(0))

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

func newHostPathType(pathType corev1.HostPathType) *corev1.HostPathType {
hostPathType := new(corev1.HostPathType)
*hostPathType = pathType
return hostPathType
}

// MountPathToVolumeSource maps a container mount path to a VolumeSource
type MountPathToVolumeSource map[string]corev1.VolumeSource

Expand All @@ -240,67 +235,67 @@ var SubscriptionPathMap = map[string](MountPathToVolumeSource){
"/run/secrets/etc-pki-entitlement": corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/etc/pki/entitlement",
Type: newHostPathType(corev1.HostPathDirectory),
Type: ptr.To(corev1.HostPathDirectory),
},
},
"/run/secrets/redhat.repo": corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/etc/yum.repos.d/redhat.repo",
Type: newHostPathType(corev1.HostPathFile),
Type: ptr.To(corev1.HostPathFile),
},
},
"/run/secrets/rhsm": corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/etc/rhsm",
Type: newHostPathType(corev1.HostPathDirectory),
Type: ptr.To(corev1.HostPathDirectory),
},
},
},
"rhcos": {
"/run/secrets/etc-pki-entitlement": corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/etc/pki/entitlement",
Type: newHostPathType(corev1.HostPathDirectory),
Type: ptr.To(corev1.HostPathDirectory),
},
},
"/run/secrets/redhat.repo": corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/etc/yum.repos.d/redhat.repo",
Type: newHostPathType(corev1.HostPathFile),
Type: ptr.To(corev1.HostPathFile),
},
},
"/run/secrets/rhsm": corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/etc/rhsm",
Type: newHostPathType(corev1.HostPathDirectory),
Type: ptr.To(corev1.HostPathDirectory),
},
},
},
"sles": {
"/etc/zypp/credentials.d": corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/etc/zypp/credentials.d",
Type: newHostPathType(corev1.HostPathDirectory),
Type: ptr.To(corev1.HostPathDirectory),
},
},
"/etc/SUSEConnect": corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/etc/SUSEConnect",
Type: newHostPathType(corev1.HostPathFileOrCreate),
Type: ptr.To(corev1.HostPathFileOrCreate),
},
},
},
"sl-micro": {
"/etc/zypp/credentials.d": corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/etc/zypp/credentials.d",
Type: newHostPathType(corev1.HostPathDirectory),
Type: ptr.To(corev1.HostPathDirectory),
},
},
"/etc/SUSEConnect": corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/etc/SUSEConnect",
Type: newHostPathType(corev1.HostPathFileOrCreate),
Type: ptr.To(corev1.HostPathFileOrCreate),
},
},
},
Expand Down Expand Up @@ -1385,7 +1380,7 @@ func transformForRuntime(obj *appsv1.DaemonSet, config *gpuv1.ClusterPolicySpec,
volMountConfig := corev1.VolumeMount{Name: volMountConfigName, MountPath: containerConfigDir}
container.VolumeMounts = append(container.VolumeMounts, volMountConfig)

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

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

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

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

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

// Compute hash of kata manager config and add an annotation with the value.
Expand Down
50 changes: 25 additions & 25 deletions controllers/transforms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ func TestTransformForRuntime(t *testing.T) {
input: NewDaemonset().
WithContainer(corev1.Container{Name: "test-ctr"}),
expectedOutput: NewDaemonset().
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
WithContainer(corev1.Container{
Name: "test-ctr",
Expand Down Expand Up @@ -410,8 +410,8 @@ func TestTransformForRuntime(t *testing.T) {
},
}),
expectedOutput: NewDaemonset().
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
WithContainer(corev1.Container{
Name: "test-ctr",
Expand Down Expand Up @@ -444,8 +444,8 @@ func TestTransformForRuntime(t *testing.T) {
},
}),
expectedOutput: NewDaemonset().
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
WithContainer(corev1.Container{
Name: "test-ctr",
Expand All @@ -472,8 +472,8 @@ func TestTransformForRuntime(t *testing.T) {
runtime: gpuv1.CRIO,
input: NewDaemonset().WithContainer(corev1.Container{Name: "test-ctr"}),
expectedOutput: NewDaemonset().
WithHostPathVolume("crio-config", "/etc/crio", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("crio-config", "/etc/crio", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithContainer(corev1.Container{
Name: "test-ctr",
Env: []corev1.EnvVar{
Expand All @@ -496,7 +496,7 @@ func TestTransformForRuntime(t *testing.T) {
input: NewDaemonset().
WithContainer(corev1.Container{Name: "nvidia-kata-manager"}),
expectedOutput: NewDaemonset().
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-config", filepath.Dir(DefaultContainerdConfigFile), ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-socket", filepath.Dir(DefaultContainerdSocketFile), nil).
WithContainer(corev1.Container{
Name: "nvidia-kata-manager",
Expand All @@ -519,7 +519,7 @@ func TestTransformForRuntime(t *testing.T) {
runtime: gpuv1.Docker,
input: NewDaemonset().WithContainer(corev1.Container{Name: "test-ctr"}),
expectedOutput: NewDaemonset().
WithHostPathVolume("docker-config", filepath.Dir(DefaultDockerConfigFile), newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("docker-config", filepath.Dir(DefaultDockerConfigFile), ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("docker-socket", filepath.Dir(DefaultDockerSocketFile), nil).
WithContainer(corev1.Container{
Name: "test-ctr",
Expand Down Expand Up @@ -840,8 +840,8 @@ func TestTransformToolkit(t *testing.T) {
{Name: "containerd-socket", MountPath: "/runtime/sock-dir/"},
},
}).
WithHostPathVolume("containerd-config", "/etc/containerd", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-config", "/etc/containerd", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-socket", "/run/containerd", nil).
WithPullSecret("pull-secret"),
},
Expand Down Expand Up @@ -919,8 +919,8 @@ func TestTransformToolkit(t *testing.T) {
{Name: "containerd-socket", MountPath: "/runtime/sock-dir/"},
},
}).
WithHostPathVolume("containerd-config", "/var/lib/rancher/k3s/agent/etc/containerd", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-config", "/var/lib/rancher/k3s/agent/etc/containerd", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-drop-in-config", "/etc/containerd/conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-socket", "/run/k3s/containerd", nil).
WithPullSecret("pull-secret"),
},
Expand Down Expand Up @@ -957,8 +957,8 @@ func TestTransformToolkit(t *testing.T) {
{Name: "crio-drop-in-config", MountPath: "/runtime/config-dir.d/"},
},
}).
WithHostPathVolume("crio-config", "/etc/crio", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)),
WithHostPathVolume("crio-config", "/etc/crio", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)),
},
{
description: "transform nvidia-container-toolkit-ctr container, cri-o runtime, cdi disabled",
Expand Down Expand Up @@ -993,8 +993,8 @@ func TestTransformToolkit(t *testing.T) {
{Name: "crio-drop-in-config", MountPath: "/runtime/config-dir.d/"},
},
}).
WithHostPathVolume("crio-config", "/etc/crio", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", newHostPathType(corev1.HostPathDirectoryOrCreate)),
WithHostPathVolume("crio-config", "/etc/crio", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("crio-drop-in-config", "/etc/crio/crio.conf.d", ptr.To(corev1.HostPathDirectoryOrCreate)),
},
}

Expand Down Expand Up @@ -1120,8 +1120,8 @@ func TestTransformMPSControlDaemon(t *testing.T) {
daemonset: NewDaemonset().
WithInitContainer(corev1.Container{Name: "mps-control-daemon-mounts"}).
WithContainer(corev1.Container{Name: "mps-control-daemon-ctr"}).
WithHostPathVolume("mps-root", "/run/nvidia/mps", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("mps-shm", "/run/nvidia/mps/shm", newHostPathType(corev1.HostPathDirectoryOrCreate)),
WithHostPathVolume("mps-root", "/run/nvidia/mps", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("mps-shm", "/run/nvidia/mps/shm", ptr.To(corev1.HostPathDirectoryOrCreate)),
clusterPolicySpec: &gpuv1.ClusterPolicySpec{
DevicePlugin: gpuv1.DevicePluginSpec{
Repository: "nvcr.io",
Expand All @@ -1146,8 +1146,8 @@ func TestTransformMPSControlDaemon(t *testing.T) {
{Name: "NVIDIA_MIG_MONITOR_DEVICES", Value: "all"},
},
}).
WithHostPathVolume("mps-root", "/var/mps", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("mps-shm", "/var/mps/shm", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("mps-root", "/var/mps", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("mps-shm", "/var/mps/shm", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithPullSecret("secret").
WithRuntimeClassName("nvidia"),
},
Expand Down Expand Up @@ -1540,7 +1540,7 @@ func TestTransformKataManager(t *testing.T) {
{Name: "containerd-config", MountPath: "/runtime/config-dir/"},
{Name: "containerd-socket", MountPath: "/runtime/sock-dir/"},
},
}).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),
}).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),
},
{
description: "transform kata manager with custom container runtime socket",
Expand Down Expand Up @@ -1594,8 +1594,8 @@ func TestTransformKataManager(t *testing.T) {
},
}).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", "/var/lib/rancher/k3s/agent/etc/containerd", newHostPathType(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("kata-artifacts", "/var/lib/kata", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-config", "/var/lib/rancher/k3s/agent/etc/containerd", ptr.To(corev1.HostPathDirectoryOrCreate)).
WithHostPathVolume("containerd-socket", "/run/k3s/containerd", nil),
},
}
Expand Down
Loading