Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parsing status.observedGeneration as string #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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