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

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