Skip to content

Commit

Permalink
Optimised fleet update function of e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: LiZhenCheng9527 <lizhencheng6@huawei.com>
  • Loading branch information
LiZhenCheng9527 committed Feb 23, 2024
1 parent 386cab0 commit be53836
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 55 deletions.
2 changes: 1 addition & 1 deletion e2e/fleet_attachedcluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var _ = ginkgo.Describe("[AttachedClusters] AttachedClusters testing", func() {

ginkgo.It("Create Fleet", func() {
// create fleet and checkout fleet status
fleetCreateErr := resources.CreateFleet(kuratorClient, fleet)
fleetCreateErr := resources.CreateOrUpdateFleet(kuratorClient, fleet)
gomega.Expect(fleetCreateErr).ShouldNot(gomega.HaveOccurred())
resources.WaitFleetFitWith(kuratorClient, namespace, fleetname, func(fleet *fleetv1a1.Fleet) bool {
return fleet.Status.Phase == fleetv1a1.ReadyPhase
Expand Down
40 changes: 19 additions & 21 deletions e2e/resources/attachedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/onsi/gomega"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

clusterv1a1 "kurator.dev/kurator/pkg/apis/cluster/v1alpha1"
kurator "kurator.dev/kurator/pkg/client-go/generated/clientset/versioned"
Expand All @@ -44,33 +45,30 @@ func NewAttachedCluster(namespace string, name string, config clusterv1a1.Secret
}

// CreateAttachedCluster create AttachedCluster.
func CreateAttachedCluster(client kurator.Interface, attachedCluster *clusterv1a1.AttachedCluster) error {
_, err := client.ClusterV1alpha1().AttachedClusters(attachedCluster.Namespace).Create(context.TODO(), attachedCluster, metav1.CreateOptions{})
if err != nil {
if apierrors.IsAlreadyExists(err) {
return UpdateAttachedCluster(client, attachedCluster)
func CreateOrUpdateAttachedCluster(client kurator.Interface, attachedCluster *clusterv1a1.AttachedCluster) error {
_, createErr := client.ClusterV1alpha1().AttachedClusters(attachedCluster.GetNamespace()).Create(context.TODO(), attachedCluster, metav1.CreateOptions{})
if createErr != nil {
if apierrors.IsAlreadyExists(createErr) {
originalAttachedCluster, getErr := client.ClusterV1alpha1().AttachedClusters(attachedCluster.GetNamespace()).Get(context.TODO(), attachedCluster.GetName(), metav1.GetOptions{})
if getErr != nil {
return getErr
}
attachedCluster.ResourceVersion = originalAttachedCluster.ResourceVersion
attachedClusterPatchData, createPatchErr := CreatePatchData(originalAttachedCluster, attachedCluster)
if createPatchErr != nil {
return createPatchErr
}
_, patchErr := client.ClusterV1alpha1().AttachedClusters(attachedCluster.GetNamespace()).Patch(context.TODO(), attachedCluster.GetName(), types.MergePatchType, attachedClusterPatchData, metav1.PatchOptions{})
if patchErr != nil {
return patchErr
}
} else {
return err
return createErr
}
}
return nil
}

// UpdateAttachedCluster update AttachedCluster
func UpdateAttachedCluster(client kurator.Interface, attachedCluster *clusterv1a1.AttachedCluster) error {
attachedClusterPresentOnCluster, attacattachedClusterGetErr := client.ClusterV1alpha1().AttachedClusters(attachedCluster.Namespace).Get(context.TODO(), attachedCluster.Name, metav1.GetOptions{})
if attacattachedClusterGetErr != nil {
return attacattachedClusterGetErr
}
DCattachedcluster := attachedClusterPresentOnCluster.DeepCopy()
DCattachedcluster.Spec = attachedCluster.Spec
_, err := client.ClusterV1alpha1().AttachedClusters(DCattachedcluster.Namespace).Update(context.TODO(), DCattachedcluster, metav1.UpdateOptions{})
if err != nil {
return err
}
return nil
}

func RemoveAttachedCluster(client kurator.Interface, namespace, name string) error {
err := client.ClusterV1alpha1().AttachedClusters(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
if err != nil {
Expand Down
33 changes: 18 additions & 15 deletions e2e/resources/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

fleetv1a1 "kurator.dev/kurator/pkg/apis/fleet/v1alpha1"
kurator "kurator.dev/kurator/pkg/client-go/generated/clientset/versioned"
Expand All @@ -46,27 +47,29 @@ func NewFleet(namespace string, name string, clusters []*corev1.ObjectReference)
}

// CreateAttachedCluster create AttachedCluster.
func CreateFleet(client kurator.Interface, fleet *fleetv1a1.Fleet) error {
_, err := client.FleetV1alpha1().Fleets(fleet.Namespace).Create(context.TODO(), fleet, metav1.CreateOptions{})
if err != nil {
if apierrors.IsAlreadyExists(err) {
return UpdateFleet(client, fleet)
func CreateOrUpdateFleet(client kurator.Interface, fleet *fleetv1a1.Fleet) error {
_, createErr := client.FleetV1alpha1().Fleets(fleet.GetNamespace()).Create(context.TODO(), fleet, metav1.CreateOptions{})
if createErr != nil {
if apierrors.IsAlreadyExists(createErr) {
originalFleet, getErr := client.FleetV1alpha1().Fleets(fleet.GetNamespace()).Get(context.TODO(), fleet.GetName(), metav1.GetOptions{})
if getErr != nil {
return getErr
}
fleet.ResourceVersion = originalFleet.ResourceVersion
fleetPatchData, createPatchErr := CreatePatchData(originalFleet, fleet)
if createPatchErr != nil {
return createPatchErr
}
if _, patchErr := client.FleetV1alpha1().Fleets(fleet.GetNamespace()).Patch(context.TODO(), fleet.GetName(), types.MergePatchType, fleetPatchData, metav1.PatchOptions{}); patchErr != nil {
return patchErr
}
} else {
return err
return createErr
}
}
return nil
}

// UpdateAttachedCluster update AttachedCluster
func UpdateFleet(client kurator.Interface, fleet *fleetv1a1.Fleet) error {
_, err := client.FleetV1alpha1().Fleets(fleet.Namespace).Update(context.TODO(), fleet, metav1.UpdateOptions{})
if err != nil {
return err
}
return nil
}

func RemoveFleet(client kurator.Interface, namespace, name string) error {
err := client.FleetV1alpha1().Fleets(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
if err != nil {
Expand Down
34 changes: 19 additions & 15 deletions e2e/resources/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
)

Expand All @@ -41,27 +42,30 @@ func NewSecret(namespace string, name string, data map[string][]byte) *corev1.Se
}

// CreateSecret create Secret.
func CreateSecret(client kubernetes.Interface, secret *corev1.Secret) error {
_, err := client.CoreV1().Secrets(secret.Namespace).Create(context.TODO(), secret, metav1.CreateOptions{})
if err != nil {
if apierrors.IsAlreadyExists(err) {
return UpdateSecret(client, secret)
func CreateOrUpdateSecret(client kubernetes.Interface, secret *corev1.Secret) error {
_, createErr := client.CoreV1().Secrets(secret.GetNamespace()).Create(context.TODO(), secret, metav1.CreateOptions{})
if createErr != nil {
if apierrors.IsAlreadyExists(createErr) {
originalSecret, getErr := client.CoreV1().Secrets(secret.GetNamespace()).Get(context.TODO(), secret.GetName(), metav1.GetOptions{})
if getErr != nil {
return getErr
}
secret.ResourceVersion = originalSecret.ResourceVersion
secretPatchData, createPatchErr := CreatePatchData(originalSecret, secret)
if createPatchErr != nil {
return createPatchErr
}
_, patchErr := client.CoreV1().Secrets(secret.GetNamespace()).Patch(context.TODO(), secret.GetName(), types.StrategicMergePatchType, secretPatchData, metav1.PatchOptions{})
if patchErr != nil {
return patchErr
}
} else {
return err
return createErr
}
}
return nil
}

// UpdateSecret update Secret
func UpdateSecret(client kubernetes.Interface, secret *corev1.Secret) error {
_, err := client.CoreV1().Secrets(secret.Namespace).Update(context.TODO(), secret, metav1.UpdateOptions{})
if err != nil {
return err
}
return nil
}

func RemoveSecret(client kubernetes.Interface, namespace, name string) error {
err := client.CoreV1().Secrets(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
if err != nil {
Expand Down
39 changes: 39 additions & 0 deletions e2e/resources/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright Kurator 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 resources

import (
"encoding/json"

jsonpatch "github.com/evanphx/json-patch"
)

func CreatePatchData(originalAttachedCluster, modifiedAttachedCluster interface{}) ([]byte, error) {
originalData, originalErr := json.Marshal(originalAttachedCluster)
if originalErr != nil {
return nil, originalErr
}
modifiedData, modifiedErr := json.Marshal(modifiedAttachedCluster)
if modifiedErr != nil {
return nil, modifiedErr
}
patchData, createErr := jsonpatch.CreateMergePatch(originalData, modifiedData)
if createErr != nil {
return nil, createErr
}
return patchData, nil
}
4 changes: 2 additions & 2 deletions e2e/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
}
attachedcluster = resources.NewAttachedCluster(namespace, memberClusterName, secretKeyRef)

secretCreateErr := resources.CreateSecret(kubeClient, secret)
secretCreateErr := resources.CreateOrUpdateSecret(kubeClient, secret)
gomega.Expect(secretCreateErr).ShouldNot(gomega.HaveOccurred())

attachedCreateErr := resources.CreateAttachedCluster(kuratorClient, attachedcluster)
attachedCreateErr := resources.CreateOrUpdateAttachedCluster(kuratorClient, attachedcluster)
gomega.Expect(attachedCreateErr).ShouldNot(gomega.HaveOccurred())
resources.WaitAttachedClusterFitWith(kuratorClient, namespace, memberClusterName, func(attachedCluster *clusterv1a1.AttachedCluster) bool {
return attachedCluster.Status.Ready
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/awslabs/goformation/v4 v4.19.5
github.com/cert-manager/cert-manager v1.10.0
github.com/coreos/go-semver v0.3.0
github.com/evanphx/json-patch v5.6.0+incompatible
github.com/fluxcd/flagger v1.34.0
github.com/fluxcd/helm-controller/api v0.32.2
github.com/fluxcd/kustomize-controller/api v1.0.0-rc.1
Expand Down Expand Up @@ -117,7 +118,6 @@ require (
github.com/emicklei/go-restful/v3 v3.10.2 // indirect
github.com/envoyproxy/go-control-plane v0.11.1 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.2 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f // indirect
github.com/fatih/camelcase v1.0.0 // indirect
Expand Down

0 comments on commit be53836

Please sign in to comment.