Added two routes to get all and one admiralty targets from kube

This commit is contained in:
pb 2025-02-20 12:46:24 +01:00
parent 3c313171c3
commit d26f0d6b1b
3 changed files with 95 additions and 2 deletions

View File

@ -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()
}

View File

@ -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){

View File

@ -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
}