-
Notifications
You must be signed in to change notification settings - Fork 297
/
inplace.go
307 lines (267 loc) · 11.9 KB
/
inplace.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
package nodepool
import (
"context"
"fmt"
"strconv"
hyperv1 "github.com/openshift/hypershift/api/hypershift/v1beta1"
"github.com/openshift/hypershift/support/api"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
k8sutilspointer "k8s.io/utils/pointer"
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)
func (r *NodePoolReconciler) reconcileMachineSet(ctx context.Context,
machineSet *capiv1.MachineSet,
nodePool *hyperv1.NodePool,
userDataSecret *corev1.Secret,
machineTemplateCR client.Object,
CAPIClusterName string,
targetVersion,
targetConfigHash, targetConfigVersionHash, machineTemplateSpecJSON string) error {
log := ctrl.LoggerFrom(ctx)
// Set annotations and labels
if machineSet.GetAnnotations() == nil {
machineSet.Annotations = map[string]string{}
}
machineSet.Annotations[nodePoolAnnotation] = client.ObjectKeyFromObject(nodePool).String()
// Delete any paused annotation
delete(machineSet.Annotations, capiv1.PausedAnnotation)
if machineSet.GetLabels() == nil {
machineSet.Labels = map[string]string{}
}
machineSet.Labels[capiv1.ClusterNameLabel] = CAPIClusterName
resourcesName := generateName(CAPIClusterName, nodePool.Spec.ClusterName, nodePool.GetName())
machineSet.Spec.MinReadySeconds = int32(0)
gvk, err := apiutil.GVKForObject(machineTemplateCR, api.Scheme)
if err != nil {
return err
}
// Set MaxUnavailable for the inplace upgrader to use
maxUnavailable, err := getInPlaceMaxUnavailable(nodePool)
if err != nil {
return err
}
machineSet.Annotations[nodePoolAnnotationMaxUnavailable] = strconv.Itoa(maxUnavailable)
// Set selector and template
machineSet.Spec.ClusterName = CAPIClusterName
if machineSet.Spec.Selector.MatchLabels == nil {
machineSet.Spec.Selector.MatchLabels = map[string]string{}
}
machineSet.Spec.Selector.MatchLabels[resourcesName] = resourcesName
machineSet.Spec.Template = capiv1.MachineTemplateSpec{
ObjectMeta: capiv1.ObjectMeta{
Labels: map[string]string{
resourcesName: resourcesName,
capiv1.ClusterNameLabel: CAPIClusterName,
},
// Annotations here propagate down to Machines
// https://cluster-api.sigs.k8s.io/developer/architecture/controllers/metadata-propagation.html#machinedeployment.
Annotations: map[string]string{
nodePoolAnnotation: client.ObjectKeyFromObject(nodePool).String(),
},
},
Spec: capiv1.MachineSpec{
ClusterName: CAPIClusterName,
Bootstrap: capiv1.Bootstrap{
// Keep current user data for later check.
DataSecretName: machineSet.Spec.Template.Spec.Bootstrap.DataSecretName,
},
InfrastructureRef: corev1.ObjectReference{
Kind: gvk.Kind,
APIVersion: gvk.GroupVersion().String(),
Namespace: machineTemplateCR.GetNamespace(),
// Keep current version for later check.
Name: machineSet.Spec.Template.Spec.InfrastructureRef.Name,
},
// Keep current version for later check.
Version: machineSet.Spec.Template.Spec.Version,
NodeDrainTimeout: nodePool.Spec.NodeDrainTimeout,
},
}
// Propagate labels.
for k, v := range nodePool.Spec.NodeLabels {
// Propagated managed labels down to Machines with a known hardcoded prefix
// so the CPO HCCO Node controller can recongnise them and apply them to Nodes.
labelKey := fmt.Sprintf("%s.%s", labelManagedPrefix, k)
machineSet.Spec.Template.Labels[labelKey] = v
}
// Propagate taints.
taintsInJSON, err := taintsToJSON(nodePool.Spec.Taints)
if err != nil {
return err
}
machineSet.Spec.Template.Annotations[nodePoolAnnotationTaints] = taintsInJSON
setMachineSetReplicas(nodePool, machineSet)
isUpdating := false
// Propagate version and userData Secret to the MachineSet.
if userDataSecret.Name != k8sutilspointer.StringDeref(machineSet.Spec.Template.Spec.Bootstrap.DataSecretName, "") {
log.Info("New user data Secret has been generated",
"current", machineSet.Spec.Template.Spec.Bootstrap.DataSecretName,
"target", userDataSecret.Name)
// TODO (alberto): possibly compare with NodePool here instead so we don't rely on impl details to drive decisions.
if targetVersion != k8sutilspointer.StringDeref(machineSet.Spec.Template.Spec.Version, "") {
log.Info("Starting version upgrade: Propagating new version to the MachineSet",
"releaseImage", nodePool.Spec.Release.Image, "target", targetVersion)
}
if targetConfigHash != nodePool.Annotations[nodePoolAnnotationCurrentConfig] {
log.Info("Starting config upgrade: Propagating new config to the MachineSet",
"current", nodePool.Annotations[nodePoolAnnotationCurrentConfig], "target", targetConfigHash)
}
machineSet.Spec.Template.Spec.Version = &targetVersion
machineSet.Spec.Template.Spec.Bootstrap.DataSecretName = k8sutilspointer.String(userDataSecret.Name)
// Signal in-place upgrade request.
machineSet.Annotations[nodePoolAnnotationTargetConfigVersion] = targetConfigVersionHash
// If the machineSet is brand new, set current version to target so in-place upgrade no-op.
if _, ok := machineSet.Annotations[nodePoolAnnotationCurrentConfigVersion]; !ok {
machineSet.Annotations[nodePoolAnnotationCurrentConfigVersion] = targetConfigVersionHash
}
isUpdating = true
}
// template spec has changed, signal a rolling upgrade.
if machineTemplateCR.GetName() != machineSet.Spec.Template.Spec.InfrastructureRef.Name {
log.Info("New machine template has been generated",
"current", machineSet.Spec.Template.Spec.InfrastructureRef.Name,
"target", machineTemplateCR.GetName())
machineSet.Spec.Template.Spec.InfrastructureRef.Name = machineTemplateCR.GetName()
isUpdating = true
}
if isUpdating {
// We return early here during a version/config/MachineTemplate update to persist the resource with new user data Secret / MachineTemplate,
// so in the next reconciling loop we get a new MachineDeployment.Generation
// and we can do a legit MachineDeploymentComplete/MachineDeployment.Status.ObservedGeneration check.
return nil
}
if machineSetInPlaceRolloutIsComplete(machineSet) {
if nodePool.Status.Version != targetVersion {
log.Info("Version upgrade complete",
"previous", nodePool.Status.Version, "new", targetVersion)
nodePool.Status.Version = targetVersion
}
if nodePool.Annotations == nil {
nodePool.Annotations = make(map[string]string)
}
if nodePool.Annotations[nodePoolAnnotationCurrentConfig] != targetConfigHash {
log.Info("Config upgrade complete",
"previous", nodePool.Annotations[nodePoolAnnotationCurrentConfig], "new", targetConfigHash)
nodePool.Annotations[nodePoolAnnotationCurrentConfig] = targetConfigHash
}
nodePool.Annotations[nodePoolAnnotationCurrentConfigVersion] = targetConfigVersionHash
if nodePool.Annotations[nodePoolAnnotationPlatformMachineTemplate] != machineTemplateCR.GetName() {
log.Info("Rolling upgrade complete",
"previous", nodePool.Annotations[nodePoolAnnotationPlatformMachineTemplate], "new", machineTemplateCR.GetName())
nodePool.Annotations[nodePoolAnnotationPlatformMachineTemplate] = machineTemplateCR.GetName()
}
}
// Bubble up upgrading NodePoolUpdatingVersionConditionType.
var status corev1.ConditionStatus
reason := ""
message := ""
status = "unknown"
removeStatusCondition(&nodePool.Status.Conditions, hyperv1.NodePoolUpdatingVersionConditionType)
if _, ok := machineSet.Annotations[nodePoolAnnotationUpgradeInProgressTrue]; ok {
status = corev1.ConditionTrue
reason = hyperv1.AsExpectedReason
}
if _, ok := machineSet.Annotations[nodePoolAnnotationUpgradeInProgressFalse]; ok {
status = corev1.ConditionFalse
reason = hyperv1.NodePoolInplaceUpgradeFailedReason
}
// Check if config needs to be updated.
isUpdatingConfig := isUpdatingConfig(nodePool, targetConfigHash)
// Check if version needs to be updated.
isUpdatingVersion := isUpdatingVersion(nodePool, targetVersion)
if isUpdatingVersion {
message = fmt.Sprintf("Updating Version, Target: %v", machineSet.Annotations[nodePoolAnnotationTargetConfigVersion])
SetStatusCondition(&nodePool.Status.Conditions, hyperv1.NodePoolCondition{
Type: hyperv1.NodePoolUpdatingVersionConditionType,
Status: status,
ObservedGeneration: nodePool.Generation,
Message: message,
Reason: reason,
})
}
if isUpdatingConfig {
message = fmt.Sprintf("Updating Config, Target: %v", machineSet.Annotations[nodePoolAnnotationTargetConfigVersion])
SetStatusCondition(&nodePool.Status.Conditions, hyperv1.NodePoolCondition{
Type: hyperv1.NodePoolUpdatingConfigConditionType,
Status: status,
ObservedGeneration: nodePool.Generation,
Message: message,
Reason: reason,
})
}
// Bubble up AvailableReplicas and Ready condition from MachineSet.
nodePool.Status.Replicas = machineSet.Status.AvailableReplicas
for _, c := range machineSet.Status.Conditions {
// This condition should aggregate and summarise readiness from underlying MachineSets and Machines
// https://github.com/kubernetes-sigs/cluster-api/issues/3486.
if c.Type == capiv1.ReadyCondition {
// this is so api server does not complain
// invalid value: \"\": status.conditions.reason in body should be at least 1 chars long"
reason := hyperv1.AsExpectedReason
if c.Reason != "" {
reason = c.Reason
}
SetStatusCondition(&nodePool.Status.Conditions, hyperv1.NodePoolCondition{
Type: hyperv1.NodePoolReadyConditionType,
Status: c.Status,
ObservedGeneration: nodePool.Generation,
Message: c.Message,
Reason: reason,
})
break
}
}
return nil
}
func machineSetInPlaceRolloutIsComplete(machineSet *capiv1.MachineSet) bool {
return machineSet.Annotations[nodePoolAnnotationCurrentConfigVersion] == machineSet.Annotations[nodePoolAnnotationTargetConfigVersion]
}
// setMachineSetReplicas sets wanted replicas:
// If autoscaling is enabled we reconcile min/max annotations and leave replicas untouched.
func setMachineSetReplicas(nodePool *hyperv1.NodePool, machineSet *capiv1.MachineSet) {
if machineSet.Annotations == nil {
machineSet.Annotations = make(map[string]string)
}
if isAutoscalingEnabled(nodePool) {
// The MachineSet replicas field should default to a value inside the (min size, max size) range based on the autoscaler annotations
// so the autoscaler can take control of the replicas field.
//
// 1. if it’s a new MachineSet, or the replicas field of the old MachineSet is < min size, use min size
// 2. if the replicas field of the old MachineSet is > max size, use max size
msReplicas := k8sutilspointer.Int32Deref(machineSet.Spec.Replicas, 0)
if msReplicas < nodePool.Spec.AutoScaling.Min {
machineSet.Spec.Replicas = &nodePool.Spec.AutoScaling.Min
} else if msReplicas > nodePool.Spec.AutoScaling.Max {
machineSet.Spec.Replicas = &nodePool.Spec.AutoScaling.Max
}
machineSet.Annotations[autoscalerMaxAnnotation] = strconv.Itoa(int(nodePool.Spec.AutoScaling.Max))
machineSet.Annotations[autoscalerMinAnnotation] = strconv.Itoa(int(nodePool.Spec.AutoScaling.Min))
}
// If autoscaling is NOT enabled we reset min/max annotations and reconcile replicas.
if !isAutoscalingEnabled(nodePool) {
machineSet.Annotations[autoscalerMaxAnnotation] = "0"
machineSet.Annotations[autoscalerMinAnnotation] = "0"
machineSet.Spec.Replicas = k8sutilspointer.Int32(k8sutilspointer.Int32Deref(nodePool.Spec.Replicas, 0))
}
}
func getInPlaceMaxUnavailable(nodePool *hyperv1.NodePool) (int, error) {
intOrPercent := intstr.FromInt(1)
if nodePool.Spec.Management.InPlace != nil {
if nodePool.Spec.Management.InPlace.MaxUnavailable != nil {
intOrPercent = *nodePool.Spec.Management.InPlace.MaxUnavailable
}
}
replicas := int(k8sutilspointer.Int32Deref(nodePool.Spec.Replicas, 0))
maxUnavailable, err := intstr.GetScaledValueFromIntOrPercent(&intOrPercent, replicas, false)
if err != nil {
return 0, err
}
if maxUnavailable == 0 {
maxUnavailable = 1
}
return maxUnavailable, nil
}