Skip to content

Commit

Permalink
Adding error reporting on failure on client cmd (#292)
Browse files Browse the repository at this point in the history
* adding error reporting on failure on client cmd

* roll back

* error reporting for dump

* revert

* adding comment
  • Loading branch information
jt-dd authored Nov 20, 2024
1 parent e5ae1af commit 14e5b84
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/kubehound/core/core_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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))
}()
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/telemetry/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
IngestorFailed
DumpStarted
DumpFinished
DumpFailed
)

const (
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 14e5b84

Please sign in to comment.