Skip to content

Commit 94690a0

Browse files
committed
Ensure condition observed generation is getting updated
1 parent f186664 commit 94690a0

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

pkg/condition/fluent.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func (m *MetaV1ConditionFluentBuilder) Apply(obj interface{}) bool {
146146

147147
conditionsSlice, ok := getConditionsSlice(obj)
148148
if ok {
149+
m.workingCondition.ObservedGeneration = getResourceGeneration(obj)
149150
changed := meta.SetStatusCondition(&conditionsSlice, m.workingCondition)
150151
m.initedTarget = false
151152
m.workingCondition = metav1.Condition{}

pkg/condition/standardized.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"k8s.io/apimachinery/pkg/api/meta"
66
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
78
"reflect"
89

910
"github.com/rancher/wrangler/v3/pkg/generic"
@@ -156,6 +157,7 @@ func (ch *MetaV1ConditionHandler) SetReason(obj interface{}, reason string) {
156157
cond := ch.findOrCreateCondition(obj)
157158
updatedCond := cond.DeepCopy()
158159
updatedCond.Reason = reason
160+
updatedCond.ObservedGeneration = getResourceGeneration(obj)
159161

160162
conditionsSlice, ok := getConditionsSlice(obj)
161163
if !ok {
@@ -178,6 +180,7 @@ func (ch *MetaV1ConditionHandler) SetMessage(obj interface{}, msg string) {
178180
cond := ch.findOrCreateCondition(obj)
179181
updatedCond := cond.DeepCopy()
180182
updatedCond.Message = msg
183+
updatedCond.ObservedGeneration = getResourceGeneration(obj)
181184

182185
conditionsSlice, ok := getConditionsSlice(obj)
183186
if !ok {
@@ -197,6 +200,7 @@ func (ch *MetaV1ConditionHandler) SetMessageIfBlank(obj interface{}, msg string)
197200
if updatedCond.Message == cond.Message {
198201
return
199202
}
203+
updatedCond.ObservedGeneration = getResourceGeneration(obj)
200204

201205
conditionsSlice, ok := getConditionsSlice(obj)
202206
if !ok {
@@ -221,6 +225,7 @@ func (ch *MetaV1ConditionHandler) setStatus(obj interface{}, status string) {
221225
cond := ch.findOrCreateCondition(obj)
222226

223227
cond.Status = statusParsed
228+
cond.ObservedGeneration = getResourceGeneration(obj)
224229
}
225230

226231
func (ch *MetaV1ConditionHandler) findOrCreateCondition(obj interface{}) *metav1.Condition {
@@ -230,10 +235,11 @@ func (ch *MetaV1ConditionHandler) findOrCreateCondition(obj interface{}) *metav1
230235
}
231236

232237
newCond := metav1.Condition{
233-
Type: ch.RootCondition.Name(),
234-
Status: metav1.ConditionUnknown,
235-
Reason: "Created",
236-
Message: "",
238+
Type: ch.RootCondition.Name(),
239+
Status: metav1.ConditionUnknown,
240+
ObservedGeneration: getResourceGeneration(obj),
241+
Reason: "Created",
242+
Message: "",
237243
}
238244

239245
conditionsSlice, ok := getConditionsSlice(obj)
@@ -249,6 +255,18 @@ func (ch *MetaV1ConditionHandler) findOrCreateCondition(obj interface{}) *metav1
249255
return findCondition(obj, ch.RootCondition.Name())
250256
}
251257

258+
// getResourceGeneration attempts to retrieve and validate the slice of conditions
259+
// from the given object. It returns the slice of metav1.Condition if successful,
260+
// and nil otherwise.
261+
func getResourceGeneration(obj interface{}) int64 {
262+
unstructuredObj, ok := obj.(unstructured.Unstructured)
263+
if !ok {
264+
return 0
265+
}
266+
267+
return unstructuredObj.GetGeneration()
268+
}
269+
252270
func setConditionsSlice(obj interface{}, conditions []metav1.Condition) {
253271
condSliceValue := getValue(obj, "Status", "Conditions")
254272
if !condSliceValue.IsValid() {

0 commit comments

Comments
 (0)