Skip to content

Commit

Permalink
Allow parsing status.observedGeneration as string
Browse files Browse the repository at this point in the history
  • Loading branch information
isugimpy committed Feb 27, 2024
1 parent 5af6753 commit e1a2e91
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
56 changes: 56 additions & 0 deletions pkg/kstatus/status/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
13 changes: 12 additions & 1 deletion pkg/kstatus/status/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package status

import (
"fmt"
"strconv"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit e1a2e91

Please sign in to comment.