gpu-operator icon indicating copy to clipboard operation
gpu-operator copied to clipboard

[Bug]: NVIDIADriver spec.labels can override the driver DaemonSet's app and component labels

Open CloneOfAlex opened this issue 1 month ago • 4 comments

Describe the bug manifests/state-driver/0500_daemonset.yaml (lines 23–24 and 64–65) appends .Driver.Spec.Labels verbatim after the operator's own app and app.kubernetes.io/component labels, on both the DaemonSet metadata and the pod template.

internal/render decodes the rendered manifest with k8s.io/apimachinery/pkg/util/yaml.NewYAMLOrJSONDecoder, which uses non-strict yaml.YAMLToJSON (vendor/k8s.io/apimachinery/pkg/util/yaml/decoder.go:100). Duplicate mapping keys are resolved last-wins without an error. I confirmed this with the vendored sigs.k8s.io/yaml: a mapping with app twice returns the second value from YAMLToJSON, while YAMLToJSONStrict returns key "app" already set in map.

Consequences, depending on the key a user sets in spec.labels:

  • app: the pod template label no longer matches the immutable selector app: {{ .Driver.AppName }}, so the DaemonSet create/update is rejected and the driver is not deployed on that node pool.
  • app.kubernetes.io/component: the DaemonSet and pods lose component=nvidia-driver. That label is used by the upgrade controller (BuildState with app.kubernetes.io/component=nvidia-driver), orphaned-pod detection (controllers/nodelabeling_controller.go:593) and the validator's isDriverManagedByOperator (cmd/nvidia-validator/main.go:713). Auto-upgrade stops seeing the driver and the validator treats the node as host-driver.

The ClusterPolicy path already protects these keys in applyCommonDaemonsetLabel (controllers/object_controls.go:809). Draft PR #2973 adds equivalent filtering for GPUCluster only.

To Reproduce Found by code review; not yet reproduced on a cluster. Expected repro:

  1. Create an NVIDIADriver with spec.labels: {app.kubernetes.io/component: custom} and upgradePolicy.autoUpgrade: true.
  2. Change the driver version.
  3. Observe that the driver DaemonSet has app.kubernetes.io/component: custom and no node receives an upgrade state label.

Same with spec.labels: {app: custom} → DaemonSet create fails with a selector/template mismatch.

Expected behavior Reserved keys in spec.labels are ignored (with a warning/condition) or rejected at admission, as on the ClusterPolicy path.

Suggested fix Filter reserved keys before rendering (shared helper with the ClusterPolicy path), and/or add CEL validation on NVIDIADriverSpec.Labels rejecting app and app.kubernetes.io/component. Optionally switch the renderer to strict YAML so duplicate keys fail loudly.

Environment (please provide the following information):

  • GPU Operator Version: main @ f806d6b
  • Found by code inspection; affects any platform using the NVIDIADriver CRD.

CloneOfAlex avatar Oct 13 '26 17:10 CloneOfAlex

@CloneOfAlex are you planning to send a fix for this one? If not, I'd like to take it. @ajavanma, your draft #2973 leaves NVIDIADriver rendering unchanged, so I'd keep this to the NVIDIADriver path and reuse your selector-key helper once it lands, unless you'd rather extend #2973 to cover it.

vjsairam avatar Oct 17 '26 08:10 vjsairam

Thanks @vjsairam. I'd prefer to keep #2973 focused on GPUCluster, so a separate NVIDIADriver fix sounds good from my side. Reusing the helper once it lands makes sense, provided @CloneOfAlex isn't already working on this.

ajavanma avatar Oct 17 '26 08:10 ajavanma

Thanks for the report. The NVIDIADriver reconciliation path already filters these labels before rendering. getDriverSpec() calls sanitizeDriverLabels(), which removes app and all app.kubernetes.io/* keys from the labels passed to the DaemonSet template. Consequently, the reported overrides cannot occur through this path. https://github.com/NVIDIA/gpu-operator/blob/e15471941743c5588e07fcc5f1415d5815e92b55/internal/state/driver.go#L561-L573 This protection was introduced in commit 414a82939 on November 7, 2023, and is present in f806d6b, the revision cited in this issue. Closing as not planned because the reported behavior is already prevented. If you can reproduce it, please share the NVIDIADriver manifest, operator version, and resulting DaemonSet so we can investigate further.

rahulait avatar Oct 27 '26 04:10 rahulait

@rahulait you're right, and thanks for the pointer. I missed that getDriverSpec() runs sanitizeDriverLabels() before rendering, so app and app.kubernetes.io/* never reach the driver template on the real reconcile path. I confirmed it on current main (internal/state/driver.go:652). Sorry for the noise.

@vjsairam @ajavanma there's nothing to pick up here for NVIDIADriver. The GPUCluster DRA templates are a different story (they don't filter app.kubernetes.io/name, see #2955 / #2973), so #2973 is still the relevant fix.

I'll also correct the NVIDIADriver rows in the render matrix on #3067: that test built the render data by hand and skipped getDriverSpec(), so it didn't go through the sanitizer.

CloneOfAlex avatar Nov 05 '26 23:11 CloneOfAlex