-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
185 lines (160 loc) · 4.22 KB
/
Copy pathconfig.go
File metadata and controls
185 lines (160 loc) · 4.22 KB
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
package config
import (
_ "embed"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
kjson "github.com/knadh/koanf/parsers/json"
kyaml "github.com/knadh/koanf/parsers/yaml"
kenv "github.com/knadh/koanf/providers/env/v2"
kfile "github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/rawbytes"
"github.com/knadh/koanf/v2"
pluginv1 "github.com/slyngdk/node-drain/api/plugins/proto/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
//go:embed default-config.yaml
var defaultConfigYaml []byte
var k = koanf.New(".")
var _config *Config
type Logger struct {
Level string `koanf:"level"`
}
type Config struct {
Log struct {
Level string `koanf:"level"`
Format string `koanf:"format"`
Loggers map[string]Logger `koanf:"loggers"`
} `koanf:"log"`
Reboot struct {
CheckInterval time.Duration `koanf:"checkInterval"`
}
Pod struct {
PriorityClassName string `koanf:"priorityClassName"`
ContainerResources ContainerResources `koanf:"containerResources"`
} `koanf:"pod"`
ContainerNode bool `koanf:"containerNode"`
}
type ContainerResources struct {
Requests ResourceList `koanf:"requests"`
Limits ResourceList `koanf:"limits"`
}
func (c *ContainerResources) ToResourceRequirement() corev1.ResourceRequirements {
requirements := corev1.ResourceRequirements{
Requests: c.Requests.toKubernetes(),
Limits: c.Limits.toKubernetes(),
}
return requirements
}
type ResourceList struct {
Memory string `koanf:"memory"`
CPU string `koanf:"cpu"`
}
func (c *ResourceList) toKubernetes() corev1.ResourceList {
if c.CPU != "" && c.Memory != "" {
list := corev1.ResourceList{}
if c.CPU != "" {
list[corev1.ResourceCPU] = resource.MustParse(c.CPU)
}
if c.Memory != "" {
list[corev1.ResourceMemory] = resource.MustParse(c.Memory)
}
return list
}
return nil
}
func (c *Config) GetLogger(name string) Logger {
if logger, ok := c.Log.Loggers[name]; ok {
if logger.Level == "" {
logger.Level = c.Log.Level
}
return logger
}
return Logger{
Level: c.Log.Level,
}
}
func LoadDefaultConfig() {
err := k.Load(rawbytes.Provider(defaultConfigYaml), kyaml.Parser())
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to load default config: %v\n", err)
os.Exit(1)
}
}
func LoadConfig() (*Config, error) {
err := k.Load(kenv.Provider(".", kenv.Opt{
Prefix: "NODEDRAIN_",
TransformFunc: func(k, v string) (string, any) {
k = strings.ReplaceAll(strings.ToLower(strings.TrimPrefix(k, "NODEDRAIN_")), "_", ".")
if strings.Contains(v, " ") {
return k, strings.Split(v, " ")
}
return k, v
},
}), nil)
if err != nil {
return nil, err
}
loadConfigFiles := func() error {
configDir := "/config"
stat, err := os.Stat(configDir)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("failed to get stat for /config: %w", err)
}
if !stat.IsDir() {
return fmt.Errorf("%s is not a directory", stat.Name())
}
files, err := os.ReadDir(configDir)
if err != nil {
return fmt.Errorf("failed to read config directory: %w", err)
}
sort.Slice(files, func(i, j int) bool {
return files[i].Name() < files[j].Name()
})
for _, f := range files {
ext := filepath.Ext(f.Name())
switch ext {
case ".yaml", ".yml":
err := k.Load(kfile.Provider(filepath.Join(configDir, f.Name())), kyaml.Parser())
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to load config from file %s: %v\n", f.Name(), err)
}
}
}
return nil
}
if err = loadConfigFiles(); err != nil {
return nil, err
}
var conf Config
err = k.Unmarshal("", &conf)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
}
_config = &conf
return &conf, nil
}
func GetKoanf() *koanf.Koanf {
return k
}
func GetConfig() *Config {
return _config
}
func GetPluginConfig(format pluginv1.ConfigFormat, pluginId string) ([]byte, error) {
pluginConfig := GetKoanf().Cut(fmt.Sprintf("plugins.%s", pluginId))
switch format {
case pluginv1.ConfigFormat_CONFIG_FORMAT_JSON:
return pluginConfig.Marshal(kjson.Parser())
case pluginv1.ConfigFormat_CONFIG_FORMAT_YAML:
return pluginConfig.Marshal(kyaml.Parser())
default:
return nil, fmt.Errorf("unknown format: %s", format)
}
}