diff --git a/pkg/kstatus/status/example_test.go b/pkg/kstatus/status/example_test.go index 773782c3..fa18bbbc 100644 --- a/pkg/kstatus/status/example_test.go +++ b/pkg/kstatus/status/example_test.go @@ -95,3 +95,59 @@ status: assert.Equal(t, expectedManifest, receivedManifest) } + +func TestExampleStringObservedGeneration(t *testing.T) { + deploymentManifest := ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test + generation: 1 + namespace: qual +status: + observedGeneration: "1" + updatedReplicas: 1 + readyReplicas: 1 + availableReplicas: 1 + replicas: 1 + conditions: + - type: Progressing + status: "True" + reason: NewReplicaSetAvailable + - type: Available + status: "True" +` + deployment := testutil.YamlToUnstructured(t, deploymentManifest) + + res, err := status.Compute(deployment) + assert.NoError(t, err) + + assert.Equal(t, status.Status("Current"), res.Status) +} + +func TestExampleStringObservedGenerationBad(t *testing.T) { + deploymentManifest := ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test + generation: 1 + namespace: qual +status: + observedGeneration: "foo" + updatedReplicas: 1 + readyReplicas: 1 + availableReplicas: 1 + replicas: 1 + conditions: + - type: Progressing + status: "True" + reason: NewReplicaSetAvailable + - type: Available + status: "True" +` + deployment := testutil.YamlToUnstructured(t, deploymentManifest) + + _, err := status.Compute(deployment) + assert.Error(t, err) +} diff --git a/pkg/kstatus/status/generic.go b/pkg/kstatus/status/generic.go index 6213d1b9..dba0c00c 100644 --- a/pkg/kstatus/status/generic.go +++ b/pkg/kstatus/status/generic.go @@ -5,6 +5,7 @@ package status import ( "fmt" + "strconv" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -81,7 +82,17 @@ func checkGeneration(u *unstructured.Unstructured) (*Result, error) { } observedGeneration, found, err := unstructured.NestedInt64(u.Object, "status", "observedGeneration") if err != nil { - return nil, fmt.Errorf("looking up status.observedGeneration from resource: %w", err) + // Last ditch to see if observedGeneration is a string. If so, convert to int64. + strObservedGeneration, found, err := unstructured.NestedString(u.Object, "status", "observedGeneration") + if err != nil { + return nil, fmt.Errorf("looking up status.observedGeneration from resource: %w", err) + } + if found { + observedGeneration, err = strconv.ParseInt(strObservedGeneration, 10, 64) + if err != nil { + return nil, fmt.Errorf("looking up status.observedGeneration from resource: %w", err) + } + } } if found { // Resource does not have this field, so we can't do this check.