-
Notifications
You must be signed in to change notification settings - Fork 415
/
machinepool_deployment_strategy.go
305 lines (255 loc) · 11.6 KB
/
machinepool_deployment_strategy.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
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
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 machinepool
import (
"context"
"math/rand"
"sort"
"time"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/intstr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
ctrl "sigs.k8s.io/controller-runtime"
)
type (
// Surger is the ability to surge a number of replica.
Surger interface {
Surge(desiredReplicaCount int) (int, error)
}
// DeleteSelector is the ability to select nodes to be delete with respect to a desired number of replicas.
DeleteSelector interface {
SelectMachinesToDelete(ctx context.Context, desiredReplicas int32, machinesByProviderID map[string]infrav1exp.AzureMachinePoolMachine) ([]infrav1exp.AzureMachinePoolMachine, error)
}
// TypedDeleteSelector is the ability to select nodes to be deleted with respect to a desired number of nodes, and
// the ability to describe the underlying type of the deployment strategy.
TypedDeleteSelector interface {
DeleteSelector
Type() infrav1exp.AzureMachinePoolDeploymentStrategyType
}
rollingUpdateStrategy struct {
infrav1exp.MachineRollingUpdateDeployment
}
)
// NewMachinePoolDeploymentStrategy constructs a strategy implementation described in the AzureMachinePoolDeploymentStrategy
// specification.
func NewMachinePoolDeploymentStrategy(strategy infrav1exp.AzureMachinePoolDeploymentStrategy) TypedDeleteSelector {
switch strategy.Type {
case infrav1exp.RollingUpdateAzureMachinePoolDeploymentStrategyType:
rollingUpdate := strategy.RollingUpdate
if rollingUpdate == nil {
rollingUpdate = &infrav1exp.MachineRollingUpdateDeployment{}
}
return &rollingUpdateStrategy{
MachineRollingUpdateDeployment: *rollingUpdate,
}
default:
// default to a rolling update strategy if unknown type
return &rollingUpdateStrategy{
MachineRollingUpdateDeployment: infrav1exp.MachineRollingUpdateDeployment{},
}
}
}
// Type is the AzureMachinePoolDeploymentStrategyType for the strategy.
func (rollingUpdateStrategy *rollingUpdateStrategy) Type() infrav1exp.AzureMachinePoolDeploymentStrategyType {
return infrav1exp.RollingUpdateAzureMachinePoolDeploymentStrategyType
}
// Surge calculates the number of replicas that can be added during an upgrade operation.
func (rollingUpdateStrategy *rollingUpdateStrategy) Surge(desiredReplicaCount int) (int, error) {
if rollingUpdateStrategy.MaxSurge == nil {
return 1, nil
}
return intstr.GetScaledValueFromIntOrPercent(rollingUpdateStrategy.MaxSurge, desiredReplicaCount, true)
}
// maxUnavailable calculates the maximum number of replicas which can be unavailable at any time.
func (rollingUpdateStrategy *rollingUpdateStrategy) maxUnavailable(desiredReplicaCount int) (int, error) {
if rollingUpdateStrategy.MaxUnavailable != nil {
val, err := intstr.GetScaledValueFromIntOrPercent(rollingUpdateStrategy.MaxUnavailable, desiredReplicaCount, false)
if err != nil {
return 0, errors.Wrap(err, "failed to get scaled value or int from maxUnavailable")
}
return val, nil
}
return 0, nil
}
// SelectMachinesToDelete selects the machines to delete based on the machine state, desired replica count, and
// the DeletePolicy.
func (rollingUpdateStrategy rollingUpdateStrategy) SelectMachinesToDelete(ctx context.Context, desiredReplicaCount int32, machinesByProviderID map[string]infrav1exp.AzureMachinePoolMachine) ([]infrav1exp.AzureMachinePoolMachine, error) {
ctx, _, done := tele.StartSpanWithLogger(
ctx,
"strategies.rollingUpdateStrategy.SelectMachinesToDelete",
)
defer done()
maxUnavailable, err := rollingUpdateStrategy.maxUnavailable(int(desiredReplicaCount))
if err != nil {
return nil, err
}
var (
order = func() func(machines []infrav1exp.AzureMachinePoolMachine) []infrav1exp.AzureMachinePoolMachine {
switch rollingUpdateStrategy.DeletePolicy {
case infrav1exp.OldestDeletePolicyType:
return orderByOldest
case infrav1exp.NewestDeletePolicyType:
return orderByNewest
default:
return orderRandom
}
}()
log = ctrl.LoggerFrom(ctx).V(4)
failedMachines = order(getFailedMachines(machinesByProviderID))
deletingMachines = order(getDeletingMachines(machinesByProviderID))
readyMachines = order(getReadyMachines(machinesByProviderID))
machinesWithoutLatestModel = order(getMachinesWithoutLatestModel(machinesByProviderID))
overProvisionCount = len(readyMachines) - int(desiredReplicaCount)
disruptionBudget = func() int {
if maxUnavailable > int(desiredReplicaCount) {
return int(desiredReplicaCount)
}
return len(readyMachines) - int(desiredReplicaCount) + maxUnavailable
}()
)
log.Info("selecting machines to delete",
"readyMachines", len(readyMachines),
"desiredReplicaCount", desiredReplicaCount,
"maxUnavailable", maxUnavailable,
"disruptionBudget", disruptionBudget,
"machinesWithoutTheLatestModel", len(machinesWithoutLatestModel),
"failedMachines", len(failedMachines),
"deletingMachines", len(deletingMachines),
)
// if we have failed or deleting machines, remove them
if len(failedMachines) > 0 || len(deletingMachines) > 0 {
log.Info("failed or deleting machines", "desiredReplicaCount", desiredReplicaCount, "maxUnavailable", maxUnavailable, "failedMachines", getProviderIDs(failedMachines), "deletingMachines", getProviderIDs(deletingMachines))
return append(failedMachines, deletingMachines...), nil
}
// if we have not yet reached our desired count, don't try to delete anything
if len(readyMachines) < int(desiredReplicaCount) {
log.Info("not enough ready machines", "desiredReplicaCount", desiredReplicaCount, "readyMachinesCount", len(readyMachines), "machinesByProviderID", len(machinesByProviderID))
return []infrav1exp.AzureMachinePoolMachine{}, nil
}
// we have too many machines, let's choose the oldest to remove
if overProvisionCount > 0 {
var toDelete []infrav1exp.AzureMachinePoolMachine
log.Info("over-provisioned", "desiredReplicaCount", desiredReplicaCount, "overProvisionCount", overProvisionCount, "machinesWithoutLatestModel", getProviderIDs(machinesWithoutLatestModel))
// we are over-provisioned try to remove old models
for _, v := range machinesWithoutLatestModel {
if len(toDelete) >= overProvisionCount {
return toDelete, nil
}
toDelete = append(toDelete, v)
}
log.Info("over-provisioned ready", "desiredReplicaCount", desiredReplicaCount, "overProvisionCount", overProvisionCount, "readyMachines", getProviderIDs(readyMachines))
// remove ready machines
for _, v := range readyMachines {
if len(toDelete) >= overProvisionCount {
return toDelete, nil
}
toDelete = append(toDelete, v)
}
return toDelete, nil
}
if len(machinesWithoutLatestModel) == 0 {
log.Info("nothing more to do since all the AzureMachinePoolMachine(s) are the latest model and not over-provisioned")
return []infrav1exp.AzureMachinePoolMachine{}, nil
}
if disruptionBudget <= 0 {
log.Info("exit early since disruption budget is less than or equal to zero", "disruptionBudget", disruptionBudget, "desiredReplicaCount", desiredReplicaCount, "maxUnavailable", maxUnavailable, "readyMachines", getProviderIDs(readyMachines), "readyMachinesCount", len(readyMachines))
return []infrav1exp.AzureMachinePoolMachine{}, nil
}
var toDelete []infrav1exp.AzureMachinePoolMachine
log.Info("removing ready machines within disruption budget", "desiredReplicaCount", desiredReplicaCount, "maxUnavailable", maxUnavailable, "readyMachines", getProviderIDs(readyMachines), "readyMachinesCount", len(readyMachines))
for _, v := range readyMachines {
if len(toDelete) >= disruptionBudget {
return toDelete, nil
}
if !v.Status.LatestModelApplied {
toDelete = append(toDelete, v)
}
}
log.Info("completed without filling toDelete", "toDelete", getProviderIDs(toDelete), "numToDelete", len(toDelete))
return toDelete, nil
}
func getFailedMachines(machinesByProviderID map[string]infrav1exp.AzureMachinePoolMachine) []infrav1exp.AzureMachinePoolMachine {
var machines []infrav1exp.AzureMachinePoolMachine
for _, v := range machinesByProviderID {
// ready status, with provisioning state Succeeded, and not marked for delete
if v.Status.ProvisioningState != nil && *v.Status.ProvisioningState == infrav1.Failed {
machines = append(machines, v)
}
}
return machines
}
// getDeletingMachines is responsible for identifying machines whose VMs are in an active state of deletion
// but whose corresponding AzureMachinePoolMachine resource has not yet been marked for deletion.
func getDeletingMachines(machinesByProviderID map[string]infrav1exp.AzureMachinePoolMachine) []infrav1exp.AzureMachinePoolMachine {
var machines []infrav1exp.AzureMachinePoolMachine
for _, v := range machinesByProviderID {
if v.Status.ProvisioningState != nil &&
// provisioning state is Deleting
*v.Status.ProvisioningState == infrav1.Deleting &&
// Ensure that the machine has not already been marked for deletion
v.DeletionTimestamp.IsZero() {
machines = append(machines, v)
}
}
return machines
}
func getReadyMachines(machinesByProviderID map[string]infrav1exp.AzureMachinePoolMachine) []infrav1exp.AzureMachinePoolMachine {
var readyMachines []infrav1exp.AzureMachinePoolMachine
for _, v := range machinesByProviderID {
// ready status, with provisioning state Succeeded, and not marked for delete
if v.Status.Ready &&
(v.Status.ProvisioningState != nil && *v.Status.ProvisioningState == infrav1.Succeeded) &&
// Don't include machines that have already been marked for delete
v.DeletionTimestamp.IsZero() &&
// Don't include machines whose VMs are in an active state of deleting
*v.Status.ProvisioningState != infrav1.Deleting {
readyMachines = append(readyMachines, v)
}
}
return readyMachines
}
func getMachinesWithoutLatestModel(machinesByProviderID map[string]infrav1exp.AzureMachinePoolMachine) []infrav1exp.AzureMachinePoolMachine {
var machinesWithLatestModel []infrav1exp.AzureMachinePoolMachine
for _, v := range machinesByProviderID {
if !v.Status.LatestModelApplied {
machinesWithLatestModel = append(machinesWithLatestModel, v)
}
}
return machinesWithLatestModel
}
func orderByNewest(machines []infrav1exp.AzureMachinePoolMachine) []infrav1exp.AzureMachinePoolMachine {
sort.Slice(machines, func(i, j int) bool {
return machines[i].ObjectMeta.CreationTimestamp.After(machines[j].ObjectMeta.CreationTimestamp.Time)
})
return machines
}
func orderByOldest(machines []infrav1exp.AzureMachinePoolMachine) []infrav1exp.AzureMachinePoolMachine {
sort.Slice(machines, func(i, j int) bool {
return machines[j].ObjectMeta.CreationTimestamp.After(machines[i].ObjectMeta.CreationTimestamp.Time)
})
return machines
}
func orderRandom(machines []infrav1exp.AzureMachinePoolMachine) []infrav1exp.AzureMachinePoolMachine {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(machines), func(i, j int) { machines[i], machines[j] = machines[j], machines[i] })
return machines
}
func getProviderIDs(machines []infrav1exp.AzureMachinePoolMachine) []string {
ids := make([]string, len(machines))
for i, machine := range machines {
ids[i] = machine.Spec.ProviderID
}
return ids
}