From cca59faeab833237b47faae3a25c563974c1774a Mon Sep 17 00:00:00 2001 From: pb Date: Thu, 10 Apr 2025 14:22:37 +0200 Subject: [PATCH] when creating source or target returns a 409, don't return an error. Post should be replaced by Put but not working --- controllers/admiralty.go | 7 +++++++ infrastructure/kubernetes.go | 7 ++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/controllers/admiralty.go b/controllers/admiralty.go index 8acfaf1..a502672 100644 --- a/controllers/admiralty.go +++ b/controllers/admiralty.go @@ -16,6 +16,7 @@ import ( jwt "github.com/golang-jwt/jwt/v5" "gopkg.in/yaml.v2" v1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" ) type KubeInfo struct { @@ -141,6 +142,12 @@ func (c *AdmiraltyController) CreateSource() { res, err := serv.CreateAdmiraltySource(c.Ctx.Request.Context(),execution) if err != nil { + if apierrors.IsAlreadyExists(err) { + c.Ctx.Output.SetStatus(409) + c.Data["json"] = map[string]string{"info" : "A source already exists for this namespace : " + execution} + c.ServeJSON() + return + } // change code to 500 c.Ctx.Output.SetStatus(500) c.Data["json"] = map[string]string{"error": err.Error()} diff --git a/infrastructure/kubernetes.go b/infrastructure/kubernetes.go index 713e409..9b0f31c 100644 --- a/infrastructure/kubernetes.go +++ b/infrastructure/kubernetes.go @@ -277,7 +277,7 @@ func (k *KubernetesService) CreateAdmiraltyTarget(context context.Context,execut err = tmpl.Execute(&tpl, map[string]string{"ExecutionId":executionId}) targetManifest = tpl.String() - resp, err := postCDRapiKube( + resp, err := putCDRapiKube( *k.Set, context, "/apis/multicluster.admiralty.io/v1alpha1/namespaces/"+ executionId +"/targets", @@ -316,7 +316,7 @@ func (k *KubernetesService) CreateAdmiraltySource(context context.Context,execut err = tmpl.Execute(&tpl, map[string]string{"ExecutionId":executionId}) sourceManifest = tpl.String() - resp, err := postCDRapiKube( + resp, err := putCDRapiKube( *k.Set, context, "/apis/multicluster.admiralty.io/v1alpha1/namespaces/"+ executionId +"/sources", @@ -326,6 +326,7 @@ func (k *KubernetesService) CreateAdmiraltySource(context context.Context,execut ) // We can add more info to the log with the content of resp if not nil + if err != nil { fmt.Println("Error trying to create a Source on remote cluster : ", err , " : ", resp) return nil, err @@ -431,7 +432,7 @@ func getCDRapiKube(client kubernetes.Clientset, ctx context.Context, path string return resp, nil } -func postCDRapiKube(client kubernetes.Clientset, ctx context.Context, path string, body []byte, params ...map[string]string) ([]byte, error){ +func putCDRapiKube(client kubernetes.Clientset, ctx context.Context, path string, body []byte, params ...map[string]string) ([]byte, error){ req := client.RESTClient(). Post(). AbsPath(path).