Skip to content

Commit d07e2ee

Browse files
committed
add ConditionsToRemove field to ReconcileResult struct
1 parent 4939d73 commit d07e2ee

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
@@ -171,6 +171,7 @@ The `ReconcileResult` that is passed into the status updater is expected to cont
171171
- `Reason` and `Message` can be set to set the status' corresponding fields.
172172
- If either one is nil, but `ReconcileError` is not, it will be filled with a value derived from the error.
173173
- `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.
174+
- `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.
174175
- `Object` contains the object to be updated.
175176
- If `Object` is nil, no status update will be performed.
176177
- `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
@@ -237,6 +237,11 @@ func (s *statusUpdater[Obj]) UpdateStatus(ctx context.Context, c client.Client,
237237
}
238238
cu.UpdateCondition(con.Type, con.Status, gen, con.Reason, con.Message)
239239
}
240+
if len(rr.ConditionsToRemove) > 0 {
241+
for _, conType := range rr.ConditionsToRemove {
242+
cu.RemoveCondition(conType)
243+
}
244+
}
240245
newCons, _ := cu.Record(rr.Object).Conditions()
241246
SetField(status, s.fieldNames[STATUS_FIELD_CONDITIONS], newCons)
242247
}
@@ -369,6 +374,9 @@ type ReconcileResult[Obj client.Object] struct {
369374
// Also note that names of conditions are globally unique, so take care to avoid conflicts with other objects.
370375
// The lastTransition timestamp of the condition will be overwritten with the current time while updating.
371376
Conditions []metav1.Condition
377+
// ConditionsToRemove is an optional slice of condition types for which the corresponding conditions should be removed from the status.
378+
// This is useful if you want to remove conditions that are no longer relevant.
379+
ConditionsToRemove []string
372380
}
373381

374382
// 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
@@ -121,7 +121,6 @@ var _ = Describe("Status Updater", func() {
121121
env := testutils.NewEnvironmentBuilder().WithFakeClient(coScheme).WithInitObjectPath("testdata", "test-02").WithDynamicObjectsWithStatus(&CustomObject{}).Build()
122122
obj := &CustomObject{}
123123
Expect(env.Client().Get(env.Ctx, controller.ObjectKey("status", "default"), obj)).To(Succeed())
124-
oldTransitionTime := conditions.GetCondition(obj.Status.Conditions, "TestConditionTrue").LastTransitionTime
125124
before := obj.DeepCopy()
126125
for _, disabledField := range controller.AllStatusFields() {
127126
By(fmt.Sprintf("Testing disabled field %s", disabledField))
@@ -130,10 +129,11 @@ var _ = Describe("Status Updater", func() {
130129
obj.Status = before.Status
131130
Expect(env.Client().Status().Patch(env.Ctx, obj, client.MergeFrom(modified))).To(Succeed())
132131
rr := controller.ReconcileResult[*CustomObject]{
133-
Object: obj,
134-
Conditions: dummyConditions(),
135-
Reason: "TestReason",
136-
Message: "TestMessage",
132+
Object: obj,
133+
Conditions: dummyConditions(),
134+
Reason: "TestReason",
135+
Message: "TestMessage",
136+
ConditionsToRemove: []string{"TestConditionTrue"},
137137
}
138138
su := preconfiguredStatusUpdaterBuilder().WithPhaseUpdateFunc(func(obj *CustomObject, rr controller.ReconcileResult[*CustomObject]) (string, error) {
139139
return PhaseSucceeded, nil
@@ -174,21 +174,14 @@ var _ = Describe("Status Updater", func() {
174174
Expect(obj.Status.Conditions).To(Equal(before.Status.Conditions))
175175
} else {
176176
Expect(obj.Status.Conditions).To(ConsistOf(
177-
MatchCondition(TestCondition().
178-
WithType("TestConditionTrue").
179-
WithStatus(metav1.ConditionTrue).
180-
WithObservedGeneration(10).
181-
WithReason("TestReasonTrue").
182-
WithMessage("TestMessageTrue").
183-
WithLastTransitionTime(oldTransitionTime)),
184177
MatchCondition(TestCondition().
185178
WithType("TestConditionFalse").
186179
WithStatus(metav1.ConditionFalse).
187180
WithObservedGeneration(10).
188181
WithReason("TestReasonFalse").
189182
WithMessage("TestMessageFalse").
190183
WithLastTransitionTime(now).
191-
WithTimestampTolerance(1*time.Second)),
184+
WithTimestampTolerance(1 * time.Second)),
192185
))
193186
}
194187
}

0 commit comments

Comments
 (0)