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

Expose more health info as well as counts in Apply results #393

Merged
merged 1 commit into from
Jun 14, 2024
Merged
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
5 changes: 3 additions & 2 deletions applylib/applyset/applyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ func (a *ApplySet) ApplyOnce(ctx context.Context) (*ApplyResults, error) {
visitedUids.Insert(lastApplied.GetUID())
tracker.lastApplied = lastApplied
results.applySuccess(gvk, nn)
tracker.isHealthy = isHealthy(lastApplied)
results.reportHealth(gvk, nn, tracker.isHealthy)
message := ""
tracker.isHealthy, message = isHealthy(lastApplied)
results.reportHealth(gvk, nn, tracker.isHealthy, message)
}

// 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 @@ -26,27 +26,27 @@ import (

// isHealthy reports whether the object should be considered "healthy"
// TODO: Replace with kstatus library
func isHealthy(u *unstructured.Unstructured) bool {
func isHealthy(u *unstructured.Unstructured) (bool, string) {
result, err := status.Compute(u)
if err != nil {
klog.Infof("unable to compute condition for %s", humanName(u))
return false
return false, result.Message
}
switch result.Status {
case status.InProgressStatus:
return false
return false, result.Message
case status.FailedStatus:
return false
return false, result.Message
case status.TerminatingStatus:
return false
return false, result.Message
case status.UnknownStatus:
klog.Warningf("unknown status for %s", humanName(u))
return false
return false, result.Message
case status.CurrentStatus:
return true
return true, result.Message
default:
klog.Warningf("unknown status value %s", result.Status)
return false
return false, result.Message
}
}

Expand Down
40 changes: 39 additions & 1 deletion applylib/applyset/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ import (
"k8s.io/klog/v2"
)

type ObjectStatus struct {
GVK schema.GroupVersionKind
NameNamespace types.NamespacedName
IsHealthy bool
IsPruned bool
Message string
Error error
}

// ApplyResults contains the results of an Apply operation.
type ApplyResults struct {
total int
Expand All @@ -31,6 +40,7 @@ type ApplyResults struct {
pruneFailCount int
healthyCount int
unhealthyCount int
Objects []ObjectStatus
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So these were previously unexported so that we could change the implementation and instead offer higher-level methods (AllApplied, All healthy). What signals do we want to expose? And which objects appear in Objects? One answer could be that we every object in the manifest appears in Objects, and then we populate Applied, Is healthy etc at the object level. But what about objects we are pruning - should they appear in Objects?

I guess the question is ... what do you want to do with this information? Do we have to commit to exposing everything, or can we make more incremental changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We mostly want just the objects. Updated the PR to reflect that. Not exposing the counts anymore. Just the list of object status.

}

// AllApplied is true if the desired state has been successfully applied for all objects.
Expand Down Expand Up @@ -62,6 +72,14 @@ func (r *ApplyResults) checkInvariants() {
// applyError records that the apply of an object failed with an error.
func (r *ApplyResults) applyError(gvk schema.GroupVersionKind, nn types.NamespacedName, err error) {
r.applyFailCount++
r.Objects = append(r.Objects, ObjectStatus{
GVK: gvk,
NameNamespace: nn,
IsHealthy: false,
IsPruned: false,
Message: "Apply Error",
Error: err,
})
klog.Warningf("error from apply on %s %s: %v", gvk, nn, err)
}

Expand All @@ -72,17 +90,37 @@ func (r *ApplyResults) applySuccess(gvk schema.GroupVersionKind, nn types.Namesp

// pruneError records that the prune of an object failed with an error.
func (r *ApplyResults) pruneError(gvk schema.GroupVersionKind, nn types.NamespacedName, err error) {
r.Objects = append(r.Objects, ObjectStatus{
GVK: gvk,
NameNamespace: nn,
IsHealthy: true,
IsPruned: true,
Message: "Prune Error",
Error: err,
})
r.pruneFailCount++
klog.Warningf("error from pruning on %s %s: %v", gvk, nn, err)
}

// pruneSuccess records that an object was pruned and this succeeded.
func (r *ApplyResults) pruneSuccess(gvk schema.GroupVersionKind, nn types.NamespacedName) {
r.Objects = append(r.Objects, ObjectStatus{
GVK: gvk,
NameNamespace: nn,
IsPruned: true,
})
r.pruneSuccessCount++
}

// reportHealth records the health of an object.
func (r *ApplyResults) reportHealth(gvk schema.GroupVersionKind, nn types.NamespacedName, isHealthy bool) {
func (r *ApplyResults) reportHealth(gvk schema.GroupVersionKind, nn types.NamespacedName, isHealthy bool, message string) {
r.Objects = append(r.Objects, ObjectStatus{
GVK: gvk,
NameNamespace: nn,
IsHealthy: isHealthy,
IsPruned: false,
Message: message,
})
if isHealthy {
r.healthyCount++
} else {
Expand Down
Loading