Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
add func to list loadbalancers
Browse files Browse the repository at this point in the history
  • Loading branch information
vlorinc committed May 15, 2024
1 parent 3d43c8d commit 4dd40ac
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
44 changes: 43 additions & 1 deletion api/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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])
Expand Down
23 changes: 23 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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()
Expand Down
26 changes: 25 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4dd40ac

Please sign in to comment.