Skip to content

Commit 4f9e5ab

Browse files
committed
add ConditionsToRemove field to ReconcileResult struct
1 parent 5309ed0 commit 4f9e5ab

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

docs/libs/status.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ The `ReconcileResult` that is passed into the status updater is expected to cont
174174
- `Reason` and `Message` can be set to set the status' corresponding fields.
175175
- If either one is nil, but `ReconcileError` is not, it will be filled with a value derived from the error.
176176
- `Conditions` contains the updated conditions. Depending on with which arguments `WithConditionUpdater` was called, the existing conditions will be either updated with these ones (keeping the other ones), or be replaced by them.
177+
- `ConditionsToRemove` is a list of condition types that should be removed from the conditions. This is mostly useful if the condition updater is used in the 'keep untouched conditions' mode.
177178
- `Object` contains the object to be updated.
178179
- If `Object` is nil, no status update will be performed.
179180
- `OldObject` holds the version of the object that will be used as a base for constructing the patch during the status update.

pkg/controller/status_updater.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ func (s *statusUpdater[Obj]) UpdateStatus(ctx context.Context, c client.Client,
265265
}
266266
cu.UpdateCondition(con.Type, con.Status, gen, con.Reason, con.Message)
267267
}
268+
if len(rr.ConditionsToRemove) > 0 {
269+
for _, conType := range rr.ConditionsToRemove {
270+
cu.RemoveCondition(conType)
271+
}
272+
}
268273
newCons, _ := cu.Record(rr.Object).Conditions()
269274
SetField(status, s.fieldNames[STATUS_FIELD_CONDITIONS], newCons)
270275
}
@@ -416,6 +421,9 @@ type ReconcileResult[Obj client.Object] struct {
416421
// Also note that names of conditions are globally unique, so take care to avoid conflicts with other objects.
417422
// The lastTransition timestamp of the condition will be overwritten with the current time while updating.
418423
Conditions []metav1.Condition
424+
// ConditionsToRemove is an optional slice of condition types for which the corresponding conditions should be removed from the status.
425+
// This is useful if you want to remove conditions that are no longer relevant.
426+
ConditionsToRemove []string
419427
}
420428

421429
// GenerateCreateConditionFunc returns a function that can be used to add a condition to the given ReconcileResult.

pkg/controller/status_updater_test.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ var _ = Describe("Status Updater", func() {
123123
env := testutils.NewEnvironmentBuilder().WithFakeClient(coScheme).WithInitObjectPath("testdata", "test-02").WithDynamicObjectsWithStatus(&CustomObject{}).Build()
124124
obj := &CustomObject{}
125125
Expect(env.Client().Get(env.Ctx, controller.ObjectKey("status", "default"), obj)).To(Succeed())
126-
oldTransitionTime := conditions.GetCondition(obj.Status.Conditions, "TestConditionTrue").LastTransitionTime
127126
before := obj.DeepCopy()
128127
for _, disabledField := range controller.AllStatusFields() {
129128
By(fmt.Sprintf("Testing disabled field %s", disabledField))
@@ -132,10 +131,11 @@ var _ = Describe("Status Updater", func() {
132131
obj.Status = before.Status
133132
Expect(env.Client().Status().Patch(env.Ctx, obj, client.MergeFrom(modified))).To(Succeed())
134133
rr := controller.ReconcileResult[*CustomObject]{
135-
Object: obj,
136-
Conditions: dummyConditions(),
137-
Reason: "TestReason",
138-
Message: "TestMessage",
134+
Object: obj,
135+
Conditions: dummyConditions(),
136+
Reason: "TestReason",
137+
Message: "TestMessage",
138+
ConditionsToRemove: []string{"TestConditionTrue"},
139139
}
140140
su := preconfiguredStatusUpdaterBuilder().WithPhaseUpdateFunc(func(obj *CustomObject, rr controller.ReconcileResult[*CustomObject]) (string, error) {
141141
return PhaseSucceeded, nil
@@ -176,21 +176,14 @@ var _ = Describe("Status Updater", func() {
176176
Expect(obj.Status.Conditions).To(Equal(before.Status.Conditions))
177177
} else {
178178
Expect(obj.Status.Conditions).To(ConsistOf(
179-
MatchCondition(TestCondition().
180-
WithType("TestConditionTrue").
181-
WithStatus(metav1.ConditionTrue).
182-
WithObservedGeneration(10).
183-
WithReason("TestReasonTrue").
184-
WithMessage("TestMessageTrue").
185-
WithLastTransitionTime(oldTransitionTime)),
186179
MatchCondition(TestCondition().
187180
WithType("TestConditionFalse").
188181
WithStatus(metav1.ConditionFalse).
189182
WithObservedGeneration(10).
190183
WithReason("TestReasonFalse").
191184
WithMessage("TestMessageFalse").
192185
WithLastTransitionTime(now).
193-
WithTimestampTolerance(1*time.Second)),
186+
WithTimestampTolerance(1 * time.Second)),
194187
))
195188
}
196189
}

0 commit comments

Comments
 (0)