Skip to content

Commit

Permalink
add list loadbalancers option to dpservice-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
vlorinc committed May 27, 2024
1 parent 33305ea commit 3a0d87e
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cli/dpservice-cli/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ var (
PrefixAliases = []string{"prefix", "prefixes", "prfx", "prfxs"}
RouteAliases = []string{"route", "routes", "rt", "rts"}
VirtualIPAliases = []string{"virtualip", "virtualips", "vip", "vips"}
LoadBalancerAliases = []string{"loadbalancer", "loadbalancers", "loadbalancer", "lbs", "lb"}
LoadBalancerAliases = []string{"loadbalancer", "loadbalancers", "lbs", "lb"}
LoadBalancerPrefixAliases = []string{"loadbalancer-prefix", "loadbalancer-prefixes", "lbprefix", "lbprfx", "lbprfxs"}
LoadBalancerTargetAliases = []string{"loadbalancer-target", "loadbalancer-targets", "lbtrgt", "lbtrgts", "lbtarget"}
NatAliases = []string{"nat", "translation"}
Expand Down
24 changes: 14 additions & 10 deletions cli/dpservice-cli/cmd/get_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ func (o *GetLoadBalancerOptions) AddFlags(fs *pflag.FlagSet) {
}

func (o *GetLoadBalancerOptions) MarkRequiredFlags(cmd *cobra.Command) error {
for _, name := range []string{"id"} {
if err := cmd.MarkFlagRequired(name); err != nil {
return err
}
}
return nil
}

Expand All @@ -71,10 +66,19 @@ func RunGetLoadBalancer(
}
defer DpdkClose(cleanup)

lb, err := client.GetLoadBalancer(ctx, opts.ID)
if err != nil && lb.Status.Code == 0 {
return fmt.Errorf("error getting loadbalancer: %w", err)
}
if opts.ID == "" {
return RunListLoadBalancers(
ctx,
dpdkClientFactory,
rendererFactory,
ListLoadBalancersOptions{},
)
} else {
lb, err := client.GetLoadBalancer(ctx, opts.ID)
if err != nil && lb.Status.Code == 0 {
return fmt.Errorf("error getting loadbalancer: %w", err)
}

return rendererFactory.RenderObject("", os.Stdout, lb)
return rendererFactory.RenderObject("", os.Stdout, lb)
}
}
1 change: 1 addition & 0 deletions cli/dpservice-cli/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func List(factory DPDKClientFactory) *cobra.Command {
ListRoutes(factory, rendererOptions),
ListLoadBalancerTargets(factory, rendererOptions),
ListNats(factory, rendererOptions),
ListLoadBalancers(factory, rendererOptions),
}

cmd.Short = fmt.Sprintf("Lists one of %v", CommandNames(subcommands))
Expand Down
99 changes: 99 additions & 0 deletions cli/dpservice-cli/cmd/list_loadbalancers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"context"
"fmt"
"os"
"sort"
"strings"

"github.com/ironcore-dev/dpservice/cli/dpservice-cli/util"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func ListLoadBalancers(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command {
var (
opts ListLoadBalancersOptions
)

cmd := &cobra.Command{
Use: "loadbalancers",
Short: "List all loadbalancers",
Example: "dpservice-cli list loadbalancers",
Aliases: LoadBalancerAliases,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return RunListLoadBalancers(
cmd.Context(),
dpdkClientFactory,
rendererFactory,
opts,
)
},
}

opts.AddFlags(cmd.Flags())

util.Must(opts.MarkRequiredFlags(cmd))

return cmd
}

type ListLoadBalancersOptions struct {
SortBy string
}

func (o *ListLoadBalancersOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.SortBy, "sort-by", "", "Column to sort by.")
}

func (o *ListLoadBalancersOptions) MarkRequiredFlags(cmd *cobra.Command) error {
return nil
}

func RunListLoadBalancers(
ctx context.Context,
dpdkClientFactory DPDKClientFactory,
rendererFactory RendererFactory,
opts ListLoadBalancersOptions,
) error {
client, cleanup, err := dpdkClientFactory.NewClient(ctx)
if err != nil {
return fmt.Errorf("error getting dpdk client: %w", err)
}
defer DpdkClose(cleanup)

loadbalancerList, err := client.ListLoadBalancers(ctx)
if err != nil {
return fmt.Errorf("error listing loadbalancers: %w", err)
}

// sort items in list
loadbalancers := loadbalancerList.Items
sort.SliceStable(loadbalancers, func(i, j int) bool {
mi, mj := loadbalancers[i], loadbalancers[j]
switch strings.ToLower(opts.SortBy) {
case "vni":
if mi.Spec.VNI != mj.Spec.VNI {
return mi.Spec.VNI < mj.Spec.VNI
}
return mi.Spec.LbVipIP.String() < mj.Spec.LbVipIP.String()
case "ip":
if mi.Spec.LbVipIP.String() != mj.Spec.LbVipIP.String() {
return mi.Spec.LbVipIP.String() < mj.Spec.LbVipIP.String()
}
return mi.Spec.VNI < mj.Spec.VNI
case "underlayroute":
return mi.Spec.UnderlayRoute.String() < mj.Spec.UnderlayRoute.String()
default:
return mi.ID < mj.ID
}
})
loadbalancerList.Items = loadbalancers

return rendererFactory.RenderList("", os.Stdout, loadbalancerList)
}
1 change: 1 addition & 0 deletions cli/dpservice-cli/dpdk/runtime/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func init() {
&api.RouteList{},
&api.VirtualIP{},
&api.LoadBalancer{},
&api.LoadBalancerList{},
&api.LoadBalancerTarget{},
&api.LoadBalancerPrefix{},
&api.LoadBalancerTargetList{},
Expand Down
21 changes: 12 additions & 9 deletions cli/dpservice-cli/renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ var DefaultTableConverter = defaultTableConverter{}
func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) {
switch obj := v.(type) {
case *api.LoadBalancer:
return t.loadBalancerTable(*obj)
return t.loadBalancerTable([]api.LoadBalancer{*obj})
case *api.LoadBalancerList:
return t.loadBalancerTable(obj.Items)
case *api.LoadBalancerTarget:
return t.loadBalancerTargetTable([]api.LoadBalancerTarget{*obj})
case *api.LoadBalancerTargetList:
Expand Down Expand Up @@ -210,17 +212,18 @@ func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) {
}
}

func (t defaultTableConverter) loadBalancerTable(lb api.LoadBalancer) (*TableData, error) {
func (t defaultTableConverter) loadBalancerTable(lbs []api.LoadBalancer) (*TableData, error) {
headers := []any{"ID", "VNI", "LbVipIP", "Lbports", "UnderlayRoute"}

columns := make([][]any, 1)

var ports = make([]string, 0, len(lb.Spec.Lbports))
for _, port := range lb.Spec.Lbports {
p := dpdkproto.Protocol_name[int32(port.Protocol)] + "/" + strconv.Itoa(int(port.Port))
ports = append(ports, p)
columns := make([][]any, len(lbs))
for i, lb := range lbs {
var ports = make([]string, 0, len(lb.Spec.Lbports))
for _, port := range lb.Spec.Lbports {
p := dpdkproto.Protocol_name[int32(port.Protocol)] + "/" + strconv.Itoa(int(port.Port))
ports = append(ports, p)
}
columns[i] = []any{lb.ID, lb.Spec.VNI, lb.Spec.LbVipIP, ports, lb.Spec.UnderlayRoute}
}
columns[0] = []any{lb.ID, lb.Spec.VNI, lb.Spec.LbVipIP, ports, lb.Spec.UnderlayRoute}

return &TableData{
Headers: headers,
Expand Down

0 comments on commit 3a0d87e

Please sign in to comment.