Skip to content

Commit 627159a

Browse files
committed
Add Drifted condition
This commit introduces a new Kubernetes condition type "Drifted" to improve observability of Helm release drift detection. Signed-off-by: Yasin Özel <yozel@nebius.com>
1 parent 865b5a6 commit 627159a

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

api/v2/condition_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const (
2929
// (uninstall/rollback) due to a failure of the last release attempt against the
3030
// latest desired state.
3131
RemediatedCondition string = "Remediated"
32+
33+
// DriftedCondition represents the status of the Helm release drift detection,
34+
// indicating that the deployed release has drifted from the desired state.
35+
DriftedCondition string = "Drifted"
3236
)
3337

3438
const (
@@ -79,4 +83,12 @@ const (
7983
// DependencyNotReadyReason represents the fact that
8084
// one of the dependencies is not ready.
8185
DependencyNotReadyReason string = "DependencyNotReady"
86+
87+
// DriftDetectedReason represents the fact that drift has been detected in the
88+
// Helm release compared to the expected state.
89+
DriftDetectedReason string = "DriftDetected"
90+
91+
// NoDriftDetectedReason represents the fact that no drift has been detected in
92+
// the Helm release compared to the expected state.
93+
NoDriftDetectedReason string = "NoDriftDetected"
8294
)

internal/reconcile/atomic_release.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,14 @@ func (r *AtomicRelease) actionForState(ctx context.Context, req *Request, state
385385
conditions.MarkTrue(req.Object, v2.ReleasedCondition, v2.UpgradeSucceededReason, "%s", msg)
386386
}
387387

388+
if req.Object.GetDriftDetection().MustDetectChanges() {
389+
conditions.Set(req.Object, &metav1.Condition{
390+
Type: v2.DriftedCondition,
391+
Status: metav1.ConditionFalse,
392+
Reason: v2.NoDriftDetectedReason,
393+
})
394+
}
395+
388396
return nil, nil
389397
case ReleaseStatusLocked:
390398
log.Info(msgWithReason("release locked", state.Reason))
@@ -440,10 +448,15 @@ func (r *AtomicRelease) actionForState(ctx context.Context, req *Request, state
440448
}
441449
}
442450

443-
r.eventRecorder.Eventf(req.Object, corev1.EventTypeWarning, "DriftDetected",
444-
"Cluster state of release %s has drifted from the desired state:\n%s",
445-
req.Object.Status.History.Latest().FullReleaseName(), diff.SummarizeDiffSet(state.Diff),
446-
)
451+
msg := fmt.Sprintf("Cluster state of release %s has drifted from the desired state:\n%s",
452+
req.Object.Status.History.Latest().FullReleaseName(), diff.SummarizeDiffSet(state.Diff))
453+
r.eventRecorder.Eventf(req.Object, corev1.EventTypeWarning, v2.DriftDetectedReason, msg)
454+
conditions.Set(req.Object, &metav1.Condition{
455+
Type: v2.DriftedCondition,
456+
Status: metav1.ConditionTrue,
457+
Reason: v2.DriftDetectedReason,
458+
Message: msg,
459+
})
447460

448461
if req.Object.GetDriftDetection().GetMode() == v2.DriftDetectionEnabled {
449462
return NewCorrectClusterDrift(r.configFactory, r.eventRecorder, state.Diff, kube.ManagedFieldsManager), nil

internal/reconcile/atomic_release_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,9 @@ func TestAtomicRelease_actionForState(t *testing.T) {
16341634
"Deployment/something/mock removed",
16351635
),
16361636
},
1637+
assertConditions: []metav1.Condition{
1638+
*conditions.TrueCondition(v2.DriftedCondition, v2.DriftDetectedReason, "Cluster state of release mock-ns/mock-release.v1 has drifted from the desired state:\nDeployment/something/mock removed"),
1639+
},
16371640
},
16381641
{
16391642
name: "drifted release only triggers event if mode is warn",
@@ -1703,6 +1706,9 @@ func TestAtomicRelease_actionForState(t *testing.T) {
17031706
"Deployment/something/mock changed (0 additions, 1 changes, 0 removals)",
17041707
),
17051708
},
1709+
assertConditions: []metav1.Condition{
1710+
*conditions.TrueCondition(v2.DriftedCondition, v2.DriftDetectedReason, "Cluster state of release mock-ns/mock-release.v1 has drifted from the desired state:\nDeployment/something/mock changed (0 additions, 1 changes, 0 removals)"),
1711+
},
17061712
},
17071713
{
17081714
name: "out-of-sync release triggers upgrade",

0 commit comments

Comments
 (0)