Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move readConfig{Dir,File} to the option package #1336

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}