diff --git a/cmd/tetragon/conf.go b/cmd/tetragon/conf.go index 26a1cbc172f..41889b1867d 100644 --- a/cmd/tetragon/conf.go +++ b/cmd/tetragon/conf.go @@ -3,9 +3,6 @@ package main import ( - "fmt" - "os" - "path/filepath" "strings" "github.com/cilium/tetragon/pkg/option" @@ -22,45 +19,6 @@ var ( } ) -func readConfigFile(path string, file string) error { - filePath := filepath.Join(path, file) - st, err := os.Stat(filePath) - if err != nil { - return err - } - if st.Mode().IsRegular() == false { - return fmt.Errorf("failed to read config file '%s' not a regular file", file) - } - - viper.AddConfigPath(path) - err = viper.MergeInConfig() - if err != nil { - return err - } - - return nil -} - -func readConfigDir(path string) error { - st, err := os.Stat(path) - if err != nil { - return err - } - if st.IsDir() == false { - return fmt.Errorf("'%s' is not a directory", path) - } - - cm, err := option.ReadDirConfig(path) - if err != nil { - return err - } - if err := viper.MergeConfigMap(cm); err != nil { - return fmt.Errorf("merge config failed %v", err) - } - - return nil -} - func readConfigSettings(defaultConfDir string, defaultConfDropIn string, dropInsDir []string) { viper.SetEnvPrefix("tetragon") replacer := strings.NewReplacer("-", "_") @@ -73,24 +31,24 @@ func readConfigSettings(defaultConfDir string, defaultConfDropIn string, dropIns // Read default drop-ins directories for _, dir := range dropInsDir { - readConfigDir(dir) + option.ReadConfigDir(dir) } // Look into cwd first, this is needed for quick development only - readConfigFile(".", "tetragon.yaml") + option.ReadConfigFile(".", "tetragon.yaml") // Look for /etc/tetragon/tetragon.yaml - readConfigFile(defaultConfDir, "tetragon.yaml") + option.ReadConfigFile(defaultConfDir, "tetragon.yaml") // Look into default /etc/tetragon/tetragon.conf.d/ now - readConfigDir(defaultConfDropIn) + option.ReadConfigDir(defaultConfDropIn) // Read now the passed key --config-dir if viper.IsSet(keyConfigDir) { configDir := viper.GetString(keyConfigDir) // viper.IsSet could return true on an empty string reset if configDir != "" { - err := readConfigDir(configDir) + err := option.ReadConfigDir(configDir) if err != nil { log.WithField(keyConfigDir, configDir).WithError(err).Fatal("Failed to read config from directory") } else { diff --git a/pkg/option/config.go b/pkg/option/config.go index ea40624ab53..d0d23d84add 100644 --- a/pkg/option/config.go +++ b/pkg/option/config.go @@ -11,6 +11,7 @@ import ( "time" "github.com/cilium/tetragon/pkg/logger" + "github.com/spf13/viper" ) type config struct { @@ -137,3 +138,42 @@ func ReadDirConfig(dirName string) (map[string]interface{}, error) { } return m, nil } + +func ReadConfigFile(path string, file string) error { + filePath := filepath.Join(path, file) + st, err := os.Stat(filePath) + if err != nil { + return err + } + if st.Mode().IsRegular() == false { + return fmt.Errorf("failed to read config file '%s' not a regular file", file) + } + + viper.AddConfigPath(path) + err = viper.MergeInConfig() + if err != nil { + return err + } + + return nil +} + +func ReadConfigDir(path string) error { + st, err := os.Stat(path) + if err != nil { + return err + } + if st.IsDir() == false { + return fmt.Errorf("'%s' is not a directory", path) + } + + cm, err := ReadDirConfig(path) + if err != nil { + return err + } + if err := viper.MergeConfigMap(cm); err != nil { + return fmt.Errorf("merge config failed %v", err) + } + + return nil +}