Skip to content

Commit

Permalink
test: Ensure finalizers are re-reconciled
Browse files Browse the repository at this point in the history
  • Loading branch information
adityabhatia committed Jan 17, 2024
1 parent b9b2c22 commit 1c696df
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions test/e2e/finalizers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright 2024 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 e2e

import (
"context"
"fmt"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
capi_e2e "sigs.k8s.io/cluster-api/test/e2e"
"sigs.k8s.io/cluster-api/test/framework"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
)

var _ = Describe("Finalizers checks with ClusterIdentity", func() {
// Before running the test create the secret used by the VSphereClusterIdentity to connect to the vCenter.
BeforeEach(func() {
createVsphereIdentitySecret(ctx, bootstrapClusterProxy)
})

capi_e2e.QuickStartSpec(ctx, func() capi_e2e.QuickStartSpecInput {
return capi_e2e.QuickStartSpecInput{
E2EConfig: e2eConfig,
ClusterctlConfigPath: clusterctlConfigPath,
BootstrapClusterProxy: bootstrapClusterProxy,
ArtifactFolder: artifactFolder,
SkipCleanup: skipCleanup,
Flavor: ptr.To("finalizers"),
PostMachinesProvisioned: func(proxy framework.ClusterProxy, namespace, clusterName string) {
// Set up a periodic patch to ensure the DeploymentZone is reconciled.
reconcileDeploymentZonePeriodically(ctx, proxy.GetClient())

// This check ensures that finalizers are resilient - i.e. correctly re-reconciled, when removed.
framework.ValidateFinalizersResilience(ctx, proxy, namespace, clusterName,
framework.CoreFinalizersAssertion,
framework.KubeadmControlPlaneFinalizersAssertion,
framework.ExpFinalizersAssertion,
VSphereFinalizers,
)
},
}
})

// Delete objects created by the test which are not in the test namespace.
AfterEach(func() {
cleanupVSphereObjects(ctx, bootstrapClusterProxy)
})

})

// reconcileDeploymentZonePeriodically forces the vSphereDeploymentZone to reconcile every 20 seconds.
// This reduces the chance of race conditions resulting in flakes in the test.
func reconcileDeploymentZonePeriodically(ctx context.Context, c ctrlclient.Client) {
deploymentZoneList := &infrav1.VSphereDeploymentZoneList{}
ticker := time.NewTicker(20 * time.Second)
stopTimer := time.NewTimer(5 * time.Minute)
go func() {
defer GinkgoRecover()
for {
select {
case <-ticker.C:
Expect(c.List(ctx, deploymentZoneList)).To(Succeed())
for _, zone := range deploymentZoneList.Items {
annotationPatch := ctrlclient.RawPatch(types.MergePatchType, []byte(fmt.Sprintf("{\"metadata\":{\"annotations\":{\"cluster.x-k8s.io/modifiedAt\":\"%v\"}}}", time.Now().Format(time.RFC3339))))
Expect(c.Patch(ctx, zone.DeepCopy(), annotationPatch)).To(Succeed())
}
case <-stopTimer.C:
ticker.Stop()
return
case <-ctx.Done():
ticker.Stop()
return
}
}
}()
}

var VSphereFinalizers = map[string][]string{
"VMFinalizer": {infrav1.VMFinalizer},
"SecretIdentitySetFinalizer": {infrav1.SecretIdentitySetFinalizer},
"VSphereClusterIdentityFinalizer": {infrav1.VSphereClusterIdentityFinalizer},
"DeploymentZoneFinalizer": {infrav1.DeploymentZoneFinalizer},
"MachineFinalizer": {infrav1.MachineFinalizer},
"IPAddressClaimFinalizer": {infrav1.IPAddressClaimFinalizer},
"ClusterFinalizer": {infrav1.ClusterFinalizer},
}

0 comments on commit 1c696df

Please sign in to comment.