-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
81 lines (74 loc) · 2.2 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
package main
import (
"os"
"github.com/spf13/viper"
"k8s.io/apimachinery/pkg/runtime/schema"
klog "k8s.io/klog/v2"
)
// config outlines which namespace to watch objects in and which objects to watch
type config struct {
Namespace string `yaml:"namespace,omitempty"`
Objects []schema.GroupVersionResource `yaml:"objects,omitempty"`
Policies []string `yaml:"policies,omitempty"`
IgnoreChildren bool `yaml:"ignoreChildren,omitempty"`
IgnoreKinds []string `yaml:"ignoreKinds,omitempty"`
IgnoreDifferingPaths []string `yaml:"ignoreDifferingPaths,omitempty"`
RegoQuery string `yaml:"regoQuery,omitempty"`
}
// getConfig returns a default config object
func getConfig() *config {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.SetConfigName("config.yaml")
if *configPath != "" {
viper.SetConfigFile(*configPath)
}
if err := viper.ReadInConfig(); err != nil {
klog.ErrorS(err, "unable to read config")
os.Exit(1)
}
conf := &config{}
if err := viper.Unmarshal(conf); err != nil {
klog.ErrorS(err, "invalid config")
os.Exit(1)
}
// Set our defaults or warn for empty values
if conf.RegoQuery == "" {
conf.RegoQuery = "data[_].main"
}
if len(conf.Policies) == 0 {
klog.Warning("no policies set, all evaluations will be futile")
}
if len(conf.Objects) == 0 {
klog.Info("no objects set, watching all discoverable objects")
}
if len(conf.IgnoreKinds) == 0 {
conf.IgnoreKinds = []string{
"apiservice",
"endpoint",
"endpoints",
"endpointslice",
"event",
"flowschema",
"lease",
"limitrange",
"namespace",
"prioritylevelconfiguration",
"replicationcontroller",
"runtimeclass",
}
}
if len(conf.IgnoreDifferingPaths) == 0 {
conf.IgnoreDifferingPaths = []string{
"Object/metadata/resourceVersion",
"Object/metadata/managedFields/0/time",
"Object/status/observedGeneration",
}
} else {
for i := range conf.IgnoreDifferingPaths {
conf.IgnoreDifferingPaths[i] = "Object/" + conf.IgnoreDifferingPaths[i]
}
}
return conf
}