Skip to content

Commit

Permalink
check observedGeneration and output Rank by compile_resources
Browse files Browse the repository at this point in the history
Signed-off-by: terasihma <[email protected]>
  • Loading branch information
terassyi committed May 19, 2023
1 parent b8277d6 commit 61089da
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
26 changes: 23 additions & 3 deletions op/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,15 @@ func GetKubernetesClusterStatus(ctx context.Context, inf cke.Infrastructure, n *
return cke.KubernetesClusterStatus{}, err
}
if obj.GroupVersionKind().Kind == "DaemonSet" {
generation, _, err := unstructured.NestedInt64(obj.UnstructuredContent(), "metadata", "generation")
if err != nil {
return cke.KubernetesClusterStatus{}, err
}
observedGeneration, _, err := unstructured.NestedInt64(obj.UnstructuredContent(), "status", "observedGeneration")
if err != nil {
return cke.KubernetesClusterStatus{}, err
}

desired, _, err := unstructured.NestedInt64(obj.UnstructuredContent(), "status", "desiredNumberScheduled")
if err != nil {
return cke.KubernetesClusterStatus{}, err
Expand All @@ -449,8 +458,16 @@ func GetKubernetesClusterStatus(ctx context.Context, inf cke.Infrastructure, n *
if err != nil {
return cke.KubernetesClusterStatus{}, err
}
s.SetResourceStatus(res.Key, obj.GetAnnotations(), len(obj.GetManagedFields()) != 0, objStatus(desired, updated, available))
s.SetResourceStatus(res.Key, obj.GetAnnotations(), len(obj.GetManagedFields()) != 0, objStatus(generation, observedGeneration, desired, updated, available))
} else if obj.GroupVersionKind().Kind == "Deployment" {
generation, _, err := unstructured.NestedInt64(obj.UnstructuredContent(), "metadata", "generation")
if err != nil {
return cke.KubernetesClusterStatus{}, err
}
observedGeneration, _, err := unstructured.NestedInt64(obj.UnstructuredContent(), "status", "observedGeneration")
if err != nil {
return cke.KubernetesClusterStatus{}, err
}
desired, _, err := unstructured.NestedInt64(obj.UnstructuredContent(), "status", "readyReplicas")
if err != nil {
return cke.KubernetesClusterStatus{}, err
Expand All @@ -463,7 +480,7 @@ func GetKubernetesClusterStatus(ctx context.Context, inf cke.Infrastructure, n *
if err != nil {
return cke.KubernetesClusterStatus{}, err
}
s.SetResourceStatus(res.Key, obj.GetAnnotations(), len(obj.GetManagedFields()) != 0, objStatus(desired, updated, available))
s.SetResourceStatus(res.Key, obj.GetAnnotations(), len(obj.GetManagedFields()) != 0, objStatus(generation, observedGeneration, desired, updated, available))
} else {
s.SetResourceStatus(res.Key, obj.GetAnnotations(), len(obj.GetManagedFields()) != 0, true)
}
Expand All @@ -472,7 +489,10 @@ func GetKubernetesClusterStatus(ctx context.Context, inf cke.Infrastructure, n *
return s, nil
}

func objStatus(desired, updated, available int64) bool {
func objStatus(generation, observedGeneration, desired, updated, available int64) bool {
if generation < observedGeneration {
return false
}
// If we get the status immediately after applying the resource, the value of desired may be 0.
// In this case, we need to return false.
if desired == 0 {
Expand Down
1 change: 1 addition & 0 deletions pkg/compile_resources/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ var Resources = []cke.ResourceDefinition{
Name: {{ printf "%q" .Name }},
Revision: {{ .Revision }},
Image: {{ printf "%q" .Image }},
Rank: {{ .Rank }},
Definition: []byte({{ printf "%q" .Definition }}),
},
{{ end -}}
Expand Down
3 changes: 1 addition & 2 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ func SortResources(res []ResourceDefinition) {
bRank := b.Rank

if aRank == bRank {
// If both of the rank is equal, keep its order.
return true
return a.Key < b.Key
}
return aRank < bRank
}
Expand Down
18 changes: 9 additions & 9 deletions static/resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 61089da

Please sign in to comment.