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

Check failure on line 26 in cmd/gpuop-cfg/internal/images/clusterpolicy.go

View workflow job for this annotation

GitHub Actions / go-check

File is not properly formatted (gofmt)
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
}
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
}
134 changes: 134 additions & 0 deletions cmd/gpuop-cfg/list-images/list-images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
# 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 listimages

import (
"context"
"fmt"
"io"
"os"

"github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/sirupsen/logrus"
cli "github.com/urfave/cli/v3"
"sigs.k8s.io/yaml"

v1 "github.com/NVIDIA/gpu-operator/api/nvidia/v1"
"github.com/NVIDIA/gpu-operator/cmd/gpuop-cfg/internal/images"
)

type options struct {
input string
}

func NewCommand(_ *logrus.Logger) *cli.Command {
listImages := cli.Command{
Name: "list-images",
Usage: "List container images referenced in GPU Operator configuration files",
}

listImages.Commands = []*cli.Command{
buildCSV(),
buildClusterPolicy(),
}

return &listImages
}

func buildCSV() *cli.Command {
opts := options{}

c := cli.Command{
Name: "csv",
Usage: "List images from a ClusterServiceVersion manifest",
Action: func(ctx context.Context, cmd *cli.Command) error {
contents, err := getContents(opts.input)
if err != nil {
return fmt.Errorf("failed to read file: %v", err)
}

spec := &v1alpha1.ClusterServiceVersion{}
if err := yaml.Unmarshal(contents, spec); err != nil {
return fmt.Errorf("failed to unmarshal csv: %v", err)
}

for _, image := range images.FromCSV(spec) {
fmt.Println(image)
}
return nil
},
}

c.Flags = []cli.Flag{
&cli.StringFlag{
Name: "input",
Usage: "Specify the input file. If this is '-' the file is read from STDIN",
Value: "-",
Destination: &opts.input,
},
}

return &c
}

func buildClusterPolicy() *cli.Command {
opts := options{}

c := cli.Command{
Name: "clusterpolicy",
Usage: "List images from a ClusterPolicy manifest",
Action: func(ctx context.Context, cmd *cli.Command) error {
contents, err := getContents(opts.input)
if err != nil {
return fmt.Errorf("failed to read file: %v", err)
}

spec := &v1.ClusterPolicy{}
if err := yaml.Unmarshal(contents, spec); err != nil {
return fmt.Errorf("failed to unmarshal clusterpolicy: %v", err)
}

operandImages, err := images.FromClusterPolicy(&spec.Spec)
if err != nil {
return err
}

for _, op := range operandImages {
fmt.Println(op.Image)
}
return nil
},
}

c.Flags = []cli.Flag{
&cli.StringFlag{
Name: "input",
Usage: "Specify the input file. If this is '-' the file is read from STDIN",
Value: "-",
Destination: &opts.input,
},
}

return &c
}

func getContents(input string) ([]byte, error) {
if input == "-" {
return io.ReadAll(os.Stdin)
}
return os.ReadFile(input)
}
2 changes: 2 additions & 0 deletions cmd/gpuop-cfg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
log "github.com/sirupsen/logrus"
cli "github.com/urfave/cli/v3"

listimages "github.com/NVIDIA/gpu-operator/cmd/gpuop-cfg/list-images"
"github.com/NVIDIA/gpu-operator/cmd/gpuop-cfg/validate"
)

Expand Down Expand Up @@ -66,6 +67,7 @@ func main() {
// Define the subcommands
c.Commands = []*cli.Command{
validate.NewCommand(logger),
listimages.NewCommand(logger),
}

err := c.Run(context.Background(), os.Args)
Expand Down
Loading
Loading