This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
upscale.go
238 lines (199 loc) · 5.84 KB
/
upscale.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
package kubernetes
import (
"fmt"
"sync"
"github.com/caos/orbos/internal/secret"
"github.com/caos/orbos/internal/api"
"github.com/pkg/errors"
"github.com/caos/orbos/internal/helpers"
"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
"github.com/caos/orbos/mntr"
)
func ensureUpScale(
monitor mntr.Monitor,
clusterID string,
desired *DesiredV0,
psf api.PushDesiredFunc,
controlplanePool *initializedPool,
workerPools []*initializedPool,
kubeAPI *infra.Address,
k8sVersion KubernetesVersion,
k8sClient *Client,
oneoff bool,
initializeMachine func(infra.Machine, *initializedPool) initializedMachine) (changed bool, err error) {
wCount := 0
for _, w := range workerPools {
wCount += w.desired.Nodes
}
monitor.WithFields(map[string]interface{}{
"control_plane_nodes": controlplanePool.desired.Nodes,
"worker_nodes": wCount,
}).Debug("Ensuring scale")
var machines []*initializedMachine
upscalingDone := true
var wg sync.WaitGroup
alignPool := func(pool *initializedPool, ensured func(int)) {
defer wg.Done()
if pool.upscaling > 0 {
upscalingDone = false
machines, alignErr := newMachines(pool.infra, pool.upscaling)
if alignErr != nil {
err = helpers.Concat(err, alignErr)
return
}
for _, machine := range machines {
initializeMachine(machine, pool)
}
}
if err != nil {
return
}
poolMachines, listErr := pool.machines()
if listErr != nil {
err = helpers.Concat(err, listErr)
return
}
machines = append(machines, poolMachines...)
if ensured != nil {
ensured(len(poolMachines))
}
}
var ensuredControlplane int
wg.Add(1)
go alignPool(controlplanePool, func(ensured int) {
ensuredControlplane = ensured
})
var ensuredWorkers int
for _, workerPool := range workerPools {
wg.Add(1)
go alignPool(workerPool, func(ensured int) {
ensuredWorkers += ensured
})
}
wg.Wait()
if err != nil {
return false, err
}
if !upscalingDone {
monitor.Info("Upscaled machines are not ready yet")
return false, nil
}
var joinCP *initializedMachine
var certsCP infra.Machine
var joinWorkers []*initializedMachine
nodes:
for _, machine := range machines {
machineMonitor := monitor.WithFields(map[string]interface{}{
"machine": machine.infra.ID(),
"tier": machine.pool.tier,
})
if machine.currentMachine.Unknown {
machineMonitor.Info("Waiting for kubernetes node to leave unknown state before proceeding")
return false, nil
}
isJoinedControlPlane := machine.pool.tier == Controlplane && machine.currentMachine.Joined
if isJoinedControlPlane && !machine.currentMachine.Updating && !machine.currentMachine.Rebooting {
certsCP = machine.infra
continue nodes
}
if isJoinedControlPlane && machine.node != nil && machine.node.Spec.Unschedulable {
machineMonitor.Info("Awaiting controlplane to become ready")
return false, nil
}
if machine.node != nil && !machine.node.Spec.Unschedulable {
continue nodes
}
if machine.currentMachine.Joined {
machineMonitor.Info("Node is already joining")
continue nodes
}
if machine.pool.tier == Controlplane && joinCP == nil {
joinCP = machine
continue nodes
}
joinWorkers = append(joinWorkers, machine)
}
if joinCP == nil && len(joinWorkers) == 0 {
monitor.WithFields(map[string]interface{}{
"controlplane": ensuredControlplane,
"workers": ensuredWorkers,
}).Debug("Scale is ensured")
return true, nil
}
var jointoken string
if certsCP != nil && (joinCP != nil || len(joinWorkers) > 0) {
runes := []rune("abcdefghijklmnopqrstuvwxyz0123456789")
jointoken = fmt.Sprintf("%s.%s", helpers.RandomStringRunes(6, runes), helpers.RandomStringRunes(16, runes))
if _, err := certsCP.Execute(nil, "sudo kubeadm token create "+jointoken); err != nil {
return false, errors.Wrap(err, "creating new join token failed")
}
defer certsCP.Execute(nil, "sudo kubeadm token delete "+jointoken)
if k8sVersion.equals(V1x18x0) {
if _, err := certsCP.Execute(nil, "sudo kubeadm init phase bootstrap-token"); err != nil {
return false, errors.Wrap(err, "Working around kubeadm bug failed, see https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/troubleshooting-kubeadm/#not-possible-to-join-a-v1-18-node-to-a-v1-17-cluster-due-to-missing-rbac")
}
}
}
var certKey []byte
doKubeadmInit := certsCP == nil
if joinCP != nil {
if doKubeadmInit && (desired.Spec.Kubeconfig != nil && desired.Spec.Kubeconfig.Value != "" || !oneoff) {
return false, errors.New("initializing a cluster is not supported when kubeconfig exists or the flag --recur is passed")
}
if !doKubeadmInit && certKey == nil {
var err error
certKey, err = certsCP.Execute(nil, "sudo kubeadm init phase upload-certs --upload-certs | tail -1")
if err != nil {
return false, errors.Wrap(err, "uploading certs failed")
}
monitor.Info("Refreshed certs")
}
var joinKubeconfig *string
joinKubeconfig, err = join(
monitor,
clusterID,
joinCP,
certsCP,
*desired,
kubeAPI,
jointoken,
k8sVersion,
string(certKey),
k8sClient)
if err != nil {
return false, err
}
if joinKubeconfig == nil || err != nil {
return false, err
}
desired.Spec.Kubeconfig = &secret.Secret{Value: *joinKubeconfig}
return false, psf(monitor.WithFields(map[string]interface{}{
"type": "kubeconfig",
}))
}
if certsCP == nil {
monitor.Info("Awaiting controlplane initialization")
return false, nil
}
for _, worker := range joinWorkers {
if _, err := join(
monitor,
clusterID,
worker,
certsCP,
*desired,
kubeAPI,
jointoken,
k8sVersion,
"",
k8sClient); err != nil {
return false, errors.Wrapf(err, "joining worker %s failed", worker.infra.ID())
}
}
for _, pool := range append(workerPools, controlplanePool) {
if err := pool.infra.EnsureMembers(); err != nil {
return false, err
}
}
return false, nil
}