/
machinedeployment_helpers.go
349 lines (304 loc) · 16.8 KB
/
machinedeployment_helpers.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
/*
Copyright 2020 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 framework
import (
"context"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/cluster-api/test/framework/internal/log"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// CreateMachineDeploymentInput is the input for CreateMachineDeployment.
type CreateMachineDeploymentInput struct {
Creator Creator
MachineDeployment *clusterv1.MachineDeployment
BootstrapConfigTemplate runtime.Object
InfraMachineTemplate runtime.Object
}
// CreateMachineDeployment creates the machine deployment and dependencies.
func CreateMachineDeployment(ctx context.Context, input CreateMachineDeploymentInput) {
By("creating a core MachineDeployment resource")
Expect(input.Creator.Create(ctx, input.MachineDeployment)).To(Succeed())
By("creating a BootstrapConfigTemplate resource")
Expect(input.Creator.Create(ctx, input.BootstrapConfigTemplate)).To(Succeed())
By("creating an InfrastructureMachineTemplate resource")
Expect(input.Creator.Create(ctx, input.InfraMachineTemplate)).To(Succeed())
}
// GetMachineDeploymentsByClusterInput is the input for GetMachineDeploymentsByCluster.
type GetMachineDeploymentsByClusterInput struct {
Lister Lister
ClusterName string
Namespace string
}
// GetMachineDeploymentsByCluster returns the MachineDeployments objects for a cluster.
// Important! this method relies on labels that are created by the CAPI controllers during the first reconciliation, so
// it is necessary to ensure this is already happened before calling it.
func GetMachineDeploymentsByCluster(ctx context.Context, input GetMachineDeploymentsByClusterInput) []*clusterv1.MachineDeployment {
deploymentList := &clusterv1.MachineDeploymentList{}
Expect(input.Lister.List(ctx, deploymentList, byClusterOptions(input.ClusterName, input.Namespace)...)).To(Succeed(), "Failed to list MachineDeployments object for Cluster %s/%s", input.Namespace, input.ClusterName)
deployments := make([]*clusterv1.MachineDeployment, len(deploymentList.Items))
for i := range deploymentList.Items {
deployments[i] = &deploymentList.Items[i]
}
return deployments
}
// WaitForMachineDeploymentNodesToExistInput is the input for WaitForMachineDeploymentNodesToExist.
type WaitForMachineDeploymentNodesToExistInput struct {
Lister Lister
Cluster *clusterv1.Cluster
MachineDeployment *clusterv1.MachineDeployment
}
// WaitForMachineDeploymentNodesToExist waits until all nodes associated with a machine deployment exist.
func WaitForMachineDeploymentNodesToExist(ctx context.Context, input WaitForMachineDeploymentNodesToExistInput, intervals ...interface{}) {
Expect(ctx).NotTo(BeNil(), "ctx is required for WaitForMachineDeploymentNodesToExist")
Expect(input.Lister).ToNot(BeNil(), "Invalid argument. input.Lister can't be nil when calling WaitForMachineDeploymentNodesToExist")
Expect(input.MachineDeployment).ToNot(BeNil(), "Invalid argument. input.MachineDeployment can't be nil when calling WaitForMachineDeploymentNodesToExist")
By("waiting for the workload nodes to exist")
Eventually(func() (int, error) {
selectorMap, err := metav1.LabelSelectorAsMap(&input.MachineDeployment.Spec.Selector)
if err != nil {
return 0, err
}
ms := &clusterv1.MachineSetList{}
if err := input.Lister.List(ctx, ms, client.InNamespace(input.Cluster.Namespace), client.MatchingLabels(selectorMap)); err != nil {
return 0, err
}
if len(ms.Items) == 0 {
return 0, errors.New("no machinesets were found")
}
machineSet := ms.Items[0]
selectorMap, err = metav1.LabelSelectorAsMap(&machineSet.Spec.Selector)
if err != nil {
return 0, err
}
machines := &clusterv1.MachineList{}
if err := input.Lister.List(ctx, machines, client.InNamespace(machineSet.Namespace), client.MatchingLabels(selectorMap)); err != nil {
return 0, err
}
count := 0
for _, machine := range machines.Items {
if machine.Status.NodeRef != nil {
count++
}
}
return count, nil
}, intervals...).Should(Equal(int(*input.MachineDeployment.Spec.Replicas)))
}
// DiscoveryAndWaitForMachineDeploymentsInput is the input type for DiscoveryAndWaitForMachineDeployments.
type DiscoveryAndWaitForMachineDeploymentsInput struct {
Lister Lister
Cluster *clusterv1.Cluster
}
// DiscoveryAndWaitForMachineDeployments discovers the MachineDeployments existing in a cluster and waits for them to be ready (all the machine provisioned).
func DiscoveryAndWaitForMachineDeployments(ctx context.Context, input DiscoveryAndWaitForMachineDeploymentsInput, intervals ...interface{}) []*clusterv1.MachineDeployment {
Expect(ctx).NotTo(BeNil(), "ctx is required for DiscoveryAndWaitForMachineDeployments")
Expect(input.Lister).ToNot(BeNil(), "Invalid argument. input.Lister can't be nil when calling DiscoveryAndWaitForMachineDeployments")
Expect(input.Cluster).ToNot(BeNil(), "Invalid argument. input.Cluster can't be nil when calling DiscoveryAndWaitForMachineDeployments")
machineDeployments := GetMachineDeploymentsByCluster(ctx, GetMachineDeploymentsByClusterInput{
Lister: input.Lister,
ClusterName: input.Cluster.Name,
Namespace: input.Cluster.Namespace,
})
for _, deployment := range machineDeployments {
WaitForMachineDeploymentNodesToExist(ctx, WaitForMachineDeploymentNodesToExistInput{
Lister: input.Lister,
Cluster: input.Cluster,
MachineDeployment: deployment,
}, intervals...)
}
return machineDeployments
}
// UpgradeMachineDeploymentsAndWaitInput is the input type for UpgradeMachineDeploymentsAndWait.
type UpgradeMachineDeploymentsAndWaitInput struct {
ClusterProxy ClusterProxy
Cluster *clusterv1.Cluster
UpgradeVersion string
MachineDeployments []*clusterv1.MachineDeployment
WaitForMachinesToBeUpgraded []interface{}
}
// UpgradeMachineDeploymentsAndWait upgrades a machine deployment and waits for its machines to be upgraded.
func UpgradeMachineDeploymentsAndWait(ctx context.Context, input UpgradeMachineDeploymentsAndWaitInput) {
Expect(ctx).NotTo(BeNil(), "ctx is required for UpgradeMachineDeploymentsAndWait")
Expect(input.ClusterProxy).ToNot(BeNil(), "Invalid argument. input.ClusterProxy can't be nil when calling UpgradeMachineDeploymentsAndWait")
Expect(input.Cluster).ToNot(BeNil(), "Invalid argument. input.Cluster can't be nil when calling UpgradeMachineDeploymentsAndWait")
Expect(input.UpgradeVersion).ToNot(BeNil(), "Invalid argument. input.UpgradeVersion can't be nil when calling UpgradeMachineDeploymentsAndWait")
Expect(input.MachineDeployments).ToNot(BeEmpty(), "Invalid argument. input.MachineDeployments can't be empty when calling UpgradeMachineDeploymentsAndWait")
mgmtClient := input.ClusterProxy.GetClient()
for _, deployment := range input.MachineDeployments {
log.Logf("Patching the new kubernetes version to Machine Deployment %s/%s", deployment.Namespace, deployment.Name)
patchHelper, err := patch.NewHelper(deployment, mgmtClient)
Expect(err).ToNot(HaveOccurred())
oldVersion := deployment.Spec.Template.Spec.Version
deployment.Spec.Template.Spec.Version = &input.UpgradeVersion
Expect(patchHelper.Patch(context.TODO(), deployment)).To(Succeed())
log.Logf("Waiting for Kubernetes versions of machines in MachineDeployment %s/%s to be upgraded from %s to %s",
deployment.Namespace, deployment.Name, *oldVersion, input.UpgradeVersion)
WaitForMachineDeploymentMachinesToBeUpgraded(ctx, WaitForMachineDeploymentMachinesToBeUpgradedInput{
Lister: mgmtClient,
Cluster: input.Cluster,
MachineCount: int(*deployment.Spec.Replicas),
KubernetesUpgradeVersion: input.UpgradeVersion,
MachineDeployment: *deployment,
}, input.WaitForMachinesToBeUpgraded...)
}
}
// WaitForMachineDeploymentRollingUpgradeToStartInput is the input for WaitForMachineDeploymentRollingUpgradeToStart.
type WaitForMachineDeploymentRollingUpgradeToStartInput struct {
Getter Getter
MachineDeployment *clusterv1.MachineDeployment
}
// WaitForMachineDeploymentRollingUpgradeToStart waits until rolling upgrade starts.
func WaitForMachineDeploymentRollingUpgradeToStart(ctx context.Context, input WaitForMachineDeploymentRollingUpgradeToStartInput, intervals ...interface{}) {
Expect(ctx).NotTo(BeNil(), "ctx is required for WaitForMachineDeploymentRollingUpgradeToStart")
Expect(input.Getter).ToNot(BeNil(), "Invalid argument. input.Getter can't be nil when calling WaitForMachineDeploymentRollingUpgradeToStart")
Expect(input.MachineDeployment).ToNot(BeNil(), "Invalid argument. input.MachineDeployment can't be nil when calling WaitForMachineDeploymentRollingUpgradeToStarts")
log.Logf("Waiting for MachineDeployment rolling upgrade to start")
Eventually(func() bool {
md := &clusterv1.MachineDeployment{}
Expect(input.Getter.Get(ctx, client.ObjectKey{Namespace: input.MachineDeployment.Namespace, Name: input.MachineDeployment.Name}, md)).To(Succeed())
return md.Status.Replicas != md.Status.AvailableReplicas
}, intervals...).Should(BeTrue())
}
// WaitForMachineDeploymentRollingUpgradeToCompleteInput is the input for WaitForMachineDeploymentRollingUpgradeToComplete.
type WaitForMachineDeploymentRollingUpgradeToCompleteInput struct {
Getter Getter
MachineDeployment *clusterv1.MachineDeployment
}
// WaitForMachineDeploymentNodesToExist waits until rolling upgrade is complete.
func WaitForMachineDeploymentRollingUpgradeToComplete(ctx context.Context, input WaitForMachineDeploymentRollingUpgradeToCompleteInput, intervals ...interface{}) {
Expect(ctx).NotTo(BeNil(), "ctx is required for WaitForMachineDeploymentRollingUpgradeToComplete")
Expect(input.Getter).ToNot(BeNil(), "Invalid argument. input.Getter can't be nil when calling WaitForMachineDeploymentRollingUpgradeToComplete")
Expect(input.MachineDeployment).ToNot(BeNil(), "Invalid argument. input.MachineDeployment can't be nil when calling WaitForMachineDeploymentRollingUpgradeToComplete")
log.Logf("Waiting for MachineDeployment rolling upgrade to complete")
Eventually(func() bool {
md := &clusterv1.MachineDeployment{}
Expect(input.Getter.Get(ctx, client.ObjectKey{Namespace: input.MachineDeployment.Namespace, Name: input.MachineDeployment.Name}, md)).To(Succeed())
return md.Status.Replicas == md.Status.AvailableReplicas
}, intervals...).Should(BeTrue())
}
// UpgradeMachineDeploymentInfrastructureRefAndWaitInput is the input type for UpgradeMachineDeploymentInfrastructureRefAndWait.
type UpgradeMachineDeploymentInfrastructureRefAndWaitInput struct {
ClusterProxy ClusterProxy
Cluster *clusterv1.Cluster
MachineDeployments []*clusterv1.MachineDeployment
WaitForMachinesToBeUpgraded []interface{}
}
// UpgradeMachineDeploymentInfrastructureRefAndWait upgrades a machine deployment infrastructure ref and waits for its machines to be upgraded.
func UpgradeMachineDeploymentInfrastructureRefAndWait(ctx context.Context, input UpgradeMachineDeploymentInfrastructureRefAndWaitInput) {
Expect(ctx).NotTo(BeNil(), "ctx is required for UpgradeMachineDeploymentInfrastructureRefAndWait")
Expect(input.ClusterProxy).ToNot(BeNil(), "Invalid argument. input.ClusterProxy can't be nil when calling UpgradeMachineDeploymentInfrastructureRefAndWait")
Expect(input.Cluster).ToNot(BeNil(), "Invalid argument. input.Cluster can't be nil when calling UpgradeMachineDeploymentInfrastructureRefAndWait")
Expect(input.MachineDeployments).ToNot(BeEmpty(), "Invalid argument. input.MachineDeployments can't be empty when calling UpgradeMachineDeploymentInfrastructureRefAndWait")
mgmtClient := input.ClusterProxy.GetClient()
for _, deployment := range input.MachineDeployments {
log.Logf("Patching the new infrastructure ref to Machine Deployment %s/%s", deployment.Namespace, deployment.Name)
// Retrieve infra object
infraRef := deployment.Spec.Template.Spec.InfrastructureRef
infraObj := &unstructured.Unstructured{}
infraObj.SetGroupVersionKind(infraRef.GroupVersionKind())
key := client.ObjectKey{
Namespace: input.Cluster.Namespace,
Name: infraRef.Name,
}
Expect(mgmtClient.Get(ctx, key, infraObj)).NotTo(HaveOccurred())
// Creates a new infra object
newInfraObj := infraObj
newInfraObjName := fmt.Sprintf("%s-%s", infraRef.Name, util.RandomString(6))
newInfraObj.SetName(newInfraObjName)
newInfraObj.SetResourceVersion("")
Expect(mgmtClient.Create(ctx, newInfraObj)).NotTo(HaveOccurred())
// Patch the new infra object's ref to the machine deployment
patchHelper, err := patch.NewHelper(deployment, mgmtClient)
Expect(err).ToNot(HaveOccurred())
infraRef.Name = newInfraObjName
deployment.Spec.Template.Spec.InfrastructureRef = infraRef
Expect(patchHelper.Patch(context.TODO(), deployment)).To(Succeed())
log.Logf("Waiting for rolling upgrade to start.")
WaitForMachineDeploymentRollingUpgradeToStart(ctx, WaitForMachineDeploymentRollingUpgradeToStartInput{
Getter: mgmtClient,
MachineDeployment: deployment,
}, input.WaitForMachinesToBeUpgraded...)
log.Logf("Waiting for rolling upgrade to complete.")
WaitForMachineDeploymentRollingUpgradeToComplete(ctx, WaitForMachineDeploymentRollingUpgradeToCompleteInput{
Getter: mgmtClient,
MachineDeployment: deployment,
}, input.WaitForMachinesToBeUpgraded...)
}
}
// machineDeploymentOptions returns a set of ListOptions that allows to get all machine objects belonging to a machine deployment.
func machineDeploymentOptions(deployment clusterv1.MachineDeployment) []client.ListOption {
return []client.ListOption{
client.MatchingLabels(deployment.Spec.Selector.MatchLabels),
}
}
// ScaleAndWaitMachineDeploymentInput is the input for ScaleAndWaitMachineDeployment.
type ScaleAndWaitMachineDeploymentInput struct {
ClusterProxy ClusterProxy
Cluster *clusterv1.Cluster
MachineDeployment *clusterv1.MachineDeployment
Replicas int32
WaitForMachineDeployments []interface{}
}
// ScaleAndWaitMachineDeployment scales MachineDeployment and waits until all machines have node ref and equal to Replicas.
func ScaleAndWaitMachineDeployment(ctx context.Context, input ScaleAndWaitMachineDeploymentInput) {
Expect(ctx).NotTo(BeNil(), "ctx is required for ScaleAndWaitMachineDeployment")
Expect(input.ClusterProxy).ToNot(BeNil(), "Invalid argument. input.ClusterProxy can't be nil when calling ScaleAndWaitMachineDeployment")
Expect(input.Cluster).ToNot(BeNil(), "Invalid argument. input.Cluster can't be nil when calling ScaleAndWaitMachineDeployment")
log.Logf("Scaling machine deployment %s/%s from %v to %v replicas", input.MachineDeployment.Namespace, input.MachineDeployment.Name, input.MachineDeployment.Spec.Replicas, input.Replicas)
patchHelper, err := patch.NewHelper(input.MachineDeployment, input.ClusterProxy.GetClient())
Expect(err).ToNot(HaveOccurred())
input.MachineDeployment.Spec.Replicas = pointer.Int32Ptr(input.Replicas)
Expect(patchHelper.Patch(ctx, input.MachineDeployment)).To(Succeed())
log.Logf("Waiting for correct number of replicas to exist")
Eventually(func() (int, error) {
selectorMap, err := metav1.LabelSelectorAsMap(&input.MachineDeployment.Spec.Selector)
if err != nil {
return -1, err
}
ms := &clusterv1.MachineSetList{}
if err := input.ClusterProxy.GetClient().List(ctx, ms, client.InNamespace(input.Cluster.Namespace), client.MatchingLabels(selectorMap)); err != nil {
return -1, err
}
if len(ms.Items) == 0 {
return -1, errors.New("no machinesets were found")
}
machineSet := ms.Items[0]
selectorMap, err = metav1.LabelSelectorAsMap(&machineSet.Spec.Selector)
if err != nil {
return -1, err
}
machines := &clusterv1.MachineList{}
if err := input.ClusterProxy.GetClient().List(ctx, machines, client.InNamespace(machineSet.Namespace), client.MatchingLabels(selectorMap)); err != nil {
return -1, err
}
nodeRefCount := 0
for _, machine := range machines.Items {
if machine.Status.NodeRef != nil {
nodeRefCount++
}
}
if len(machines.Items) != nodeRefCount {
return -1, errors.New("Machine count does not match existing nodes count")
}
return nodeRefCount, nil
}, input.WaitForMachineDeployments...).Should(Equal(int(*input.MachineDeployment.Spec.Replicas)))
}