-
Notifications
You must be signed in to change notification settings - Fork 328
/
conf.go
101 lines (84 loc) · 2.32 KB
/
conf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/cilium/tetragon/pkg/option"
"github.com/spf13/viper"
)
var (
adminTgConfDir = "/etc/tetragon/"
adminTgConfDropIn = "/etc/tetragon/tetragon.conf.d/"
packageTgConfDropIns = []string{
"/usr/lib/tetragon/tetragon.conf.d/",
"/usr/local/lib/tetragon/tetragon.conf.d/",
}
)
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("-", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()
// First set default conf file and format
viper.SetConfigName("tetragon")
viper.SetConfigType("yaml")
// Read default drop-ins directories
for _, dir := range dropInsDir {
readConfigDir(dir)
}
// Look into cwd first, this is needed for quick development only
readConfigFile(".", "tetragon.yaml")
// Look for /etc/tetragon/tetragon.yaml
readConfigFile(defaultConfDir, "tetragon.yaml")
// Look into default /etc/tetragon/tetragon.conf.d/ now
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)
if err != nil {
log.WithField(keyConfigDir, configDir).WithError(err).Fatal("Failed to read config from directory")
} else {
log.WithField(keyConfigDir, configDir).Info("Loaded config from directory")
}
}
}
}