/
cluster_helpers.go
256 lines (219 loc) · 9.11 KB
/
cluster_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
/*
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"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/cluster-api/test/framework/internal/log"
"sigs.k8s.io/cluster-api/test/framework/options"
"sigs.k8s.io/controller-runtime/pkg/client"
"k8s.io/apimachinery/pkg/runtime"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
)
// CreateClusterInput is the input for CreateCluster.
type CreateClusterInput struct {
Creator Creator
Cluster *clusterv1.Cluster
InfraCluster runtime.Object
}
// CreateCluster will create the Cluster and InfraCluster objects.
func CreateCluster(ctx context.Context, input CreateClusterInput, intervals ...interface{}) {
By("creating an InfrastructureCluster resource")
Expect(input.Creator.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 := input.Creator.Create(ctx, input.Cluster); err != nil {
log.Logf("Failed to create a cluster: %+v", err)
return err
}
return nil
}, intervals...).Should(Succeed())
}
// GetAllClustersByNamespaceInput is the input for GetAllClustersByNamespace.
type GetAllClustersByNamespaceInput struct {
Lister Lister
Namespace string
}
// GetAllClustersByNamespace returns the list of Cluster object in a namespace
func GetAllClustersByNamespace(ctx context.Context, input GetAllClustersByNamespaceInput) []*clusterv1.Cluster {
clusterList := &clusterv1.ClusterList{}
Expect(input.Lister.List(ctx, clusterList, client.InNamespace(input.Namespace))).To(Succeed(), "Failed to list clusters in namespace %s", input.Namespace)
clusters := make([]*clusterv1.Cluster, len(clusterList.Items))
for i := range clusterList.Items {
clusters[i] = &clusterList.Items[i]
}
return clusters
}
// GetClusterByNameInput is the input for GetClusterByName.
type GetClusterByNameInput struct {
Getter Getter
Name string
Namespace string
}
// GetClusterByName returns a Cluster object given his name
func GetClusterByName(ctx context.Context, input GetClusterByNameInput) *clusterv1.Cluster {
cluster := &clusterv1.Cluster{}
key := client.ObjectKey{
Namespace: input.Namespace,
Name: input.Name,
}
Expect(input.Getter.Get(ctx, key, cluster)).To(Succeed(), "Failed to get Cluster object %s/%s", input.Namespace, input.Name)
return cluster
}
// WaitForClusterToProvisionInput is the input for WaitForClusterToProvision.
type WaitForClusterToProvisionInput struct {
Getter Getter
Cluster *clusterv1.Cluster
}
// WaitForClusterToProvision will wait for a cluster to have a phase status of provisioned.
func WaitForClusterToProvision(ctx context.Context, input WaitForClusterToProvisionInput, intervals ...interface{}) {
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 := input.Getter.Get(ctx, key, cluster); err != nil {
return "", err
}
return cluster.Status.Phase, nil
}, intervals...).Should(Equal(string(clusterv1.ClusterPhaseProvisioned)))
}
// DeleteClusterInput is the input for DeleteCluster.
type DeleteClusterInput struct {
Deleter Deleter
Cluster *clusterv1.Cluster
}
// DeleteCluster deletes the cluster and waits for everything the cluster owned to actually be gone.
func DeleteCluster(ctx context.Context, input DeleteClusterInput) {
if options.SkipResourceCleanup {
return
}
By(fmt.Sprintf("deleting cluster %s", input.Cluster.GetName()))
Expect(input.Deleter.Delete(ctx, input.Cluster)).To(Succeed())
}
// WaitForClusterDeletedInput is the input for WaitForClusterDeleted.
type WaitForClusterDeletedInput struct {
Getter Getter
Cluster *clusterv1.Cluster
}
// WaitForClusterDeleted waits until the cluster object has been deleted.
func WaitForClusterDeleted(ctx context.Context, input WaitForClusterDeletedInput, intervals ...interface{}) {
if options.SkipResourceCleanup {
return
}
By(fmt.Sprintf("waiting for cluster %s to be deleted", input.Cluster.GetName()))
Eventually(func() bool {
cluster := &clusterv1.Cluster{}
key := client.ObjectKey{
Namespace: input.Cluster.GetNamespace(),
Name: input.Cluster.GetName(),
}
return apierrors.IsNotFound(input.Getter.Get(ctx, key, cluster))
}, intervals...).Should(BeTrue())
}
// DiscoveryAndWaitForClusterInput is the input type for DiscoveryAndWaitForCluster.
type DiscoveryAndWaitForClusterInput struct {
Getter Getter
Namespace string
Name string
}
// DiscoveryAndWaitForCluster discovers a cluster object in a namespace and waits for the cluster infrastructure to be provisioned.
func DiscoveryAndWaitForCluster(ctx context.Context, input DiscoveryAndWaitForClusterInput, intervals ...interface{}) *clusterv1.Cluster {
Expect(ctx).NotTo(BeNil(), "ctx is required for DiscoveryAndWaitForCluster")
Expect(input.Getter).ToNot(BeNil(), "Invalid argument. input.Getter can't be nil when calling DiscoveryAndWaitForCluster")
Expect(input.Namespace).ToNot(BeNil(), "Invalid argument. input.Namespace can't be empty when calling DiscoveryAndWaitForCluster")
Expect(input.Name).ToNot(BeNil(), "Invalid argument. input.Name can't be empty when calling DiscoveryAndWaitForCluster")
cluster := GetClusterByName(ctx, GetClusterByNameInput{
Getter: input.Getter,
Name: input.Name,
Namespace: input.Namespace,
})
Expect(cluster).ToNot(BeNil(), "Failed to get the Cluster object")
WaitForClusterToProvision(ctx, WaitForClusterToProvisionInput{
Getter: input.Getter,
Cluster: cluster,
}, intervals...)
return cluster
}
// DeleteClusterAndWaitInput is the input type for DeleteClusterAndWait.
type DeleteClusterAndWaitInput struct {
Client client.Client
Cluster *clusterv1.Cluster
}
// DeleteClusterAndWait deletes a cluster object and waits for it to be gone.
func DeleteClusterAndWait(ctx context.Context, input DeleteClusterAndWaitInput, intervals ...interface{}) {
Expect(ctx).NotTo(BeNil(), "ctx is required for DeleteClusterAndWait")
Expect(input.Client).ToNot(BeNil(), "Invalid argument. input.Client can't be nil when calling DeleteClusterAndWait")
Expect(input.Cluster).ToNot(BeNil(), "Invalid argument. input.Cluster can't be nil when calling DeleteClusterAndWait")
DeleteCluster(ctx, DeleteClusterInput{
Deleter: input.Client,
Cluster: input.Cluster,
})
log.Logf("Waiting for the Cluster object to be deleted")
WaitForClusterDeleted(ctx, WaitForClusterDeletedInput{
Getter: input.Client,
Cluster: input.Cluster,
}, intervals...)
//TODO: consider if to move in another func (what if there are more than one cluster?)
log.Logf("Check for all the Cluster API resources being deleted")
resources := GetCAPIResources(ctx, GetCAPIResourcesInput{
Lister: input.Client,
Namespace: input.Cluster.Namespace,
})
Expect(resources).To(BeEmpty(), "There are still Cluster API resources in the %q namespace", input.Cluster.Namespace)
}
// DeleteAllClustersAndWaitInput is the input type for DeleteAllClustersAndWait.
type DeleteAllClustersAndWaitInput struct {
Client client.Client
Namespace string
}
// DeleteAllClustersAndWait deletes a cluster object and waits for it to be gone.
func DeleteAllClustersAndWait(ctx context.Context, input DeleteAllClustersAndWaitInput, intervals ...interface{}) {
Expect(ctx).NotTo(BeNil(), "ctx is required for DeleteAllClustersAndWait")
Expect(input.Client).ToNot(BeNil(), "Invalid argument. input.Client can't be nil when calling DeleteAllClustersAndWait")
Expect(input.Namespace).ToNot(BeEmpty(), "Invalid argument. input.Namespace can't be empty when calling DeleteAllClustersAndWait")
clusters := GetAllClustersByNamespace(ctx, GetAllClustersByNamespaceInput{
Lister: input.Client,
Namespace: input.Namespace,
})
for _, c := range clusters {
DeleteCluster(ctx, DeleteClusterInput{
Deleter: input.Client,
Cluster: c,
})
}
for _, c := range clusters {
log.Logf("Waiting for the Cluster %s/%s to be deleted", c.Namespace, c.Name)
WaitForClusterDeleted(ctx, WaitForClusterDeletedInput{
Getter: input.Client,
Cluster: c,
}, intervals...)
}
}
// byClusterOptions returns a set of ListOptions that allows to identify all the objects belonging to a Cluster.
func byClusterOptions(name, namespace string) []client.ListOption {
return []client.ListOption{
client.InNamespace(namespace),
client.MatchingLabels{
clusterv1.ClusterLabelName: name,
},
}
}