From 14e5b84a0651a69a0cd1cb70cfedf0b37d1b50a6 Mon Sep 17 00:00:00 2001 From: jt-dd <112463504+jt-dd@users.noreply.github.com> Date: Wed, 20 Nov 2024 18:24:13 +0100 Subject: [PATCH] Adding error reporting on failure on client cmd (#292) * adding error reporting on failure on client cmd * roll back * error reporting for dump * revert * adding comment --- pkg/kubehound/core/core_dump.go | 16 ++++++++++++++-- pkg/telemetry/events/events.go | 2 ++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pkg/kubehound/core/core_dump.go b/pkg/kubehound/core/core_dump.go index c6e1e588..6f62cbee 100644 --- a/pkg/kubehound/core/core_dump.go +++ b/pkg/kubehound/core/core_dump.go @@ -25,6 +25,13 @@ func DumpCore(ctx context.Context, khCfg *config.KubehoundConfig, upload bool) ( l := log.Logger(ctx) clusterName, err := config.GetClusterName(ctx) + defer func() { + if err != nil { + errMsg := fmt.Errorf("fatal error: %w", err) + l.Error("Error occurred", log.ErrorField(errMsg)) + _ = events.PushEvent(ctx, events.DumpFailed, fmt.Sprintf("%s", errMsg)) + } + }() if err != nil { return "", fmt.Errorf("collector cluster info: %w", err) } @@ -36,6 +43,7 @@ func DumpCore(ctx context.Context, khCfg *config.KubehoundConfig, upload bool) ( span, ctx := span.SpanRunFromContext(ctx, span.DumperLaunch) span.SetTag(ext.ManualKeep, true) + l = log.Logger(ctx) defer func() { span.Finish(tracer.WithError(err)) }() @@ -53,12 +61,16 @@ func DumpCore(ctx context.Context, khCfg *config.KubehoundConfig, upload bool) ( if upload { // Clean up the temporary directory when done defer func() { - err = os.RemoveAll(khCfg.Collector.File.Directory) + // This error is scope to the defer and not be handled by the other defer function + err := os.RemoveAll(khCfg.Collector.File.Directory) if err != nil { + errMsg := fmt.Errorf("Failed to remove temporary directory: %w", err) l.Error("Failed to remove temporary directory", log.ErrorField(err)) + _ = events.PushEvent(ctx, events.DumpFailed, fmt.Sprintf("%s", errMsg)) } }() - puller, err := blob.NewBlobStorage(khCfg, khCfg.Ingestor.Blob) + var puller *blob.BlobStore + puller, err = blob.NewBlobStorage(khCfg, khCfg.Ingestor.Blob) if err != nil { return "", err } diff --git a/pkg/telemetry/events/events.go b/pkg/telemetry/events/events.go index 5e008e3f..e9b5494d 100644 --- a/pkg/telemetry/events/events.go +++ b/pkg/telemetry/events/events.go @@ -18,6 +18,7 @@ const ( IngestorFailed DumpStarted DumpFinished + DumpFailed ) const ( @@ -47,6 +48,7 @@ var map2msg = map[EventAction]EventActionDetails{ DumpStarted: {Title: "Dump started", Level: statsd.Info, Action: EventActionStart}, DumpFinished: {Title: "Dump finished", Level: statsd.Info, Action: EventActionFinish}, + DumpFailed: {Title: "Dump failed", Level: statsd.Error, Action: EventActionFail}, } func (ea EventAction) Tags(ctx context.Context) []string {