Skip to content

Commit

Permalink
use patch merged Create and Update
Browse files Browse the repository at this point in the history
Signed-off-by: LiZhenCheng9527 <[email protected]>
  • Loading branch information
LiZhenCheng9527 committed Feb 21, 2024
1 parent 193bbb3 commit 74598d6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 54 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
36 changes: 15 additions & 21 deletions e2e/resources/attachedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package resources

import (
"context"
"encoding/json"

"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 +46,25 @@ 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) {
patchAttachedCluster, err := json.Marshal(attachedCluster)
if err != nil {
return err
}
_, patchErr := client.ClusterV1alpha1().AttachedClusters(attachedCluster.GetNamespace()).Patch(context.TODO(), attachedCluster.GetName(), types.MergePatchType, patchAttachedCluster, 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
30 changes: 12 additions & 18 deletions e2e/resources/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package resources

import (
"context"
"encoding/json"

"github.com/onsi/gomega"
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,33 +48,25 @@ 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{})
func CreateOrUpdateFleet(client kurator.Interface, fleet *fleetv1a1.Fleet) error {
_, err := client.FleetV1alpha1().Fleets(fleet.GetNamespace()).Create(context.TODO(), fleet, metav1.CreateOptions{})
if err != nil {
if apierrors.IsAlreadyExists(err) {
return UpdateFleet(client, fleet)
patchFleet, error := json.Marshal(fleet)
if error != nil {
return error
}
_, patchErr := client.FleetV1alpha1().Fleets(fleet.GetNamespace()).Patch(context.TODO(), fleet.GetName(), types.MergePatchType, patchFleet, metav1.PatchOptions{})
if patchErr != nil {
return patchErr
}
} else {
return err
}
}
return nil
}

// UpdateAttachedCluster update AttachedCluster
func UpdateFleet(client kurator.Interface, fleet *fleetv1a1.Fleet) error {
fleetOnCluster, fleetGetErr := client.FleetV1alpha1().Fleets(fleet.Namespace).Get(context.TODO(), fleet.Name, metav1.GetOptions{})
if fleetGetErr != nil {
return fleetGetErr
}
DCfleet := fleetOnCluster.DeepCopy()
DCfleet.Spec = fleet.Spec
_, err := client.FleetV1alpha1().Fleets(fleet.Namespace).Update(context.TODO(), DCfleet, 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
24 changes: 12 additions & 12 deletions e2e/resources/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package resources

import (
"context"
"encoding/json"

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 +43,25 @@ 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{})
func CreateOrUpdateSecret(client kubernetes.Interface, secret *corev1.Secret) error {
_, err := client.CoreV1().Secrets(secret.GetNamespace()).Create(context.TODO(), secret, metav1.CreateOptions{})
if err != nil {
if apierrors.IsAlreadyExists(err) {
return UpdateSecret(client, secret)
patchSecret, error := json.Marshal(secret)
if error != nil {
return error
}
_, patchErr := client.CoreV1().Secrets(secret.GetNamespace()).Patch(context.TODO(), secret.GetName(), types.MergePatchType, patchSecret, metav1.PatchOptions{})
if patchErr != nil {
return patchErr
}
} else {
return err
}
}
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
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)
resources.WaitAttachedClusterFitWith(kuratorClient, namespace, memberClusterName, func(attachedCluster *clusterv1a1.AttachedCluster) bool {
return attachedCluster.Status.Ready
})
Expand Down

0 comments on commit 74598d6

Please sign in to comment.