Skip to content

Commit

Permalink
Fix flags for rootcmd (#301)
Browse files Browse the repository at this point in the history
* Fix flags for rootcmd

* Pushing new config path

* Fix

* add cfgFile to config command

* fix logs

* fixing remote chain in ingest command

* fix unit tests
  • Loading branch information
jt-dd authored Dec 9, 2024
1 parent 5965f9b commit 30084a5
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/kubehound/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
Short: "Show the current configuration",
Long: `[devOnly] Show the current configuration`,
PreRunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.InitializeKubehoundConfig(cobraCmd.Context(), "", true, true)
return cmd.InitializeKubehoundConfig(cobraCmd.Context(), cfgFile, true, true)
},
RunE: func(cobraCmd *cobra.Command, args []string) error {
// Adding datadog setup
Expand Down
6 changes: 3 additions & 3 deletions cmd/kubehound/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var (
viper.BindPFlag(config.IngestorAPIEndpoint, cobraCmd.Flags().Lookup("khaas-server")) //nolint: errcheck
viper.BindPFlag(config.IngestorAPIInsecure, cobraCmd.Flags().Lookup("insecure")) //nolint: errcheck

return cmd.InitializeKubehoundConfig(cobraCmd.Context(), "", true, true)
return cmd.InitializeKubehoundConfig(cobraCmd.Context(), cfgFile, true, true)
},
RunE: func(cobraCmd *cobra.Command, args []string) error {
// using compress feature
Expand All @@ -62,7 +62,7 @@ var (
return fmt.Errorf("dump core: %w", err)
}
// Running the ingestion on KHaaS
if cobraCmd.Flags().Lookup("khaas-server").Value.String() != "" {
if khCfg.Ingestor.API.Endpoint != "" {
return core.CoreClientGRPCIngest(cobraCmd.Context(), khCfg.Ingestor, khCfg.Dynamic.ClusterName, khCfg.Dynamic.RunID.String())
}

Expand All @@ -77,7 +77,7 @@ var (
PreRunE: func(cobraCmd *cobra.Command, args []string) error {
viper.Set(config.CollectorFileDirectory, args[0])

return cmd.InitializeKubehoundConfig(cobraCmd.Context(), "", true, true)
return cmd.InitializeKubehoundConfig(cobraCmd.Context(), cfgFile, true, true)
},
RunE: func(cobraCmd *cobra.Command, args []string) error {
// Passing the Kubehound config from viper
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubehound/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
PreRunE: func(cobraCmd *cobra.Command, args []string) error {
cmd.BindFlagCluster(cobraCmd)

return cmd.InitializeKubehoundConfig(cobraCmd.Context(), "", true, true)
return cmd.InitializeKubehoundConfig(cobraCmd.Context(), cfgFile, true, true)
},
RunE: func(cobraCmd *cobra.Command, args []string) error {
// Passing the Kubehound config from viper
Expand All @@ -56,7 +56,7 @@ var (
cobraCmd.MarkFlagRequired("cluster") //nolint: errcheck
}

return cmd.InitializeKubehoundConfig(cobraCmd.Context(), "", false, true)
return cmd.InitializeKubehoundConfig(cobraCmd.Context(), cfgFile, false, true)
},
RunE: func(cobraCmd *cobra.Command, args []string) error {
// Passing the Kubehound config from viper
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubehound/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ var (
)

func init() {
rootCmd.Flags().StringVarP(&cfgFile, "config", "c", cfgFile, "application config file")
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", cfgFile, "application config file")

rootCmd.Flags().BoolVar(&skipBackend, "skip-backend", skipBackend, "skip the auto deployment of the backend stack (janusgraph, mongodb, and UI)")
rootCmd.PersistentFlags().BoolVar(&skipBackend, "skip-backend", skipBackend, "skip the auto deployment of the backend stack (janusgraph, mongodb, and UI)")

cmd.InitRootCmd(rootCmd)
}
3 changes: 2 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func NewKubehoundConfig(ctx context.Context, configPath string, inLine bool) *Ku
var cfg *KubehoundConfig
switch {
case len(configPath) != 0:
l.Info("Loading application configuration from file", log.String("path", configPath))
cfg = MustLoadConfig(ctx, configPath)
case inLine:
l.Info("Loading application from inline command")
Expand Down Expand Up @@ -207,10 +206,12 @@ func unmarshalConfig(v *viper.Viper) (*KubehoundConfig, error) {

// NewConfig creates a new config instance from the provided file using viper.
func NewConfig(ctx context.Context, v *viper.Viper, configPath string) (*KubehoundConfig, error) {
l := log.Logger(ctx)
// Configure default values
SetDefaultValues(ctx, v)

// Loading inLine config path
l.Info("Loading application configuration from file", log.String("path", configPath))
v.SetConfigType(DefaultConfigType)
v.SetConfigFile(configPath)

Expand Down
4 changes: 2 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestMustLoadConfig(t *testing.T) {
},
Ingestor: IngestorConfig{
API: IngestorAPIConfig{
Endpoint: "127.0.0.1:9000",
Endpoint: "",
Insecure: false,
},
Blob: &BlobConfig{
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestMustLoadConfig(t *testing.T) {
},
Ingestor: IngestorConfig{
API: IngestorAPIConfig{
Endpoint: "127.0.0.1:9000",
Endpoint: "",
Insecure: false,
},
Blob: &BlobConfig{
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/ingestor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

const (
DefaultIngestorAPIEndpoint = "127.0.0.1:9000"
DefaultIngestorAPIEndpoint = ""
DefaultIngestorAPIInsecure = false
DefaultBucketName = "" // we want to let it empty because we can easily abort if it's not configured
DefaultTempDir = "/tmp/kubehound"
Expand Down

0 comments on commit 30084a5

Please sign in to comment.