From 4dd40ac65133bec764ead7ad7c25d01b6162c707 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Wed, 15 May 2024 10:31:41 +0200 Subject: [PATCH] add func to list loadbalancers --- api/conversion.go | 44 +++++++++++++++++++++++++++++++++++++++++++- api/types.go | 23 +++++++++++++++++++++++ client/client.go | 26 +++++++++++++++++++++++++- 3 files changed, 91 insertions(+), 2 deletions(-) diff --git a/api/conversion.go b/api/conversion.go index 1077d6f..4608d25 100644 --- a/api/conversion.go +++ b/api/conversion.go @@ -12,7 +12,7 @@ import ( proto "github.com/ironcore-dev/dpservice-go/proto" ) -func ProtoLoadBalancerToLoadBalancer(dpdkLB *proto.GetLoadBalancerResponse, lbID string) (*LoadBalancer, error) { +func ProtoLoadBalancerRequestToLoadBalancer(dpdkLB *proto.GetLoadBalancerResponse, lbID string) (*LoadBalancer, error) { var underlayRoute netip.Addr if underlayRouteString := string(dpdkLB.GetUnderlayRoute()); underlayRouteString != "" { @@ -58,6 +58,48 @@ func ProtoLoadBalancerToLoadBalancer(dpdkLB *proto.GetLoadBalancerResponse, lbID }, nil } +func ProtoLoadBalancerToLoadBalancer(dpdkLB *proto.Loadbalancer) (*LoadBalancer, error) { + + var underlayRoute netip.Addr + if underlayRouteString := string(dpdkLB.GetUnderlayRoute()); underlayRouteString != "" { + var err error + underlayRoute, err = netip.ParseAddr(string(dpdkLB.GetUnderlayRoute())) + if err != nil { + return nil, fmt.Errorf("error parsing underlay ip: %w", err) + } + } + var lbip netip.Addr + if lbipString := string(dpdkLB.GetIp().GetAddress()); lbipString != "" { + var err error + lbip, err = netip.ParseAddr(string(dpdkLB.GetIp().GetAddress())) + if err != nil { + return nil, fmt.Errorf("error parsing lb ip: %w", err) + } + } + var lbports = make([]LBPort, 0, len(dpdkLB.GetPorts())) + var p LBPort + for _, lbport := range dpdkLB.GetPorts() { + p.Protocol = uint32(lbport.Protocol) + p.Port = lbport.Port + lbports = append(lbports, p) + } + + return &LoadBalancer{ + TypeMeta: TypeMeta{ + Kind: LoadBalancerKind, + }, + LoadBalancerMeta: LoadBalancerMeta{ + ID: string(dpdkLB.Id), + }, + Spec: LoadBalancerSpec{ + VNI: dpdkLB.Vni, + LbVipIP: &lbip, + Lbports: lbports, + UnderlayRoute: &underlayRoute, + }, + }, nil +} + func StringLbportToLbport(lbport string) (LBPort, error) { p := strings.Split(lbport, "/") protocolName := strings.ToLower(p[0]) diff --git a/api/types.go b/api/types.go index 269cb6d..ffb3101 100644 --- a/api/types.go +++ b/api/types.go @@ -200,6 +200,28 @@ type LBPort struct { Port uint32 `json:"port"` } +type LoadBalancerList struct { + TypeMeta `json:",inline"` + LoadBalancerListMeta `json:"metadata"` + Status Status `json:"status"` + Items []LoadBalancer `json:"items"` +} + +type LoadBalancerListMeta struct { +} + +func (l *LoadBalancerList) GetItems() []Object { + res := make([]Object, len(l.Items)) + for i := range l.Items { + res[i] = &l.Items[i] + } + return res +} + +func (m *LoadBalancerList) GetStatus() Status { + return m.Status +} + type LoadBalancerTarget struct { TypeMeta `json:",inline"` LoadBalancerTargetMeta `json:"metadata"` @@ -633,6 +655,7 @@ var ( InterfaceKind = reflect.TypeOf(Interface{}).Name() InterfaceListKind = reflect.TypeOf(InterfaceList{}).Name() LoadBalancerKind = reflect.TypeOf(LoadBalancer{}).Name() + LoadBalancerListKind = reflect.TypeOf(LoadBalancerList{}).Name() LoadBalancerTargetKind = reflect.TypeOf(LoadBalancerTarget{}).Name() LoadBalancerTargetListKind = reflect.TypeOf(LoadBalancerTargetList{}).Name() LoadBalancerPrefixKind = reflect.TypeOf(LoadBalancerPrefix{}).Name() diff --git a/client/client.go b/client/client.go index ffadb4d..5e3763b 100644 --- a/client/client.go +++ b/client/client.go @@ -16,6 +16,7 @@ import ( type Client interface { GetLoadBalancer(ctx context.Context, id string, ignoredErrors ...[]uint32) (*api.LoadBalancer, error) + ListLoadBalancers(ctx context.Context, ignoredErrors ...[]uint32) (*api.LoadBalancerList, error) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer, ignoredErrors ...[]uint32) (*api.LoadBalancer, error) DeleteLoadBalancer(ctx context.Context, id string, ignoredErrors ...[]uint32) (*api.LoadBalancer, error) @@ -93,7 +94,30 @@ func (c *client) GetLoadBalancer(ctx context.Context, id string, ignoredErrors . if res.GetStatus().GetCode() != 0 { return retLoadBalancer, errors.GetError(res.Status, ignoredErrors) } - return api.ProtoLoadBalancerToLoadBalancer(res, id) + return api.ProtoLoadBalancerRequestToLoadBalancer(res, id) +} + +func (c *client) ListLoadBalancers(ctx context.Context, ignoredErrors ...[]uint32) (*api.LoadBalancerList, error) { + res, err := c.DPDKironcoreClient.ListLoadBalancers(ctx, &dpdkproto.ListLoadBalancersRequest{}) + if err != nil { + return nil, err + } + + lbs := make([]api.LoadBalancer, len(res.GetLoadbalancers())) + for i, dpdkLb := range res.GetLoadbalancers() { + lb, err := api.ProtoLoadBalancerToLoadBalancer(dpdkLb) + if err != nil { + return nil, err + } + + lbs[i] = *lb + } + + return &api.LoadBalancerList{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerListKind}, + Items: lbs, + Status: api.ProtoStatusToStatus(res.Status), + }, nil } func (c *client) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer, ignoredErrors ...[]uint32) (*api.LoadBalancer, error) {