From 430152489dcb6c7e8f4ba37288f945774c208eb5 Mon Sep 17 00:00:00 2001 From: Max Jonas Werner Date: Fri, 2 Jul 2021 16:39:04 +0200 Subject: [PATCH] feat: collect status errors in map and print it in the error 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. --- controllers/kustomization_healthcheck.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/controllers/kustomization_healthcheck.go b/controllers/kustomization_healthcheck.go index 0f6cd3027..81291f3ca 100644 --- a/controllers/kustomization_healthcheck.go +++ b/controllers/kustomization_healthcheck.go @@ -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) } @@ -81,7 +83,11 @@ 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 { @@ -89,11 +95,11 @@ func (hc *KustomizeHealthCheck) Assess(pollInterval time.Duration) error { 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