Skip to content

Commit d2cda98

Browse files
authored
Merge pull request #1187 from fluxcd/fix-cel-observed-gen
runtime/cel: fix requiring int `.status.observedGeneration`
2 parents 4f94dc6 + 3619c73 commit d2cda98

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

runtime/cel/status_evaluator.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,14 @@ func (s *StatusEvaluator) Evaluate(ctx context.Context, u *unstructured.Unstruct
8888
unsObj := u.UnstructuredContent()
8989

9090
// Check if the object has the field status.observedGeneration
91-
// and if it differs from metadata.generation, in which case we
92-
// return status InProgress.
91+
// as an int64 and if it differs from metadata.generation, in which
92+
// case we return status InProgress. If the field is not an int64
93+
// (e.g. some CRDs type it as a string), we skip this built-in check
94+
// and rely solely on the user-provided CEL expressions.
9395
observedGeneration, ok, err := unstructured.NestedInt64(unsObj, "status", "observedGeneration")
94-
if err != nil {
95-
return nil, err
96-
}
97-
if ok {
96+
if err == nil && ok {
9897
generation, ok, err := unstructured.NestedInt64(unsObj, "metadata", "generation")
99-
if err != nil {
100-
return nil, err
101-
}
102-
if ok && observedGeneration != generation {
98+
if err == nil && ok && observedGeneration != generation {
10399
return &status.Result{Status: status.InProgressStatus}, nil
104100
}
105101
}

runtime/cel/status_evaluator_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,36 @@ func TestStatusEvaluator_Evaluate(t *testing.T) {
314314
},
315315
result: status.Result{Status: status.InProgressStatus},
316316
},
317+
{
318+
name: "object with non-int64 status.observedGeneration is tolerated and delegated to CEL expressions",
319+
exprs: kustomize.HealthCheckExpressions{
320+
Current: "status.observedGeneration == '2'",
321+
},
322+
obj: map[string]any{
323+
"metadata": map[string]any{
324+
"generation": int64(2),
325+
},
326+
"status": map[string]any{
327+
"observedGeneration": "2",
328+
},
329+
},
330+
result: status.Result{Status: status.CurrentStatus},
331+
},
332+
{
333+
name: "object with non-int64 metadata.generation is tolerated and delegated to CEL expressions",
334+
exprs: kustomize.HealthCheckExpressions{
335+
Current: "metadata.generation == '2'",
336+
},
337+
obj: map[string]any{
338+
"metadata": map[string]any{
339+
"generation": "2",
340+
},
341+
"status": map[string]any{
342+
"observedGeneration": int64(2),
343+
},
344+
},
345+
result: status.Result{Status: status.CurrentStatus},
346+
},
317347
} {
318348
t.Run(tt.name, func(t *testing.T) {
319349
t.Parallel()

0 commit comments

Comments
 (0)