-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
config.go
121 lines (101 loc) · 2.47 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
package config
import (
"io/ioutil"
"gopkg.in/yaml.v2"
)
const (
defaultLintLevel = "ok"
// AllNamespaces represents all namespaces.
AllNamespaces string = ""
)
// Config tracks Popeye configuration options.
type Config struct {
Popeye `yaml:"popeye"`
Flags *Flags
LintLevel int
}
// NewConfig create a new Popeye configuration.
func NewConfig(flags *Flags) (*Config, error) {
cfg := Config{Popeye: NewPopeye()}
if isSet(flags.Spinach) {
f, err := ioutil.ReadFile(*flags.Spinach)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(f, &cfg); err != nil {
return nil, err
}
}
cfg.Flags = flags
if flags.AllNamespaces != nil && *flags.AllNamespaces {
all := AllNamespaces
flags.Namespace = &all
}
cfg.LintLevel = int(ToIssueLevel(flags.LintLevel))
return &cfg, nil
}
// LinterLevel returns the current lint level.
func (c *Config) LinterLevel() int {
return c.LintLevel
}
// Sections returns a collection of sanitizers categories.
func (c *Config) Sections() []string {
if c.Flags.Sections != nil {
return *c.Flags.Sections
}
return []string{}
}
// CPUResourceLimits returns memory over/under allocation thresholds.
func (c *Config) CPUResourceLimits() Allocations {
return c.CPU
}
// MEMResourceLimits returns memory over/under allocation thresholds.
func (c *Config) MEMResourceLimits() Allocations {
return c.MEM
}
// NodeCPULimit returns the node cpu threshold if set otherwise the default.
func (c *Config) NodeCPULimit() float64 {
l := c.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.Pod.Limits.CPU
if l == 0 {
return defaultCPULimit
}
return l
}
// RestartsLimit returns pod restarts limit.
func (c *Config) RestartsLimit() int {
l := c.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.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.Node.Limits.Memory
if l == 0 {
return defaultMEMLimit
}
return l
}
// ----------------------------------------------------------------------------
// Helpers...
// IsSet checks if a string flag is set.
func isSet(s *string) bool {
return s != nil && *s != ""
}