Skip to content

Commit e1a2e91

Browse files
committed
Allow parsing status.observedGeneration as string
1 parent 5af6753 commit e1a2e91

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

pkg/kstatus/status/example_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,59 @@ status:
9595

9696
assert.Equal(t, expectedManifest, receivedManifest)
9797
}
98+
99+
func TestExampleStringObservedGeneration(t *testing.T) {
100+
deploymentManifest := `
101+
apiVersion: apps/v1
102+
kind: Deployment
103+
metadata:
104+
name: test
105+
generation: 1
106+
namespace: qual
107+
status:
108+
observedGeneration: "1"
109+
updatedReplicas: 1
110+
readyReplicas: 1
111+
availableReplicas: 1
112+
replicas: 1
113+
conditions:
114+
- type: Progressing
115+
status: "True"
116+
reason: NewReplicaSetAvailable
117+
- type: Available
118+
status: "True"
119+
`
120+
deployment := testutil.YamlToUnstructured(t, deploymentManifest)
121+
122+
res, err := status.Compute(deployment)
123+
assert.NoError(t, err)
124+
125+
assert.Equal(t, status.Status("Current"), res.Status)
126+
}
127+
128+
func TestExampleStringObservedGenerationBad(t *testing.T) {
129+
deploymentManifest := `
130+
apiVersion: apps/v1
131+
kind: Deployment
132+
metadata:
133+
name: test
134+
generation: 1
135+
namespace: qual
136+
status:
137+
observedGeneration: "foo"
138+
updatedReplicas: 1
139+
readyReplicas: 1
140+
availableReplicas: 1
141+
replicas: 1
142+
conditions:
143+
- type: Progressing
144+
status: "True"
145+
reason: NewReplicaSetAvailable
146+
- type: Available
147+
status: "True"
148+
`
149+
deployment := testutil.YamlToUnstructured(t, deploymentManifest)
150+
151+
_, err := status.Compute(deployment)
152+
assert.Error(t, err)
153+
}

pkg/kstatus/status/generic.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package status
55

66
import (
77
"fmt"
8+
"strconv"
89

910
corev1 "k8s.io/api/core/v1"
1011
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -81,7 +82,17 @@ func checkGeneration(u *unstructured.Unstructured) (*Result, error) {
8182
}
8283
observedGeneration, found, err := unstructured.NestedInt64(u.Object, "status", "observedGeneration")
8384
if err != nil {
84-
return nil, fmt.Errorf("looking up status.observedGeneration from resource: %w", err)
85+
// Last ditch to see if observedGeneration is a string. If so, convert to int64.
86+
strObservedGeneration, found, err := unstructured.NestedString(u.Object, "status", "observedGeneration")
87+
if err != nil {
88+
return nil, fmt.Errorf("looking up status.observedGeneration from resource: %w", err)
89+
}
90+
if found {
91+
observedGeneration, err = strconv.ParseInt(strObservedGeneration, 10, 64)
92+
if err != nil {
93+
return nil, fmt.Errorf("looking up status.observedGeneration from resource: %w", err)
94+
}
95+
}
8596
}
8697
if found {
8798
// Resource does not have this field, so we can't do this check.

0 commit comments

Comments
 (0)