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

Make dpservice-cli more like kubectl #36

Merged
merged 6 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Command() *cobra.Command {
rendererOptions := &RendererOptions{}

cmd := &cobra.Command{
Use: "dpservice-cli",
Use: "dpservice-cli [command]",
Args: cobra.NoArgs,
SilenceUsage: true,
SilenceErrors: true,
Expand Down
18 changes: 9 additions & 9 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,14 @@ func ParsePrefixArgs(args []string) ([]netip.Prefix, error) {
}

var (
InterfaceAliases = []string{"interfaces", "iface", "ifaces"}
PrefixAliases = []string{"prefixes", "prfx", "prfxs"}
RouteAliases = []string{"routes", "rt", "rts"}
VirtualIPAliases = []string{"virtualips", "vip", "vips"}
LoadBalancerAliases = []string{"loadbalancers", "loadbalancer", "lbs", "lb"}
LoadBalancerPrefixAliases = []string{"loadbalancer-prefixes", "lbprfx", "lbprfxs"}
LoadBalancerTargetAliases = []string{"loadbalancer-targets", "lbtrgt", "lbtrgts", "lbtarget"}
NatAliases = []string{"translation"}
InterfaceAliases = []string{"interface", "interfaces", "iface", "ifaces"}
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"}
LoadBalancerPrefixAliases = []string{"loadbalancer-prefix", "loadbalancer-prefixes", "lbprefix", "lbprfx", "lbprfxs"}
LoadBalancerTargetAliases = []string{"loadbalancer-target", "loadbalancer-targets", "lbtrgt", "lbtrgts", "lbtarget"}
NatAliases = []string{"nat", "translation"}
NeighborNatAliases = []string{"nnat", "ngbnat", "neighnat"}
FirewallRuleAliases = []string{"fwrule", "fw-rule", "firewallrules", "fwrules", "fw-rules"}
FirewallRuleAliases = []string{"firewallrule", "fwrule", "fw-rule", "firewallrules", "fwrules", "fw-rules"}
)
2 changes: 1 addition & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Create(factory DPDKClientFactory) *cobra.Command {
sourcesOptions := &SourcesOptions{}

cmd := &cobra.Command{
Use: "create",
Use: "create [command]",
Aliases: []string{"add"},
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Delete(factory DPDKClientFactory) *cobra.Command {
rendererOptions := &RendererOptions{Output: "name"}

cmd := &cobra.Command{
Use: "delete",
Use: "delete [command]",
Aliases: []string{"del"},
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
7 changes: 6 additions & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Get(factory DPDKClientFactory) *cobra.Command {
rendererOptions := &RendererOptions{Output: "table"}

cmd := &cobra.Command{
Use: "get",
Use: "get [command]",
Args: cobra.NoArgs,
RunE: SubcommandRequired,
}
Expand All @@ -29,6 +29,11 @@ func Get(factory DPDKClientFactory) *cobra.Command {
GetVni(factory, rendererOptions),
GetVersion(factory, rendererOptions),
GetInit(factory, rendererOptions),
// Aliases for list commands
GetLoadBalancerPrefix(factory, rendererOptions),
GetLoadBalancerTarget(factory, rendererOptions),
GetPrefix(factory, rendererOptions),
GetRoute(factory, rendererOptions),
}

cmd.Short = fmt.Sprintf("Gets one of %v", CommandNames(subcommands))
Expand Down
33 changes: 23 additions & 10 deletions cmd/get_firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ func GetFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory Render
Example: "dpservice-cli get fwrule --rule-id=1 --interface-id=vm1",
Aliases: FirewallRuleAliases,
Args: cobra.ExactArgs(0),
PreRunE: func(cmd *cobra.Command, args []string) error {
filter, _ := cmd.Flags().GetString("rule-id")
if filter != "" {
if err := cmd.MarkFlagRequired("interface-id"); err != nil {
return err
}
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {

return RunGetFirewallRule(
Expand Down Expand Up @@ -53,11 +62,6 @@ func (o *GetFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) {
}

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

Expand All @@ -73,10 +77,19 @@ func RunGetFirewallRule(
}
defer DpdkClose(cleanup)

fwrule, err := client.GetFirewallRule(ctx, opts.InterfaceID, opts.RuleID)
if err != nil && fwrule.Status.Code == 0 {
return fmt.Errorf("error getting firewall rule: %w", err)
}
if opts.RuleID == "" {
return RunListFirewallRules(
ctx,
dpdkClientFactory,
rendererFactory,
ListFirewallRulesOptions{InterfaceID: opts.InterfaceID},
)
} else {
fwrule, err := client.GetFirewallRule(ctx, opts.InterfaceID, opts.RuleID)
if err != nil && fwrule.Status.Code == 0 {
return fmt.Errorf("error getting firewall rule: %w", err)
}

return rendererFactory.RenderObject("", os.Stdout, fwrule)
return rendererFactory.RenderObject("", os.Stdout, fwrule)
}
}
42 changes: 23 additions & 19 deletions cmd/get_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ func (o *GetInterfaceOptions) AddFlags(fs *pflag.FlagSet) {
}

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

Expand All @@ -70,22 +65,31 @@ func RunGetInterface(
}
defer DpdkClose(cleanup)

iface, err := client.GetInterface(ctx, opts.ID)
if err != nil && iface.Status.Code == 0 {
return fmt.Errorf("error getting interface: %w", err)
}

if rendererFactory.GetWide() {
nat, err := client.GetNat(ctx, iface.ID)
if err == nil {
iface.Spec.Nat = nat
if opts.ID == "" {
return RunListInterfaces(
ctx,
dpdkClientFactory,
rendererFactory,
ListInterfacesOptions{},
)
} else {
iface, err := client.GetInterface(ctx, opts.ID)
if err != nil && iface.Status.Code == 0 {
return fmt.Errorf("error getting interface: %w", err)
}

vip, err := client.GetVirtualIP(ctx, iface.ID)
if err == nil {
iface.Spec.VIP = vip
if rendererFactory.GetWide() {
nat, err := client.GetNat(ctx, iface.ID)
if err == nil {
iface.Spec.Nat = nat
}

vip, err := client.GetVirtualIP(ctx, iface.ID)
if err == nil {
iface.Spec.VIP = vip
}
}
}

return rendererFactory.RenderObject("", os.Stdout, iface)
return rendererFactory.RenderObject("", os.Stdout, iface)
}
}
38 changes: 38 additions & 0 deletions cmd/get_loadbalancer_prefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0

package cmd

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

func GetLoadBalancerPrefix(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command {
var (
opts ListLoadBalancerPrefixesOptions
)

cmd := &cobra.Command{
Use: "lbprefix <--interface-id>",
Short: "List loadbalancer prefixes on interface.",
Example: "dpservice-cli get lbprefix --interface-id=vm1",
Aliases: LoadBalancerPrefixAliases,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {

return RunListLoadBalancerPrefixes(
cmd.Context(),
dpdkClientFactory,
rendererFactory,
opts,
)
},
}

opts.AddFlags(cmd.Flags())

util.Must(opts.MarkRequiredFlags(cmd))

return cmd
}
38 changes: 38 additions & 0 deletions cmd/get_loadbalancer_target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0

package cmd

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

func GetLoadBalancerTarget(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command {
var (
opts ListLoadBalancerTargetOptions
)

cmd := &cobra.Command{
Use: "lbtarget <--lb-id>",
Short: "List LoadBalancer Targets",
Example: "dpservice-cli get lbtarget --lb-id=1",
Aliases: LoadBalancerTargetAliases,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {

return RunListLoadBalancerTargets(
cmd.Context(),
dpdkClientFactory,
rendererFactory,
opts,
)
},
}

opts.AddFlags(cmd.Flags())

util.Must(opts.MarkRequiredFlags(cmd))

return cmd
}
25 changes: 20 additions & 5 deletions cmd/get_nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"

"github.com/ironcore-dev/dpservice-cli/util"
"github.com/ironcore-dev/dpservice-go/api"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -51,11 +52,6 @@ func (o *GetNatOptions) AddFlags(fs *pflag.FlagSet) {
}

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

Expand All @@ -71,6 +67,25 @@ func RunGetNat(
}
defer DpdkClose(cleanup)

if opts.InterfaceID == "" {
ifaces, err := client.ListInterfaces(ctx)
if err != nil && ifaces.Status.Code == 0 {
return fmt.Errorf("error listing interfaces: %w", err)
}
natList := api.NatList{
TypeMeta: api.TypeMeta{Kind: api.NatListKind},
}
for _, iface := range ifaces.Items {
nat, err := client.GetNat(ctx, iface.ID)
if err != nil && nat.Status.Code == 0 {
return fmt.Errorf("error getting nat: %w", err)
}
natList.Items = append(natList.Items, *nat)
}

return rendererFactory.RenderList("", os.Stdout, &natList)
}

nat, err := client.GetNat(ctx, opts.InterfaceID)
if err != nil && nat.Status.Code == 0 {
return fmt.Errorf("error getting nat: %w", err)
Expand Down
37 changes: 37 additions & 0 deletions cmd/get_prefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0

package cmd

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

func GetPrefix(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command {
var (
opts ListPrefixesOptions
)

cmd := &cobra.Command{
Use: "prefix <--interface-id>",
Short: "List prefix(es) on interface.",
Example: "dpservice-cli get prefix --interface-id=vm1",
Args: cobra.ExactArgs(0),
Aliases: PrefixAliases,
RunE: func(cmd *cobra.Command, args []string) error {
return RunListPrefixes(
cmd.Context(),
dpdkClientFactory,
rendererFactory,
opts,
)
},
}

opts.AddFlags(cmd.Flags())

util.Must(opts.MarkRequiredFlags(cmd))

return cmd
}
38 changes: 38 additions & 0 deletions cmd/get_route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0

package cmd

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

func GetRoute(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command {
var (
opts ListRoutesOptions
)

cmd := &cobra.Command{
Use: "route <--vni>",
Short: "List routes of specified VNI",
Example: "dpservice-cli get route --vni=100",
Aliases: RouteAliases,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {

return RunGetRoute(
cmd.Context(),
dpdkClientFactory,
rendererFactory,
opts,
)
},
}

opts.AddFlags(cmd.Flags())

util.Must(opts.MarkRequiredFlags(cmd))

return cmd
}
Loading
Loading