Skip to content

Commit

Permalink
Capture error as part of health computation and propagate it to healt…
Browse files Browse the repository at this point in the history
…h tracker. This will be returned to the caller as part of object result
  • Loading branch information
barney-s committed Aug 13, 2024
1 parent 2a9fe22 commit 4dbcbb2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 28 deletions.
6 changes: 3 additions & 3 deletions applylib/applyset/applyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions applylib/applyset/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
62 changes: 45 additions & 17 deletions applylib/applyset/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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++
Expand Down

0 comments on commit 4dbcbb2

Please sign in to comment.