Skip to content
Draft
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
60 changes: 60 additions & 0 deletions cmd/gpuop-cfg/internal/images/clusterpolicy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
# Copyright (c), NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/

package images

import (
"fmt"

v1 "github.com/NVIDIA/gpu-operator/api/nvidia/v1"
)

type OperandImage struct {
Name string
Image string
}

func FromClusterPolicy(spec *v1.ClusterPolicySpec) ([]OperandImage, error) {
type operand struct {
name string
spec interface{}
}

operands := []operand{
{"Driver", &spec.Driver},
{"Toolkit", &spec.Toolkit},
{"DevicePlugin", &spec.DevicePlugin},
{"DCGMExporter", &spec.DCGMExporter},
{"DCGM", &spec.DCGM},
{"GPUFeatureDiscovery", &spec.GPUFeatureDiscovery},
{"MIGManager", &spec.MIGManager},
{"GPUDirectStorage", spec.GPUDirectStorage},
{"VFIOManager", &spec.VFIOManager},
{"SandboxDevicePlugin", &spec.SandboxDevicePlugin},
{"VGPUDeviceManager", &spec.VGPUDeviceManager},
}

var images []OperandImage
for _, op := range operands {
path, err := v1.ImagePath(op.spec)
if err != nil {
return nil, fmt.Errorf("failed to construct image path for %s: %v", op.name, err)
}
images = append(images, OperandImage{Name: op.name, Image: path})
}

return images, nil
}
151 changes: 151 additions & 0 deletions cmd/gpuop-cfg/internal/images/clusterpolicy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**
# Copyright (c), NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/

package images

import (
"testing"

v1 "github.com/NVIDIA/gpu-operator/api/nvidia/v1"
)

func newClusterPolicySpec() *v1.ClusterPolicySpec {
return &v1.ClusterPolicySpec{
Driver: v1.DriverSpec{
Repository: "nvcr.io/nvidia",
Image: "driver",
Version: "550.127.05",
},
Toolkit: v1.ToolkitSpec{
Repository: "nvcr.io/nvidia/k8s",
Image: "container-toolkit",
Version: "v1.16.1",
},
DevicePlugin: v1.DevicePluginSpec{
Repository: "nvcr.io/nvidia",
Image: "k8s-device-plugin",
Version: "v0.16.1",
},
DCGMExporter: v1.DCGMExporterSpec{
Repository: "nvcr.io/nvidia/k8s",
Image: "dcgm-exporter",
Version: "3.3.6",
},
DCGM: v1.DCGMSpec{
Repository: "nvcr.io/nvidia/cloud-native",
Image: "dcgm",
Version: "3.3.6",
},
GPUFeatureDiscovery: v1.GPUFeatureDiscoverySpec{
Repository: "nvcr.io/nvidia",
Image: "gpu-feature-discovery",
Version: "v0.16.1",
},
MIGManager: v1.MIGManagerSpec{
Repository: "nvcr.io/nvidia/cloud-native",
Image: "k8s-mig-manager",
Version: "v0.8.0",
},
GPUDirectStorage: &v1.GPUDirectStorageSpec{
Repository: "nvcr.io/nvidia/cloud-native",
Image: "nvidia-fs",
Version: "2.20.5",
},
VFIOManager: v1.VFIOManagerSpec{
Repository: "nvcr.io/nvidia",
Image: "vfio-manager",
Version: "v0.4.0",
},
SandboxDevicePlugin: v1.SandboxDevicePluginSpec{
Repository: "nvcr.io/nvidia",
Image: "kubevirt-gpu-device-plugin",
Version: "v1.2.7",
},
VGPUDeviceManager: v1.VGPUDeviceManagerSpec{
Repository: "nvcr.io/nvidia/cloud-native",
Image: "vgpu-device-manager",
Version: "v0.2.7",
},
}
}

func Test_FromClusterPolicy(t *testing.T) {
tests := []struct {
name string
spec *v1.ClusterPolicySpec
wantImages []OperandImage
}{
{
name: "constructs image paths from repository, image, and version",
spec: newClusterPolicySpec(),
wantImages: []OperandImage{
{Name: "Driver", Image: "nvcr.io/nvidia/driver:550.127.05"},
{Name: "Toolkit", Image: "nvcr.io/nvidia/k8s/container-toolkit:v1.16.1"},
{Name: "DevicePlugin", Image: "nvcr.io/nvidia/k8s-device-plugin:v0.16.1"},
{Name: "DCGMExporter", Image: "nvcr.io/nvidia/k8s/dcgm-exporter:3.3.6"},
{Name: "DCGM", Image: "nvcr.io/nvidia/cloud-native/dcgm:3.3.6"},
{Name: "GPUFeatureDiscovery", Image: "nvcr.io/nvidia/gpu-feature-discovery:v0.16.1"},
{Name: "MIGManager", Image: "nvcr.io/nvidia/cloud-native/k8s-mig-manager:v0.8.0"},
{Name: "GPUDirectStorage", Image: "nvcr.io/nvidia/cloud-native/nvidia-fs:2.20.5"},
{Name: "VFIOManager", Image: "nvcr.io/nvidia/vfio-manager:v0.4.0"},
{Name: "SandboxDevicePlugin", Image: "nvcr.io/nvidia/kubevirt-gpu-device-plugin:v1.2.7"},
{Name: "VGPUDeviceManager", Image: "nvcr.io/nvidia/cloud-native/vgpu-device-manager:v0.2.7"},
},
},
{
name: "uses image as full path when repository and version are empty",
spec: func() *v1.ClusterPolicySpec {
s := newClusterPolicySpec()
s.Driver = v1.DriverSpec{
Image: "nvcr.io/nvidia/driver:550.127.05",
}
return s
}(),
wantImages: []OperandImage{
{Name: "Driver", Image: "nvcr.io/nvidia/driver:550.127.05"},
{Name: "Toolkit", Image: "nvcr.io/nvidia/k8s/container-toolkit:v1.16.1"},
{Name: "DevicePlugin", Image: "nvcr.io/nvidia/k8s-device-plugin:v0.16.1"},
{Name: "DCGMExporter", Image: "nvcr.io/nvidia/k8s/dcgm-exporter:3.3.6"},
{Name: "DCGM", Image: "nvcr.io/nvidia/cloud-native/dcgm:3.3.6"},
{Name: "GPUFeatureDiscovery", Image: "nvcr.io/nvidia/gpu-feature-discovery:v0.16.1"},
{Name: "MIGManager", Image: "nvcr.io/nvidia/cloud-native/k8s-mig-manager:v0.8.0"},
{Name: "GPUDirectStorage", Image: "nvcr.io/nvidia/cloud-native/nvidia-fs:2.20.5"},
{Name: "VFIOManager", Image: "nvcr.io/nvidia/vfio-manager:v0.4.0"},
{Name: "SandboxDevicePlugin", Image: "nvcr.io/nvidia/kubevirt-gpu-device-plugin:v1.2.7"},
{Name: "VGPUDeviceManager", Image: "nvcr.io/nvidia/cloud-native/vgpu-device-manager:v0.2.7"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := FromClusterPolicy(tt.spec)
if err != nil {
t.Fatalf("FromClusterPolicy() unexpected error: %v", err)
}
if len(got) != len(tt.wantImages) {
t.Fatalf("FromClusterPolicy() returned %d images, want %d", len(got), len(tt.wantImages))
}
for i, op := range got {
if op.Name != tt.wantImages[i].Name {
t.Errorf("FromClusterPolicy()[%d].Name = %q, want %q", i, op.Name, tt.wantImages[i].Name)
}
if op.Image != tt.wantImages[i].Image {
t.Errorf("FromClusterPolicy()[%d].Image = %q, want %q", i, op.Image, tt.wantImages[i].Image)
}
}
})
}
}
44 changes: 44 additions & 0 deletions cmd/gpuop-cfg/internal/images/csv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
# Copyright (c), NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/

package images

import (
"strings"

"github.com/operator-framework/api/pkg/operators/v1alpha1"
)

func FromCSV(csv *v1alpha1.ClusterServiceVersion) []string {
var images []string

for _, image := range csv.Spec.RelatedImages {
images = append(images, image.Image)
}

deployment := csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs[0]
ctr := deployment.Spec.Template.Spec.Containers[0]
images = append(images, ctr.Image)

for _, env := range ctr.Env {
if !strings.HasSuffix(env.Name, "_IMAGE") {
continue
}
images = append(images, env.Value)
}

return images
}
104 changes: 104 additions & 0 deletions cmd/gpuop-cfg/internal/images/csv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
# Copyright (c), NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/

package images

import (
"testing"

"github.com/operator-framework/api/pkg/operators/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
)

func newCSV(relatedImages []v1alpha1.RelatedImage, containerImage string, envVars []corev1.EnvVar) *v1alpha1.ClusterServiceVersion {
return &v1alpha1.ClusterServiceVersion{
Spec: v1alpha1.ClusterServiceVersionSpec{
RelatedImages: relatedImages,
InstallStrategy: v1alpha1.NamedInstallStrategy{
StrategySpec: v1alpha1.StrategyDetailsDeployment{
DeploymentSpecs: []v1alpha1.StrategyDeploymentSpec{
{
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Image: containerImage,
Env: envVars,
},
},
},
},
},
},
},
},
},
},
}
}

func Test_FromCSV(t *testing.T) {
tests := []struct {
name string
csv *v1alpha1.ClusterServiceVersion
want []string
}{
{
name: "collects related images, container image, and IMAGE env vars",
csv: newCSV(
[]v1alpha1.RelatedImage{
{Image: "nvcr.io/nvidia/gpu-operator:v24.9.0"},
{Image: "nvcr.io/nvidia/driver:550.127.05"},
},
"nvcr.io/nvidia/gpu-operator:v24.9.0",
[]corev1.EnvVar{
{Name: "DRIVER_IMAGE", Value: "nvcr.io/nvidia/driver:550"},
{Name: "TOOLKIT_IMAGE", Value: "nvcr.io/nvidia/toolkit:1.16"},
{Name: "LOG_LEVEL", Value: "debug"},
},
),
want: []string{
"nvcr.io/nvidia/gpu-operator:v24.9.0",
"nvcr.io/nvidia/driver:550.127.05",
"nvcr.io/nvidia/gpu-operator:v24.9.0",
"nvcr.io/nvidia/driver:550",
"nvcr.io/nvidia/toolkit:1.16",
},
},
{
name: "no related images and no env vars",
csv: newCSV(nil, "nvcr.io/nvidia/gpu-operator:v24.9.0", nil),
want: []string{
"nvcr.io/nvidia/gpu-operator:v24.9.0",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FromCSV(tt.csv)
if len(got) != len(tt.want) {
t.Fatalf("FromCSV() returned %d images, want %d", len(got), len(tt.want))
}
for i, image := range got {
if image != tt.want[i] {
t.Errorf("FromCSV()[%d] = %q, want %q", i, image, tt.want[i])
}
}
})
}
}
Loading
Loading