when creating source or target returns a 409, don't return an error. Post should be replaced by Put but not working

This commit is contained in:
pb 2025-04-10 14:22:37 +02:00
parent 2cf8923d95
commit cca59faeab
2 changed files with 11 additions and 3 deletions

View File

@ -16,6 +16,7 @@ import (
jwt "github.com/golang-jwt/jwt/v5" jwt "github.com/golang-jwt/jwt/v5"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
) )
type KubeInfo struct { type KubeInfo struct {
@ -141,6 +142,12 @@ func (c *AdmiraltyController) CreateSource() {
res, err := serv.CreateAdmiraltySource(c.Ctx.Request.Context(),execution) res, err := serv.CreateAdmiraltySource(c.Ctx.Request.Context(),execution)
if err != nil { 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 // change code to 500
c.Ctx.Output.SetStatus(500) c.Ctx.Output.SetStatus(500)
c.Data["json"] = map[string]string{"error": err.Error()} c.Data["json"] = map[string]string{"error": err.Error()}

View File

@ -277,7 +277,7 @@ func (k *KubernetesService) CreateAdmiraltyTarget(context context.Context,execut
err = tmpl.Execute(&tpl, map[string]string{"ExecutionId":executionId}) err = tmpl.Execute(&tpl, map[string]string{"ExecutionId":executionId})
targetManifest = tpl.String() targetManifest = tpl.String()
resp, err := postCDRapiKube( resp, err := putCDRapiKube(
*k.Set, *k.Set,
context, context,
"/apis/multicluster.admiralty.io/v1alpha1/namespaces/"+ executionId +"/targets", "/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}) err = tmpl.Execute(&tpl, map[string]string{"ExecutionId":executionId})
sourceManifest = tpl.String() sourceManifest = tpl.String()
resp, err := postCDRapiKube( resp, err := putCDRapiKube(
*k.Set, *k.Set,
context, context,
"/apis/multicluster.admiralty.io/v1alpha1/namespaces/"+ executionId +"/sources", "/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 // We can add more info to the log with the content of resp if not nil
if err != nil { if err != nil {
fmt.Println("Error trying to create a Source on remote cluster : ", err , " : ", resp) fmt.Println("Error trying to create a Source on remote cluster : ", err , " : ", resp)
return nil, err return nil, err
@ -431,7 +432,7 @@ func getCDRapiKube(client kubernetes.Clientset, ctx context.Context, path string
return resp, nil 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(). req := client.RESTClient().
Post(). Post().
AbsPath(path). AbsPath(path).