-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathdeprecated.go
270 lines (232 loc) · 9.26 KB
/
deprecated.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
/*
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"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
cabpkv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3"
"k8s.io/apimachinery/pkg/runtime"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha3"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// eventuallyInterval is the polling interval used by gomega's Eventually
// Deprecated
eventuallyInterval = 10 * time.Second
)
// MachineDeployment contains the objects needed to create a
// CAPI MachineDeployment resource and its associated template
// resources.
// Deprecated. Please use the individual create/assert methods.
type MachineDeployment struct {
MachineDeployment *clusterv1.MachineDeployment
BootstrapConfigTemplate runtime.Object
InfraMachineTemplate runtime.Object
}
// Node contains all the pieces necessary to make a single node
// Deprecated.
type Node struct {
Machine *clusterv1.Machine
InfraMachine runtime.Object
BootstrapConfig runtime.Object
}
// ControlplaneClusterInput defines the necessary dependencies to run a multi-node control plane cluster.
// Deprecated.
type ControlplaneClusterInput struct {
Management ManagementCluster
Cluster *clusterv1.Cluster
InfraCluster runtime.Object
Nodes []Node
MachineDeployment MachineDeployment
RelatedResources []runtime.Object
CreateTimeout time.Duration
DeleteTimeout time.Duration
ControlPlane *controlplanev1.KubeadmControlPlane
MachineTemplate runtime.Object
}
// SetDefaults defaults the struct fields if necessary.
// Deprecated.
func (input *ControlplaneClusterInput) SetDefaults() {
if input.CreateTimeout == 0 {
input.CreateTimeout = 10 * time.Minute
}
if input.DeleteTimeout == 0 {
input.DeleteTimeout = 5 * time.Minute
}
}
// ControlPlaneCluster creates an n node control plane cluster.
// Assertions:
// * The number of nodes in the created cluster will equal the number
// of control plane nodes plus the number of replicas in the machine
// deployment.
// Deprecated. Please use the supplied functions below to get the exact behavior desired.
func (input *ControlplaneClusterInput) ControlPlaneCluster() {
ctx := context.Background()
Expect(input.Management).ToNot(BeNil())
mgmtClient, err := input.Management.GetClient()
Expect(err).NotTo(HaveOccurred(), "stack: %+v", err)
By("creating an InfrastructureCluster resource")
Expect(mgmtClient.Create(ctx, input.InfraCluster)).To(Succeed())
// This call happens in an eventually because of a race condition with the
// webhook server. If the latter isn't fully online then this call will
// fail.
By("creating a Cluster resource linked to the InfrastructureCluster resource")
Eventually(func() error {
if err := mgmtClient.Create(ctx, input.Cluster); err != nil {
fmt.Printf("%+v\n", err)
return err
}
return nil
}, input.CreateTimeout, eventuallyInterval).Should(BeNil())
By("creating related resources")
for i := range input.RelatedResources {
obj := input.RelatedResources[i]
By(fmt.Sprintf("creating a/an %s resource", obj.GetObjectKind().GroupVersionKind()))
Eventually(func() error {
return mgmtClient.Create(ctx, obj)
}, input.CreateTimeout, eventuallyInterval).Should(BeNil())
}
By("creating the machine template")
Expect(mgmtClient.Create(ctx, input.MachineTemplate)).To(Succeed())
By("creating a KubeadmControlPlane")
Eventually(func() error {
err := mgmtClient.Create(ctx, input.ControlPlane)
if err != nil {
fmt.Println(err)
}
return err
}, input.CreateTimeout, 10*time.Second).Should(BeNil())
By("waiting for cluster to enter the provisioned phase")
Eventually(func() (string, error) {
cluster := &clusterv1.Cluster{}
key := client.ObjectKey{
Namespace: input.Cluster.GetNamespace(),
Name: input.Cluster.GetName(),
}
if err := mgmtClient.Get(ctx, key, cluster); err != nil {
return "", err
}
return cluster.Status.Phase, nil
}, input.CreateTimeout, eventuallyInterval).Should(Equal(string(clusterv1.ClusterPhaseProvisioned)))
// Create the machine deployment if the replica count >0.
if machineDeployment := input.MachineDeployment.MachineDeployment; machineDeployment != nil {
if replicas := machineDeployment.Spec.Replicas; replicas != nil && *replicas > 0 {
By("creating a core MachineDeployment resource")
Expect(mgmtClient.Create(ctx, machineDeployment)).To(Succeed())
By("creating a BootstrapConfigTemplate resource")
Expect(mgmtClient.Create(ctx, input.MachineDeployment.BootstrapConfigTemplate)).To(Succeed())
By("creating an InfrastructureMachineTemplate resource")
Expect(mgmtClient.Create(ctx, input.MachineDeployment.InfraMachineTemplate)).To(Succeed())
}
By("waiting for the workload nodes to exist")
Eventually(func() ([]v1.Node, error) {
workloadClient, err := input.Management.GetWorkloadClient(ctx, input.Cluster.Namespace, input.Cluster.Name)
if err != nil {
return nil, errors.Wrap(err, "failed to get workload client")
}
nodeList := v1.NodeList{}
if err := workloadClient.List(ctx, &nodeList); err != nil {
return nil, err
}
return nodeList.Items, nil
}, input.CreateTimeout, 10*time.Second).Should(HaveLen(int(*machineDeployment.Spec.Replicas)))
}
By("waiting for all machines to be running")
inClustersNamespaceListOption := client.InNamespace(input.Cluster.Namespace)
matchClusterListOption := client.MatchingLabels{clusterv1.ClusterLabelName: input.Cluster.Name}
Eventually(func() (bool, error) {
// Get a list of all the Machine resources that belong to the Cluster.
machineList := &clusterv1.MachineList{}
if err := mgmtClient.List(ctx, machineList, inClustersNamespaceListOption, matchClusterListOption); err != nil {
return false, err
}
for _, machine := range machineList.Items {
if machine.Status.Phase != string(clusterv1.MachinePhaseRunning) {
return false, errors.Errorf("machine %s is not running, it's %s", machine.Name, machine.Status.Phase)
}
}
return true, nil
}, input.CreateTimeout, eventuallyInterval).Should(BeTrue())
// wait for the control plane to be ready
By("waiting for the control plane to be ready")
Eventually(func() bool {
controlplane := &controlplanev1.KubeadmControlPlane{}
key := client.ObjectKey{
Namespace: input.ControlPlane.GetNamespace(),
Name: input.ControlPlane.GetName(),
}
if err := mgmtClient.Get(ctx, key, controlplane); err != nil {
fmt.Println(err.Error())
return false
}
return controlplane.Status.Initialized
}, input.CreateTimeout, 10*time.Second).Should(BeTrue())
}
// CleanUpCoreArtifacts deletes the cluster and waits for everything to be gone.
// Assertions made on objects owned by the Cluster:
// * All Machines are removed
// * All MachineSets are removed
// * All MachineDeployments are removed
// * All KubeadmConfigs are removed
// * All Secrets are removed
// Deprecated
func (input *ControlplaneClusterInput) CleanUpCoreArtifacts() {
input.SetDefaults()
ctx := context.Background()
mgmtClient, err := input.Management.GetClient()
Expect(err).NotTo(HaveOccurred(), "stack: %+v", err)
By(fmt.Sprintf("deleting cluster %s", input.Cluster.GetName()))
Expect(mgmtClient.Delete(ctx, input.Cluster)).To(Succeed())
Eventually(func() bool {
clusters := clusterv1.ClusterList{}
if err := mgmtClient.List(ctx, &clusters); err != nil {
fmt.Println(err.Error())
return false
}
return len(clusters.Items) == 0
}, input.DeleteTimeout, eventuallyInterval).Should(BeTrue())
lbl, err := labels.Parse(fmt.Sprintf("%s=%s", clusterv1.ClusterLabelName, input.Cluster.GetClusterName()))
Expect(err).ToNot(HaveOccurred())
listOpts := &client.ListOptions{LabelSelector: lbl}
By("ensuring all CAPI artifacts have been deleted")
ensureArtifactsDeleted(ctx, mgmtClient, listOpts)
}
// Deprecated
func ensureArtifactsDeleted(ctx context.Context, mgmtClient Lister, opt client.ListOption) {
// assertions
ml := &clusterv1.MachineList{}
Expect(mgmtClient.List(ctx, ml, opt)).To(Succeed())
Expect(ml.Items).To(HaveLen(0))
msl := &clusterv1.MachineSetList{}
Expect(mgmtClient.List(ctx, msl, opt)).To(Succeed())
Expect(msl.Items).To(HaveLen(0))
mdl := &clusterv1.MachineDeploymentList{}
Expect(mgmtClient.List(ctx, mdl, opt)).To(Succeed())
Expect(mdl.Items).To(HaveLen(0))
kcpl := &controlplanev1.KubeadmControlPlaneList{}
Expect(mgmtClient.List(ctx, kcpl, opt)).To(Succeed())
Expect(kcpl.Items).To(HaveLen(0))
kcl := &cabpkv1.KubeadmConfigList{}
Expect(mgmtClient.List(ctx, kcl, opt)).To(Succeed())
Expect(kcl.Items).To(HaveLen(0))
sl := &v1.SecretList{}
Expect(mgmtClient.List(ctx, sl, opt)).To(Succeed())
Expect(sl.Items).To(HaveLen(0))
}