From 4dbcbb2d732b237d37db01d5aa04bb3ba2e3b6a0 Mon Sep 17 00:00:00 2001 From: Barni S Date: Mon, 12 Aug 2024 23:12:03 -0400 Subject: [PATCH] Capture error as part of health computation and propagate it to health tracker. This will be returned to the caller as part of object result --- applylib/applyset/applyset.go | 6 ++-- applylib/applyset/health.go | 16 ++++----- applylib/applyset/results.go | 62 +++++++++++++++++++++++++---------- 3 files changed, 56 insertions(+), 28 deletions(-) diff --git a/applylib/applyset/applyset.go b/applylib/applyset/applyset.go index a5471107..5b168cdc 100644 --- a/applylib/applyset/applyset.go +++ b/applylib/applyset/applyset.go @@ -36,7 +36,7 @@ import ( kubectlapply "sigs.k8s.io/kubebuilder-declarative-pattern/applylib/forked/github.com/kubernetes/kubectl/pkg/cmd/apply" ) -type ComputeHealthCallback func(*unstructured.Unstructured) (bool, string) +type ComputeHealthCallback func(*unstructured.Unstructured) (bool, string, error) // ApplySet is a set of objects that we want to apply to the cluster. // @@ -270,8 +270,8 @@ func (a *ApplySet) ApplyOnce(ctx context.Context) (*ApplyResults, error) { tracker.lastApplied = lastApplied results.applySuccess(gvk, nn) message := "" - tracker.isHealthy, message = a.computeHealth(lastApplied) - results.reportHealth(gvk, nn, tracker.isHealthy, message) + tracker.isHealthy, message, err = a.computeHealth(lastApplied) + results.reportHealth(gvk, nn, tracker.isHealthy, message, err) } // We want to be more cautions on pruning and only do it if all manifests are applied. diff --git a/applylib/applyset/health.go b/applylib/applyset/health.go index 4e763101..5643737b 100644 --- a/applylib/applyset/health.go +++ b/applylib/applyset/health.go @@ -25,27 +25,27 @@ import ( ) // IsHealthy reports whether the object should be considered "healthy" -func IsHealthy(u *unstructured.Unstructured) (bool, string) { +func IsHealthy(u *unstructured.Unstructured) (bool, string, error) { result, err := status.Compute(u) if err != nil { klog.Infof("unable to compute condition for %s", humanName(u)) - return false, result.Message + return false, result.Message, err } switch result.Status { case status.InProgressStatus: - return false, result.Message + return false, result.Message, nil case status.FailedStatus: - return false, result.Message + return false, result.Message, nil case status.TerminatingStatus: - return false, result.Message + return false, result.Message, nil case status.UnknownStatus: klog.Warningf("unknown status for %s", humanName(u)) - return false, result.Message + return false, result.Message, nil case status.CurrentStatus: - return true, result.Message + return true, result.Message, nil default: klog.Warningf("unknown status value %s", result.Status) - return false, result.Message + return false, result.Message, nil } } diff --git a/applylib/applyset/results.go b/applylib/applyset/results.go index 901484c1..f09e0b39 100644 --- a/applylib/applyset/results.go +++ b/applylib/applyset/results.go @@ -22,13 +22,23 @@ import ( "k8s.io/klog/v2" ) +type HealthInfo struct { + IsHealthy bool + Message string + Error error +} + +type ApplyInfo struct { + IsPruned bool + Message string + Error error +} + type ObjectStatus struct { GVK schema.GroupVersionKind NameNamespace types.NamespacedName - IsHealthy bool - IsPruned bool - Message string - Error error + Apply ApplyInfo + Health HealthInfo } // ApplyResults contains the results of an Apply operation. @@ -75,10 +85,14 @@ func (r *ApplyResults) applyError(gvk schema.GroupVersionKind, nn types.Namespac r.Objects = append(r.Objects, ObjectStatus{ GVK: gvk, NameNamespace: nn, - IsHealthy: false, - IsPruned: false, - Message: "Apply Error", - Error: err, + Health: HealthInfo{ + IsHealthy: false, + }, + Apply: ApplyInfo{ + IsPruned: false, + Message: "Apply Error", + Error: err, + }, }) klog.Warningf("error from apply on %s %s: %v", gvk, nn, err) } @@ -93,10 +107,14 @@ func (r *ApplyResults) pruneError(gvk schema.GroupVersionKind, nn types.Namespac r.Objects = append(r.Objects, ObjectStatus{ GVK: gvk, NameNamespace: nn, - IsHealthy: true, - IsPruned: true, - Message: "Prune Error", - Error: err, + Health: HealthInfo{ + IsHealthy: true, + }, + Apply: ApplyInfo{ + IsPruned: true, + Message: "Prune Error", + Error: err, + }, }) r.pruneFailCount++ klog.Warningf("error from pruning on %s %s: %v", gvk, nn, err) @@ -107,19 +125,29 @@ func (r *ApplyResults) pruneSuccess(gvk schema.GroupVersionKind, nn types.Namesp r.Objects = append(r.Objects, ObjectStatus{ GVK: gvk, NameNamespace: nn, - IsPruned: true, + Health: HealthInfo{ + IsHealthy: true, + }, + Apply: ApplyInfo{ + IsPruned: true, + }, }) r.pruneSuccessCount++ } // reportHealth records the health of an object. -func (r *ApplyResults) reportHealth(gvk schema.GroupVersionKind, nn types.NamespacedName, isHealthy bool, message string) { +func (r *ApplyResults) reportHealth(gvk schema.GroupVersionKind, nn types.NamespacedName, isHealthy bool, message string, err error) { r.Objects = append(r.Objects, ObjectStatus{ GVK: gvk, NameNamespace: nn, - IsHealthy: isHealthy, - IsPruned: false, - Message: message, + Health: HealthInfo{ + IsHealthy: isHealthy, + Message: message, + Error: err, + }, + Apply: ApplyInfo{ + IsPruned: false, + }, }) if isHealthy { r.healthyCount++