Skip to content

Commit

Permalink
adding error reporting on failure on client cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
jt-dd committed Nov 20, 2024
1 parent e5ae1af commit de201a2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cmd/kubehound/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/DataDog/KubeHound/pkg/cmd"
"github.com/DataDog/KubeHound/pkg/config"
"github.com/DataDog/KubeHound/pkg/kubehound/core"
"github.com/DataDog/KubeHound/pkg/telemetry/events"
"github.com/DataDog/KubeHound/pkg/telemetry/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -44,6 +45,7 @@ var (

// Create a temporary directory
tmpDir, err := os.MkdirTemp("", "kubehound")
defer cmd.ReportError(cobraCmd.Context(), events.DumpFinished, err)
if err != nil {
return fmt.Errorf("create temporary directory: %w", err)
}
Expand All @@ -63,7 +65,9 @@ var (
}
// Running the ingestion on KHaaS
if cobraCmd.Flags().Lookup("khaas-server").Value.String() != "" {
return core.CoreClientGRPCIngest(cobraCmd.Context(), khCfg.Ingestor, khCfg.Dynamic.ClusterName, khCfg.Dynamic.RunID.String())
err = core.CoreClientGRPCIngest(cobraCmd.Context(), khCfg.Ingestor, khCfg.Dynamic.ClusterName, khCfg.Dynamic.RunID.String())

return err
}

return err
Expand All @@ -82,6 +86,7 @@ var (
RunE: func(cobraCmd *cobra.Command, args []string) error {
// Passing the Kubehound config from viper
khCfg, err := cmd.GetConfig()
defer cmd.ReportError(cobraCmd.Context(), events.DumpFinished, err)
if err != nil {
return fmt.Errorf("get config: %w", err)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/kubehound/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/DataDog/KubeHound/pkg/cmd"
"github.com/DataDog/KubeHound/pkg/config"
"github.com/DataDog/KubeHound/pkg/kubehound/core"
"github.com/DataDog/KubeHound/pkg/telemetry/events"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -61,6 +62,7 @@ var (
RunE: func(cobraCmd *cobra.Command, args []string) error {
// Passing the Kubehound config from viper
khCfg, err := cmd.GetConfig()
defer cmd.ReportError(cobraCmd.Context(), events.IngestorFailed, err)
if err != nil {
return fmt.Errorf("get config: %w", err)
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"strings"

"github.com/DataDog/KubeHound/pkg/telemetry/events"
"github.com/DataDog/KubeHound/pkg/telemetry/log"
)

Expand All @@ -29,3 +30,12 @@ func AskForConfirmation(ctx context.Context) (bool, error) {
return AskForConfirmation(ctx)
}
}

func ReportError(ctx context.Context, action events.EventAction, err error) {
l := log.Logger(ctx)
if err != nil {
errMsg := fmt.Errorf("fatal error: %w", err)
l.Error("Error occurred", log.ErrorField(errMsg))
_ = events.PushEvent(ctx, action, fmt.Sprintf("%s", errMsg))
}
}

0 comments on commit de201a2

Please sign in to comment.