-
-
Notifications
You must be signed in to change notification settings - Fork 276
/
config.go
267 lines (226 loc) Β· 5.32 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package config
import (
"io/ioutil"
"github.com/rs/zerolog"
"gopkg.in/yaml.v2"
"k8s.io/cli-runtime/pkg/genericclioptions"
restclient "k8s.io/client-go/rest"
clientcmd "k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
const (
defaultWidth = 80
defaultLogLevel = "debug"
defaultLintLevel = "ok"
)
// Config tracks Popeye configuration options.
type Config struct {
Popeye Popeye `yaml:"popeye"`
Spinach string
ClearScreen bool
LogLevel string
LintLevel string
Sections []string
flags *genericclioptions.ConfigFlags
clientConfig clientcmd.ClientConfig
rawConfig *clientcmdapi.Config
restConfig *restclient.Config
}
// New create a new Popeye configuration.
func New() *Config {
return &Config{
Popeye: NewPopeye(),
LogLevel: defaultLogLevel,
LintLevel: defaultLintLevel,
}
}
// Init a popeye configuration from file or default if no file given.
func (c *Config) Init(f *genericclioptions.ConfigFlags) error {
c.flags = f
var cfg Config
if len(c.Spinach) != 0 {
f, err := ioutil.ReadFile(c.Spinach)
if err != nil {
return err
}
if err := yaml.Unmarshal(f, &cfg); err != nil {
return err
}
}
cfg.Popeye.LogLevel = ToLogLevel(c.LogLevel)
cfg.Popeye.LintLevel = ToLintLevel(c.LintLevel)
cfg.Sections = c.Sections
cfg.ClearScreen = c.ClearScreen
cfg.flags = f
*c = cfg
return nil
}
// NodeCPULimit returns the node cpu threshold if set otherwise the default.
func (c *Config) NodeCPULimit() float64 {
l := c.Popeye.Node.Limits.CPU
if l == 0 {
return defaultCPULimit
}
return l
}
// PodCPULimit returns the pod cpu threshold if set otherwise the default.
func (c *Config) PodCPULimit() float64 {
l := c.Popeye.Pod.Limits.CPU
if l == 0 {
return defaultCPULimit
}
return l
}
// ExcludedNode returns excluded nodes if any.
func (c *Config) ExcludedNode(n string) bool {
return c.Popeye.Node.excluded(n)
}
// ExcludedService returns excluded services if any.
func (c *Config) ExcludedService(s string) bool {
return c.Popeye.Service.excluded(s)
}
// ExcludedNS checks if a namespace should be excluded from the scan.
func (c *Config) ExcludedNS(n string) bool {
return c.Popeye.Namespace.excluded(n)
}
// RestartsLimit returns pod restarts limit.
func (c *Config) RestartsLimit() int {
l := c.Popeye.Pod.Restarts
if l == 0 {
return defaultRestarts
}
return l
}
// PodMEMLimit returns the pod mem threshold if set otherwise the default.
func (c *Config) PodMEMLimit() float64 {
l := c.Popeye.Pod.Limits.Memory
if l == 0 {
return defaultMEMLimit
}
return l
}
// NodeMEMLimit returns the pod mem threshold if set otherwise the default.
func (c *Config) NodeMEMLimit() float64 {
l := c.Popeye.Node.Limits.Memory
if l == 0 {
return defaultMEMLimit
}
return l
}
// ActiveNamespace returns the desired namespace if set or all if not.
func (c *Config) ActiveNamespace() string {
cfg, err := c.RawConfig()
if err != nil {
return "n/a"
}
if isSet(c.flags.Namespace) {
return *c.flags.Namespace
}
ctx := cfg.CurrentContext
if isSet(c.flags.Context) {
ctx = *c.flags.Context
}
if c, ok := cfg.Contexts[ctx]; ok {
return c.Namespace
}
return ""
}
// ActiveCluster get the current cluster name.
func (c *Config) ActiveCluster() string {
cfg, err := c.RawConfig()
if err != nil {
return "n/a"
}
if isSet(c.flags.ClusterName) {
return *c.flags.ClusterName
}
ctx := cfg.CurrentContext
if isSet(c.flags.Context) {
ctx = *c.flags.Context
}
if ctx, ok := cfg.Contexts[ctx]; ok {
return ctx.Cluster
}
return "n/a"
}
// RawConfig fetch the current kubeconfig with no overrides.
func (c *Config) RawConfig() (clientcmdapi.Config, error) {
if c.rawConfig != nil {
return *c.rawConfig, nil
}
err := c.ensureClientConfig()
if err != nil {
return clientcmdapi.Config{}, err
}
raw, err := c.clientConfig.RawConfig()
if err != nil {
return clientcmdapi.Config{}, err
}
c.rawConfig = &raw
return *c.rawConfig, nil
}
// RESTConfig fetch the current REST api-server connection.
func (c *Config) RESTConfig() (*restclient.Config, error) {
if c.restConfig != nil {
return c.restConfig, nil
}
err := c.ensureClientConfig()
if err != nil {
return nil, err
}
if c.restConfig, err = c.flags.ToRESTConfig(); err != nil {
return nil, err
}
return c.restConfig, nil
}
func (c *Config) ensureClientConfig() error {
if c.clientConfig == nil {
c.clientConfig = c.flags.ToRawKubeConfigLoader()
}
return nil
}
// ToLogLevel convert a string to a level.
func ToLogLevel(level string) zerolog.Level {
switch level {
case "debug":
return zerolog.DebugLevel
case "warn":
return zerolog.WarnLevel
case "error":
return zerolog.ErrorLevel
case "fatal":
return zerolog.FatalLevel
default:
return zerolog.InfoLevel
}
}
// Level tracks lint check level.
type Level int
const (
// OkLevel denotes no linting issues.
OkLevel Level = iota
// InfoLevel denotes FIY linting issues.
InfoLevel
// WarnLevel denotes a warning issue.
WarnLevel
// ErrorLevel denotes a serious issue.
ErrorLevel
)
// ToLintLevel convert a string to a level.
func ToLintLevel(level string) Level {
switch level {
case "ok":
return OkLevel
case "info":
return InfoLevel
case "warn":
return WarnLevel
case "error":
return ErrorLevel
default:
return OkLevel
}
}
func isSet(s *string) bool {
return s != nil && *s != ""
}