🐛 Remove configpath field from the config and call os.MkdirAll before regenerating config

This commit is contained in:
M. Mert Yildiran
2023-03-19 20:58:20 +03:00
parent b05dbed71e
commit e1ada1768d
5 changed files with 19 additions and 20 deletions

View File

@@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"path"
"github.com/creasty/defaults" "github.com/creasty/defaults"
"github.com/kubeshark/kubeshark/config" "github.com/kubeshark/kubeshark/config"
@@ -28,7 +29,7 @@ var configCmd = &cobra.Command{
return nil 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 { } else {
template, err := utils.PrettyYaml(configWithDefaults) template, err := utils.PrettyYaml(configWithDefaults)
if err != nil { if err != nil {
@@ -52,5 +53,5 @@ func init() {
log.Debug().Err(err).Send() 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")))
} }

View File

@@ -32,7 +32,6 @@ func init() {
} }
rootCmd.PersistentFlags().StringSlice(config.SetCommandName, []string{}, fmt.Sprintf("Override values using --%s", config.SetCommandName)) 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") rootCmd.PersistentFlags().BoolP(config.DebugFlag, "d", false, "Enable debug mode")
} }

View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strconv" "strconv"
@@ -62,10 +63,9 @@ func InitConfig(cmd *cobra.Command) error {
return err return err
} }
configFilePathFlag := cmd.Flags().Lookup(ConfigFilePathCommandName) ConfigFilePath = path.Join(misc.GetDotFolderPath(), "config.yaml")
ConfigFilePath = configFilePathFlag.Value.String()
if err := loadConfigFile(&Config); err != nil { if err := loadConfigFile(&Config); err != nil {
if configFilePathFlag.Changed || !os.IsNotExist(err) { if !os.IsNotExist(err) {
return fmt.Errorf("invalid config, %w\n"+ return fmt.Errorf("invalid config, %w\n"+
"you can regenerate the file by removing it (%v) and using `kubeshark config -r`", err, ConfigFilePath) "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) 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 { if err := os.WriteFile(ConfigFilePath, data, 0644); err != nil {
return fmt.Errorf("failed writing config, err: %v", err) return fmt.Errorf("failed writing config, err: %v", err)
} }
@@ -139,9 +147,7 @@ func initFlag(f *pflag.Flag) {
configElemValue := reflect.ValueOf(&Config).Elem() configElemValue := reflect.ValueOf(&Config).Elem()
var flagPath []string 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, "-")...) flagPath = append(flagPath, strings.Split(f.Name, "-")...)

View File

@@ -2,7 +2,6 @@ package config
import ( import (
"os" "os"
"path"
"path/filepath" "path/filepath"
"github.com/kubeshark/kubeshark/config/configStructs" "github.com/kubeshark/kubeshark/config/configStructs"
@@ -12,9 +11,8 @@ import (
) )
const ( const (
SelfNamespaceConfigName = "selfnamespace" SelfNamespaceConfigName = "selfnamespace"
ConfigFilePathCommandName = "configpath" KubeConfigPathConfigName = "kube-configpath"
KubeConfigPathConfigName = "kube-configpath"
) )
func CreateDefaultConfig() ConfigStruct { func CreateDefaultConfig() ConfigStruct {
@@ -33,17 +31,12 @@ type ConfigStruct struct {
Kube KubeConfig `yaml:"kube"` Kube KubeConfig `yaml:"kube"`
SelfNamespace string `yaml:"selfnamespace" default:"kubeshark"` SelfNamespace string `yaml:"selfnamespace" default:"kubeshark"`
DumpLogs bool `yaml:"dumplogs" default:"false"` DumpLogs bool `yaml:"dumplogs" default:"false"`
ConfigFilePath string `yaml:"configpath,omitempty" readonly:""`
HeadlessMode bool `yaml:"headless" default:"false"` HeadlessMode bool `yaml:"headless" default:"false"`
License string `yaml:"license" default:""` License string `yaml:"license" default:""`
Scripting configStructs.ScriptingConfig `yaml:"scripting"` Scripting configStructs.ScriptingConfig `yaml:"scripting"`
ResourceLabels map[string]string `yaml:"resourceLabels" default:"{}"` 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 { func (config *ConfigStruct) ImagePullPolicy() v1.PullPolicy {
return v1.PullPolicy(config.Tap.Docker.ImagePullPolicy) return v1.PullPolicy(config.Tap.Docker.ImagePullPolicy)
} }

View File

@@ -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.") 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!") log.Error().Err(err).Msg("Failed write file!")
} else { } 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:") log.Info().Str("path", filePath).Msg("You can find the ZIP file with all logs at:")