Skip to content

Commit

Permalink
Use kStatus to compute status (#3743)
Browse files Browse the repository at this point in the history
After applying the object, compute the status using the kStatus library.
  • Loading branch information
justinsb committed Jan 26, 2023
1 parent 3c8e2ac commit 11a17b6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 69 deletions.
20 changes: 19 additions & 1 deletion porch/controllers/remoterootsyncsets/pkg/applyset/applyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/klog/v2"
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
)

// ApplySet is a set of objects that we want to apply to the cluster.
Expand Down Expand Up @@ -210,7 +211,24 @@ func (a *ApplySet) ApplyOnce(ctx context.Context) (*ApplyResults, error) {
tracker.lastApplied = applied
results.applySuccess(gvk, nn)

tracker.isHealthy = isHealthy(applied)
health, err := computeHealth(applied)
if err != nil {
klog.Warningf("error computing health: %v", err)
tracker.isHealthy = false
} else {
switch health.Status {
case status.CurrentStatus:
tracker.isHealthy = true
case status.InProgressStatus:
// TODO: Do we want a different status here?
tracker.isHealthy = false
case status.FailedStatus:
tracker.isHealthy = false
default:
klog.Warningf("unexpected health status %v", health)
tracker.isHealthy = false
}
}
results.reportHealth(gvk, nn, tracker.isHealthy)
}
return results, nil
Expand Down
71 changes: 4 additions & 67 deletions porch/controllers/remoterootsyncsets/pkg/applyset/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,74 +15,11 @@
package applyset

import (
"encoding/json"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog/v2"
)

// isHealthy reports whether the object should be considered "healthy"
// TODO: Replace with kstatus library
func isHealthy(u *unstructured.Unstructured) bool {
ready := true

statusConditions, found, err := unstructured.NestedFieldNoCopy(u.Object, "status", "conditions")
if err != nil || !found {
klog.Infof("status conditions not found for %s", u.GroupVersionKind())
return true
}

statusConditionsList, ok := statusConditions.([]interface{})
if !ok {
klog.Warningf("expected status.conditions to be list, got %T", statusConditions)
return true
}
for i := range statusConditionsList {
condition := statusConditionsList[i]
conditionMap, ok := condition.(map[string]interface{})
if !ok {
klog.Warningf("expected status.conditions[%d] to be map, got %T", i, condition)
return true
}

conditionType := ""
conditionStatus := ""
for k, v := range conditionMap {
switch k {
case "type":
s, ok := v.(string)
if !ok {
klog.Warningf("expected status.conditions[].type to be string, got %T", v)
} else {
conditionType = s
}
case "status":
s, ok := v.(string)
if !ok {
klog.Warningf("expected status.conditions[].status to be string, got %T", v)
} else {
conditionStatus = s
}
}
}

// TODO: Check conditionType?

switch conditionStatus {
case "True":
// ready

case "False":
j, _ := json.Marshal(condition)
klog.Infof("status.conditions indicates object is not ready: %v", string(j))
ready = false

case "":
klog.Warningf("ignoring status.conditions[] type %q with unknown status %q", conditionType, conditionStatus)

}
}
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
)

klog.Infof("isHealthy %s => %v", u.GroupVersionKind(), ready)
return ready
func computeHealth(u *unstructured.Unstructured) (*status.Result, error) {
return status.Compute(u)
}
2 changes: 1 addition & 1 deletion porch/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ require (
k8s.io/klog/v2 v2.70.1
k8s.io/kube-aggregator v0.24.0-beta.0
k8s.io/utils v0.0.0-20220823124924-e9cbc92d1a73
sigs.k8s.io/cli-utils v0.34.0
sigs.k8s.io/controller-runtime v0.13.0
sigs.k8s.io/kustomize/kyaml v0.13.9
sigs.k8s.io/yaml v1.3.0
Expand Down Expand Up @@ -187,7 +188,6 @@ require (
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
k8s.io/kubectl v0.25.3 // indirect
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.33 // indirect
sigs.k8s.io/cli-utils v0.34.0 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/kustomize/api v0.12.1 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
Expand Down

0 comments on commit 11a17b6

Please sign in to comment.