-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
175 lines (148 loc) · 3.96 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
package main
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
// Dimension is a single CloudWatch metric dimension.
type Dimension struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
// Metric configures the CloudWatch metric.
type Metric struct {
Namespace string `yaml:"namespace"`
Name string `yaml:"name"`
Dimensions []Dimension `yaml:"dimensions"`
}
const (
KindDaemonSet = "DaemonSet"
KindDeployment = "Deployment"
KindStatefulSet = "StatefulSet"
)
const (
ModeAllOfThem = "AllOfThem"
ModeAtLeastOne = "AtLeastOne"
)
// Target is a single Kubernetes target to scan.
type Target struct {
// Type of target. Supported: "Deployment", "StatefulSet", and "DaemonSet".
Kind string `yaml:"kind"`
// Namespace of the target resource.
Namespace string `yaml:"namespace"`
// Name of the target resource.
Name string `yaml:"name"`
// Mode used for scanning target. "AllOfThem" requires all replicas to
// be ready. "AtLeastOne" only requires at least one replica to be ready.
Mode string `yaml:"mode"`
}
// Config is the central configuration. Use NewConfig to create a new config.
type Config struct {
Seconds int `yaml:"seconds"`
Dry bool `yaml:"dry"`
Metric Metric `yaml:"metric"`
Targets []Target `yaml:"targets"`
Logging struct {
Level string `yaml:"level"`
Pretty bool `yaml:"pretty"`
} `yaml:"logging"`
}
// NewConfig reads configuration from provided file and performs checks.
func NewConfig(configPath string) (Config, error) {
c := Config{}
configFile, err := os.ReadFile(configPath)
if err != nil {
return c, fmt.Errorf("read config: %w", err)
}
err = yaml.Unmarshal(configFile, &c)
if err != nil {
return c, fmt.Errorf("unmarshal config: %w", err)
}
// Config.Seconds
if c.Seconds < 5 {
return c, fmt.Errorf("config seconds smaller than 5: %v", c.Seconds)
}
// Config.Metric
if err = ValidateMetric(c.Metric); err != nil {
return c, fmt.Errorf("failed validating metric config: %w", err)
}
// Config.Targets
if err = ValidateTargets(c.Targets); err != nil {
return c, fmt.Errorf("failed validating targets config: %w", err)
}
// Config.Logging.Level
if c.Logging.Level == "" {
c.Logging.Level = "debug"
}
if !ContainsString([]string{"info", "debug"}, c.Logging.Level) {
return c, fmt.Errorf(
"logging.level not supported: %s", c.Logging.Level,
)
}
return c, nil
}
// ValidateMetric validates metric configuration.
func ValidateMetric(metric Metric) error {
if metric.Namespace == "" {
return fmt.Errorf("missing: metric.namespace")
}
if metric.Name == "" {
return fmt.Errorf("missing: metric.name")
}
for i, dimension := range metric.Dimensions {
if dimension.Name == "" {
return fmt.Errorf(
"missing: metric.dimensions[%v].name", i,
)
}
if dimension.Value == "" {
return fmt.Errorf(
"missing: metric.dimensions[%v].value", i,
)
}
}
return nil
}
// ValidateTargets validates targets configuration.
func ValidateTargets(targets []Target) error {
if len(targets) == 0 {
return fmt.Errorf("missing: targets")
}
for i, target := range targets {
if target.Kind == "" {
return fmt.Errorf("missing: target[%v].kind", i)
}
allowedTargetKinds := []string{
KindDeployment, KindStatefulSet, KindDaemonSet,
}
if !ContainsString(allowedTargetKinds, target.Kind) {
return fmt.Errorf(
"target[%v].kind not supported: %s", i, target.Kind,
)
}
if target.Namespace == "" {
return fmt.Errorf("missing: target[%v].namespace", i)
}
if target.Name == "" {
return fmt.Errorf("missing: target[%v].name", i)
}
if target.Mode == "" {
return fmt.Errorf("missing: target[%v].mode", i)
}
allowedTargetModes := []string{ModeAllOfThem, ModeAtLeastOne}
if !ContainsString(allowedTargetModes, target.Mode) {
return fmt.Errorf(
"target[%v].mode not supported: %s", i, target.Mode,
)
}
}
return nil
}
func ContainsString(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}