diff --git a/cmd/config.go b/cmd/config.go index e7cf618fc..f74a086ee 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "path" "github.com/creasty/defaults" "github.com/kubeshark/kubeshark/config" @@ -28,7 +29,7 @@ var configCmd = &cobra.Command{ return nil } - log.Info().Str("config-path", config.Config.ConfigFilePath).Msg("Template file written to config path.") + log.Info().Str("config-path", config.ConfigFilePath).Msg("Template file written to config path.") } else { template, err := utils.PrettyYaml(configWithDefaults) if err != nil { @@ -52,5 +53,5 @@ func init() { log.Debug().Err(err).Send() } - configCmd.Flags().BoolP(configStructs.RegenerateConfigName, "r", defaultConfig.Config.Regenerate, fmt.Sprintf("Regenerate the config file with default values to path %s or to chosen path using --%s", defaultConfig.ConfigFilePath, config.ConfigFilePathCommandName)) + configCmd.Flags().BoolP(configStructs.RegenerateConfigName, "r", defaultConfig.Config.Regenerate, fmt.Sprintf("Regenerate the config file with default values to path %s", path.Join(misc.GetDotFolderPath(), "config.yaml"))) } diff --git a/cmd/root.go b/cmd/root.go index 2f818c9dd..cd3cff8d3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -32,7 +32,6 @@ func init() { } rootCmd.PersistentFlags().StringSlice(config.SetCommandName, []string{}, fmt.Sprintf("Override values using --%s", config.SetCommandName)) - rootCmd.PersistentFlags().String(config.ConfigFilePathCommandName, defaultConfig.ConfigFilePath, fmt.Sprintf("Override config file path using --%s", config.ConfigFilePathCommandName)) rootCmd.PersistentFlags().BoolP(config.DebugFlag, "d", false, "Enable debug mode") } diff --git a/config/config.go b/config/config.go index 2c0f9d4be..63e126607 100644 --- a/config/config.go +++ b/config/config.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "path" "path/filepath" "reflect" "strconv" @@ -62,10 +63,9 @@ func InitConfig(cmd *cobra.Command) error { return err } - configFilePathFlag := cmd.Flags().Lookup(ConfigFilePathCommandName) - ConfigFilePath = configFilePathFlag.Value.String() + ConfigFilePath = path.Join(misc.GetDotFolderPath(), "config.yaml") if err := loadConfigFile(&Config); err != nil { - if configFilePathFlag.Changed || !os.IsNotExist(err) { + if !os.IsNotExist(err) { return fmt.Errorf("invalid config, %w\n"+ "you can regenerate the file by removing it (%v) and using `kubeshark config -r`", err, ConfigFilePath) } @@ -97,6 +97,14 @@ func WriteConfig(config *ConfigStruct) error { } data := []byte(template) + + if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) { + err = os.MkdirAll(filepath.Dir(ConfigFilePath), 0700) + if err != nil { + return fmt.Errorf("failed creating directories, err: %v", err) + } + } + if err := os.WriteFile(ConfigFilePath, data, 0644); err != nil { return fmt.Errorf("failed writing config, err: %v", err) } @@ -139,9 +147,7 @@ func initFlag(f *pflag.Flag) { configElemValue := reflect.ValueOf(&Config).Elem() var flagPath []string - if !utils.Contains([]string{ConfigFilePathCommandName}, f.Name) { - flagPath = append(flagPath, cmdName) - } + flagPath = append(flagPath, cmdName) flagPath = append(flagPath, strings.Split(f.Name, "-")...) diff --git a/config/configStruct.go b/config/configStruct.go index 92cd98f2e..95970d52f 100644 --- a/config/configStruct.go +++ b/config/configStruct.go @@ -2,7 +2,6 @@ package config import ( "os" - "path" "path/filepath" "github.com/kubeshark/kubeshark/config/configStructs" @@ -12,9 +11,8 @@ import ( ) const ( - SelfNamespaceConfigName = "selfnamespace" - ConfigFilePathCommandName = "configpath" - KubeConfigPathConfigName = "kube-configpath" + SelfNamespaceConfigName = "selfnamespace" + KubeConfigPathConfigName = "kube-configpath" ) func CreateDefaultConfig() ConfigStruct { @@ -33,17 +31,12 @@ type ConfigStruct struct { Kube KubeConfig `yaml:"kube"` SelfNamespace string `yaml:"selfnamespace" default:"kubeshark"` DumpLogs bool `yaml:"dumplogs" default:"false"` - ConfigFilePath string `yaml:"configpath,omitempty" readonly:""` HeadlessMode bool `yaml:"headless" default:"false"` License string `yaml:"license" default:""` Scripting configStructs.ScriptingConfig `yaml:"scripting"` ResourceLabels map[string]string `yaml:"resourceLabels" default:"{}"` } -func (config *ConfigStruct) SetDefaults() { - config.ConfigFilePath = path.Join(misc.GetDotFolderPath(), "config.yaml") -} - func (config *ConfigStruct) ImagePullPolicy() v1.PullPolicy { return v1.PullPolicy(config.Tap.Docker.ImagePullPolicy) } diff --git a/misc/fsUtils/kubesharkLogsUtils.go b/misc/fsUtils/kubesharkLogsUtils.go index bc0728f1a..a2ad82d73 100644 --- a/misc/fsUtils/kubesharkLogsUtils.go +++ b/misc/fsUtils/kubesharkLogsUtils.go @@ -73,10 +73,10 @@ func DumpLogs(ctx context.Context, provider *kubernetes.Provider, filePath strin log.Debug().Str("namespace", config.Config.SelfNamespace).Msg("Successfully added events.") } - if err := AddFileToZip(zipWriter, config.Config.ConfigFilePath); err != nil { + if err := AddFileToZip(zipWriter, config.ConfigFilePath); err != nil { log.Error().Err(err).Msg("Failed write file!") } else { - log.Debug().Str("file-path", config.Config.ConfigFilePath).Msg("Successfully added file.") + log.Debug().Str("file-path", config.ConfigFilePath).Msg("Successfully added file.") } log.Info().Str("path", filePath).Msg("You can find the ZIP file with all logs at:")