|
| 1 | +/* |
| 2 | +Copyright 2024 The Karmada Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package e2e |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/onsi/ginkgo/v2" |
| 24 | + "github.com/onsi/gomega" |
| 25 | + "k8s.io/apimachinery/pkg/api/meta" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/util/rand" |
| 28 | + "k8s.io/apimachinery/pkg/util/sets" |
| 29 | + |
| 30 | + clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" |
| 31 | + remedyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/remedy/v1alpha1" |
| 32 | + "github.com/karmada-io/karmada/test/e2e/framework" |
| 33 | + karmadaresource "github.com/karmada-io/karmada/test/e2e/framework/resource/karmada" |
| 34 | +) |
| 35 | + |
| 36 | +var _ = framework.SerialDescribe("remedy testing", func() { |
| 37 | + var remedyName string |
| 38 | + var remedy *remedyv1alpha1.Remedy |
| 39 | + var targetCluster string |
| 40 | + |
| 41 | + ginkgo.Context("remedy.spec.decisionMatches is not empty", func() { |
| 42 | + ginkgo.BeforeEach(func() { |
| 43 | + targetCluster = framework.ClusterNames()[0] |
| 44 | + remedyName = remedyNamePrefix + rand.String(RandomStrLength) |
| 45 | + remedy = &remedyv1alpha1.Remedy{ |
| 46 | + ObjectMeta: metav1.ObjectMeta{Name: remedyName}, |
| 47 | + Spec: remedyv1alpha1.RemedySpec{ |
| 48 | + DecisionMatches: []remedyv1alpha1.DecisionMatch{ |
| 49 | + { |
| 50 | + ClusterConditionMatch: &remedyv1alpha1.ClusterConditionRequirement{ |
| 51 | + ConditionType: remedyv1alpha1.ServiceDomainNameResolutionReady, |
| 52 | + Operator: remedyv1alpha1.ClusterConditionEqual, |
| 53 | + ConditionStatus: string(metav1.ConditionFalse), |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + Actions: []remedyv1alpha1.RemedyAction{remedyv1alpha1.TrafficControl}, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + karmadaresource.CreateRemedy(karmadaClient, remedy) |
| 62 | + }) |
| 63 | + |
| 64 | + ginkgo.It("Cluster domain name resolution function encounters an exception and recover", func() { |
| 65 | + ginkgo.By(fmt.Sprintf("update Cluster(%s) %s condition to false", targetCluster, remedyv1alpha1.ServiceDomainNameResolutionReady), func() { |
| 66 | + clusterObj, err := karmadaClient.ClusterV1alpha1().Clusters().Get(context.TODO(), targetCluster, metav1.GetOptions{}) |
| 67 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 68 | + |
| 69 | + meta.SetStatusCondition(&clusterObj.Status.Conditions, metav1.Condition{ |
| 70 | + Type: string(remedyv1alpha1.ServiceDomainNameResolutionReady), |
| 71 | + Status: metav1.ConditionFalse, |
| 72 | + }) |
| 73 | + _, err = karmadaClient.ClusterV1alpha1().Clusters().UpdateStatus(context.TODO(), clusterObj, metav1.UpdateOptions{}) |
| 74 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 75 | + }) |
| 76 | + |
| 77 | + ginkgo.By("wait cluster status has TrafficControl RemedyAction", func() { |
| 78 | + framework.WaitClusterFitWith(controlPlaneClient, targetCluster, func(cluster *clusterv1alpha1.Cluster) bool { |
| 79 | + actions := sets.NewString(cluster.Status.RemedyActions...) |
| 80 | + return actions.Has(string(remedyv1alpha1.TrafficControl)) |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + ginkgo.By(fmt.Sprintf("recover Cluster(%s) %s condition to true", targetCluster, remedyv1alpha1.ServiceDomainNameResolutionReady), func() { |
| 85 | + clusterObj, err := karmadaClient.ClusterV1alpha1().Clusters().Get(context.TODO(), targetCluster, metav1.GetOptions{}) |
| 86 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 87 | + |
| 88 | + meta.SetStatusCondition(&clusterObj.Status.Conditions, metav1.Condition{ |
| 89 | + Type: string(remedyv1alpha1.ServiceDomainNameResolutionReady), |
| 90 | + Status: metav1.ConditionTrue, |
| 91 | + }) |
| 92 | + _, err = karmadaClient.ClusterV1alpha1().Clusters().UpdateStatus(context.TODO(), clusterObj, metav1.UpdateOptions{}) |
| 93 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 94 | + }) |
| 95 | + |
| 96 | + ginkgo.By("wait cluster status doesn't has TrafficControl RemedyAction", func() { |
| 97 | + framework.WaitClusterFitWith(controlPlaneClient, targetCluster, func(cluster *clusterv1alpha1.Cluster) bool { |
| 98 | + actions := sets.NewString(cluster.Status.RemedyActions...) |
| 99 | + return !actions.Has(string(remedyv1alpha1.TrafficControl)) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + ginkgo.By("cleanup: remove Remedy resource", func() { |
| 104 | + karmadaresource.RemoveRemedy(karmadaClient, remedyName) |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + ginkgo.It("Cluster domain name resolution function encounters an exception, then remove the remedy resource", func() { |
| 109 | + ginkgo.By(fmt.Sprintf("update Cluster(%s) %s condition to false", targetCluster, remedyv1alpha1.ServiceDomainNameResolutionReady), func() { |
| 110 | + clusterObj, err := karmadaClient.ClusterV1alpha1().Clusters().Get(context.TODO(), targetCluster, metav1.GetOptions{}) |
| 111 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 112 | + |
| 113 | + meta.SetStatusCondition(&clusterObj.Status.Conditions, metav1.Condition{ |
| 114 | + Type: string(remedyv1alpha1.ServiceDomainNameResolutionReady), |
| 115 | + Status: metav1.ConditionFalse, |
| 116 | + }) |
| 117 | + _, err = karmadaClient.ClusterV1alpha1().Clusters().UpdateStatus(context.TODO(), clusterObj, metav1.UpdateOptions{}) |
| 118 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 119 | + }) |
| 120 | + |
| 121 | + ginkgo.By("wait cluster status has TrafficControl RemedyAction", func() { |
| 122 | + framework.WaitClusterFitWith(controlPlaneClient, targetCluster, func(cluster *clusterv1alpha1.Cluster) bool { |
| 123 | + actions := sets.NewString(cluster.Status.RemedyActions...) |
| 124 | + return actions.Has(string(remedyv1alpha1.TrafficControl)) |
| 125 | + }) |
| 126 | + }) |
| 127 | + |
| 128 | + ginkgo.By("remove Remedy resource", func() { |
| 129 | + karmadaresource.RemoveRemedy(karmadaClient, remedyName) |
| 130 | + }) |
| 131 | + |
| 132 | + ginkgo.By("wait cluster status doesn't has TrafficControl RemedyAction", func() { |
| 133 | + framework.WaitClusterFitWith(controlPlaneClient, targetCluster, func(cluster *clusterv1alpha1.Cluster) bool { |
| 134 | + actions := sets.NewString(cluster.Status.RemedyActions...) |
| 135 | + return !actions.Has(string(remedyv1alpha1.TrafficControl)) |
| 136 | + }) |
| 137 | + }) |
| 138 | + |
| 139 | + ginkgo.By(fmt.Sprintf("cleanup: recover Cluster(%s) %s to true", targetCluster, remedyv1alpha1.ServiceDomainNameResolutionReady), func() { |
| 140 | + clusterObj, err := karmadaClient.ClusterV1alpha1().Clusters().Get(context.TODO(), targetCluster, metav1.GetOptions{}) |
| 141 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 142 | + |
| 143 | + meta.SetStatusCondition(&clusterObj.Status.Conditions, metav1.Condition{ |
| 144 | + Type: string(remedyv1alpha1.ServiceDomainNameResolutionReady), |
| 145 | + Status: metav1.ConditionTrue, |
| 146 | + }) |
| 147 | + _, err = karmadaClient.ClusterV1alpha1().Clusters().UpdateStatus(context.TODO(), clusterObj, metav1.UpdateOptions{}) |
| 148 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 149 | + }) |
| 150 | + }) |
| 151 | + }) |
| 152 | + |
| 153 | + ginkgo.Context("test with nil decision matches remedy", func() { |
| 154 | + ginkgo.BeforeEach(func() { |
| 155 | + targetCluster = framework.ClusterNames()[0] |
| 156 | + remedyName = remedyNamePrefix + rand.String(RandomStrLength) |
| 157 | + remedy = &remedyv1alpha1.Remedy{ |
| 158 | + ObjectMeta: metav1.ObjectMeta{Name: remedyName}, |
| 159 | + Spec: remedyv1alpha1.RemedySpec{ |
| 160 | + Actions: []remedyv1alpha1.RemedyAction{remedyv1alpha1.TrafficControl}, |
| 161 | + }, |
| 162 | + } |
| 163 | + |
| 164 | + }) |
| 165 | + |
| 166 | + ginkgo.It("Create an immediately type remedy, then remove it", func() { |
| 167 | + ginkgo.By("create Remedy resource", func() { |
| 168 | + karmadaresource.CreateRemedy(karmadaClient, remedy) |
| 169 | + }) |
| 170 | + |
| 171 | + ginkgo.By("wait cluster status has TrafficControl RemedyAction", func() { |
| 172 | + framework.WaitClusterFitWith(controlPlaneClient, targetCluster, func(cluster *clusterv1alpha1.Cluster) bool { |
| 173 | + actions := sets.NewString(cluster.Status.RemedyActions...) |
| 174 | + return actions.Has(string(remedyv1alpha1.TrafficControl)) |
| 175 | + }) |
| 176 | + }) |
| 177 | + |
| 178 | + ginkgo.By("remove Remedy resource", func() { |
| 179 | + karmadaresource.RemoveRemedy(karmadaClient, remedyName) |
| 180 | + }) |
| 181 | + |
| 182 | + ginkgo.By("wait cluster status doesn't has TrafficControl RemedyAction", func() { |
| 183 | + framework.WaitClusterFitWith(controlPlaneClient, targetCluster, func(cluster *clusterv1alpha1.Cluster) bool { |
| 184 | + actions := sets.NewString(cluster.Status.RemedyActions...) |
| 185 | + return !actions.Has(string(remedyv1alpha1.TrafficControl)) |
| 186 | + }) |
| 187 | + }) |
| 188 | + }) |
| 189 | + }) |
| 190 | +}) |
0 commit comments