Skip to content

Commit 910d4ad

Browse files
authored
Merge pull request #5109 from XiShanYongYe-Chang/fix-update-cluster-conflict
fix update cluster conflict error in the remedy e2e test
2 parents 3f750e9 + fecd9a7 commit 910d4ad

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

test/e2e/framework/cluster.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/onsi/gomega"
2727
apierrors "k8s.io/apimachinery/pkg/api/errors"
28+
"k8s.io/apimachinery/pkg/api/meta"
2829
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2930
"k8s.io/apimachinery/pkg/util/wait"
3031
"k8s.io/client-go/dynamic"
@@ -352,3 +353,20 @@ func SetClusterRegion(c client.Client, clusterName string, regionName string) er
352353
return true, nil
353354
})
354355
}
356+
357+
// UpdateClusterStatusCondition updates the target cluster status condition.
358+
func UpdateClusterStatusCondition(client karmada.Interface, clusterName string, condition metav1.Condition) {
359+
gomega.Eventually(func() (bool, error) {
360+
cluster, err := client.ClusterV1alpha1().Clusters().Get(context.TODO(), clusterName, metav1.GetOptions{})
361+
if err != nil {
362+
return false, err
363+
}
364+
365+
meta.SetStatusCondition(&cluster.Status.Conditions, condition)
366+
_, err = client.ClusterV1alpha1().Clusters().UpdateStatus(context.TODO(), cluster, metav1.UpdateOptions{})
367+
if err != nil {
368+
return false, err
369+
}
370+
return true, nil
371+
}, pollTimeout, pollInterval).Should(gomega.Equal(true))
372+
}

test/e2e/remedy_test.go

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ limitations under the License.
1717
package e2e
1818

1919
import (
20-
"context"
2120
"fmt"
2221

2322
"github.com/onsi/ginkgo/v2"
24-
"github.com/onsi/gomega"
25-
"k8s.io/apimachinery/pkg/api/meta"
2623
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2724
"k8s.io/apimachinery/pkg/util/rand"
2825
"k8s.io/apimachinery/pkg/util/sets"
@@ -66,15 +63,10 @@ var _ = framework.SerialDescribe("remedy testing", func() {
6663

6764
ginkgo.It("Cluster domain name resolution function encounters an exception and recover", func() {
6865
ginkgo.By(fmt.Sprintf("update Cluster(%s) %s condition to false", targetCluster, remedyv1alpha1.ServiceDomainNameResolutionReady), func() {
69-
clusterObj, err := karmadaClient.ClusterV1alpha1().Clusters().Get(context.TODO(), targetCluster, metav1.GetOptions{})
70-
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
71-
72-
meta.SetStatusCondition(&clusterObj.Status.Conditions, metav1.Condition{
66+
framework.UpdateClusterStatusCondition(karmadaClient, targetCluster, metav1.Condition{
7367
Type: string(remedyv1alpha1.ServiceDomainNameResolutionReady),
7468
Status: metav1.ConditionFalse,
7569
})
76-
_, err = karmadaClient.ClusterV1alpha1().Clusters().UpdateStatus(context.TODO(), clusterObj, metav1.UpdateOptions{})
77-
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
7870
})
7971

8072
ginkgo.By(fmt.Sprintf("wait Cluster(%s) status has TrafficControl RemedyAction", targetCluster), func() {
@@ -86,15 +78,10 @@ var _ = framework.SerialDescribe("remedy testing", func() {
8678
})
8779

8880
ginkgo.By(fmt.Sprintf("recover Cluster(%s) %s condition to true", targetCluster, remedyv1alpha1.ServiceDomainNameResolutionReady), func() {
89-
clusterObj, err := karmadaClient.ClusterV1alpha1().Clusters().Get(context.TODO(), targetCluster, metav1.GetOptions{})
90-
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
91-
92-
meta.SetStatusCondition(&clusterObj.Status.Conditions, metav1.Condition{
81+
framework.UpdateClusterStatusCondition(karmadaClient, targetCluster, metav1.Condition{
9382
Type: string(remedyv1alpha1.ServiceDomainNameResolutionReady),
9483
Status: metav1.ConditionTrue,
9584
})
96-
_, err = karmadaClient.ClusterV1alpha1().Clusters().UpdateStatus(context.TODO(), clusterObj, metav1.UpdateOptions{})
97-
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
9885
})
9986

10087
ginkgo.By(fmt.Sprintf("wait Cluster(%s) status doesn't has TrafficControl RemedyAction", targetCluster), func() {
@@ -112,15 +99,10 @@ var _ = framework.SerialDescribe("remedy testing", func() {
11299

113100
ginkgo.It("Cluster domain name resolution function encounters an exception, then remove the remedy resource", func() {
114101
ginkgo.By(fmt.Sprintf("update Cluster(%s) %s condition to false", targetCluster, remedyv1alpha1.ServiceDomainNameResolutionReady), func() {
115-
clusterObj, err := karmadaClient.ClusterV1alpha1().Clusters().Get(context.TODO(), targetCluster, metav1.GetOptions{})
116-
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
117-
118-
meta.SetStatusCondition(&clusterObj.Status.Conditions, metav1.Condition{
102+
framework.UpdateClusterStatusCondition(karmadaClient, targetCluster, metav1.Condition{
119103
Type: string(remedyv1alpha1.ServiceDomainNameResolutionReady),
120104
Status: metav1.ConditionFalse,
121105
})
122-
_, err = karmadaClient.ClusterV1alpha1().Clusters().UpdateStatus(context.TODO(), clusterObj, metav1.UpdateOptions{})
123-
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
124106
})
125107

126108
ginkgo.By(fmt.Sprintf("wait Cluster(%s) status has TrafficControl RemedyAction", targetCluster), func() {
@@ -144,15 +126,10 @@ var _ = framework.SerialDescribe("remedy testing", func() {
144126
})
145127

146128
ginkgo.By(fmt.Sprintf("cleanup: recover Cluster(%s) %s to true", targetCluster, remedyv1alpha1.ServiceDomainNameResolutionReady), func() {
147-
clusterObj, err := karmadaClient.ClusterV1alpha1().Clusters().Get(context.TODO(), targetCluster, metav1.GetOptions{})
148-
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
149-
150-
meta.SetStatusCondition(&clusterObj.Status.Conditions, metav1.Condition{
129+
framework.UpdateClusterStatusCondition(karmadaClient, targetCluster, metav1.Condition{
151130
Type: string(remedyv1alpha1.ServiceDomainNameResolutionReady),
152131
Status: metav1.ConditionTrue,
153132
})
154-
_, err = karmadaClient.ClusterV1alpha1().Clusters().UpdateStatus(context.TODO(), clusterObj, metav1.UpdateOptions{})
155-
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
156133
})
157134
})
158135
})

0 commit comments

Comments
 (0)