From d26f0d6b1b5fd77d8794b086d989bafa76dc8cb7 Mon Sep 17 00:00:00 2001 From: pb Date: Thu, 20 Feb 2025 12:46:24 +0100 Subject: [PATCH] Added two routes to get all and one admiralty targets from kube --- controllers/admiralty.go | 46 +++++++++++++++++++++++++++++++-- infrastructure/interface.go | 1 + infrastructure/kubernetes.go | 50 ++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/controllers/admiralty.go b/controllers/admiralty.go index f0fb480..80dfee1 100644 --- a/controllers/admiralty.go +++ b/controllers/admiralty.go @@ -1,6 +1,9 @@ package controllers import ( + "oc-datacenter/infrastructure" + "slices" + beego "github.com/beego/beego/v2/server/web" ) @@ -11,8 +14,47 @@ type AdmiraltyController struct { // @Title GetAllTargets // @Description find all Admiralty Target -// @Success 200 {booking} models.booking -// @router /admiralty/targets [get] +// @Success 200 +// @router /targets [get] func (c *AdmiraltyController) GetAllTargets() { + serv, err := infrastructure.NewService() + if err != nil { + // change code to 500 + c.Ctx.Output.SetStatus(500) + c.ServeJSON() + c.Data["json"] = map[string]string{"error": err.Error()} + return + } + res, err := serv.GetTargets(c.Ctx.Request.Context()) + c.Data["json"] = res + c.ServeJSON() +} + +// @Title GetOneTarget +// @Description find one Admiralty Target +// @Param id path string true "the name of the target to get" +// @Success 200 +// @router /targets/:id [get] +func (c *AdmiraltyController) GetOneTarget() { + id := c.Ctx.Input.Param(":id") + serv, err := infrastructure.NewService() + if err != nil { + // change code to 500 + c.Ctx.Output.SetStatus(500) + c.ServeJSON() + c.Data["json"] = map[string]string{"error": err.Error()} + return + } + + res, err := serv.GetTargets(c.Ctx.Request.Context()) + id = "target-"+id + found := slices.Contains(res,id) + if !found { + c.Ctx.Output.SetStatus(404) + c.ServeJSON() + } + + c.Data["json"] = id + c.ServeJSON() } \ No newline at end of file diff --git a/infrastructure/interface.go b/infrastructure/interface.go index 0fcc234..f152602 100644 --- a/infrastructure/interface.go +++ b/infrastructure/interface.go @@ -13,6 +13,7 @@ type Infrastructure interface { CreateServiceAccount(ctx context.Context, ns string) error CreateRoleBinding(ctx context.Context, ns string, roleBinding string, role string) error CreateRole(ctx context.Context, ns string, role string, groups [][]string, resources [][]string, verbs [][]string) error + GetTargets(ctx context.Context) ([]string,error) } var _service = map[string]func() (Infrastructure, error){ diff --git a/infrastructure/kubernetes.go b/infrastructure/kubernetes.go index 7096275..0c99268 100644 --- a/infrastructure/kubernetes.go +++ b/infrastructure/kubernetes.go @@ -2,6 +2,7 @@ package infrastructure import ( "context" + "encoding/json" "errors" "fmt" "oc-datacenter/conf" @@ -156,3 +157,52 @@ func (k *KubernetesService) GetToken(ctx context.Context, ns string, duration in } return token.Status.Token, nil } + +func (k *KubernetesService) GetTargets(ctx context.Context) ([]string,error){ + + var listTargets []string + resp, err := k.Set.RESTClient(). + Get(). + AbsPath("/apis/multicluster.admiralty.io/v1alpha1/targets"). + DoRaw(ctx) // from https://stackoverflow.com/questions/60764908/how-to-access-kubernetes-crd-using-client-go + + if err != nil { + fmt.Println("TODO : handle the error generated when contacting kube API") + fmt.Println("Error from k8s API : ", err) + return nil,err + } + fmt.Println(string(resp)) + var targetDict map[string]interface{} + err = json.Unmarshal(resp,&targetDict) + if err != nil { + fmt.Println("TODO: handle the error when unmarshalling k8s API response") + return nil, err + } + + b, _ := json.MarshalIndent(targetDict,""," ") + fmt.Println(string(b)) + + data := targetDict["items"].([]interface{}) + + for _, item := range data { + var metadata metav1.ObjectMeta + item := item.(map[string]interface{}) + byteMetada, err := json.Marshal(item["metadata"]) + + if err != nil { + fmt.Println("Error while Marshalling metadata field") + return nil,err + } + err = json.Unmarshal(byteMetada,&metadata) + if err != nil { + fmt.Println("Error while Unmarshalling metadata field to the library object") + return nil,err + } + + listTargets = append(listTargets, metadata.Name) + } + // parse targets to retrieve the info we need + // fmt.Println(targetDict) + return listTargets,nil + +} \ No newline at end of file