[Feature]: Correct drift on operand DaemonSets instead of comparing only the stored hash annotation
Motivation
createOrUpdateObjs (internal/state/state_skel.go:301) skips the update when the DaemonSet's NvidiaAnnotationHashKey annotation equals the hash of the desired object. The hash is computed from the desired object only, so if someone edits the live DaemonSet (image, env, tolerations) and leaves the annotation alone, the operator never restores it. Drift persists until the CR spec changes.
Proposal Use server-side apply with a dedicated field manager for operand objects: apply the desired object on each reconcile and let the API server compute the no-op. This corrects drift on fields the operator owns, leaves fields owned by others alone, and replaces the create-then-update pattern in #3070. If SSA is too large a change, compare a hash of the relevant live fields rather than the stored annotation.
Related: #3070.
@CloneOfAlex
Could you provide more details about the problem you encountered and your proposed solution?
Thanks @francisguillier, here's the detail.
The problem
Both DaemonSet reconcile paths decide whether to update by comparing a hash of the desired object with the nvidia.com/last-applied-hash annotation on the live object. Neither looks at the live spec itself:
- ClusterPolicy operands:
isDaemonsetSpecChanged(controllers/object_controls.go:4813), called from:4797 - NVIDIADriver / GPUCluster states:
createOrUpdateObjs(internal/state/state_skel.go:342-350)
If someone edits a managed DaemonSet out-of-band (kubectl set image, an edited env var or toleration, a debug patch left behind) and doesn't touch that annotation, the hash still matches. The operator logs "DaemonSet identical, skipping update", and the drift stays until the desired spec changes for some other reason. The DaemonSet watch does fire on the edit, but the reconcile it triggers is a no-op.
To be transparent: this comes from code review, not an observed incident. Two tests against current main, one per path:
TestCreateOrUpdateObjsDoesNotRevertDaemonSetDrift: create viacreateOrUpdateObjs, change the live image, reconcile again with the same desired state → 0 Updates, drifted image still in place.TestIsDaemonsetSpecChangedIgnoresLiveDrift: live DaemonSet with a drifted image and the correct hash annotation →isDaemonsetSpecChangedreturnsfalse.
internal/state/state_skel_drift_test.go
package state
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
)
func desiredOperandDaemonSet(t *testing.T, image string) *unstructured.Unstructured {
t.Helper()
ds := &appsv1.DaemonSet{
TypeMeta: metav1.TypeMeta{APIVersion: "apps/v1", Kind: "DaemonSet"},
ObjectMeta: metav1.ObjectMeta{Name: "nvidia-dcgm-exporter", Namespace: "test-ns"},
Spec: appsv1.DaemonSetSpec{
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"app": "dcgm-exporter"}},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "dcgm-exporter"}},
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "exporter", Image: image}}},
},
},
}
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(ds)
require.NoError(t, err)
return &unstructured.Unstructured{Object: m}
}
// An out-of-band edit to a managed DaemonSet that leaves the
// nvidia.com/last-applied-hash annotation alone is never reverted: the hash is
// computed from the desired object only, so it still matches and Update is skipped.
func TestCreateOrUpdateObjsDoesNotRevertDaemonSetDrift(t *testing.T) {
updates := 0
c := fake.NewClientBuilder().WithScheme(skelTestScheme(t)).
WithInterceptorFuncs(interceptor.Funcs{
Update: func(ctx context.Context, cl client.WithWatch, obj client.Object, opts ...client.UpdateOption) error {
updates++
return cl.Update(ctx, obj, opts...)
},
}).Build()
skel := newTestSkel(t, c)
ctx := context.Background()
key := types.NamespacedName{Namespace: "test-ns", Name: "nvidia-dcgm-exporter"}
// Reconcile 1: operator creates the DaemonSet with the desired image.
require.NoError(t, skel.createOrUpdateObjs(ctx, noopSetControllerReference,
[]*unstructured.Unstructured{desiredOperandDaemonSet(t, "nvcr.io/nvidia/dcgm-exporter:4.0.0")}))
// Someone edits the live DaemonSet (kubectl edit / set image); annotations untouched.
live := &appsv1.DaemonSet{}
require.NoError(t, c.Get(ctx, key, live))
live.Spec.Template.Spec.Containers[0].Image = "example.com/patched:debug"
require.NoError(t, c.Update(ctx, live))
updates = 0
// Reconcile 2: same desired state as before.
require.NoError(t, skel.createOrUpdateObjs(ctx, noopSetControllerReference,
[]*unstructured.Unstructured{desiredOperandDaemonSet(t, "nvcr.io/nvidia/dcgm-exporter:4.0.0")}))
require.NoError(t, c.Get(ctx, key, live))
assert.Equal(t, 0, updates, "hash matched, so no Update was issued")
assert.Equal(t, "example.com/patched:debug", live.Spec.Template.Spec.Containers[0].Image,
"drifted image is left in place")
}
controllers/daemonset_drift_test.go
package controllers
import (
"testing"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/NVIDIA/gpu-operator/internal/utils"
)
func operandDaemonSet(image string) *appsv1.DaemonSet {
return &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{Name: "nvidia-device-plugin-daemonset", Annotations: map[string]string{}},
Spec: appsv1.DaemonSetSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nvidia-device-plugin", Image: image}}},
},
},
}
}
// Same behavior on the ClusterPolicy path: isDaemonsetSpecChanged compares the
// desired object's hash with the annotation on the live object, never the live
// spec itself, so a drifted live DaemonSet is reported as unchanged.
func TestIsDaemonsetSpecChangedIgnoresLiveDrift(t *testing.T) {
desired := operandDaemonSet("nvcr.io/nvidia/k8s-device-plugin:v0.17.0")
live := operandDaemonSet("example.com/patched:debug") // drifted spec
live.Annotations[NvidiaAnnotationHashKey] = utils.GetObjectHash(operandDaemonSet("nvcr.io/nvidia/k8s-device-plugin:v0.17.0"))
assert.False(t, isDaemonsetSpecChanged(live, desired), "drift in the live spec is not detected")
}
A policy question first: should every operand be reverted?
The hash skip isn't accidental. internal/state/driver.go:440-449 uses the same hash specifically to avoid touching the driver DaemonSet unless the driver spec really changed, because driver restarts are disruptive. That matters for drift correction:
- The driver DaemonSet uses
updateStrategy: OnDelete(manifests/state-driver/0500_daemonset.yaml:38), with rollout handled by the upgrade controller. Reverting drift there creates a new revision. With driver auto-upgrade enabled, the upgrade controller treats that as a driver upgrade across every node: cordon, drain, driver reload. Without it, pods only pick up the reverted spec whenever they next restart. - The other operands use RollingUpdate, so a revert is a normal rolling restart of that DaemonSet.
So I'd suggest auto-correcting drift for RollingUpdate operands, and for the driver either surfacing drift (an event plus a condition on the CR) without reverting, or reverting only behind an explicit opt-in. I'd like a maintainer view on that before choosing an implementation.
Proposed solution
-
Detect drift cheaply via
metadata.generation(small PR, both paths). A DaemonSet'sgenerationincrements on every spec change and not on metadata-only changes. After the operator writes a DaemonSet, it records the resulting generation in an annotation such asnvidia.com/last-applied-generation. On each reconcile,live.generation != recordedmeans someone else changed the spec, so the operator re-applies (or, for the driver, reports drift). Steady state costs nothing, it catches any spec edit, and it sidesteps the main problem with my original fallback idea. A hash of selected live fields is fragile: API-server defaulting (imagePullPolicy,terminationMessagePath, probe defaults, …) makes the live object differ from the rendered one, so a naive comparison would update on every reconcile. -
Server-side apply (longer term). SSA with a dedicated field manager is still the cleanest end state. It reverts only fields the operator owns, leaves fields owned by others alone, and replaces the create-then-update pattern from #3070. But it's a bigger change than the issue implied:
- Existing objects have fields owned through
Updateoperations. Without migrating managed fields (client-go'scsaupgrade), fields removed from future manifests wouldn't be pruned, and the first applies would conflict with the existing Update-based owner unless forced. - Applying the driver DaemonSet on every reconcile would need the driver-manager image logic from
driver.go:440preserved, so that an image-only bump doesn't trigger a driver upgrade.
I'd treat SSA as its own design discussion, combined with #3070.
- Existing objects have fields owned through
I'm happy to open a PR for step 1 (with the tests above flipped to assert correction) once there's a decision on the driver DaemonSet.
@CloneOfAlex
I’m not sure I understand the deployment problem you’re trying to solve. Could you please provide:
- Issue summary: What is happening, and what behavior do you expect?
- Steps to reproduce: Include the relevant environment details and configuration.
- Proposed fix: Explain how the fix addresses the problem. Without these details, we won’t be able to assess the issue and will close it. Thanks
@francisguillier here it is in that format.
Issue summary
If an operand DaemonSet is edited out-of-band (kubectl set image, kubectl set env, a leftover debug patch), the operator never restores it. Both reconcile paths compare the desired hash with the nvidia.com/last-applied-hash annotation, not with the live spec, so the edit persists until the CR changes for some other reason. Expected: the operator restores the fields it owns, or at least reports the drift.
Steps to reproduce
- Install with ClusterPolicy defaults.
kubectl -n gpu-operator set env ds/nvidia-dcgm-exporter DRIFT=1- The DaemonSet watch triggers a reconcile, the operator logs that the DaemonSet is identical and skips the update, and
DRIFT=1stays. - Unit level:
TestIsDaemonsetSpecChangedIgnoresLiveDriftandTestCreateOrUpdateObjsDoesNotRevertDaemonSetDriftfrom my previous comment.
Proposed fix
Record the DaemonSet's metadata.generation after each operator write. When the live generation differs, the spec was changed by someone else, so re-apply. For the driver DaemonSet (OnDelete, where a revert would trigger a node-by-node driver upgrade), emit an event/condition instead of reverting, unless you prefer otherwise. That's the one decision I need from a maintainer before opening a PR.