• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NVIDIA / gpu-operator / 29513807383

16 Jul 2026 03:58PM UTC coverage: 32.688% (+0.9%) from 31.812%
29513807383

Pull #2571

github

karthikvetrivel
Add golden render tests for GPUCluster operand manifests

Signed-off-by: Karthik Vetrivel <kvetrivel@nvidia.com>
Pull Request #2571: Add GPUCluster CRD and controller for DRA-based stack

513 of 1343 new or added lines in 31 files covered. (38.2%)

5 existing lines in 4 files now uncovered.

4663 of 14265 relevant lines covered (32.69%)

0.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/cmd/gpu-operator/main.go
1
/*
2
Copyright 2021.
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
        "flag"
21
        "fmt"
22
        "os"
23
        "strings"
24
        "time"
25

26
        // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
27
        // to ensure that exec-entrypoint and run can make use of them.
28
        "go.uber.org/zap/zapcore"
29
        _ "k8s.io/client-go/plugin/pkg/client/auth"
30
        "sigs.k8s.io/controller-runtime/pkg/cache"
31

32
        "github.com/NVIDIA/k8s-operator-libs/pkg/upgrade"
33
        apiconfigv1 "github.com/openshift/api/config/v1"
34
        apiimagev1 "github.com/openshift/api/image/v1"
35
        secv1 "github.com/openshift/api/security/v1"
36
        promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
37
        corev1 "k8s.io/api/core/v1"
38
        apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
39
        "k8s.io/apimachinery/pkg/runtime"
40
        utilruntime "k8s.io/apimachinery/pkg/util/runtime"
41
        clientgoscheme "k8s.io/client-go/kubernetes/scheme"
42
        ctrl "sigs.k8s.io/controller-runtime"
43
        "sigs.k8s.io/controller-runtime/pkg/healthz"
44
        "sigs.k8s.io/controller-runtime/pkg/log/zap"
45
        metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
46
        "sigs.k8s.io/controller-runtime/pkg/webhook"
47

48
        clusterpolicyv1 "github.com/NVIDIA/gpu-operator/api/nvidia/v1"
49
        nvidiav1alpha1 "github.com/NVIDIA/gpu-operator/api/nvidia/v1alpha1"
50
        "github.com/NVIDIA/gpu-operator/controllers"
51
        "github.com/NVIDIA/gpu-operator/controllers/clusterinfo"
52
        "github.com/NVIDIA/gpu-operator/internal/consts"
53
        "github.com/NVIDIA/gpu-operator/internal/info"
54
        "github.com/NVIDIA/gpu-operator/internal/predicates"
55
        // +kubebuilder:scaffold:imports
56
)
57

58
var (
59
        scheme   = runtime.NewScheme()
60
        setupLog = ctrl.Log.WithName("setup")
61
)
62

63
func init() {
×
64
        utilruntime.Must(clientgoscheme.AddToScheme(scheme))
×
65
        utilruntime.Must(clusterpolicyv1.AddToScheme(scheme))
×
66
        utilruntime.Must(apiextensionsv1.AddToScheme(scheme))
×
67
        utilruntime.Must(nvidiav1alpha1.AddToScheme(scheme))
×
68
        utilruntime.Must(promv1.AddToScheme(scheme))
×
69
        utilruntime.Must(secv1.Install(scheme))
×
70
        utilruntime.Must(apiconfigv1.Install(scheme))
×
71
        utilruntime.Must(apiimagev1.Install(scheme))
×
72
}
×
73

74
func main() {
×
75
        var metricsAddr string
×
76
        var enableLeaderElection bool
×
77
        var leaderElectionNamespace string
×
78
        var probeAddr string
×
79
        var renewDeadline time.Duration
×
80

×
81
        flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
×
82
        flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
×
83
        flag.BoolVar(&enableLeaderElection, "leader-elect", false,
×
84
                "Enable leader election for controller manager. "+
×
85
                        "Enabling this will ensure there is only one active controller manager.")
×
86
        flag.StringVar(&leaderElectionNamespace, "leader-elect-namespace", "",
×
87
                "Set the leader lease namespace for the controller manager. "+
×
88
                        "If undefined, the leader election namespace defaults to the namespace the operator is running in.")
×
89
        flag.DurationVar(&renewDeadline, "leader-lease-renew-deadline", 0,
×
90
                "Set the leader lease renew deadline duration (e.g. \"10s\") of the controller manager. "+
×
91
                        "Only enabled when the --leader-elect flag is set. "+
×
92
                        "If undefined, the renew deadline defaults to the controller-runtime manager's default RenewDeadline. "+
×
93
                        "By setting this option, the LeaseDuration is also set as RenewDealine + 5s.")
×
94

×
95
        opts := zap.Options{
×
96
                StacktraceLevel: zapcore.PanicLevel,
×
97
        }
×
98
        opts.BindFlags(flag.CommandLine)
×
99
        flag.Parse()
×
100

×
101
        logger := zap.New(zap.UseFlagOptions(&opts))
×
102
        ctrl.SetLogger(logger)
×
103

×
104
        ctrl.Log.Info(fmt.Sprintf("version: %s", info.GetVersionString()))
×
105

×
106
        metricsOptions := metricsserver.Options{
×
107
                BindAddress: metricsAddr,
×
108
        }
×
109

×
110
        webhookServer := webhook.NewServer(webhook.Options{
×
111
                Port: 9443,
×
112
        })
×
113

×
114
        operatorNamespace := os.Getenv("OPERATOR_NAMESPACE")
×
115

×
116
        if operatorNamespace == "" {
×
117
                logger.Error(nil, "OPERATOR_NAMESPACE environment variable not set, cannot proceed")
×
118
                // we cannot do anything without the operator namespace,
×
119
                // let the operator Pod run into `CrashloopBackOff`
×
120

×
121
                os.Exit(1)
×
122
        }
×
123

124
        openshiftNamespace := consts.OpenshiftNamespace
×
125
        cacheOptions := cache.Options{
×
126
                DefaultNamespaces: map[string]cache.Config{
×
127
                        operatorNamespace: {},
×
128
                        // Also cache resources in the openshift namespace to retrieve ImageStreams when on an openshift  cluster
×
129
                        openshiftNamespace: {},
×
130
                },
×
131
        }
×
132

×
133
        options := ctrl.Options{
×
134
                Scheme:                  scheme,
×
135
                Metrics:                 metricsOptions,
×
136
                HealthProbeBindAddress:  probeAddr,
×
137
                LeaderElection:          enableLeaderElection,
×
138
                LeaderElectionNamespace: leaderElectionNamespace,
×
139
                LeaderElectionID:        "53822513.nvidia.com",
×
140
                WebhookServer:           webhookServer,
×
141
                Cache:                   cacheOptions,
×
142
        }
×
143

×
144
        if enableLeaderElection && int(renewDeadline) != 0 {
×
145
                leaseDuration := renewDeadline + 5*time.Second
×
146

×
147
                options.RenewDeadline = &renewDeadline
×
148
                options.LeaseDuration = &leaseDuration
×
149
        }
×
150

151
        mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
×
152
        if err != nil {
×
153
                setupLog.Error(err, "unable to start manager")
×
154
                os.Exit(1)
×
155
        }
×
156

157
        ctx := ctrl.SetupSignalHandler()
×
158

×
159
        setupLog.Info("initializing operator metrics")
×
160
        operatorMetrics := controllers.InitOperatorMetrics()
×
161

×
162
        if err = (&controllers.ClusterPolicyReconciler{
×
163
                Namespace:       operatorNamespace,
×
164
                Client:          mgr.GetClient(),
×
165
                Log:             ctrl.Log.WithName("controllers").WithName("ClusterPolicy"),
×
166
                Scheme:          mgr.GetScheme(),
×
167
                OperatorMetrics: operatorMetrics,
×
168
        }).SetupWithManager(ctx, mgr); err != nil {
×
169
                setupLog.Error(err, "unable to create controller", "controller", "ClusterPolicy")
×
170
                os.Exit(1)
×
171
        }
×
172

173
        // setup upgrade controller
174
        upgrade.SetDriverName("gpu")
×
175
        upgradeLogger := ctrl.Log.WithName("controllers").WithName("Upgrade")
×
176
        clusterUpgradeStateManager, err := upgrade.NewClusterUpgradeStateManager(
×
177
                upgradeLogger,
×
178
                mgr.GetConfig(),
×
179
                // nolint:staticcheck
×
180
                // TODO: update k8s-operator-libs to leverage events.EventRecorder instead
×
181
                mgr.GetEventRecorderFor("nvidia-gpu-operator"),
×
182
                upgrade.StateOptions{},
×
183
        )
×
184
        if err != nil {
×
185
                setupLog.Error(err, "unable to create new ClusterUpdateStateManager", "controller", "Upgrade")
×
186
                os.Exit(1)
×
187
        }
×
188
        clusterUpgradeStateManager = clusterUpgradeStateManager.
×
189
                WithPodDeletionEnabled(gpuPodSpecFilter).
×
190
                WithValidationEnabled("app=nvidia-operator-validator").
×
191
                WithRestartOnlyPredicate(predicates.DriverPodRestartOnly(upgradeLogger))
×
192

×
193
        if err = (&controllers.UpgradeReconciler{
×
194
                Client:          mgr.GetClient(),
×
195
                Log:             upgradeLogger,
×
196
                Scheme:          mgr.GetScheme(),
×
197
                StateManager:    clusterUpgradeStateManager,
×
198
                OperatorMetrics: operatorMetrics,
×
199
        }).SetupWithManager(ctx, mgr); err != nil {
×
200
                setupLog.Error(err, "unable to create controller", "controller", "Upgrade")
×
201
                os.Exit(1)
×
202
        }
×
203

204
        clusterInfo, err := clusterinfo.New(
×
205
                ctx,
×
206
                clusterinfo.WithKubernetesConfig(mgr.GetConfig()),
×
207
                clusterinfo.WithOneShot(false),
×
208
        )
×
209
        if err != nil {
×
210
                setupLog.Error(err, "failed to get cluster wide information needed by controllers")
×
211
                os.Exit(1)
×
212
        }
×
213

214
        if err = (&controllers.NVIDIADriverReconciler{
×
215
                Namespace:   operatorNamespace,
×
216
                Client:      mgr.GetClient(),
×
217
                Scheme:      mgr.GetScheme(),
×
218
                ClusterInfo: clusterInfo,
×
219
        }).SetupWithManager(ctx, mgr); err != nil {
×
220
                setupLog.Error(err, "unable to create controller", "controller", "NVIDIADriver")
×
221
                os.Exit(1)
×
222
        }
×
223

224
        if err = (&controllers.NodeLabelingReconciler{
×
225
                Namespace: operatorNamespace,
×
226
                Client:    mgr.GetClient(),
×
227
                Scheme:    mgr.GetScheme(),
×
228
                Log:       ctrl.Log.WithName("controllers").WithName("NodeLabeling"),
×
229
        }).SetupWithManager(ctx, mgr); err != nil {
×
230
                setupLog.Error(err, "unable to create controller", "controller", "NodeLabeling")
×
231
                os.Exit(1)
×
232
        }
×
233

NEW
234
        if err = (&controllers.GPUClusterReconciler{
×
NEW
235
                Namespace:   operatorNamespace,
×
NEW
236
                Client:      mgr.GetClient(),
×
NEW
237
                Scheme:      mgr.GetScheme(),
×
NEW
238
                ClusterInfo: clusterInfo,
×
NEW
239
        }).SetupWithManager(ctx, mgr); err != nil {
×
NEW
240
                setupLog.Error(err, "unable to create controller", "controller", "GPUCluster")
×
NEW
241
                os.Exit(1)
×
NEW
242
        }
×
243
        // +kubebuilder:scaffold:builder
244
        if err := mgr.AddHealthzCheck("health", healthz.Ping); err != nil {
×
245
                setupLog.Error(err, "unable to set up health check")
×
246
                os.Exit(1)
×
247
        }
×
248
        if err := mgr.AddReadyzCheck("check", healthz.Ping); err != nil {
×
249
                setupLog.Error(err, "unable to set up ready check")
×
250
                os.Exit(1)
×
251
        }
×
252

253
        setupLog.Info("starting manager")
×
254
        if err := mgr.Start(ctx); err != nil {
×
255
                setupLog.Error(err, "problem running manager")
×
256
                os.Exit(1)
×
257
        }
×
258
}
259

260
func gpuPodSpecFilter(pod corev1.Pod) bool {
×
261
        gpuInResourceList := func(rl corev1.ResourceList) bool {
×
262
                for resourceName := range rl {
×
263
                        str := string(resourceName)
×
264
                        if strings.HasPrefix(str, "nvidia.com/gpu") || strings.HasPrefix(str, "nvidia.com/mig-") {
×
265
                                return true
×
266
                        }
×
267
                }
268
                return false
×
269
        }
270

271
        //  ignore pods other than in running and pending state
272
        if pod.Status.Phase != corev1.PodRunning && pod.Status.Phase != corev1.PodPending {
×
273
                return false
×
274
        }
×
275

276
        for _, c := range pod.Spec.Containers {
×
277
                if gpuInResourceList(c.Resources.Limits) || gpuInResourceList(c.Resources.Requests) {
×
278
                        return true
×
279
                }
×
280
        }
281
        return false
×
282
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc