This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
/
initialize.go
366 lines (324 loc) · 9.73 KB
/
initialize.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package kubernetes
import (
"fmt"
"sort"
"strings"
macherrs "k8s.io/apimachinery/pkg/api/errors"
"github.com/caos/orbos/internal/operator/common"
"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
"github.com/caos/orbos/mntr"
core "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
)
type initializedPool struct {
upscaling int
downscaling []*initializedMachine
infra infra.Pool
tier Tier
desired Pool
machines func() ([]*initializedMachine, error)
}
func (i *initializedMachines) forEach(baseMonitor mntr.Monitor, do func(machine *initializedMachine, machineMonitor mntr.Monitor) (goon bool)) {
if i == nil {
return
}
for _, machine := range *i {
if !do(machine, baseMonitor.WithField("machine", machine.infra.ID())) {
break
}
}
}
type initializeFunc func(initializedPool, []*initializedMachine) error
type uninitializeMachineFunc func(id string)
type initializeMachineFunc func(machine infra.Machine, pool *initializedPool) *initializedMachine
func (i *initializedPool) enhance(initialize initializeFunc) {
original := i.machines
i.machines = func() ([]*initializedMachine, error) {
machines, err := original()
if err != nil {
return nil, err
}
if err := initialize(*i, machines); err != nil {
return nil, err
}
return machines, nil
}
}
type initializedMachine struct {
infra infra.Machine
reconcile func() error
currentNodeagent *common.NodeAgentCurrent
desiredNodeagent *common.NodeAgentSpec
currentMachine *Machine
pool *initializedPool
node *v1.Node
}
func initialize(
monitor mntr.Monitor,
curr *CurrentCluster,
desired DesiredV0,
nodeAgentsCurrent *common.CurrentNodeAgents,
nodeAgentsDesired *common.DesiredNodeAgents,
providerPools map[string]map[string]infra.Pool,
k8s *Client,
postInit func(machine *initializedMachine)) (
controlplane *initializedPool,
controlplaneMachines []*initializedMachine,
workers []*initializedPool,
workerMachines []*initializedMachine,
initializeMachine initializeMachineFunc,
uninitializeMachine uninitializeMachineFunc,
err error) {
curr.Status = "running"
initializePool := func(infraPool infra.Pool, desired Pool, tier Tier) (*initializedPool, error) {
pool := &initializedPool{
infra: infraPool,
tier: tier,
desired: desired,
}
pool.machines = func() ([]*initializedMachine, error) {
infraMachines, err := infraPool.GetMachines()
if err != nil {
return nil, err
}
machines := make([]*initializedMachine, len(infraMachines))
for i, infraMachine := range infraMachines {
machines[i] = initializeMachine(infraMachine, pool)
if machines[i].currentMachine.Updating || machines[i].currentMachine.Rebooting {
curr.Status = "maintaining"
}
}
sort.Sort(initializedMachines(machines))
return machines, nil
}
machines, err := pool.machines()
if err != nil {
return pool, err
}
var replace initializedMachines
var backReplacement int
for _, machine := range machines {
if req, _, _ := machine.infra.ReplacementRequired(); req {
replace = append(replace, machine)
continue
}
if machine.currentMachine.Joined {
backReplacement++
}
}
upscale := desired.Nodes + len(replace) - len(machines)
if upscale > 0 {
pool.upscaling = upscale
return pool, nil
}
if len(replace) > 0 {
for backReplacement >= desired.Nodes && len(replace) > 0 {
backReplacement--
pool.downscaling = append(pool.downscaling, replace[0])
replace = replace[1:]
}
return pool, nil
}
pool.downscaling = machines[desired.Nodes:]
return pool, nil
}
initializeMachine = func(machine infra.Machine, pool *initializedPool) *initializedMachine {
current := &Machine{
Metadata: MachineMetadata{
Tier: pool.tier,
Provider: pool.desired.Provider,
Pool: pool.desired.Pool,
},
}
var node *v1.Node
if k8s.Available() {
var k8sNodeErr error
node, k8sNodeErr = k8s.GetNode(machine.ID())
if k8sNodeErr != nil {
if macherrs.IsNotFound(k8sNodeErr) {
node = nil
} else {
current.Unknown = true
}
}
}
naSpec, _ := nodeAgentsDesired.Get(machine.ID())
naCurr, _ := nodeAgentsCurrent.Get(machine.ID())
// Retry if kubeapi returns other error than "NotFound"
reconcile := func() error { return nil }
if node != nil && !current.Unknown {
reconcile = reconcileNodeFunc(*node, monitor, pool.desired, k8s, pool.tier, naSpec, naCurr)
current.Joined = true
for _, cond := range node.Status.Conditions {
if cond.Type == v1.NodeReady {
current.Ready = true
current.Updating = k8s.Tainted(node, updating)
current.Rebooting = k8s.Tainted(node, rebooting)
}
}
}
curr.Machines.Set(machine.ID(), current)
machineMonitor := monitor.WithField("machine", machine.ID())
naSpec.ChangesAllowed = !pool.desired.UpdatesDisabled
k8sSoftware := ParseString(desired.Spec.Versions.Kubernetes).DefineSoftware()
if !softwareDefines(*naSpec.Software, k8sSoftware) {
k8sSoftware.Merge(KubernetesSoftware(naCurr.Software))
if !softwareContains(*naSpec.Software, k8sSoftware) {
naSpec.Software.Merge(k8sSoftware)
machineMonitor.Changed("Kubernetes software desired")
}
}
initMachine := &initializedMachine{
infra: machine,
currentNodeagent: naCurr,
desiredNodeagent: naSpec,
reconcile: reconcile,
currentMachine: current,
pool: pool,
node: node,
}
postInit(initMachine)
return initMachine
}
for providerName, provider := range providerPools {
pools:
for poolName, pool := range provider {
if desired.Spec.ControlPlane.Provider == providerName && desired.Spec.ControlPlane.Pool == poolName {
controlplane, err = initializePool(pool, desired.Spec.ControlPlane, Controlplane)
if err != nil {
return controlplane,
controlplaneMachines,
workers,
workerMachines,
initializeMachine,
uninitializeMachine,
err
}
controlplaneMachines, err = controlplane.machines()
if err != nil {
return controlplane,
controlplaneMachines,
workers,
workerMachines,
initializeMachine,
uninitializeMachine,
err
}
continue
}
for _, desiredPool := range desired.Spec.Workers {
if providerName == desiredPool.Provider && poolName == desiredPool.Pool {
workerPool, err := initializePool(pool, *desiredPool, Workers)
if err != nil {
return controlplane,
controlplaneMachines,
workers,
workerMachines,
initializeMachine,
uninitializeMachine,
err
}
workers = append(workers, workerPool)
initializedWorkerMachines, err := workerPool.machines()
if err != nil {
return controlplane,
controlplaneMachines,
workers,
workerMachines,
initializeMachine,
uninitializeMachine,
err
}
workerMachines = append(workerMachines, initializedWorkerMachines...)
continue pools
}
}
}
}
for _, machine := range append(controlplaneMachines, workerMachines...) {
if !machine.currentMachine.Ready {
curr.Status = "degraded"
}
if machine.currentMachine.Updating || !machine.currentMachine.Joined || !machine.currentMachine.FirewallIsReady {
curr.Status = "maintaining"
break
}
}
return controlplane,
controlplaneMachines,
workers,
workerMachines,
initializeMachine, func(id string) {
nodeAgentsDesired.Delete(id)
curr.Machines.Delete(id)
}, nil
}
func reconcileNodeFunc(node v1.Node, monitor mntr.Monitor, pool Pool, k8s *Client, tier Tier, naSpec *common.NodeAgentSpec, naCurr *common.NodeAgentCurrent) func() error {
n := &node
reconcileNode := false
reconcileMonitor := monitor.WithField("node", n.Name)
handleMaybe := func(maybeMonitorFields map[string]interface{}) {
if maybeMonitorFields != nil {
reconcileNode = true
reconcileMonitor = monitor.WithFields(maybeMonitorFields)
}
}
handleMaybe(reconcileLabel(n, "orbos.ch/pool", pool.Pool))
handleMaybe(reconcileLabel(n, "orbos.ch/tier", string(tier)))
handleMaybe(reconcileTaints(n, pool, k8s, naSpec, naCurr))
if !reconcileNode {
return func() error { return nil }
}
return func() error {
reconcileMonitor.Info("Reconciling node")
return k8s.updateNode(n)
}
}
func reconcileTaints(node *v1.Node, pool Pool, k8s *Client, naSpec *common.NodeAgentSpec, naCurr *common.NodeAgentCurrent) map[string]interface{} {
desiredTaints := pool.Taints.ToK8sTaints()
newTaints := append([]core.Taint{}, desiredTaints...)
updateTaints := false
// user defined taints
outer:
for _, existing := range node.Spec.Taints {
if strings.HasPrefix(existing.Key, "node.kubernetes.io/") || strings.HasPrefix(existing.Key, taintKeyPrefix) {
newTaints = append(newTaints, existing)
continue
}
for _, des := range desiredTaints {
if existing.Key == des.Key &&
existing.Effect == des.Effect &&
existing.Value == des.Value {
continue outer
}
}
updateTaints = true
break
}
// internal taints
if k8s.Tainted(node, updating) && node.Labels["orbos.ch/updating"] == node.Status.NodeInfo.KubeletVersion {
newTaints = k8s.RemoveFromTaints(newTaints, updating)
updateTaints = true
}
if k8s.Tainted(node, rebooting) && naCurr.Booted.After(naSpec.RebootRequired) {
newTaints = k8s.RemoveFromTaints(newTaints, rebooting)
updateTaints = true
}
if !updateTaints && len(node.Spec.Taints) == len(newTaints) {
return nil
}
node.Spec.Taints = newTaints
return map[string]interface{}{"taints": desiredTaints}
}
func reconcileLabel(node *v1.Node, key, value string) map[string]interface{} {
if node.Labels[key] == value {
return nil
}
if node.Labels == nil {
node.Labels = make(map[string]string)
}
node.Labels[key] = value
return map[string]interface{}{
fmt.Sprintf("label.%s", key): value,
}
}