Skip to content

Commit

Permalink
feat: collect status errors in map and print it in the error
Browse files Browse the repository at this point in the history
Whenever a health check times out now, the most recently collected
error for each resource (if any) will be printed as part of the error
message.
  • Loading branch information
Max Jonas Werner committed Jul 2, 2021
1 parent 63d83ff commit 4301524
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions controllers/kustomization_healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ func (hc *KustomizeHealthCheck) Assess(pollInterval time.Duration) error {
opts := polling.Options{PollInterval: pollInterval, UseCache: true}
eventsChan := hc.statusPoller.Poll(ctx, objMetadata, opts)
coll := collector.NewResourceStatusCollector(objMetadata)
statusErrs := make(map[object.ObjMetadata]error)
done := coll.ListenWithObserver(eventsChan, collector.ObserverFunc(
func(statusCollector *collector.ResourceStatusCollector, e event.Event) {
var rss []*event.ResourceStatus
for _, rs := range statusCollector.ResourceStatuses {
if rs.Error != nil {
coll.Error = rs.Error
cancel()
return
statusErrs[rs.Identifier] = rs.Error
} else {
// delete the most recently recorded error for this resources as it's not valid, anymore now
delete(statusErrs, rs.Identifier)
}
rss = append(rss, rs)
}
Expand All @@ -81,19 +83,23 @@ func (hc *KustomizeHealthCheck) Assess(pollInterval time.Duration) error {

<-done

if ctx.Err() == context.DeadlineExceeded || coll.Error != nil {
if coll.Error != nil {
return coll.Error
}

if ctx.Err() == context.DeadlineExceeded {
ids := []string{}
for _, rs := range coll.ResourceStatuses {
if rs.Status != status.CurrentStatus {
id := hc.objMetadataToString(rs.Identifier)
ids = append(ids, id)
}
}
if ctx.Err() == context.DeadlineExceeded {
return fmt.Errorf("Health check timed out for [%v]", strings.Join(ids, ", "))
} else {
return fmt.Errorf("Health check failed for [%v]: %w", strings.Join(ids, ", "), coll.Error)
var errsArr []string
for res, err := range statusErrs {
errsArr = append(errsArr, fmt.Sprintf("%s: %s", hc.objMetadataToString(res), err))
}
return fmt.Errorf("Health check failed for [%v]: %v", strings.Join(ids, ", "), strings.Join(errsArr, ", "))
}

return nil
Expand Down

0 comments on commit 4301524

Please sign in to comment.