/
config.go
198 lines (184 loc) · 6.21 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
/*******************************************************************************
*
* Copyright 2020 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*******************************************************************************/
package controllers
import (
"time"
"github.com/sapcc/maintenance-controller/constants"
"github.com/sapcc/maintenance-controller/plugin"
"github.com/sapcc/maintenance-controller/plugin/impl"
"github.com/sapcc/maintenance-controller/state"
"github.com/sapcc/ucfgwrap"
)
type ProfileDescriptor struct {
Name string `config:"name" validate:"required"`
Operational StateDescriptor
MaintenanceRequired StateDescriptor `config:"maintenance-required"`
InMaintenance StateDescriptor `config:"in-maintenance"`
}
type StateDescriptor struct {
Notify string
Transitions []TransitionDescriptor
}
type TransitionDescriptor struct {
Check string `config:"check" validate:"required"`
Next string `config:"next" validate:"required"`
Trigger string
}
// ConfigDescriptor describes the configuration structure to be parsed.
type ConfigDescriptor struct {
Intervals struct {
Requeue time.Duration `config:"requeue" validate:"required"`
} `config:"intervals" validate:"required"`
Instances plugin.InstancesDescriptor
Profiles []ProfileDescriptor
Dashboard struct {
LabelFilter []string `config:"labelFilter"`
} `config:"dashboard"`
}
// Config represents the controllers global configuration.
type Config struct {
// RequeueInterval defines a duration after the a node is reconceiled again by the controller
RequeueInterval time.Duration
// Profiles contains all known profiles
Profiles map[string]state.Profile
// Contains reference to all plugins and their instances
Registry plugin.Registry
// Keys of labels to show on the dashboard
DashboardLabelFilter []string
}
// LoadConfig (re-)initializes the config with values provided by the given ucfg.Config.
func LoadConfig(config *ucfgwrap.Config) (*Config, error) {
var global ConfigDescriptor
err := config.Unpack(&global)
if err != nil {
return nil, err
}
registry := plugin.NewRegistry()
addPluginsToRegistry(®istry)
err = registry.LoadInstances(config, &global.Instances)
if err != nil {
return nil, err
}
profileMap, err := loadProfiles(global.Profiles, ®istry)
if err != nil {
return nil, err
}
dashboardLabelFilter := make([]string, 0)
if global.Dashboard.LabelFilter != nil {
dashboardLabelFilter = global.Dashboard.LabelFilter
}
return &Config{
RequeueInterval: global.Intervals.Requeue,
Profiles: profileMap,
Registry: registry,
DashboardLabelFilter: dashboardLabelFilter,
}, nil
}
func loadProfiles(profiles []ProfileDescriptor, registry *plugin.Registry) (map[string]state.Profile, error) {
profileMap := make(map[string]state.Profile)
// add an empty default profile
profileMap[constants.DefaultProfileName] = state.Profile{
Name: constants.DefaultProfileName,
Chains: map[state.NodeStateLabel]state.PluginChains{
state.Operational: {},
state.InMaintenance: {},
state.Required: {},
},
}
for _, profile := range profiles {
operationalChain, err := loadPluginChains(profile.Operational, registry)
if err != nil {
return nil, err
}
requiredChain, err := loadPluginChains(profile.MaintenanceRequired, registry)
if err != nil {
return nil, err
}
maintenanceChain, err := loadPluginChains(profile.InMaintenance, registry)
if err != nil {
return nil, err
}
profileMap[profile.Name] = state.Profile{
Name: profile.Name,
Chains: map[state.NodeStateLabel]state.PluginChains{
state.Operational: operationalChain,
state.Required: requiredChain,
state.InMaintenance: maintenanceChain,
},
}
}
return profileMap, nil
}
func loadPluginChains(config StateDescriptor, registry *plugin.Registry) (state.PluginChains, error) {
var chains state.PluginChains
notificationChain, err := registry.NewNotificationChain(config.Notify)
if err != nil {
return chains, err
}
chains.Notification = notificationChain
chains.Transitions = make([]state.Transition, 0)
for _, transitionConfig := range config.Transitions {
var transition state.Transition
checkChain, err := registry.NewCheckChain(transitionConfig.Check)
if err != nil {
return chains, err
}
transition.Check = checkChain
triggerChain, err := registry.NewTriggerChain(transitionConfig.Trigger)
if err != nil {
return chains, err
}
transition.Trigger = triggerChain
label, err := state.ValidateLabel(transitionConfig.Next)
if err != nil {
return chains, err
}
transition.Next = label
chains.Transitions = append(chains.Transitions, transition)
}
return chains, nil
}
// addPluginsToRegistry adds known plugins to the registry.
func addPluginsToRegistry(registry *plugin.Registry) {
addChecker := func(checker plugin.Checker) {
registry.CheckPlugins[checker.ID()] = checker
}
addChecker(&impl.Affinity{})
addChecker(&impl.AnyLabel{})
addChecker(&impl.ClusterSemver{})
addChecker(&impl.Condition{})
addChecker(&impl.HasAnnotation{})
addChecker(&impl.HasLabel{})
addChecker(&impl.KubernikusCount{})
addChecker(&impl.MaxMaintenance{})
addChecker(&impl.NodeCount{})
addChecker(&impl.PrometheusInstant{})
addChecker(&impl.Stagger{})
addChecker(&impl.TimeWindow{})
addChecker(&impl.Wait{})
addChecker(&impl.WaitExclude{})
addNotifier := func(notifier plugin.Notifier) {
registry.NotificationPlugins[notifier.ID()] = notifier
}
addNotifier(&impl.Mail{})
addNotifier(&impl.SlackWebhook{})
addNotifier(&impl.SlackThread{})
registry.TriggerPlugins["alterAnnotation"] = &impl.AlterAnnotation{}
registry.TriggerPlugins["alterLabel"] = &impl.AlterLabel{}
}