-
Notifications
You must be signed in to change notification settings - Fork 321
/
config.go
194 lines (154 loc) · 3.87 KB
/
config.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
package option
import (
"bytes"
"fmt"
"os"
"path/filepath"
"time"
"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/metrics"
"github.com/spf13/viper"
)
type config struct {
Debug bool
ProcFS string
KernelVersion string
HubbleLib string
BTF string
Verbosity int
ForceSmallProgs bool
ForceLargeProgs bool
EnableCilium bool
EnableProcessNs bool
EnableProcessCred bool
EnableK8s bool
K8sKubeConfigPath string
DisableKprobeMulti bool
GopsAddr string
BpfDir string
LogOpts map[string]string
RBSize int
RBSizeTotal int
RBQueueSize int
ProcessCacheSize int
DataCacheSize int
MetricsServer string
MetricsLabelFilter metrics.LabelFilter
ServerAddress string
TracingPolicy string
TracingPolicyDir string
ExportFilename string
ExportFileMaxSizeMB int
ExportFileRotationInterval time.Duration
ExportFileMaxBackups int
ExportFileCompress bool
ExportRateLimit int
ExportFilePerm string
// Export aggregation options
EnableExportAggregation bool
ExportAggregationWindowSize time.Duration
ExportAggregationBufferSize uint64
CpuProfile string
MemProfile string
PprofAddr string
EventQueueSize uint
ReleasePinned bool
EnablePolicyFilter bool
EnablePolicyFilterDebug bool
EnablePidSetFilter bool
EnableMsgHandlingLatency bool
KMods []string
EnablePodInfo bool
EnableTracingPolicyCRD bool
ExposeStackAddresses bool
UsernameMetadata int
HealthServerAddress string
HealthServerInterval int
}
var (
log = logger.GetLogger()
// Config contains all the configuration used by Tetragon.
Config = config{
// Initialize global defaults below.
// ProcFS defaults to /proc.
ProcFS: "/proc",
// LogOpts contains logger parameters
LogOpts: make(map[string]string),
// Enable all metrics labels by default
MetricsLabelFilter: DefaultLabelFilter(),
}
)
// ReadDirConfig reads the given directory and returns a map that maps the
// filename to the contents of that file.
func ReadDirConfig(dirName string) (map[string]interface{}, error) {
m := map[string]interface{}{}
files, err := os.ReadDir(dirName)
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("unable to read configuration directory: %s", err)
}
for _, f := range files {
if f.IsDir() {
continue
}
fName := filepath.Join(dirName, f.Name())
// the file can still be a symlink to a directory
if f.Type()&os.ModeSymlink == 0 {
absFileName, err := filepath.EvalSymlinks(fName)
if err != nil {
log.WithError(err).Warnf("Unable to read configuration file %q", absFileName)
continue
}
fName = absFileName
}
fi, err := os.Stat(fName)
if err != nil {
log.WithError(err).Warnf("Unable to read configuration file %q", fName)
continue
}
if fi.Mode().IsDir() {
continue
}
b, err := os.ReadFile(fName)
if err != nil {
log.WithError(err).Warnf("Unable to read configuration file %q", fName)
continue
}
m[f.Name()] = string(bytes.TrimSpace(b))
}
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() {
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() {
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
}