Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Fix cluster pause webhook to set the temporary pause annotation on cl…
Browse files Browse the repository at this point in the history
…uster's TKR label change (#2514)

* Fix cluster pause webhook to set the temporary pause annotation on cluster's TKR label change

Signed-off-by: Marjan Alavi <malavi@vmware.com>

* Move cluster pause webhooks under addons
  • Loading branch information
maralavi authored Jun 9, 2022
1 parent fbc30e8 commit 0c20ca1
Showing 10 changed files with 592 additions and 41 deletions.
162 changes: 153 additions & 9 deletions addons/controllers/cluster_pause_webhook_test.go
Original file line number Diff line number Diff line change
@@ -9,15 +9,19 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

clusterapiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/vmware-tanzu/tanzu-framework/addons/test/testutil"
"github.com/vmware-tanzu/tanzu-framework/apis/run/v1alpha3"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/constants"
)

const (
clusterpPauseWebhookManifestFile = "testdata/webhooks/cluster-pause-webhook-manifests.yaml"
testClusterName = "pause-test-cluster"
)

var _ = Describe("when cluster paused state is managed by webhook", func() {
@@ -40,20 +44,58 @@ var _ = Describe("when cluster paused state is managed by webhook", func() {
// Create the webhooks
f, err := os.Open(clusterpPauseWebhookManifestFile)
Expect(err).ToNot(HaveOccurred())
err = testutil.DeleteResources(f, cfg, dynamicClient, false)
err = testutil.DeleteResources(f, cfg, dynamicClient, true)
Expect(err).ToNot(HaveOccurred())
f.Close()

err = k8sClient.Delete(ctx, &clusterapiv1beta1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: testClusterName, Namespace: addonNamespace},
})
Expect(err).ToNot(HaveOccurred())
})

Context("if cluster topology version changes", func() {
Context("if the value of the cluster's TKR label changes", func() {
It("webhook should set pause state in cluster object", func() {
// Create a cluster object
cluster := &clusterapiv1beta1.Cluster{}
cluster.Name = "pause-test-cluster"
cluster.Name = testClusterName
cluster.Namespace = addonNamespace
cluster.Spec.Topology = &clusterapiv1beta1.Topology{
Version: "v1.19.3---vmware.1",
cluster.Labels = map[string]string{v1alpha3.LabelTKR: "v1.19.3---vmware.1"}
err := k8sClient.Create(ctx, cluster)
Expect(err).ToNot(HaveOccurred())

// It shouldn't be paused
key := client.ObjectKey{
Namespace: cluster.Namespace,
Name: cluster.Name,
}
err = k8sClient.Get(ctx, key, cluster)
cluster = cluster.DeepCopy()
Expect(err).ToNot(HaveOccurred())
if cluster.Annotations != nil {
_, ok := cluster.Annotations[constants.ClusterPauseLabel]
Expect(ok).ToNot(BeTrue())
}
Expect(cluster.Spec.Paused).ToNot(BeTrue())

// Update cluster version
cluster.Labels = map[string]string{v1alpha3.LabelTKR: "v1.20.5---vmware.1"}
err = k8sClient.Update(ctx, cluster)
Expect(err).ToNot(HaveOccurred())

// Cluster should be paused
Expect(cluster.Annotations).ToNot(BeNil())
Expect(cluster.Annotations[constants.ClusterPauseLabel]).To(Equal(cluster.Labels[v1alpha3.LabelTKR]))
Expect(cluster.Spec.Paused).To(BeTrue())
})
})

Context("if previously the cluster did not have any label and during an update, TKR label is set", func() {
It("webhook should set pause state in the cluster object", func() {
// Create a cluster object
cluster := &clusterapiv1beta1.Cluster{}
cluster.Name = testClusterName
cluster.Namespace = addonNamespace
err := k8sClient.Create(ctx, cluster)
Expect(err).ToNot(HaveOccurred())

@@ -72,16 +114,118 @@ var _ = Describe("when cluster paused state is managed by webhook", func() {
Expect(cluster.Spec.Paused).ToNot(BeTrue())

// Update cluster version
cluster.Spec.Topology = &clusterapiv1beta1.Topology{
Version: "v1.20.5---vmware.1",
cluster.Labels = map[string]string{v1alpha3.LabelTKR: "v1.19.3---vmware.1"}
err = k8sClient.Update(ctx, cluster)
Expect(err).ToNot(HaveOccurred())

Expect(cluster.Annotations).ToNot(BeNil())
Expect(cluster.Annotations[constants.ClusterPauseLabel]).To(Equal(cluster.Labels[v1alpha3.LabelTKR]))
Expect(cluster.Spec.Paused).To(BeTrue())
})
})

Context("if previously the cluster did not have a TKR label and during an update, TKR label is set", func() {
It("webhook should set pause state in the cluster object", func() {
// Create a cluster object
cluster := &clusterapiv1beta1.Cluster{}
cluster.Name = testClusterName
cluster.Namespace = addonNamespace
cluster.Labels = map[string]string{"someLabel": "v1.19.3---vmware.1"}
err := k8sClient.Create(ctx, cluster)
Expect(err).ToNot(HaveOccurred())

// It shouldn't be paused
key := client.ObjectKey{
Namespace: cluster.Namespace,
Name: cluster.Name,
}
err = k8sClient.Get(ctx, key, cluster)
cluster = cluster.DeepCopy()
Expect(err).ToNot(HaveOccurred())
if cluster.Annotations != nil {
_, ok := cluster.Annotations[constants.ClusterPauseLabel]
Expect(ok).ToNot(BeTrue())
}
Expect(cluster.Spec.Paused).ToNot(BeTrue())

// Update cluster version
cluster.Labels = map[string]string{v1alpha3.LabelTKR: "v1.19.3---vmware.1"}
err = k8sClient.Update(ctx, cluster)
Expect(err).ToNot(HaveOccurred())

// Cluster should be paused
Expect(cluster.Annotations).ToNot(BeNil())
Expect(cluster.Annotations[constants.ClusterPauseLabel]).To(Equal(cluster.Spec.Topology.Version))
Expect(cluster.Annotations[constants.ClusterPauseLabel]).To(Equal(cluster.Labels[v1alpha3.LabelTKR]))
Expect(cluster.Spec.Paused).To(BeTrue())
})
})

Context("if no change in the value of the cluster's TKR label", func() {
It("webhook should not set pause state in the cluster object", func() {
// Create a cluster object
cluster := &clusterapiv1beta1.Cluster{}
cluster.Name = testClusterName
cluster.Namespace = addonNamespace
cluster.Labels = map[string]string{v1alpha3.LabelTKR: "v1.19.3---vmware.1"}
err := k8sClient.Create(ctx, cluster)
Expect(err).ToNot(HaveOccurred())

// It shouldn't be paused
key := client.ObjectKey{
Namespace: cluster.Namespace,
Name: cluster.Name,
}
err = k8sClient.Get(ctx, key, cluster)
cluster = cluster.DeepCopy()
Expect(err).ToNot(HaveOccurred())
if cluster.Annotations != nil {
_, ok := cluster.Annotations[constants.ClusterPauseLabel]
Expect(ok).ToNot(BeTrue())
}
Expect(cluster.Spec.Paused).ToNot(BeTrue())

// Update cluster version
cluster.Labels = map[string]string{v1alpha3.LabelTKR: "v1.19.3---vmware.1"}
err = k8sClient.Update(ctx, cluster)
Expect(err).ToNot(HaveOccurred())

// Cluster should not be paused
Expect(cluster.Spec.Paused).ToNot(BeTrue())
Expect(cluster.Annotations).To(BeNil())
})
})

Context("if cluster's TKR label is removed during update", func() {
It("webhook should not set pause state in the cluster object", func() {
// Create a cluster object
cluster := &clusterapiv1beta1.Cluster{}
cluster.Name = testClusterName
cluster.Namespace = addonNamespace
cluster.Labels = map[string]string{v1alpha3.LabelTKR: "v1.19.3---vmware.1"}
err := k8sClient.Create(ctx, cluster)
Expect(err).ToNot(HaveOccurred())

// It shouldn't be paused
key := client.ObjectKey{
Namespace: cluster.Namespace,
Name: cluster.Name,
}
err = k8sClient.Get(ctx, key, cluster)
cluster = cluster.DeepCopy()
Expect(err).ToNot(HaveOccurred())
if cluster.Annotations != nil {
_, ok := cluster.Annotations[constants.ClusterPauseLabel]
Expect(ok).ToNot(BeTrue())
}
Expect(cluster.Spec.Paused).ToNot(BeTrue())

// Update cluster version
cluster.Labels = map[string]string{}
err = k8sClient.Update(ctx, cluster)
Expect(err).ToNot(HaveOccurred())

// Cluster should not be paused
Expect(cluster.Spec.Paused).ToNot(BeTrue())
Expect(cluster.Annotations).To(BeNil())
})
})
})
2 changes: 1 addition & 1 deletion addons/controllers/suite_test.go
Original file line number Diff line number Diff line change
@@ -337,7 +337,7 @@ var _ = BeforeSuite(func(done Done) {
Expect(err).ToNot(HaveOccurred())
err = (&cniv1alpha1.CalicoConfig{}).SetupWebhookWithManager(mgr)
Expect(err).ToNot(HaveOccurred())
err = (&webhooks.ClusterPause{Client: k8sClient}).SetupWebhookWithManager(mgr)
err = (&addonwebhooks.ClusterPause{Client: k8sClient}).SetupWebhookWithManager(mgr)
Expect(err).ToNot(HaveOccurred())
clusterbootstrapWebhook := addonwebhooks.ClusterBootstrap{
Client: k8sClient,
Loading

0 comments on commit 0c20ca1

Please # to comment.