Skip to content

Commit

Permalink
refactor: Move readConfig{Dir,File} to the option package
Browse files Browse the repository at this point in the history
Move readConfig{Dir,File} to the option package. I'd like to use these
functions to read tetragon operator configurations.

Ref: #794

Signed-off-by: Michi Mutsuzaki <michi@isovalent.com>
  • Loading branch information
michi-covalent committed Aug 8, 2023
1 parent 7dd647f commit 181cffd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 47 deletions.
52 changes: 5 additions & 47 deletions cmd/tetragon/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
package main

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/cilium/tetragon/pkg/option"
Expand All @@ -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("-", "_")
Expand All @@ -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 {
Expand Down
40 changes: 40 additions & 0 deletions pkg/option/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/cilium/tetragon/pkg/logger"
"github.com/spf13/viper"
)

type config struct {
Expand Down Expand Up @@ -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
}

0 comments on commit 181cffd

Please sign in to comment.