-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
clusterctl_helpers.go
338 lines (292 loc) · 15.4 KB
/
clusterctl_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
/*
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 clusterctl
import (
"context"
"os"
"path/filepath"
. "github.com/onsi/gomega"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/test/framework"
"sigs.k8s.io/cluster-api/test/framework/internal/log"
)
// InitManagementClusterAndWatchControllerLogsInput is the input type for InitManagementClusterAndWatchControllerLogs.
type InitManagementClusterAndWatchControllerLogsInput struct {
ClusterProxy framework.ClusterProxy
ClusterctlConfigPath string
CoreProvider string
BootstrapProviders []string
ControlPlaneProviders []string
InfrastructureProviders []string
LogFolder string
DisableMetricsCollection bool
ClusterctlBinaryPath string
}
// InitManagementClusterAndWatchControllerLogs initializes a management using clusterctl and setup watches for controller logs.
// Important: Considering we want to support test suites using existing clusters, clusterctl init is executed only in case
// there are no provider controllers in the cluster; but controller logs watchers are created regardless of the pre-existing providers.
func InitManagementClusterAndWatchControllerLogs(ctx context.Context, input InitManagementClusterAndWatchControllerLogsInput, intervals ...interface{}) {
Expect(ctx).NotTo(BeNil(), "ctx is required for InitManagementClusterAndWatchControllerLogs")
Expect(input.ClusterProxy).ToNot(BeNil(), "Invalid argument. input.ClusterProxy can't be nil when calling InitManagementClusterAndWatchControllerLogs")
Expect(input.ClusterctlConfigPath).To(BeAnExistingFile(), "Invalid argument. input.ClusterctlConfigPath must be an existing file when calling InitManagementClusterAndWatchControllerLogs")
Expect(input.InfrastructureProviders).ToNot(BeEmpty(), "Invalid argument. input.InfrastructureProviders can't be empty when calling InitManagementClusterAndWatchControllerLogs")
Expect(os.MkdirAll(input.LogFolder, 0750)).To(Succeed(), "Invalid argument. input.LogFolder can't be created for InitManagementClusterAndWatchControllerLogs")
if input.CoreProvider == "" {
input.CoreProvider = config.ClusterAPIProviderName
}
if len(input.BootstrapProviders) == 0 {
input.BootstrapProviders = []string{config.KubeadmBootstrapProviderName}
}
if len(input.ControlPlaneProviders) == 0 {
input.ControlPlaneProviders = []string{config.KubeadmControlPlaneProviderName}
}
client := input.ClusterProxy.GetClient()
controllersDeployments := framework.GetControllerDeployments(ctx, framework.GetControllerDeploymentsInput{
Lister: client,
})
if len(controllersDeployments) == 0 {
initInput := InitInput{
// pass reference to the management cluster hosting this test
KubeconfigPath: input.ClusterProxy.GetKubeconfigPath(),
// pass the clusterctl config file that points to the local provider repository created for this test
ClusterctlConfigPath: input.ClusterctlConfigPath,
// setup the desired list of providers for a single-tenant management cluster
CoreProvider: input.CoreProvider,
BootstrapProviders: input.BootstrapProviders,
ControlPlaneProviders: input.ControlPlaneProviders,
InfrastructureProviders: input.InfrastructureProviders,
// setup clusterctl logs folder
LogFolder: input.LogFolder,
}
if input.ClusterctlBinaryPath != "" {
InitWithBinary(ctx, input.ClusterctlBinaryPath, initInput)
} else {
Init(ctx, initInput)
}
}
log.Logf("Waiting for provider controllers to be running")
controllersDeployments = framework.GetControllerDeployments(ctx, framework.GetControllerDeploymentsInput{
Lister: client,
})
Expect(controllersDeployments).ToNot(BeEmpty(), "The list of controller deployments should not be empty")
for _, deployment := range controllersDeployments {
framework.WaitForDeploymentsAvailable(ctx, framework.WaitForDeploymentsAvailableInput{
Getter: client,
Deployment: deployment,
GetLister: client,
ClientSet: input.ClusterProxy.GetClientSet(),
LogPath: filepath.Join(input.LogFolder, "controllers"),
}, intervals...)
// Start streaming logs from all controller providers
framework.WatchDeploymentLogs(ctx, framework.WatchDeploymentLogsInput{
GetLister: client,
ClientSet: input.ClusterProxy.GetClientSet(),
Deployment: deployment,
LogPath: filepath.Join(input.LogFolder, "controllers"),
})
if !input.DisableMetricsCollection {
framework.WatchPodMetrics(ctx, framework.WatchPodMetricsInput{
GetLister: client,
ClientSet: input.ClusterProxy.GetClientSet(),
Deployment: deployment,
MetricsPath: filepath.Join(input.LogFolder, "controllers"),
})
}
}
}
// UpgradeManagementClusterAndWaitInput is the input type for UpgradeManagementClusterAndWait.
type UpgradeManagementClusterAndWaitInput struct {
ClusterProxy framework.ClusterProxy
ClusterctlConfigPath string
Contract string
LogFolder string
}
// UpgradeManagementClusterAndWait upgrades provider a management cluster using clusterctl, and waits for the cluster to be ready.
func UpgradeManagementClusterAndWait(ctx context.Context, input UpgradeManagementClusterAndWaitInput, intervals ...interface{}) {
Expect(ctx).NotTo(BeNil(), "ctx is required for UpgradeManagementClusterAndWait")
Expect(input.ClusterProxy).ToNot(BeNil(), "Invalid argument. input.ClusterProxy can't be nil when calling UpgradeManagementClusterAndWait")
Expect(input.ClusterctlConfigPath).To(BeAnExistingFile(), "Invalid argument. input.ClusterctlConfigPath must be an existing file when calling UpgradeManagementClusterAndWait")
Expect(input.Contract).ToNot(BeEmpty(), "Invalid argument. input.Contract can't be empty when calling UpgradeManagementClusterAndWait")
Expect(os.MkdirAll(input.LogFolder, 0750)).To(Succeed(), "Invalid argument. input.LogFolder can't be created for UpgradeManagementClusterAndWait")
Upgrade(ctx, UpgradeInput{
ClusterctlConfigPath: input.ClusterctlConfigPath,
KubeconfigPath: input.ClusterProxy.GetKubeconfigPath(),
Contract: input.Contract,
LogFolder: input.LogFolder,
})
client := input.ClusterProxy.GetClient()
log.Logf("Waiting for provider controllers to be running")
controllersDeployments := framework.GetControllerDeployments(ctx, framework.GetControllerDeploymentsInput{
Lister: client,
ExcludeNamespaces: []string{"capi-webhook-system"}, // this namespace has been dropped in v1alpha4; this ensures we are not waiting for deployments being deleted as part of the upgrade process
})
Expect(controllersDeployments).ToNot(BeEmpty(), "The list of controller deployments should not be empty")
for _, deployment := range controllersDeployments {
framework.WaitForDeploymentsAvailable(ctx, framework.WaitForDeploymentsAvailableInput{
Getter: client,
Deployment: deployment,
GetLister: client,
ClientSet: input.ClusterProxy.GetClientSet(),
LogPath: filepath.Join(input.LogFolder, "controllers"),
}, intervals...)
// Start streaming logs from all controller providers
framework.WatchDeploymentLogs(ctx, framework.WatchDeploymentLogsInput{
GetLister: client,
ClientSet: input.ClusterProxy.GetClientSet(),
Deployment: deployment,
LogPath: filepath.Join(input.LogFolder, "controllers"),
})
framework.WatchPodMetrics(ctx, framework.WatchPodMetricsInput{
GetLister: client,
ClientSet: input.ClusterProxy.GetClientSet(),
Deployment: deployment,
MetricsPath: filepath.Join(input.LogFolder, "controllers"),
})
}
}
// ApplyClusterTemplateAndWaitInput is the input type for ApplyClusterTemplateAndWait.
type ApplyClusterTemplateAndWaitInput struct {
ClusterProxy framework.ClusterProxy
ConfigCluster ConfigClusterInput
CNIManifestPath string
WaitForClusterIntervals []interface{}
WaitForControlPlaneIntervals []interface{}
WaitForMachineDeployments []interface{}
WaitForMachinePools []interface{}
Args []string // extra args to be used during `kubectl apply`
ControlPlaneWaiters
}
// Waiter is a function that runs and waits for a long running operation to finish and updates the result.
type Waiter func(ctx context.Context, input ApplyClusterTemplateAndWaitInput, result *ApplyClusterTemplateAndWaitResult)
// ControlPlaneWaiters are Waiter functions for the control plane.
type ControlPlaneWaiters struct {
WaitForControlPlaneInitialized Waiter
WaitForControlPlaneMachinesReady Waiter
}
// ApplyClusterTemplateAndWaitResult is the output type for ApplyClusterTemplateAndWait.
type ApplyClusterTemplateAndWaitResult struct {
Cluster *clusterv1.Cluster
ControlPlane *controlplanev1.KubeadmControlPlane
MachineDeployments []*clusterv1.MachineDeployment
MachinePools []*clusterv1exp.MachinePool
}
// ExpectedWorkerNodes returns the expected number of worker nodes that will
// be provisioned by the given cluster template.
func (r *ApplyClusterTemplateAndWaitResult) ExpectedWorkerNodes() int32 {
expectedWorkerNodes := int32(0)
for _, md := range r.MachineDeployments {
if md.Spec.Replicas != nil {
expectedWorkerNodes += *md.Spec.Replicas
}
}
for _, mp := range r.MachinePools {
if mp.Spec.Replicas != nil {
expectedWorkerNodes += *mp.Spec.Replicas
}
}
return expectedWorkerNodes
}
// ExpectedTotalNodes returns the expected number of nodes that will
// be provisioned by the given cluster template.
func (r *ApplyClusterTemplateAndWaitResult) ExpectedTotalNodes() int32 {
expectedNodes := r.ExpectedWorkerNodes()
if r.ControlPlane != nil && r.ControlPlane.Spec.Replicas != nil {
expectedNodes += *r.ControlPlane.Spec.Replicas
}
return expectedNodes
}
// ApplyClusterTemplateAndWait gets a cluster template using clusterctl, and waits for the cluster to be ready.
// Important! this method assumes the cluster uses a KubeadmControlPlane and MachineDeployments.
func ApplyClusterTemplateAndWait(ctx context.Context, input ApplyClusterTemplateAndWaitInput, result *ApplyClusterTemplateAndWaitResult) {
setDefaults(&input)
Expect(ctx).NotTo(BeNil(), "ctx is required for ApplyClusterTemplateAndWait")
Expect(input.ClusterProxy).ToNot(BeNil(), "Invalid argument. input.ClusterProxy can't be nil when calling ApplyClusterTemplateAndWait")
Expect(result).ToNot(BeNil(), "Invalid argument. result can't be nil when calling ApplyClusterTemplateAndWait")
Expect(input.ConfigCluster.ControlPlaneMachineCount).ToNot(BeNil())
Expect(input.ConfigCluster.WorkerMachineCount).ToNot(BeNil())
log.Logf("Creating the workload cluster with name %q using the %q template (Kubernetes %s, %d control-plane machines, %d worker machines)",
input.ConfigCluster.ClusterName, valueOrDefault(input.ConfigCluster.Flavor), input.ConfigCluster.KubernetesVersion, *input.ConfigCluster.ControlPlaneMachineCount, *input.ConfigCluster.WorkerMachineCount)
log.Logf("Getting the cluster template yaml")
workloadClusterTemplate := ConfigCluster(ctx, ConfigClusterInput{
// pass reference to the management cluster hosting this test
KubeconfigPath: input.ConfigCluster.KubeconfigPath,
// pass the clusterctl config file that points to the local provider repository created for this test,
ClusterctlConfigPath: input.ConfigCluster.ClusterctlConfigPath,
// select template
Flavor: input.ConfigCluster.Flavor,
// define template variables
Namespace: input.ConfigCluster.Namespace,
ClusterName: input.ConfigCluster.ClusterName,
KubernetesVersion: input.ConfigCluster.KubernetesVersion,
ControlPlaneMachineCount: input.ConfigCluster.ControlPlaneMachineCount,
WorkerMachineCount: input.ConfigCluster.WorkerMachineCount,
InfrastructureProvider: input.ConfigCluster.InfrastructureProvider,
// setup clusterctl logs folder
LogFolder: input.ConfigCluster.LogFolder,
})
Expect(workloadClusterTemplate).ToNot(BeNil(), "Failed to get the cluster template")
log.Logf("Applying the cluster template yaml to the cluster")
Expect(input.ClusterProxy.Apply(ctx, workloadClusterTemplate, input.Args...)).To(Succeed())
log.Logf("Waiting for the cluster infrastructure to be provisioned")
result.Cluster = framework.DiscoveryAndWaitForCluster(ctx, framework.DiscoveryAndWaitForClusterInput{
Getter: input.ClusterProxy.GetClient(),
Namespace: input.ConfigCluster.Namespace,
Name: input.ConfigCluster.ClusterName,
}, input.WaitForClusterIntervals...)
log.Logf("Waiting for control plane to be initialized")
input.WaitForControlPlaneInitialized(ctx, input, result)
if input.CNIManifestPath != "" {
log.Logf("Installing a CNI plugin to the workload cluster")
workloadCluster := input.ClusterProxy.GetWorkloadCluster(ctx, result.Cluster.Namespace, result.Cluster.Name)
cniYaml, err := os.ReadFile(input.CNIManifestPath)
Expect(err).ShouldNot(HaveOccurred())
Expect(workloadCluster.Apply(ctx, cniYaml)).ShouldNot(HaveOccurred())
}
log.Logf("Waiting for control plane to be ready")
input.WaitForControlPlaneMachinesReady(ctx, input, result)
log.Logf("Waiting for the machine deployments to be provisioned")
result.MachineDeployments = framework.DiscoveryAndWaitForMachineDeployments(ctx, framework.DiscoveryAndWaitForMachineDeploymentsInput{
Lister: input.ClusterProxy.GetClient(),
Cluster: result.Cluster,
}, input.WaitForMachineDeployments...)
log.Logf("Waiting for the machine pools to be provisioned")
result.MachinePools = framework.DiscoveryAndWaitForMachinePools(ctx, framework.DiscoveryAndWaitForMachinePoolsInput{
Getter: input.ClusterProxy.GetClient(),
Lister: input.ClusterProxy.GetClient(),
Cluster: result.Cluster,
}, input.WaitForMachineDeployments...)
}
// setDefaults sets the default values for ApplyClusterTemplateAndWaitInput if not set.
// Currently, we set the default ControlPlaneWaiters here, which are implemented for KubeadmControlPlane.
func setDefaults(input *ApplyClusterTemplateAndWaitInput) {
if input.WaitForControlPlaneInitialized == nil {
input.WaitForControlPlaneInitialized = func(ctx context.Context, input ApplyClusterTemplateAndWaitInput, result *ApplyClusterTemplateAndWaitResult) {
result.ControlPlane = framework.DiscoveryAndWaitForControlPlaneInitialized(ctx, framework.DiscoveryAndWaitForControlPlaneInitializedInput{
Lister: input.ClusterProxy.GetClient(),
Cluster: result.Cluster,
}, input.WaitForControlPlaneIntervals...)
}
}
if input.WaitForControlPlaneMachinesReady == nil {
input.WaitForControlPlaneMachinesReady = func(ctx context.Context, input ApplyClusterTemplateAndWaitInput, result *ApplyClusterTemplateAndWaitResult) {
framework.WaitForControlPlaneAndMachinesReady(ctx, framework.WaitForControlPlaneAndMachinesReadyInput{
GetLister: input.ClusterProxy.GetClient(),
Cluster: result.Cluster,
ControlPlane: result.ControlPlane,
}, input.WaitForControlPlaneIntervals...)
}
}
}