Skip to content

Commit 1c2d89c

Browse files
authored
Merge pull request #2966 from fabriziopandini/e2e-cluster-cleanup
🐛[e2e] add workaround for panic in high level helpers
2 parents 5a2b54e + 106c749 commit 1c2d89c

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

test/e2e/common.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ func dumpSpecResourcesAndCleanup(ctx context.Context, specName string, clusterPr
5656

5757
if !skipCleanup {
5858
Byf("Deleting cluster %s/%s", cluster.Namespace, cluster.Name)
59-
framework.DeleteClusterAndWait(ctx, framework.DeleteClusterAndWaitInput{
60-
Client: clusterProxy.GetClient(),
61-
Cluster: cluster,
59+
// While https://github.com/kubernetes-sigs/cluster-api/issues/2955 is addressed in future iterations, there is a chance
60+
// that cluster variable is not set even if the cluster exists, so we are calling DeleteAllClustersAndWait
61+
// instead of DeleteClusterAndWait
62+
framework.DeleteAllClustersAndWait(ctx, framework.DeleteAllClustersAndWaitInput{
63+
Client: clusterProxy.GetClient(),
64+
Namespace: namespace.Name,
6265
}, intervalsGetter(specName, "wait-delete-cluster")...)
6366

6467
Byf("Deleting namespace used for hosting the %q test spec", specName)

test/framework/cluster_helpers.go

+51-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
. "github.com/onsi/ginkgo"
2424
. "github.com/onsi/gomega"
25-
2625
apierrors "k8s.io/apimachinery/pkg/api/errors"
2726
"sigs.k8s.io/cluster-api/test/framework/options"
2827
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -56,6 +55,24 @@ func CreateCluster(ctx context.Context, input CreateClusterInput, intervals ...i
5655
}, intervals...).Should(Succeed())
5756
}
5857

58+
// GetAllClustersByNamespaceInput is the input for GetAllClustersByNamespace.
59+
type GetAllClustersByNamespaceInput struct {
60+
Lister Lister
61+
Namespace string
62+
}
63+
64+
// GetClustersByNamespace returns the list of Cluster object in a namespace
65+
func GetAllClustersByNamespace(ctx context.Context, input GetAllClustersByNamespaceInput) []*clusterv1.Cluster {
66+
clusterList := &clusterv1.ClusterList{}
67+
Expect(input.Lister.List(ctx, clusterList, client.InNamespace(input.Namespace))).To(Succeed(), "Failed to list clusters in namespace %s", input.Namespace)
68+
69+
clusters := make([]*clusterv1.Cluster, len(clusterList.Items))
70+
for i := range clusterList.Items {
71+
clusters[i] = &clusterList.Items[i]
72+
}
73+
return clusters
74+
}
75+
5976
// GetClusterByNameInput is the input for GetClusterByName.
6077
type GetClusterByNameInput struct {
6178
Getter Getter
@@ -194,6 +211,39 @@ func DeleteClusterAndWait(ctx context.Context, input DeleteClusterAndWaitInput,
194211
Expect(resources).To(BeEmpty(), "There are still Cluster API resources in the %q namespace", input.Cluster.Namespace)
195212
}
196213

214+
// DeleteAllClustersAndWaitInput is the input type for DeleteAllClustersAndWait.
215+
type DeleteAllClustersAndWaitInput struct {
216+
Client client.Client
217+
Namespace string
218+
}
219+
220+
// DeleteAllClustersAndWait deletes a cluster object and waits for it to be gone.
221+
func DeleteAllClustersAndWait(ctx context.Context, input DeleteAllClustersAndWaitInput, intervals ...interface{}) {
222+
Expect(ctx).NotTo(BeNil(), "ctx is required for DeleteAllClustersAndWait")
223+
Expect(input.Client).ToNot(BeNil(), "Invalid argument. input.Client can't be nil when calling DeleteAllClustersAndWait")
224+
Expect(input.Namespace).ToNot(BeEmpty(), "Invalid argument. input.Namespace can't be empty when calling DeleteAllClustersAndWait")
225+
226+
clusters := GetAllClustersByNamespace(ctx, GetAllClustersByNamespaceInput{
227+
Lister: input.Client,
228+
Namespace: input.Namespace,
229+
})
230+
231+
for _, c := range clusters {
232+
DeleteCluster(ctx, DeleteClusterInput{
233+
Deleter: input.Client,
234+
Cluster: c,
235+
})
236+
}
237+
238+
for _, c := range clusters {
239+
fmt.Fprintf(GinkgoWriter, "Waiting for the Cluster %s/%s to be deleted\n", c.Namespace, c.Name)
240+
WaitForClusterDeleted(ctx, WaitForClusterDeletedInput{
241+
Getter: input.Client,
242+
Cluster: c,
243+
}, intervals...)
244+
}
245+
}
246+
197247
// byClusterOptions returns a set of ListOptions that allows to identify all the objects belonging to a Cluster.
198248
func byClusterOptions(name, namespace string) []client.ListOption {
199249
return []client.ListOption{

0 commit comments

Comments
 (0)