Skip to content

Commit 05279b1

Browse files
Replace kubectl usage with utility program to apply CRDs
kubectl is heavyweight and often has CVEs, so we are forced to bump its version even if the final gpu-operator image does not contain kubectl bits. This change lets us remove that dependency and use client-go functions to manage CRDs. Signed-off-by: Rajath Agasthya <[email protected]>
1 parent c5d513d commit 05279b1

File tree

85 files changed

+19995
-98
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+19995
-98
lines changed

cmd/apply-crds/main.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
Copyright (c), NVIDIA CORPORATION. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"os"
23+
24+
"github.com/NVIDIA/k8s-operator-libs/pkg/crdutil"
25+
log "github.com/sirupsen/logrus"
26+
"github.com/urfave/cli/v2"
27+
28+
"github.com/NVIDIA/gpu-operator/internal/info"
29+
)
30+
31+
var logger = log.New()
32+
33+
type config struct {
34+
Debug bool
35+
crdsPaths *cli.StringSlice
36+
}
37+
38+
func main() {
39+
config := config{
40+
crdsPaths: cli.NewStringSlice(),
41+
}
42+
43+
// Create the top-level CLI
44+
c := cli.NewApp()
45+
c.Name = "apply-crds"
46+
c.Usage = "Tools for managing Custom Resource Definitions (CRDs) for NVIDIA GPU Operator"
47+
c.Version = info.GetVersionString()
48+
49+
// Setup the flags for this command
50+
c.Flags = []cli.Flag{
51+
&cli.BoolFlag{
52+
Name: "debug",
53+
Aliases: []string{"d"},
54+
Usage: "Enable debug-level logging",
55+
Destination: &config.Debug,
56+
EnvVars: []string{"DEBUG"},
57+
},
58+
}
59+
60+
// Set log-level for all subcommands
61+
c.Before = func(c *cli.Context) error {
62+
logLevel := log.InfoLevel
63+
if config.Debug {
64+
logLevel = log.DebugLevel
65+
}
66+
logger.SetLevel(logLevel)
67+
return nil
68+
}
69+
70+
// Common flags for both apply and delete subcommands
71+
commonFlags := []cli.Flag{
72+
&cli.StringSliceFlag{
73+
Name: "crds-path",
74+
Usage: "Path to CRD manifest file or directory (can be specified multiple times, directories are searched recursively)",
75+
Required: true,
76+
Destination: config.crdsPaths,
77+
},
78+
}
79+
80+
// Define the subcommands
81+
c.Commands = []*cli.Command{
82+
{
83+
Name: "apply",
84+
Usage: "Apply CRDs from the specified path",
85+
Flags: commonFlags,
86+
Action: func(c *cli.Context) error {
87+
return runApply(c.Context, config)
88+
},
89+
},
90+
{
91+
Name: "delete",
92+
Usage: "Delete CRDs from the specified path",
93+
Flags: commonFlags,
94+
Action: func(c *cli.Context) error {
95+
return runDelete(c.Context, config)
96+
},
97+
},
98+
}
99+
100+
err := c.Run(os.Args)
101+
if err != nil {
102+
log.Errorf("%v", err)
103+
log.Exit(1)
104+
}
105+
}
106+
107+
func runApply(ctx context.Context, cfg config) error {
108+
paths := cfg.crdsPaths.Value()
109+
logger.Infof("Applying CRDs from %d path(s): %v", len(paths), paths)
110+
111+
if err := crdutil.ProcessCRDs(ctx, crdutil.CRDOperationApply, paths...); err != nil {
112+
return fmt.Errorf("failed to apply CRDs: %w", err)
113+
}
114+
115+
logger.Info("Successfully applied CRDs")
116+
return nil
117+
}
118+
119+
func runDelete(ctx context.Context, cfg config) error {
120+
paths := cfg.crdsPaths.Value()
121+
logger.Infof("Deleting CRDs from %d path(s): %v", len(paths), paths)
122+
123+
if err := crdutil.ProcessCRDs(ctx, crdutil.CRDOperationDelete, paths...); err != nil {
124+
return fmt.Errorf("failed to delete CRDs: %w", err)
125+
}
126+
127+
logger.Info("Successfully deleted CRDs")
128+
return nil
129+
}

deployments/gpu-operator/templates/cleanup_crd.yaml

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,12 @@ spec:
3535
image: {{ include "gpu-operator.fullimage" . }}
3636
imagePullPolicy: {{ .Values.operator.imagePullPolicy }}
3737
command:
38-
- sh
39-
- -c
40-
- >
41-
kubectl delete clusterpolicy cluster-policy;
42-
kubectl delete crd clusterpolicies.nvidia.com;
43-
kubectl delete crd nvidiadrivers.nvidia.com --ignore-not-found=true;
44-
{{- if .Values.nfd.enabled -}}
45-
kubectl delete crd nodefeatures.nfd.k8s-sigs.io --ignore-not-found=true;
46-
kubectl delete crd nodefeaturegroups.nfd.k8s-sigs.io --ignore-not-found=true;
47-
kubectl delete crd nodefeaturerules.nfd.k8s-sigs.io --ignore-not-found=true;
48-
{{- end }}
38+
- /usr/bin/apply-crds
39+
args:
40+
- --crds-path=/opt/gpu-operator/nvidia.com_clusterpolicies.yaml
41+
- --crds-path=/opt/gpu-operator/nvidia.com_nvidiadrivers.yaml;
42+
{{- if .Values.nfd.enabled }}
43+
- --crds-path=/opt/gpu-operator/nfd-api-crds.yaml;
44+
{{- end }}
4945
restartPolicy: OnFailure
5046
{{- end }}

deployments/gpu-operator/templates/upgrade_crd.yaml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,12 @@ spec:
8383
image: {{ include "gpu-operator.fullimage" . }}
8484
imagePullPolicy: {{ .Values.operator.imagePullPolicy }}
8585
command:
86-
- sh
87-
- -c
88-
- >
89-
kubectl apply -f /opt/gpu-operator/nvidia.com_clusterpolicies.yaml;
90-
kubectl apply -f /opt/gpu-operator/nvidia.com_nvidiadrivers.yaml;
86+
- /usr/bin/apply-crds
87+
args:
88+
- --crds-path=/opt/gpu-operator/nvidia.com_clusterpolicies.yaml
89+
- --crds-path=/opt/gpu-operator/nvidia.com_nvidiadrivers.yaml;
9190
{{- if .Values.nfd.enabled }}
92-
kubectl apply -f /opt/gpu-operator/nfd-api-crds.yaml;
91+
- --crds-path=/opt/gpu-operator/nfd-api-crds.yaml;
9392
{{- end }}
9493
restartPolicy: OnFailure
9594
{{- end }}

docker/Dockerfile

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ RUN dnf install -y --allowerasing \
4747

4848
WORKDIR /workspace
4949

50-
# Install must-gather dependency: `kubectl`
51-
ARG TARGETARCH
52-
RUN OS_ARCH=${TARGETARCH/x86_64/amd64} && OS_ARCH=${OS_ARCH/aarch64/arm64} && \
53-
curl -LO https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/${OS_ARCH}/kubectl && \
54-
chmod +x ./kubectl
55-
5650
FROM nvcr.io/nvidia/cuda:12.9.1-base-ubi9 AS sample-builder
5751

5852
RUN dnf install -y --allowerasing \
@@ -96,7 +90,7 @@ LABEL vsc-ref=${GIT_COMMIT}
9690

9791
WORKDIR /
9892
COPY --from=builder /workspace/gpu-operator /usr/bin/
99-
COPY --from=cuda-base /workspace/kubectl /usr/bin/
93+
COPY --from=builder /workspace/apply-crds /usr/bin/
10094
COPY --from=builder /workspace/nvidia-validator /usr/bin/
10195
COPY --from=sample-builder /build/vectorAdd /usr/bin/vectorAdd
10296
# TODO: Copy the compat libs from the 'sample-builder' image instead.

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ require (
66
github.com/Masterminds/sprig/v3 v3.3.0
77
github.com/NVIDIA/go-nvlib v0.8.1
88
github.com/NVIDIA/k8s-kata-manager v0.2.3
9-
github.com/NVIDIA/k8s-operator-libs v0.0.0-20250709180754-c80af13d73e3
9+
github.com/NVIDIA/k8s-operator-libs v0.0.0-20251027171627-45ccd0c3dd32
1010
github.com/NVIDIA/nvidia-container-toolkit v1.18.0
1111
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
1212
github.com/go-logr/logr v1.4.3
13-
github.com/onsi/ginkgo/v2 v2.26.0
13+
github.com/onsi/ginkgo/v2 v2.27.1
1414
github.com/onsi/gomega v1.38.2
1515
github.com/openshift/api v0.0.0-20251021124544-a2cb0c5d994d
1616
github.com/openshift/client-go v0.0.0-20251015124057-db0dee36e235

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ github.com/NVIDIA/go-nvlib v0.8.1 h1:OPEHVvn3zcV5OXB68A7WRpeCnYMRSPl7LdeJH/d3gZI
1616
github.com/NVIDIA/go-nvlib v0.8.1/go.mod h1:7mzx9FSdO9fXWP9NKuZmWkCwhkEcSWQFe2tmFwtLb9c=
1717
github.com/NVIDIA/k8s-kata-manager v0.2.3 h1:d5+gRFqU5el/fKMXhHUaPY7haj+dbHL4nDsO/q05LBo=
1818
github.com/NVIDIA/k8s-kata-manager v0.2.3/go.mod h1:xx5OUiMsHyKbyX0JjKHqAftvqS8vx00LFn/5EaMdtB4=
19-
github.com/NVIDIA/k8s-operator-libs v0.0.0-20250709180754-c80af13d73e3 h1:vGT+oyUY7kOGLd71Cz0NfRVEep23jdd4fi+PYsZEj88=
20-
github.com/NVIDIA/k8s-operator-libs v0.0.0-20250709180754-c80af13d73e3/go.mod h1:0GPZJRwr6nY1IVhGUyzG9YfKhNFQq8UlhYe4u7jVF0U=
19+
github.com/NVIDIA/k8s-operator-libs v0.0.0-20251027171627-45ccd0c3dd32 h1:TWudaaTt7QwN/cQwPOm1wgesGLOc8hoik9GubKgnph0=
20+
github.com/NVIDIA/k8s-operator-libs v0.0.0-20251027171627-45ccd0c3dd32/go.mod h1:WbVhWGKqRcwjRKj8MYsYJas73G1YdU3oLW5ggDvTWXs=
2121
github.com/NVIDIA/nvidia-container-toolkit v1.18.0 h1:bXoKq9C1WHU5fF6VqXvX3RkMzpp4ihTUgBPrh66vTf0=
2222
github.com/NVIDIA/nvidia-container-toolkit v1.18.0/go.mod h1:ZxWSG7fnFo2Z7xSGtMyZVF7WnTbj1lgx4dMrBLUq90g=
2323
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
@@ -59,8 +59,8 @@ github.com/gkampitakis/ciinfo v0.3.2 h1:JcuOPk8ZU7nZQjdUhctuhQofk7BGHuIy0c9Ez8BN
5959
github.com/gkampitakis/ciinfo v0.3.2/go.mod h1:1NIwaOcFChN4fa/B0hEBdAb6npDlFL8Bwx4dfRLRqAo=
6060
github.com/gkampitakis/go-diff v1.3.2 h1:Qyn0J9XJSDTgnsgHRdz9Zp24RaJeKMUHg2+PDZZdC4M=
6161
github.com/gkampitakis/go-diff v1.3.2/go.mod h1:LLgOrpqleQe26cte8s36HTWcTmMEur6OPYerdAAS9tk=
62-
github.com/gkampitakis/go-snaps v0.5.14 h1:3fAqdB6BCPKHDMHAKRwtPUwYexKtGrNuw8HX/T/4neo=
63-
github.com/gkampitakis/go-snaps v0.5.14/go.mod h1:HNpx/9GoKisdhw9AFOBT1N7DBs9DiHo/hGheFGBZ+mc=
62+
github.com/gkampitakis/go-snaps v0.5.15 h1:amyJrvM1D33cPHwVrjo9jQxX8g/7E2wYdZ+01KS3zGE=
63+
github.com/gkampitakis/go-snaps v0.5.15/go.mod h1:HNpx/9GoKisdhw9AFOBT1N7DBs9DiHo/hGheFGBZ+mc=
6464
github.com/go-errors/errors v1.5.1 h1:ZwEMSLRCapFLflTpT7NKaAc7ukJ8ZPEjzlxt8rPN8bk=
6565
github.com/go-errors/errors v1.5.1/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
6666
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
@@ -150,8 +150,8 @@ github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J
150150
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
151151
github.com/olareg/olareg v0.1.2 h1:75G8X6E9FUlzL/CSjgFcYfMgNzlc7CxULpUUNsZBIvI=
152152
github.com/olareg/olareg v0.1.2/go.mod h1:TWs+N6pO1S4bdB6eerzUm/ITRQ6kw91mVf9ZYeGtw+Y=
153-
github.com/onsi/ginkgo/v2 v2.26.0 h1:1J4Wut1IlYZNEAWIV3ALrT9NfiaGW2cDCJQSFQMs/gE=
154-
github.com/onsi/ginkgo/v2 v2.26.0/go.mod h1:qhEywmzWTBUY88kfO0BRvX4py7scov9yR+Az2oavUzw=
153+
github.com/onsi/ginkgo/v2 v2.27.1 h1:0LJC8MpUSQnfnp4n/3W3GdlmJP3ENGF0ZPzjQGLPP7s=
154+
github.com/onsi/ginkgo/v2 v2.27.1/go.mod h1:wmy3vCqiBjirARfVhAqFpYt8uvX0yaFe+GudAqqcCqA=
155155
github.com/onsi/gomega v1.38.2 h1:eZCjf2xjZAqe+LeWvKb5weQ+NcPwX84kqJ0cZNxok2A=
156156
github.com/onsi/gomega v1.38.2/go.mod h1:W2MJcYxRGV63b418Ai34Ud0hEdTVXq9NW9+Sx6uXf3k=
157157
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=

vendor/github.com/NVIDIA/k8s-operator-libs/pkg/crdutil/README.md

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)