From 916d1778d9b4b889f4bc67c1d5750237d35643df Mon Sep 17 00:00:00 2001 From: vilo Date: Mon, 24 Apr 2023 15:50:13 +0200 Subject: [PATCH 01/65] add GetLoadbalancer function --- .vscode/launch.json | 17 ++++++ cmd/common.go | 17 +++--- cmd/create.go | 6 +- cmd/create_interface.go | 6 +- cmd/create_prefix.go | 4 +- cmd/create_route.go | 4 +- cmd/create_virtualip.go | 4 +- cmd/delete.go | 6 +- cmd/delete_interface.go | 2 +- cmd/delete_prefix.go | 2 +- cmd/delete_route.go | 2 +- cmd/delete_virtualip.go | 2 +- cmd/get.go | 1 + cmd/get_interface.go | 2 +- cmd/get_loadbalancer.go | 103 +++++++++++++++++++++++++++++++++ cmd/get_prefix.go | 2 +- cmd/get_route.go | 2 +- cmd/get_virtualip.go | 2 +- cover.out | 8 +-- dpdk/api/conversion.go | 43 ++++++++++++++ dpdk/api/register.go | 2 +- dpdk/api/types.go | 57 +++++++++++++++--- dpdk/client/client.go | 20 ++++++- dpdk/client/dynamic/dynamic.go | 4 +- dpdk/runtime/decoder.go | 2 +- go.mod | 18 +++--- go.sum | 91 +++++------------------------ io/reader_test.go | 2 +- main.go | 2 +- renderer/renderer.go | 18 +++++- sources/sources.go | 4 +- 31 files changed, 317 insertions(+), 138 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 cmd/get_loadbalancer.go diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..1c3d619 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${fileDirname}", + "args": ["get", "loadbalancer", "1"] + //"args": ["get", "route", "0.0.0.0/24", "0", "fc00:2::64:0:1", "--vni", "100"] + } + ] +} \ No newline at end of file diff --git a/cmd/common.go b/cmd/common.go index 64a0482..0a390d3 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -23,9 +23,9 @@ import ( "strconv" "time" - "github.com/onmetal/dpservice-go-library/dpdk/client" - "github.com/onmetal/dpservice-go-library/renderer" - "github.com/onmetal/dpservice-go-library/sources" + "github.com/onmetal/dpservice-cli/dpdk/client" + "github.com/onmetal/dpservice-cli/renderer" + "github.com/onmetal/dpservice-cli/sources" dpdkproto "github.com/onmetal/net-dpservice-go/proto" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -187,7 +187,7 @@ func ParseRouteKeyArgs(args []string) ([]RouteKey, error) { return nil, fmt.Errorf("expected args to be a multiple of 3 but got %d", len(args)) } - keys := make([]RouteKey, len(args)%3) + keys := make([]RouteKey, len(args)/3) for i := 0; i < len(args); i += 3 { key, err := ParseRouteKey(args[i], args[i+1], args[i+2]) if err != nil { @@ -213,8 +213,9 @@ 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"} + 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"} ) diff --git a/cmd/create.go b/cmd/create.go index a601d65..cef7d65 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -19,9 +19,9 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-go-library/dpdk/api" - "github.com/onmetal/dpservice-go-library/dpdk/client/dynamic" - "github.com/onmetal/dpservice-go-library/sources" + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/client/dynamic" + "github.com/onmetal/dpservice-cli/sources" "github.com/spf13/cobra" ) diff --git a/cmd/create_interface.go b/cmd/create_interface.go index d2b1659..83ff374 100644 --- a/cmd/create_interface.go +++ b/cmd/create_interface.go @@ -20,9 +20,9 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-go-library/dpdk/api" - "github.com/onmetal/dpservice-go-library/flag" - "github.com/onmetal/dpservice-go-library/util" + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/flag" + "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/create_prefix.go b/cmd/create_prefix.go index 038bab5..604ebb9 100644 --- a/cmd/create_prefix.go +++ b/cmd/create_prefix.go @@ -20,8 +20,8 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-go-library/dpdk/api" - "github.com/onmetal/dpservice-go-library/util" + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/create_route.go b/cmd/create_route.go index a2e80de..dd47bc3 100644 --- a/cmd/create_route.go +++ b/cmd/create_route.go @@ -21,8 +21,8 @@ import ( "os" "strconv" - "github.com/onmetal/dpservice-go-library/dpdk/api" - "github.com/onmetal/dpservice-go-library/util" + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/create_virtualip.go b/cmd/create_virtualip.go index 3e96408..36c4c7f 100644 --- a/cmd/create_virtualip.go +++ b/cmd/create_virtualip.go @@ -20,8 +20,8 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-go-library/dpdk/api" - "github.com/onmetal/dpservice-go-library/util" + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/delete.go b/cmd/delete.go index bfc8ea9..ac04806 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -19,9 +19,9 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-go-library/dpdk/api" - "github.com/onmetal/dpservice-go-library/dpdk/client/dynamic" - "github.com/onmetal/dpservice-go-library/sources" + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/client/dynamic" + "github.com/onmetal/dpservice-cli/sources" "github.com/spf13/cobra" ) diff --git a/cmd/delete_interface.go b/cmd/delete_interface.go index 888d22b..f329dec 100644 --- a/cmd/delete_interface.go +++ b/cmd/delete_interface.go @@ -18,7 +18,7 @@ import ( "context" "fmt" - "github.com/onmetal/dpservice-go-library/util" + "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/delete_prefix.go b/cmd/delete_prefix.go index 7725b0c..38b4dc8 100644 --- a/cmd/delete_prefix.go +++ b/cmd/delete_prefix.go @@ -19,7 +19,7 @@ import ( "fmt" "net/netip" - "github.com/onmetal/dpservice-go-library/util" + "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/delete_route.go b/cmd/delete_route.go index 0b90e5a..bb3c8c0 100644 --- a/cmd/delete_route.go +++ b/cmd/delete_route.go @@ -18,7 +18,7 @@ import ( "context" "fmt" - "github.com/onmetal/dpservice-go-library/util" + "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/delete_virtualip.go b/cmd/delete_virtualip.go index b2d4c7b..90f0c94 100644 --- a/cmd/delete_virtualip.go +++ b/cmd/delete_virtualip.go @@ -18,7 +18,7 @@ import ( "context" "fmt" - "github.com/onmetal/dpservice-go-library/util" + "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/get.go b/cmd/get.go index eae0a2c..f3d7c0d 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -36,6 +36,7 @@ func Get(factory DPDKClientFactory) *cobra.Command { GetPrefix(factory, rendererOptions), GetRoute(factory, rendererOptions), GetVirtualIP(factory, rendererOptions), + GetLoadBalancer(factory, rendererOptions), } cmd.Short = fmt.Sprintf("Gets/Lists one of %v", CommandNames(subcommands)) diff --git a/cmd/get_interface.go b/cmd/get_interface.go index 08db029..ae4f9b7 100644 --- a/cmd/get_interface.go +++ b/cmd/get_interface.go @@ -19,7 +19,7 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-go-library/util" + "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/get_loadbalancer.go b/cmd/get_loadbalancer.go new file mode 100644 index 0000000..c386712 --- /dev/null +++ b/cmd/get_loadbalancer.go @@ -0,0 +1,103 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func GetLoadBalancer(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + var ( + opts GetLoadBalancerOptions + ) + + cmd := &cobra.Command{ + Use: "loadbalancer", + Short: "Get or list loadbalancer(s)", + Aliases: LoadBalancerAliases, + RunE: func(cmd *cobra.Command, args []string) error { + loadbalancerIDs := args + return RunGetLoadBalancer( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + loadbalancerIDs, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type GetLoadBalancerOptions struct { +} + +func (o *GetLoadBalancerOptions) AddFlags(fs *pflag.FlagSet) { +} + +func (o *GetLoadBalancerOptions) MarkRequiredFlags(cmd *cobra.Command) error { + return nil +} + +func RunGetLoadBalancer( + ctx context.Context, + dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, + loadbalancerIDs []string, + opts GetLoadBalancerOptions, +) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error getting dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + renderer, err := rendererFactory.NewRenderer("", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + + if len(loadbalancerIDs) == 0 { + // TODO implement list lbs + return fmt.Errorf("list loadbalancers not implemented") + } + + for _, loadbalancerID := range loadbalancerIDs { + lb, err := client.GetLoadBalancer(ctx, loadbalancerID) + if err != nil { + return fmt.Errorf("error getting loadbalancer: %w", err) + } + + if err := renderer.Render(lb); err != nil { + return fmt.Errorf("error rendering loadbalancer %s: %w", loadbalancerID, err) + } + } + return nil +} diff --git a/cmd/get_prefix.go b/cmd/get_prefix.go index 3a3425e..c2d46ac 100644 --- a/cmd/get_prefix.go +++ b/cmd/get_prefix.go @@ -20,7 +20,7 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-go-library/util" + "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/get_route.go b/cmd/get_route.go index c732a18..d6711c3 100644 --- a/cmd/get_route.go +++ b/cmd/get_route.go @@ -19,7 +19,7 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-go-library/util" + "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/get_virtualip.go b/cmd/get_virtualip.go index 3d25bd3..642fd75 100644 --- a/cmd/get_virtualip.go +++ b/cmd/get_virtualip.go @@ -19,7 +19,7 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-go-library/util" + "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cover.out b/cover.out index e3ac315..c27914a 100644 --- a/cover.out +++ b/cover.out @@ -1,5 +1,5 @@ mode: set -github.com/onmetal/dpservice-go-library/io/reader.go:28.59,32.2 1 1 -github.com/onmetal/dpservice-go-library/io/reader.go:34.62,38.2 3 1 -github.com/onmetal/dpservice-go-library/io/reader.go:40.41,43.2 2 0 -github.com/onmetal/dpservice-go-library/io/reader.go:45.56,49.2 3 1 +github.com/onmetal/dpservice-cli/io/reader.go:28.59,32.2 1 1 +github.com/onmetal/dpservice-cli/io/reader.go:34.62,38.2 3 1 +github.com/onmetal/dpservice-cli/io/reader.go:40.41,43.2 2 0 +github.com/onmetal/dpservice-cli/io/reader.go:45.56,49.2 3 1 diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index 36451bf..4aba571 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -21,6 +21,49 @@ import ( proto "github.com/onmetal/net-dpservice-go/proto" ) +func ProtoLoadBalancerToLoadBalancer(dpdkLB *proto.GetLoadBalancerResponse, lbID string) (*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.GetLbVipIP().Address); lbipString != "" { + var err error + lbip, err = netip.ParseAddr(string(dpdkLB.GetLbVipIP().Address)) + if err != nil { + return nil, fmt.Errorf("error parsing lb ip: %w", err) + } + } + var ports = make([]uint32, len(dpdkLB.Lbports)) + for _, port := range dpdkLB.Lbports { + ports = append(ports, port.Port) + } + + return &LoadBalancer{ + TypeMeta: TypeMeta{ + Kind: LoadBalancerKind, + }, + LoadBalancerMeta: LoadBalancerMeta{ + ID: lbID, + }, + Spec: LoadBalancerSpec{ + VNI: dpdkLB.Vni, + LbVipIP: lbip, + Lbports: ports, + UnderlayRoute: underlayRoute, + }, + Status: LoadBalancerStatus{ + Error: dpdkLB.Status.Error, + Message: dpdkLB.Status.Message, + }, + }, nil +} + func ProtoInterfaceToInterface(dpdkIface *proto.Interface) (*Interface, error) { var ips []netip.Addr diff --git a/dpdk/api/register.go b/dpdk/api/register.go index b82da8f..bf12d08 100644 --- a/dpdk/api/register.go +++ b/dpdk/api/register.go @@ -15,7 +15,7 @@ package api import ( - "github.com/onmetal/dpservice-go-library/dpdk/runtime" + "github.com/onmetal/dpservice-cli/dpdk/runtime" ) var DefaultScheme = runtime.NewScheme() diff --git a/dpdk/api/types.go b/dpdk/api/types.go index a368763..5606389 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -123,6 +123,46 @@ func (m *VirtualIPMeta) GetName() string { type VirtualIPSpec struct { } +type LoadBalancer struct { + TypeMeta `json:",inline"` + LoadBalancerMeta `json:"metadata"` + Spec LoadBalancerSpec `json:"spec"` + Status LoadBalancerStatus `json:"status"` +} + +type LoadBalancerMeta struct { + ID string `json:"id"` +} + +func (m *LoadBalancerMeta) GetName() string { + return m.ID +} + +type LoadBalancerSpec struct { + VNI uint32 `json:"vni"` + LbVipIP netip.Addr `json:"lbVipIP"` + Lbports []uint32 `json:"lbports"` + UnderlayRoute netip.Addr `json:"underlayRoute"` +} + +type LoadBalancerStatus struct { + Error int32 `json:"error,omitempty"` + Message string `json:"message,omitempty"` +} + +type LoadBalancerList struct { + TypeMeta `json:",inline"` + Items []LoadBalancer `json:"items"` +} + +func (l *LoadBalancerList) GetItems() []Object { + res := make([]Object, len(l.Items)) + for i := range l.Items { + res[i] = &l.Items[i] + } + return res +} + type Interface struct { TypeMeta `json:",inline"` InterfaceMeta `json:"metadata"` @@ -162,11 +202,14 @@ func (l *InterfaceList) GetItems() []Object { } var ( - InterfaceKind = reflect.TypeOf(Interface{}).Name() - InterfaceListKind = reflect.TypeOf(InterfaceList{}).Name() - PrefixKind = reflect.TypeOf(Prefix{}).Name() - PrefixListKind = reflect.TypeOf(PrefixList{}).Name() - VirtualIPKind = reflect.TypeOf(VirtualIP{}).Name() - RouteKind = reflect.TypeOf(Route{}).Name() - RouteListKind = reflect.TypeOf(RouteList{}).Name() + LoadBalancerKind = reflect.TypeOf(Interface{}).Name() + InterfaceKind = reflect.TypeOf(Interface{}).Name() + InterfaceListKind = reflect.TypeOf(InterfaceList{}).Name() + LoadbalancerKind = reflect.TypeOf(Interface{}).Name() + LoadbalancerListKind = reflect.TypeOf(InterfaceList{}).Name() + PrefixKind = reflect.TypeOf(Prefix{}).Name() + PrefixListKind = reflect.TypeOf(PrefixList{}).Name() + VirtualIPKind = reflect.TypeOf(VirtualIP{}).Name() + RouteKind = reflect.TypeOf(Route{}).Name() + RouteListKind = reflect.TypeOf(RouteList{}).Name() ) diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 40d17e1..324d069 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -19,13 +19,15 @@ import ( "fmt" "net/netip" - "github.com/onmetal/dpservice-go-library/dpdk/api" - apierrors "github.com/onmetal/dpservice-go-library/dpdk/api/errors" - "github.com/onmetal/dpservice-go-library/netiputil" + "github.com/onmetal/dpservice-cli/dpdk/api" + apierrors "github.com/onmetal/dpservice-cli/dpdk/api/errors" + "github.com/onmetal/dpservice-cli/netiputil" dpdkproto "github.com/onmetal/net-dpservice-go/proto" ) type Client interface { + GetLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) + GetInterface(ctx context.Context, id string) (*api.Interface, error) ListInterfaces(ctx context.Context) (*api.InterfaceList, error) CreateInterface(ctx context.Context, iface *api.Interface) (*api.Interface, error) @@ -52,6 +54,18 @@ func NewClient(protoClient dpdkproto.DPDKonmetalClient) Client { return &client{protoClient} } +func (c *client) GetLoadBalancer(ctx context.Context, name string) (*api.LoadBalancer, error) { + res, err := c.DPDKonmetalClient.GetLoadBalancer(ctx, &dpdkproto.GetLoadBalancerRequest{LoadBalancerID: []byte(name)}) + if err != nil { + return nil, err + } + if errorCode := res.GetStatus().GetError(); errorCode != 0 { + return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + } + lb, err := api.ProtoLoadBalancerToLoadBalancer(res, name) + return lb, err +} + func (c *client) GetInterface(ctx context.Context, name string) (*api.Interface, error) { res, err := c.DPDKonmetalClient.GetInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(name)}) if err != nil { diff --git a/dpdk/client/dynamic/dynamic.go b/dpdk/client/dynamic/dynamic.go index 1daa608..13d848d 100644 --- a/dpdk/client/dynamic/dynamic.go +++ b/dpdk/client/dynamic/dynamic.go @@ -19,8 +19,8 @@ import ( "fmt" "net/netip" - "github.com/onmetal/dpservice-go-library/dpdk/api" - structured "github.com/onmetal/dpservice-go-library/dpdk/client" + "github.com/onmetal/dpservice-cli/dpdk/api" + structured "github.com/onmetal/dpservice-cli/dpdk/client" ) type ObjectKey interface { diff --git a/dpdk/runtime/decoder.go b/dpdk/runtime/decoder.go index 5701a8b..c7b3de1 100644 --- a/dpdk/runtime/decoder.go +++ b/dpdk/runtime/decoder.go @@ -20,7 +20,7 @@ import ( "io" yaml2 "github.com/ghodss/yaml" - dpsvcio "github.com/onmetal/dpservice-go-library/io" + dpsvcio "github.com/onmetal/dpservice-cli/io" "gopkg.in/yaml.v2" ) diff --git a/go.mod b/go.mod index f43263f..3b5a23c 100644 --- a/go.mod +++ b/go.mod @@ -1,32 +1,32 @@ -module github.com/onmetal/dpservice-go-library +module github.com/onmetal/dpservice-cli go 1.18 require ( github.com/ghodss/yaml v1.0.0 - github.com/google/addlicense v1.0.0 + github.com/google/addlicense v1.1.1 github.com/jedib0t/go-pretty/v6 v6.3.9 - github.com/onmetal/net-dpservice-go v0.1.3 + github.com/onmetal/net-dpservice-go v0.1.9 github.com/onsi/ginkgo/v2 v2.2.0 github.com/onsi/gomega v1.20.2 github.com/spf13/cobra v1.4.0 github.com/spf13/pflag v1.0.5 - google.golang.org/grpc v1.49.0 + google.golang.org/grpc v1.53.0 gopkg.in/yaml.v2 v2.4.0 ) require ( github.com/bmatcuk/doublestar/v4 v4.0.2 // indirect github.com/golang/protobuf v1.5.2 // indirect - github.com/google/go-cmp v0.5.8 // indirect + github.com/google/go-cmp v0.5.9 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect github.com/rivo/uniseg v0.2.0 // indirect - golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect + golang.org/x/net v0.5.0 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect - golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect - golang.org/x/text v0.3.7 // indirect - google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154 // indirect + golang.org/x/sys v0.4.0 // indirect + golang.org/x/text v0.6.0 // indirect + google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect google.golang.org/protobuf v1.28.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 027bf0e..f1a2406 100644 --- a/go.sum +++ b/go.sum @@ -1,47 +1,27 @@ -cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/bmatcuk/doublestar/v4 v4.0.2 h1:X0krlUVAVmtr2cRoTqR8aDMrDqnB36ht8wpWTiQ3jsA= github.com/bmatcuk/doublestar/v4 v4.0.2/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= -github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= -github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= -github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= -github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= -github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= -github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= -github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= -github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/google/addlicense v1.0.0 h1:cqvo5suPWlsk6r6o42Fs2K66xYCl2tnhVPUYoP3EnO4= -github.com/google/addlicense v1.0.0/go.mod h1:Sm/DHu7Jk+T5miFHHehdIjbi4M5+dJDRS3Cq0rncIxA= -github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/addlicense v1.1.1 h1:jpVf9qPbU8rz5MxKo7d+RMcNHkqxi4YJi/laauX4aAE= +github.com/google/addlicense v1.1.1/go.mod h1:Sm/DHu7Jk+T5miFHHehdIjbi4M5+dJDRS3Cq0rncIxA= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= -github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jedib0t/go-pretty/v6 v6.3.9 h1:GAK/1WJY9WVVrKd601HGB89ihLBDfJnUIJye31PY+uk= github.com/jedib0t/go-pretty/v6 v6.3.9/go.mod h1:MgmISkTWDSFu0xOqiZ0mKNntMQ2mDgOcwOkwBEkMDJI= github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/onmetal/net-dpservice-go v0.1.3 h1:wkZRzcn06chZ2faWkevRx1Jmx07fDQi53eFq4jmc2GA= -github.com/onmetal/net-dpservice-go v0.1.3/go.mod h1:xclo8jzH6XJsWuz1WB/ivgSYLii5PqufJQ3jr77SD5k= +github.com/onmetal/net-dpservice-go v0.1.9 h1:PXJDl5SFe/BnOeDCAnTJJlRTRuj33YgsEkifrHb+0DA= +github.com/onmetal/net-dpservice-go v0.1.9/go.mod h1:dbItLY1P+nYl7/c04JdJTEaMCG6GLXgAZsMP/OuZBmg= github.com/onsi/ginkgo/v2 v2.2.0 h1:3ZNA3L1c5FYDFTTxbFeVGGD8jYvjYauHD30YgLxVsNI= github.com/onsi/ginkgo/v2 v2.2.0/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk= github.com/onsi/gomega v1.20.2 h1:8uQq0zMgLEfa0vRrrBgaJF2gyW9Da9BmfGV+OyUzfkY= @@ -49,7 +29,6 @@ github.com/onsi/gomega v1.20.2/go.mod h1:iYAIXgPSaDHak0LCMA+AWBpIKBr8WZicMxnE8lu github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -62,57 +41,21 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.4 h1:wZRexSlwd7ZXfKINDLsO4r7WBt3gTKONc6K/VesHvHM= github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= -golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= -golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw= +golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= -golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k= +golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= -google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= -google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154 h1:bFFRpT+e8JJVY7lMMfvezL1ZIwqiwmPl2bsE2yx4HqM= -google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= -google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= -google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.49.0 h1:WTLtQzmQori5FUH25Pq4WT22oCsv8USpQ+F6rqtsmxw= -google.golang.org/grpc v1.49.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI= -google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= -google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= -google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= -google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= -google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= -google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= +google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f h1:BWUVssLB0HVOSY78gIdvk1dTVYtT1y8SBWtPYuTJ/6w= +google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM= +google.golang.org/grpc v1.53.0 h1:LAv2ds7cmFV/XTS3XG1NneeENYrXGmorPxsBbptIjNc= +google.golang.org/grpc v1.53.0/go.mod h1:OnIrk0ipVdj4N5d9IUoFUx72/VlD7+jUsHwZgwSMQpw= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= @@ -124,5 +67,3 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/io/reader_test.go b/io/reader_test.go index 5dbf346..0ca3ab4 100644 --- a/io/reader_test.go +++ b/io/reader_test.go @@ -17,7 +17,7 @@ package io_test import ( "bytes" - . "github.com/onmetal/dpservice-go-library/io" + . "github.com/onmetal/dpservice-cli/io" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" ) diff --git a/main.go b/main.go index cc875dc..f017dc8 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-go-library/cmd" + "github.com/onmetal/dpservice-cli/cmd" ) func main() { diff --git a/renderer/renderer.go b/renderer/renderer.go index 00db685..80c66b5 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -23,7 +23,7 @@ import ( "github.com/ghodss/yaml" "github.com/jedib0t/go-pretty/v6/table" - "github.com/onmetal/dpservice-go-library/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api" ) type Renderer interface { @@ -147,6 +147,8 @@ var DefaultTableConverter = defaultTableConverter{} func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) { switch obj := v.(type) { + case *api.LoadBalancer: + return t.loadbalancerTable(*obj) case *api.Interface: return t.interfaceTable([]api.Interface{*obj}) case *api.InterfaceList: @@ -166,6 +168,20 @@ func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) { } } +func (t defaultTableConverter) loadbalancerTable(lbs api.LoadBalancer) (*TableData, error) { + headers := []any{"ID", "VNI", "LbVipIP", "Lbports", "UnderlayRoute"} + + columns := make([][]any, 1) + //for i, lb := range lbs { + columns[0] = []any{lbs.ID, lbs.Spec.VNI, lbs.Spec.LbVipIP, lbs.Spec.Lbports, lbs.Spec.UnderlayRoute} + //} + + return &TableData{ + Headers: headers, + Columns: columns, + }, nil +} + func (t defaultTableConverter) interfaceTable(ifaces []api.Interface) (*TableData, error) { headers := []any{"ID", "VNI", "Device", "IPs", "UnderlayIP"} diff --git a/sources/sources.go b/sources/sources.go index fe8500b..cb6b19a 100644 --- a/sources/sources.go +++ b/sources/sources.go @@ -21,8 +21,8 @@ import ( "os" "path/filepath" - "github.com/onmetal/dpservice-go-library/dpdk/api" - runtime2 "github.com/onmetal/dpservice-go-library/dpdk/runtime" + "github.com/onmetal/dpservice-cli/dpdk/api" + runtime2 "github.com/onmetal/dpservice-cli/dpdk/runtime" ) type Iterator struct { From 3abaccbfd5b2b35eeb90c85e9f30be071fd6984e Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Wed, 26 Apr 2023 16:32:21 +0200 Subject: [PATCH 02/65] add CreateLoadBalancer function --- .vscode/launch.json | 6 +- cmd/create.go | 1 + cmd/create_loadbalancer.go | 126 +++++++++++++++++++++++++++++++++++++ cmd/get_loadbalancer.go | 3 +- dpdk/api/conversion.go | 25 +++++++- dpdk/api/register.go | 1 + dpdk/api/types.go | 19 ++++-- dpdk/client/client.go | 43 ++++++++++++- renderer/renderer.go | 12 +++- 9 files changed, 220 insertions(+), 16 deletions(-) create mode 100644 cmd/create_loadbalancer.go diff --git a/.vscode/launch.json b/.vscode/launch.json index 1c3d619..66f2018 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,9 +9,11 @@ "type": "go", "request": "launch", "mode": "auto", - "program": "${fileDirname}", - "args": ["get", "loadbalancer", "1"] + "program": "main.go", + //"program": "${fileDirname}", + //"args": ["create", "loadbalancer", "7", "--lbports", "389", "--vip", "10.3.4.5", "--vni", "100"] //"args": ["get", "route", "0.0.0.0/24", "0", "fc00:2::64:0:1", "--vni", "100"] + "args": ["get", "lb", "5"] } ] } \ No newline at end of file diff --git a/cmd/create.go b/cmd/create.go index cef7d65..f35b8e6 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -47,6 +47,7 @@ func Create(dpdkClientFactory DPDKClientFactory) *cobra.Command { CreatePrefix(dpdkClientFactory, rendererOptions), CreateRoute(dpdkClientFactory, rendererOptions), CreateVirtualIP(dpdkClientFactory, rendererOptions), + CreateLoadBalancer(dpdkClientFactory, rendererOptions), } cmd.Short = fmt.Sprintf("Creates one of %v", CommandNames(subcommands)) diff --git a/cmd/create_loadbalancer.go b/cmd/create_loadbalancer.go new file mode 100644 index 0000000..9022a9b --- /dev/null +++ b/cmd/create_loadbalancer.go @@ -0,0 +1,126 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "net/netip" + "os" + + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/flag" + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func CreateLoadBalancer(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + var ( + opts CreateLoadBalancerOptions + ) + + cmd := &cobra.Command{ + Use: "loadbalancer --vni --vip --lbports [ports]", + Short: "Create a loadbalancer", + Aliases: LoadBalancerAliases, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + loadbalancerID := args[0] + return RunCreateLoadBalancer( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + loadbalancerID, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type CreateLoadBalancerOptions struct { + VNI uint32 + LbVipIP netip.Addr + Lbports []string +} + +func (o *CreateLoadBalancerOptions) AddFlags(fs *pflag.FlagSet) { + fs.Uint32Var(&o.VNI, "vni", o.VNI, "VNI to add the loadbalancer to.") + //fs.IP("vip", net.IP(o.LbVipIP.AsSlice()), "VIP to assign to the loadbalancer.") + flag.AddrVar(fs, &o.LbVipIP, "vip", o.LbVipIP, "VIP to assign to the loadbalancer.") + fs.StringSliceVar(&o.Lbports, "lbports", o.Lbports, "LB ports to assign to the loadbalancer") + +} + +func (o *CreateLoadBalancerOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"vni", "vip", "lbports"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunCreateLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, loadbalancerID string, opts CreateLoadBalancerOptions) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + renderer, err := rendererFactory.NewRenderer("created", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + + var ports = make([]api.LBPort, 0) + for _, p := range opts.Lbports { + port, err := api.StringLbportToLbport(p) + if err != nil { + return err + } + ports = append(ports, port) + } + + lb, err := client.CreateLoadBalancer(ctx, &api.LoadBalancer{ + LoadBalancerMeta: api.LoadBalancerMeta{ + ID: loadbalancerID, + }, + Spec: api.LoadBalancerSpec{ + VNI: opts.VNI, + LbVipIP: opts.LbVipIP, + Lbports: ports, + }, + }) + fmt.Println(lb) + if err != nil { + return fmt.Errorf("error creating loadbalancer: %w", err) + } + + if err := renderer.Render(lb); err != nil { + return fmt.Errorf("error rendering loadbalancer: %w", err) + } + return nil +} diff --git a/cmd/get_loadbalancer.go b/cmd/get_loadbalancer.go index c386712..7db912c 100644 --- a/cmd/get_loadbalancer.go +++ b/cmd/get_loadbalancer.go @@ -30,7 +30,7 @@ func GetLoadBalancer(dpdkClientFactory DPDKClientFactory, rendererFactory Render ) cmd := &cobra.Command{ - Use: "loadbalancer", + Use: "loadbalancer ", Short: "Get or list loadbalancer(s)", Aliases: LoadBalancerAliases, RunE: func(cmd *cobra.Command, args []string) error { @@ -85,7 +85,6 @@ func RunGetLoadBalancer( } if len(loadbalancerIDs) == 0 { - // TODO implement list lbs return fmt.Errorf("list loadbalancers not implemented") } diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index 4aba571..952c2a0 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -17,7 +17,10 @@ package api import ( "fmt" "net/netip" + "strconv" + "strings" + dpdkproto "github.com/onmetal/net-dpservice-go/proto" proto "github.com/onmetal/net-dpservice-go/proto" ) @@ -39,9 +42,12 @@ func ProtoLoadBalancerToLoadBalancer(dpdkLB *proto.GetLoadBalancerResponse, lbID return nil, fmt.Errorf("error parsing lb ip: %w", err) } } - var ports = make([]uint32, len(dpdkLB.Lbports)) + var ports = make([]LBPort, 0) + var p LBPort for _, port := range dpdkLB.Lbports { - ports = append(ports, port.Port) + p.Protocol = uint32(port.Protocol) + p.Port = port.Port + ports = append(ports, p) } return &LoadBalancer{ @@ -64,6 +70,21 @@ func ProtoLoadBalancerToLoadBalancer(dpdkLB *proto.GetLoadBalancerResponse, lbID }, nil } +func LbipToProtoLbip(lbip netip.Addr) *proto.LBIP { + return &proto.LBIP{IpVersion: NetIPAddrToProtoIPVersion(lbip), Address: []byte(lbip.String())} +} + +func StringLbportToLbport(lbport string) (LBPort, error) { + p := strings.Split(lbport, "/") + protocolName := p[0] + protocol := dpdkproto.Protocol_value[protocolName] + port, err := strconv.Atoi(p[1]) + if err != nil { + return LBPort{}, fmt.Errorf("error parsing port number: %w", err) + } + return LBPort{Protocol: uint32(protocol), Port: uint32(port)}, nil +} + func ProtoInterfaceToInterface(dpdkIface *proto.Interface) (*Interface, error) { var ips []netip.Addr diff --git a/dpdk/api/register.go b/dpdk/api/register.go index bf12d08..a00c455 100644 --- a/dpdk/api/register.go +++ b/dpdk/api/register.go @@ -29,6 +29,7 @@ func init() { &Route{}, &RouteList{}, &VirtualIP{}, + &LoadBalancer{}, ); err != nil { panic(err) } diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 5606389..f8880d8 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -123,6 +123,7 @@ func (m *VirtualIPMeta) GetName() string { type VirtualIPSpec struct { } +// Loadbalancer section type LoadBalancer struct { TypeMeta `json:",inline"` LoadBalancerMeta `json:"metadata"` @@ -141,7 +142,7 @@ func (m *LoadBalancerMeta) GetName() string { type LoadBalancerSpec struct { VNI uint32 `json:"vni"` LbVipIP netip.Addr `json:"lbVipIP"` - Lbports []uint32 `json:"lbports"` + Lbports []LBPort `json:"lbports"` UnderlayRoute netip.Addr `json:"underlayRoute"` } @@ -150,6 +151,16 @@ type LoadBalancerStatus struct { Message string `json:"message,omitempty"` } +type LBIP struct { + IpVersion bool `json:"ipVersion"` + Address []byte `json:"address"` +} + +type LBPort struct { + Protocol uint32 `json:"protocol"` + Port uint32 `json:"port"` +} + type LoadBalancerList struct { TypeMeta `json:",inline"` Items []LoadBalancer `json:"items"` @@ -163,6 +174,7 @@ func (l *LoadBalancerList) GetItems() []Object { return res } +// Interface section type Interface struct { TypeMeta `json:",inline"` InterfaceMeta `json:"metadata"` @@ -202,11 +214,10 @@ func (l *InterfaceList) GetItems() []Object { } var ( - LoadBalancerKind = reflect.TypeOf(Interface{}).Name() InterfaceKind = reflect.TypeOf(Interface{}).Name() InterfaceListKind = reflect.TypeOf(InterfaceList{}).Name() - LoadbalancerKind = reflect.TypeOf(Interface{}).Name() - LoadbalancerListKind = reflect.TypeOf(InterfaceList{}).Name() + LoadBalancerKind = reflect.TypeOf(LoadBalancer{}).Name() + LoadBalancerListKind = reflect.TypeOf(LoadBalancerList{}).Name() PrefixKind = reflect.TypeOf(Prefix{}).Name() PrefixListKind = reflect.TypeOf(PrefixList{}).Name() VirtualIPKind = reflect.TypeOf(VirtualIP{}).Name() diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 324d069..d80b328 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -27,6 +27,7 @@ import ( type Client interface { GetLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) + CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) (*api.LoadBalancer, error) GetInterface(ctx context.Context, id string) (*api.Interface, error) ListInterfaces(ctx context.Context) (*api.InterfaceList, error) @@ -54,18 +55,54 @@ func NewClient(protoClient dpdkproto.DPDKonmetalClient) Client { return &client{protoClient} } -func (c *client) GetLoadBalancer(ctx context.Context, name string) (*api.LoadBalancer, error) { - res, err := c.DPDKonmetalClient.GetLoadBalancer(ctx, &dpdkproto.GetLoadBalancerRequest{LoadBalancerID: []byte(name)}) +func (c *client) GetLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) { + res, err := c.DPDKonmetalClient.GetLoadBalancer(ctx, &dpdkproto.GetLoadBalancerRequest{LoadBalancerID: []byte(id)}) if err != nil { return nil, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) } - lb, err := api.ProtoLoadBalancerToLoadBalancer(res, name) + lb, err := api.ProtoLoadBalancerToLoadBalancer(res, id) return lb, err } +func (c *client) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) (*api.LoadBalancer, error) { + var lbPorts = make([]*dpdkproto.LBPort, 0) + for _, p := range lb.Spec.Lbports { + lbPort := &dpdkproto.LBPort{Port: p.Port, Protocol: dpdkproto.Protocol(p.Protocol)} + lbPorts = append(lbPorts, lbPort) + } + res, err := c.DPDKonmetalClient.CreateLoadBalancer(ctx, &dpdkproto.CreateLoadBalancerRequest{ + LoadBalancerID: []byte(lb.LoadBalancerMeta.ID), + Vni: lb.Spec.VNI, + LbVipIP: api.LbipToProtoLbip(lb.Spec.LbVipIP), + Lbports: lbPorts, + }) + if err != nil { + return nil, err + } + if errorCode := res.GetStatus().GetError(); errorCode != 0 { + return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + } + + underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) + if err != nil { + return nil, fmt.Errorf("error parsing underlay route: %w", err) + } + lb.Spec.UnderlayRoute = underlayRoute + + return &api.LoadBalancer{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerKind}, + LoadBalancerMeta: lb.LoadBalancerMeta, + Spec: lb.Spec, + Status: api.LoadBalancerStatus{ + Error: res.Status.Error, + Message: res.Status.Message, + }, + }, nil +} + func (c *client) GetInterface(ctx context.Context, name string) (*api.Interface, error) { res, err := c.DPDKonmetalClient.GetInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(name)}) if err != nil { diff --git a/renderer/renderer.go b/renderer/renderer.go index 80c66b5..d7f402c 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -19,11 +19,13 @@ import ( "encoding/json" "fmt" "io" + "strconv" "strings" "github.com/ghodss/yaml" "github.com/jedib0t/go-pretty/v6/table" "github.com/onmetal/dpservice-cli/dpdk/api" + dpdkproto "github.com/onmetal/net-dpservice-go/proto" ) type Renderer interface { @@ -172,9 +174,13 @@ func (t defaultTableConverter) loadbalancerTable(lbs api.LoadBalancer) (*TableDa headers := []any{"ID", "VNI", "LbVipIP", "Lbports", "UnderlayRoute"} columns := make([][]any, 1) - //for i, lb := range lbs { - columns[0] = []any{lbs.ID, lbs.Spec.VNI, lbs.Spec.LbVipIP, lbs.Spec.Lbports, lbs.Spec.UnderlayRoute} - //} + + var ports = make([]string, 0, len(lbs.Spec.Lbports)) + for _, port := range lbs.Spec.Lbports { + p := dpdkproto.Protocol_name[int32(port.Protocol)] + "/" + strconv.Itoa(int(port.Port)) + ports = append(ports, p) + } + columns[0] = []any{lbs.ID, lbs.Spec.VNI, lbs.Spec.LbVipIP, ports, lbs.Spec.UnderlayRoute} return &TableData{ Headers: headers, From 9abbcee311491c3f3a387d3b27a69cadff0f5f06 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 27 Apr 2023 16:18:14 +0200 Subject: [PATCH 03/65] add DeleteLoadBalancer function --- cmd/create_loadbalancer.go | 10 ++--- cmd/delete.go | 1 + cmd/delete_loadbalnacer.go | 78 ++++++++++++++++++++++++++++++++++++++ cmd/get_loadbalancer.go | 3 +- dpdk/api/conversion.go | 12 +++--- dpdk/client/client.go | 14 ++++++- 6 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 cmd/delete_loadbalnacer.go diff --git a/cmd/create_loadbalancer.go b/cmd/create_loadbalancer.go index 9022a9b..9108acc 100644 --- a/cmd/create_loadbalancer.go +++ b/cmd/create_loadbalancer.go @@ -33,8 +33,9 @@ func CreateLoadBalancer(dpdkClientFactory DPDKClientFactory, rendererFactory Ren ) cmd := &cobra.Command{ - Use: "loadbalancer --vni --vip --lbports [ports]", + Use: "loadbalancer ", Short: "Create a loadbalancer", + Example: "dpservice-cli create lb 4 --vni 100 --vip 10.20.30.40 --lbports TCP/443,UDP/53", Aliases: LoadBalancerAliases, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { @@ -64,10 +65,8 @@ type CreateLoadBalancerOptions struct { func (o *CreateLoadBalancerOptions) AddFlags(fs *pflag.FlagSet) { fs.Uint32Var(&o.VNI, "vni", o.VNI, "VNI to add the loadbalancer to.") - //fs.IP("vip", net.IP(o.LbVipIP.AsSlice()), "VIP to assign to the loadbalancer.") flag.AddrVar(fs, &o.LbVipIP, "vip", o.LbVipIP, "VIP to assign to the loadbalancer.") fs.StringSliceVar(&o.Lbports, "lbports", o.Lbports, "LB ports to assign to the loadbalancer") - } func (o *CreateLoadBalancerOptions) MarkRequiredFlags(cmd *cobra.Command) error { @@ -95,11 +94,11 @@ func RunCreateLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFact return fmt.Errorf("error creating renderer: %w", err) } - var ports = make([]api.LBPort, 0) + var ports = make([]api.LBPort, 0, len(opts.Lbports)) for _, p := range opts.Lbports { port, err := api.StringLbportToLbport(p) if err != nil { - return err + return fmt.Errorf("error converting port: %w", err) } ports = append(ports, port) } @@ -114,7 +113,6 @@ func RunCreateLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFact Lbports: ports, }, }) - fmt.Println(lb) if err != nil { return fmt.Errorf("error creating loadbalancer: %w", err) } diff --git a/cmd/delete.go b/cmd/delete.go index ac04806..19af7b5 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -46,6 +46,7 @@ func Delete(factory DPDKClientFactory) *cobra.Command { DeletePrefix(factory), DeleteRoute(factory), DeleteVirtualIP(factory), + DeleteLoadBalancer(factory), } cmd.Short = fmt.Sprintf("Deletes one of %v", CommandNames(subcommands)) diff --git a/cmd/delete_loadbalnacer.go b/cmd/delete_loadbalnacer.go new file mode 100644 index 0000000..0c001b3 --- /dev/null +++ b/cmd/delete_loadbalnacer.go @@ -0,0 +1,78 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func DeleteLoadBalancer(factory DPDKClientFactory) *cobra.Command { + var ( + opts DeleteLoadBalancerOptions + ) + + cmd := &cobra.Command{ + Use: "loadbalancer [ ...]", + Short: "Delete loadbalancer(s)", + Aliases: LoadBalancerAliases, + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + loadbalancerIDs := args + return RunDeleteLoadBalancer(cmd.Context(), factory, loadbalancerIDs, opts) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type DeleteLoadBalancerOptions struct { +} + +func (o *DeleteLoadBalancerOptions) AddFlags(fs *pflag.FlagSet) { +} + +func (o *DeleteLoadBalancerOptions) MarkRequiredFlags(cmd *cobra.Command) error { + return nil +} + +func RunDeleteLoadBalancer(ctx context.Context, factory DPDKClientFactory, loadbalancerIDs []string, opts DeleteLoadBalancerOptions) error { + client, cleanup, err := factory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + for _, loadbalancerID := range loadbalancerIDs { + if err := client.DeleteLoadBalancer(ctx, loadbalancerID); err != nil { + return fmt.Errorf("Error deleting loadbalancer %s: %v\n", loadbalancerID, err) + } + + fmt.Println("Deleted loadbalancer", loadbalancerID) + } + return nil +} diff --git a/cmd/get_loadbalancer.go b/cmd/get_loadbalancer.go index 7db912c..f46426e 100644 --- a/cmd/get_loadbalancer.go +++ b/cmd/get_loadbalancer.go @@ -32,6 +32,7 @@ func GetLoadBalancer(dpdkClientFactory DPDKClientFactory, rendererFactory Render cmd := &cobra.Command{ Use: "loadbalancer ", Short: "Get or list loadbalancer(s)", + Example: "dpservice-cli get lb 4", Aliases: LoadBalancerAliases, RunE: func(cmd *cobra.Command, args []string) error { loadbalancerIDs := args @@ -85,7 +86,7 @@ func RunGetLoadBalancer( } if len(loadbalancerIDs) == 0 { - return fmt.Errorf("list loadbalancers not implemented") + return fmt.Errorf("need to specify loadbalancer id") } for _, loadbalancerID := range loadbalancerIDs { diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index 952c2a0..8f92307 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -42,12 +42,12 @@ func ProtoLoadBalancerToLoadBalancer(dpdkLB *proto.GetLoadBalancerResponse, lbID return nil, fmt.Errorf("error parsing lb ip: %w", err) } } - var ports = make([]LBPort, 0) + var lbports = make([]LBPort, 0, len(dpdkLB.Lbports)) var p LBPort - for _, port := range dpdkLB.Lbports { - p.Protocol = uint32(port.Protocol) - p.Port = port.Port - ports = append(ports, p) + for _, lbport := range dpdkLB.Lbports { + p.Protocol = uint32(lbport.Protocol) + p.Port = lbport.Port + lbports = append(lbports, p) } return &LoadBalancer{ @@ -60,7 +60,7 @@ func ProtoLoadBalancerToLoadBalancer(dpdkLB *proto.GetLoadBalancerResponse, lbID Spec: LoadBalancerSpec{ VNI: dpdkLB.Vni, LbVipIP: lbip, - Lbports: ports, + Lbports: lbports, UnderlayRoute: underlayRoute, }, Status: LoadBalancerStatus{ diff --git a/dpdk/client/client.go b/dpdk/client/client.go index d80b328..a330e1d 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -28,6 +28,7 @@ import ( type Client interface { GetLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) (*api.LoadBalancer, error) + DeleteLoadBalancer(ctx context.Context, id string) error GetInterface(ctx context.Context, id string) (*api.Interface, error) ListInterfaces(ctx context.Context) (*api.InterfaceList, error) @@ -68,7 +69,7 @@ func (c *client) GetLoadBalancer(ctx context.Context, id string) (*api.LoadBalan } func (c *client) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) (*api.LoadBalancer, error) { - var lbPorts = make([]*dpdkproto.LBPort, 0) + var lbPorts = make([]*dpdkproto.LBPort, 0, len(lb.Spec.Lbports)) for _, p := range lb.Spec.Lbports { lbPort := &dpdkproto.LBPort{Port: p.Port, Protocol: dpdkproto.Protocol(p.Protocol)} lbPorts = append(lbPorts, lbPort) @@ -103,6 +104,17 @@ func (c *client) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) ( }, nil } +func (c *client) DeleteLoadBalancer(ctx context.Context, id string) error { + res, err := c.DPDKonmetalClient.DeleteLoadBalancer(ctx, &dpdkproto.DeleteLoadBalancerRequest{LoadBalancerID: []byte(id)}) + if err != nil { + return err + } + if errorCode := res.GetError(); errorCode != 0 { + return apierrors.NewStatusError(errorCode, res.GetMessage()) + } + return nil +} + func (c *client) GetInterface(ctx context.Context, name string) (*api.Interface, error) { res, err := c.DPDKonmetalClient.GetInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(name)}) if err != nil { From 42194be63f37801be06fd18d042fbba55a707fc0 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 2 May 2023 09:01:43 +0200 Subject: [PATCH 04/65] add ListLoadBalancerPrefixes func --- .vscode/launch.json | 2 +- cmd/common.go | 11 ++-- cmd/delete_prefix.go | 2 +- cmd/get.go | 1 + cmd/get_loadbalancer_prefix.go | 112 +++++++++++++++++++++++++++++++++ dpdk/api/conversion.go | 14 ++++- dpdk/api/types.go | 5 +- dpdk/client/client.go | 26 ++++++++ renderer/renderer.go | 5 +- 9 files changed, 165 insertions(+), 13 deletions(-) create mode 100644 cmd/get_loadbalancer_prefix.go diff --git a/.vscode/launch.json b/.vscode/launch.json index 66f2018..637f08e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,7 +13,7 @@ //"program": "${fileDirname}", //"args": ["create", "loadbalancer", "7", "--lbports", "389", "--vip", "10.3.4.5", "--vni", "100"] //"args": ["get", "route", "0.0.0.0/24", "0", "fc00:2::64:0:1", "--vni", "100"] - "args": ["get", "lb", "5"] + "args": ["get", "lbprefix", "--interface-id", "vm1"] } ] } \ No newline at end of file diff --git a/cmd/common.go b/cmd/common.go index 0a390d3..cc59881 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -213,9 +213,10 @@ 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"} + 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"} ) diff --git a/cmd/delete_prefix.go b/cmd/delete_prefix.go index 38b4dc8..5998299 100644 --- a/cmd/delete_prefix.go +++ b/cmd/delete_prefix.go @@ -83,7 +83,7 @@ func RunDeletePrefix(ctx context.Context, factory DPDKClientFactory, prefixes [] if err := client.DeletePrefix(ctx, opts.InterfaceID, prefix); err != nil { fmt.Printf("Error deleting prefix %s/%v: %v\n", opts.InterfaceID, prefix, err) } - fmt.Printf("Deleted prefix %s/%v", opts.InterfaceID, prefix) + fmt.Printf("Deleted prefix %s/%v\n", opts.InterfaceID, prefix) } return nil } diff --git a/cmd/get.go b/cmd/get.go index f3d7c0d..a9d5634 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -37,6 +37,7 @@ func Get(factory DPDKClientFactory) *cobra.Command { GetRoute(factory, rendererOptions), GetVirtualIP(factory, rendererOptions), GetLoadBalancer(factory, rendererOptions), + GetLoadBalancerPrefix(factory, rendererOptions), } cmd.Short = fmt.Sprintf("Gets/Lists one of %v", CommandNames(subcommands)) diff --git a/cmd/get_loadbalancer_prefix.go b/cmd/get_loadbalancer_prefix.go new file mode 100644 index 0000000..91231a8 --- /dev/null +++ b/cmd/get_loadbalancer_prefix.go @@ -0,0 +1,112 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "net/netip" + "os" + + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func GetLoadBalancerPrefix(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + var ( + opts GetPrefixOptions + ) + + cmd := &cobra.Command{ + Use: "lbprefix [...]", + Short: "Get or list loadbalancer prefix(es)", + Aliases: LoadBalancerPrefixAliases, + RunE: func(cmd *cobra.Command, args []string) error { + prefixes, err := ParsePrefixArgs(args) + if err != nil { + return err + } + + return RunGetLoadBalancerPrefix( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + prefixes, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type GetLoadBalancerPrefixOptions struct { + InterfaceID string +} + +func (o *GetLoadBalancerPrefixOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID of the prefix.") +} + +func (o *GetLoadBalancerPrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"interface-id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunGetLoadBalancerPrefix( + ctx context.Context, + factory DPDKClientFactory, + rendererFactory RendererFactory, + prefixes []netip.Prefix, + opts GetPrefixOptions, +) error { + client, cleanup, err := factory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + renderer, err := rendererFactory.NewRenderer("", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + + if len(prefixes) == 0 { + prefixList, err := client.ListLoadBalancerPrefixes(ctx, opts.InterfaceID) + if err != nil { + return fmt.Errorf("error listing loadbalancer prefixes: %w", err) + } + + if err := renderer.Render(prefixList); err != nil { + return fmt.Errorf("error rendering list: %w", err) + } + return nil + } + + return fmt.Errorf("getting individual loadbalancer prefixes is not implemented") +} diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index 8f92307..f4d0bee 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -20,7 +20,6 @@ import ( "strconv" "strings" - dpdkproto "github.com/onmetal/net-dpservice-go/proto" proto "github.com/onmetal/net-dpservice-go/proto" ) @@ -77,7 +76,7 @@ func LbipToProtoLbip(lbip netip.Addr) *proto.LBIP { func StringLbportToLbport(lbport string) (LBPort, error) { p := strings.Split(lbport, "/") protocolName := p[0] - protocol := dpdkproto.Protocol_value[protocolName] + protocol := proto.Protocol_value[protocolName] port, err := strconv.Atoi(p[1]) if err != nil { return LBPort{}, fmt.Errorf("error parsing port number: %w", err) @@ -192,7 +191,7 @@ func ProtoPrefixToPrefix(interfaceID string, dpdkPrefix *proto.Prefix) (*Prefix, InterfaceID: interfaceID, Prefix: prefix, }, - Spec: PrefixSpec{}, + Spec: PrefixSpec{UnderlayRoute: dpdkPrefix.UnderlayRoute}, }, nil } @@ -224,3 +223,12 @@ func ProtoRouteToRoute(vni uint32, dpdkRoute *proto.Route) (*Route, error) { Spec: RouteSpec{}, }, nil } + +func ProtoLBPrefixToProtoPrefix(lbprefix proto.LBPrefix) *proto.Prefix { + return &proto.Prefix{ + IpVersion: lbprefix.IpVersion, + Address: lbprefix.Address, + PrefixLength: lbprefix.PrefixLength, + UnderlayRoute: lbprefix.UnderlayRoute, + } +} diff --git a/dpdk/api/types.go b/dpdk/api/types.go index f8880d8..a757722 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -103,6 +103,7 @@ func (m *PrefixMeta) GetName() string { } type PrefixSpec struct { + UnderlayRoute []byte `json:"underplayRoute"` } type VirtualIP struct { @@ -123,7 +124,7 @@ func (m *VirtualIPMeta) GetName() string { type VirtualIPSpec struct { } -// Loadbalancer section +// LoadBalancer section type LoadBalancer struct { TypeMeta `json:",inline"` LoadBalancerMeta `json:"metadata"` @@ -174,6 +175,8 @@ func (l *LoadBalancerList) GetItems() []Object { return res } +// InterfaceLoadBalancerPrefix section + // Interface section type Interface struct { TypeMeta `json:",inline"` diff --git a/dpdk/client/client.go b/dpdk/client/client.go index a330e1d..14d8fad 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -30,6 +30,8 @@ type Client interface { CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) (*api.LoadBalancer, error) DeleteLoadBalancer(ctx context.Context, id string) error + ListLoadBalancerPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) + GetInterface(ctx context.Context, id string) (*api.Interface, error) ListInterfaces(ctx context.Context) (*api.InterfaceList, error) CreateInterface(ctx context.Context, iface *api.Interface) (*api.Interface, error) @@ -115,6 +117,30 @@ func (c *client) DeleteLoadBalancer(ctx context.Context, id string) error { return nil } +func (c *client) ListLoadBalancerPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) { + res, err := c.DPDKonmetalClient.ListInterfaceLoadBalancerPrefixes(ctx, &dpdkproto.ListInterfaceLoadBalancerPrefixesRequest{ + InterfaceID: []byte(interfaceID), + }) + if err != nil { + return nil, err + } + + prefixes := make([]api.Prefix, len(res.GetPrefixes())) + for i, dpdkPrefix := range res.GetPrefixes() { + prefix, err := api.ProtoPrefixToPrefix(interfaceID, api.ProtoLBPrefixToProtoPrefix(*dpdkPrefix)) + if err != nil { + return nil, err + } + + prefixes[i] = *prefix + } + + return &api.PrefixList{ + TypeMeta: api.TypeMeta{Kind: api.PrefixListKind}, + Items: prefixes, + }, nil +} + func (c *client) GetInterface(ctx context.Context, name string) (*api.Interface, error) { res, err := c.DPDKonmetalClient.GetInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(name)}) if err != nil { diff --git a/renderer/renderer.go b/renderer/renderer.go index d7f402c..8b7db74 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -19,6 +19,7 @@ import ( "encoding/json" "fmt" "io" + "net" "strconv" "strings" @@ -203,11 +204,11 @@ func (t defaultTableConverter) interfaceTable(ifaces []api.Interface) (*TableDat } func (t defaultTableConverter) prefixTable(prefixes []api.Prefix) (*TableData, error) { - headers := []any{"Prefix"} + headers := []any{"Prefix", "UnderlayRoute"} columns := make([][]any, len(prefixes)) for i, prefix := range prefixes { - columns[i] = []any{prefix.Prefix} + columns[i] = []any{prefix.Prefix, net.ParseIP(string(prefix.Spec.UnderlayRoute))} } return &TableData{ From db2e4549ae609b0df4a890267801fb209fe4cf61 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 2 May 2023 13:42:49 +0200 Subject: [PATCH 05/65] add Create/Delete LoadBalancerPrefix, Completion --- .vscode/launch.json | 2 +- cmd/command.go | 1 + cmd/completion.go | 94 +++++++++++++++++++++++ cmd/create.go | 1 + cmd/create_loadbalancer_prefix.go | 119 ++++++++++++++++++++++++++++++ cmd/delete.go | 1 + cmd/delete_loadbalnacer.go | 2 +- cmd/delete_loadblanacer_prefix.go | 89 ++++++++++++++++++++++ dpdk/client/client.go | 51 ++++++++++++- go.mod | 2 + go.sum | 8 +- sudo | 1 + 12 files changed, 366 insertions(+), 5 deletions(-) create mode 100644 cmd/completion.go create mode 100644 cmd/create_loadbalancer_prefix.go create mode 100644 cmd/delete_loadblanacer_prefix.go create mode 100644 sudo diff --git a/.vscode/launch.json b/.vscode/launch.json index 637f08e..0be9cfc 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,7 +13,7 @@ //"program": "${fileDirname}", //"args": ["create", "loadbalancer", "7", "--lbports", "389", "--vip", "10.3.4.5", "--vni", "100"] //"args": ["get", "route", "0.0.0.0/24", "0", "fc00:2::64:0:1", "--vni", "100"] - "args": ["get", "lbprefix", "--interface-id", "vm1"] + "args": ["create", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] } ] } \ No newline at end of file diff --git a/cmd/command.go b/cmd/command.go index 2bbaa6e..0aa1dc0 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -33,6 +33,7 @@ func Command() *cobra.Command { Create(dpdkClientOptions), Get(dpdkClientOptions), Delete(dpdkClientOptions), + completionCmd, ) return cmd diff --git a/cmd/completion.go b/cmd/completion.go new file mode 100644 index 0000000..a7e40f9 --- /dev/null +++ b/cmd/completion.go @@ -0,0 +1,94 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +// completionCmd represents the completion command +var completionCmd = &cobra.Command{ + Use: "completion [bash|zsh|fish|powershell]", + Short: "Generate completion script", + Long: fmt.Sprintf(`To load completions: + +Bash: + + $ source <(%[1]s completion bash) + + # To load completions for each session, execute once: + # Linux: + $ %[1]s completion bash > /etc/bash_completion.d/%[1]s + # macOS: + $ %[1]s completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s + +Zsh: + + # If shell completion is not already enabled in your environment, + # you will need to enable it. You can execute the following once: + + $ echo "autoload -U compinit; compinit" >> ~/.zshrc + + # To load completions for each session, execute once: + $ %[1]s completion zsh > "${fpath[1]}/_%[1]s" + + # You will need to start a new shell for this setup to take effect. + +fish: + + $ %[1]s completion fish | source + + # To load completions for each session, execute once: + $ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish + +PowerShell: + + PS> %[1]s completion powershell | Out-String | Invoke-Expression + + # To load completions for every new session, run: + PS> %[1]s completion powershell > %[1]s.ps1 + # and source this file from your PowerShell profile. +`, "dpservice-cli"), + DisableFlagsInUseLine: true, + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + Run: func(cmd *cobra.Command, args []string) { + switch args[0] { + case "bash": + cmd.Root().GenBashCompletion(os.Stdout) + case "zsh": + cmd.Root().GenZshCompletion(os.Stdout) + case "fish": + cmd.Root().GenFishCompletion(os.Stdout, true) + case "powershell": + cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) + } + }, +} + +func init() { + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // completionCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // completionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/create.go b/cmd/create.go index f35b8e6..3c0636c 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -48,6 +48,7 @@ func Create(dpdkClientFactory DPDKClientFactory) *cobra.Command { CreateRoute(dpdkClientFactory, rendererOptions), CreateVirtualIP(dpdkClientFactory, rendererOptions), CreateLoadBalancer(dpdkClientFactory, rendererOptions), + CreateLoadBalancerPrefix(dpdkClientFactory, rendererOptions), } cmd.Short = fmt.Sprintf("Creates one of %v", CommandNames(subcommands)) diff --git a/cmd/create_loadbalancer_prefix.go b/cmd/create_loadbalancer_prefix.go new file mode 100644 index 0000000..5f3c4ec --- /dev/null +++ b/cmd/create_loadbalancer_prefix.go @@ -0,0 +1,119 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "net/netip" + "os" + + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func CreateLoadBalancerPrefix( + dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, +) *cobra.Command { + var ( + opts CreateLoadBalancerPrefixOptions + ) + + cmd := &cobra.Command{ + Use: "lbprefix ", + Short: "Create a loadbalancer prefix", + Args: cobra.ExactArgs(1), + Aliases: PrefixAliases, + RunE: func(cmd *cobra.Command, args []string) error { + prefix, err := netip.ParsePrefix(args[0]) + if err != nil { + return fmt.Errorf("error parsing prefix: %w", err) + } + + return RunCreateLoadBalancerPrefix( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + prefix, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type CreateLoadBalancerPrefixOptions struct { + InterfaceID string +} + +func (o *CreateLoadBalancerPrefixOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "ID of the interface to create the prefix for.") +} + +func (o *CreateLoadBalancerPrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"interface-id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunCreateLoadBalancerPrefix( + ctx context.Context, + dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, + prefix netip.Prefix, + opts CreateLoadBalancerPrefixOptions, +) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + renderer, err := rendererFactory.NewRenderer("created", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + + res, err := client.CreateLoadBalancerPrefix(ctx, &api.Prefix{ + PrefixMeta: api.PrefixMeta{ + InterfaceID: opts.InterfaceID, + Prefix: prefix, + }, + Spec: api.PrefixSpec{}, + }) + if err != nil { + return fmt.Errorf("error creating prefix: %w", err) + } + + if err := renderer.Render(res); err != nil { + return fmt.Errorf("error rendering prefix: %w", err) + } + return nil +} diff --git a/cmd/delete.go b/cmd/delete.go index 19af7b5..ddd7f42 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -47,6 +47,7 @@ func Delete(factory DPDKClientFactory) *cobra.Command { DeleteRoute(factory), DeleteVirtualIP(factory), DeleteLoadBalancer(factory), + DeleteLoadBalancerPrefix(factory), } cmd.Short = fmt.Sprintf("Deletes one of %v", CommandNames(subcommands)) diff --git a/cmd/delete_loadbalnacer.go b/cmd/delete_loadbalnacer.go index 0c001b3..8c8fd27 100644 --- a/cmd/delete_loadbalnacer.go +++ b/cmd/delete_loadbalnacer.go @@ -69,7 +69,7 @@ func RunDeleteLoadBalancer(ctx context.Context, factory DPDKClientFactory, loadb for _, loadbalancerID := range loadbalancerIDs { if err := client.DeleteLoadBalancer(ctx, loadbalancerID); err != nil { - return fmt.Errorf("Error deleting loadbalancer %s: %v\n", loadbalancerID, err) + return fmt.Errorf("error deleting loadbalancer %s: %v", loadbalancerID, err) } fmt.Println("Deleted loadbalancer", loadbalancerID) diff --git a/cmd/delete_loadblanacer_prefix.go b/cmd/delete_loadblanacer_prefix.go new file mode 100644 index 0000000..b47ecce --- /dev/null +++ b/cmd/delete_loadblanacer_prefix.go @@ -0,0 +1,89 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "net/netip" + + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func DeleteLoadBalancerPrefix(factory DPDKClientFactory) *cobra.Command { + var ( + opts DeleteLoadBalancerPrefixOptions + ) + + cmd := &cobra.Command{ + Use: "lbprefix [...]", + Short: "Delete a loadbalancer prefix", + Aliases: PrefixAliases, + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + prefixes, err := ParsePrefixArgs(args) + if err != nil { + return err + } + + return RunDeleteLoadBalancerPrefix(cmd.Context(), factory, prefixes, opts) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type DeleteLoadBalancerPrefixOptions struct { + InterfaceID string +} + +func (o *DeleteLoadBalancerPrefixOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID of the prefix.") +} + +func (o *DeleteLoadBalancerPrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"interface-id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunDeleteLoadBalancerPrefix(ctx context.Context, factory DPDKClientFactory, prefixes []netip.Prefix, opts DeleteLoadBalancerPrefixOptions) error { + client, cleanup, err := factory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error deleting dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + for _, prefix := range prefixes { + if err := client.DeleteLoadBalancerPrefix(ctx, opts.InterfaceID, prefix); err != nil { + fmt.Printf("Error deleting prefix %s/%v: %v\n", opts.InterfaceID, prefix, err) + } + fmt.Printf("Deleted prefix %s/%v\n", opts.InterfaceID, prefix) + } + return nil +} diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 14d8fad..743efb7 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -31,6 +31,8 @@ type Client interface { DeleteLoadBalancer(ctx context.Context, id string) error ListLoadBalancerPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) + CreateLoadBalancerPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix, error) + DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) error GetInterface(ctx context.Context, id string) (*api.Interface, error) ListInterfaces(ctx context.Context) (*api.InterfaceList, error) @@ -141,6 +143,50 @@ func (c *client) ListLoadBalancerPrefixes(ctx context.Context, interfaceID strin }, nil } +func (c *client) CreateLoadBalancerPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix, error) { + res, err := c.DPDKonmetalClient.CreateInterfaceLoadBalancerPrefix(ctx, &dpdkproto.CreateInterfaceLoadBalancerPrefixRequest{ + InterfaceID: &dpdkproto.InterfaceIDMsg{ + InterfaceID: []byte(prefix.InterfaceID), + }, + Prefix: &dpdkproto.Prefix{ + IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Prefix.Addr()), + Address: []byte(prefix.Prefix.Addr().String()), + PrefixLength: uint32(prefix.Prefix.Bits()), + }, + }) + if err != nil { + return nil, err + } + if errorCode := res.GetStatus().GetError(); errorCode != 0 { + return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + } + return &api.Prefix{ + TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, + PrefixMeta: prefix.PrefixMeta, + Spec: prefix.Spec, + }, nil +} + +func (c *client) DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) error { + res, err := c.DPDKonmetalClient.DeleteInterfaceLoadBalancerPrefix(ctx, &dpdkproto.DeleteInterfaceLoadBalancerPrefixRequest{ + InterfaceID: &dpdkproto.InterfaceIDMsg{ + InterfaceID: []byte(interfaceID), + }, + Prefix: &dpdkproto.Prefix{ + IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Addr()), + Address: []byte(prefix.Addr().String()), + PrefixLength: uint32(prefix.Bits()), + }, + }) + if err != nil { + return err + } + if errorCode := res.GetError(); errorCode != 0 { + return apierrors.NewStatusError(errorCode, res.GetMessage()) + } + return nil +} + func (c *client) GetInterface(ctx context.Context, name string) (*api.Interface, error) { res, err := c.DPDKonmetalClient.GetInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(name)}) if err != nil { @@ -306,8 +352,9 @@ func (c *client) CreatePrefix(ctx context.Context, prefix *api.Prefix) (*api.Pre return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) } return &api.Prefix{ - TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, - Spec: prefix.Spec, + TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, + PrefixMeta: prefix.PrefixMeta, + Spec: prefix.Spec, }, nil } diff --git a/go.mod b/go.mod index 3b5a23c..af61ca0 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-cmp v0.5.9 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/kr/pretty v0.2.0 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect github.com/rivo/uniseg v0.2.0 // indirect golang.org/x/net v0.5.0 // indirect @@ -28,6 +29,7 @@ require ( golang.org/x/text v0.6.0 // indirect google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect google.golang.org/protobuf v1.28.1 // indirect + gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index f1a2406..3e07247 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,11 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jedib0t/go-pretty/v6 v6.3.9 h1:GAK/1WJY9WVVrKd601HGB89ihLBDfJnUIJye31PY+uk= github.com/jedib0t/go-pretty/v6 v6.3.9/go.mod h1:MgmISkTWDSFu0xOqiZ0mKNntMQ2mDgOcwOkwBEkMDJI= +github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= +github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/onmetal/net-dpservice-go v0.1.9 h1:PXJDl5SFe/BnOeDCAnTJJlRTRuj33YgsEkifrHb+0DA= @@ -60,8 +65,9 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/sudo b/sudo new file mode 100644 index 0000000..2c5ecea --- /dev/null +++ b/sudo @@ -0,0 +1 @@ +Error running command: accepts 1 arg(s), received 2 From 57c926935967d82b71b3347710215346bad71a92 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Wed, 3 May 2023 15:03:00 +0200 Subject: [PATCH 06/65] add create/get LoadBalancerTarget funcs --- .vscode/launch.json | 4 +- cmd/common.go | 1 + cmd/create.go | 1 + cmd/create_loadbalancer_target.go | 118 ++++++++++++++++++++++++++++++ cmd/delete_loadblanacer_prefix.go | 8 +- cmd/get.go | 1 + cmd/get_loadbalancer_target.go | 102 ++++++++++++++++++++++++++ dpdk/api/conversion.go | 12 +++ dpdk/api/types.go | 46 +++++++++--- dpdk/client/client.go | 49 +++++++++++++ renderer/renderer.go | 26 ++++++- 11 files changed, 349 insertions(+), 19 deletions(-) create mode 100644 cmd/create_loadbalancer_target.go create mode 100644 cmd/get_loadbalancer_target.go diff --git a/.vscode/launch.json b/.vscode/launch.json index 0be9cfc..d675d60 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,7 +13,9 @@ //"program": "${fileDirname}", //"args": ["create", "loadbalancer", "7", "--lbports", "389", "--vip", "10.3.4.5", "--vni", "100"] //"args": ["get", "route", "0.0.0.0/24", "0", "fc00:2::64:0:1", "--vni", "100"] - "args": ["create", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] + //"args": ["create", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] + //"args": ["create", "lbtarget", "ff80::1", "--lb-id", "1"] + "args": ["get", "lbtarget", "1"] } ] } \ No newline at end of file diff --git a/cmd/common.go b/cmd/common.go index cc59881..e10ab8d 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -219,4 +219,5 @@ var ( VirtualIPAliases = []string{"virtualips", "vip", "vips"} LoadBalancerAliases = []string{"loadbalancers", "loadbalancer", "lbs", "lb"} LoadBalancerPrefixAliases = []string{"loadbalancer-prefixes", "lbprfx", "lbprfxs"} + LoadBalancerTargetAliases = []string{"loadbalancer-targets", "lbtrgt", "lbtrgts"} ) diff --git a/cmd/create.go b/cmd/create.go index 3c0636c..5f0beca 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -49,6 +49,7 @@ func Create(dpdkClientFactory DPDKClientFactory) *cobra.Command { CreateVirtualIP(dpdkClientFactory, rendererOptions), CreateLoadBalancer(dpdkClientFactory, rendererOptions), CreateLoadBalancerPrefix(dpdkClientFactory, rendererOptions), + CreateLoadBalancerTarget(dpdkClientFactory, rendererOptions), } cmd.Short = fmt.Sprintf("Creates one of %v", CommandNames(subcommands)) diff --git a/cmd/create_loadbalancer_target.go b/cmd/create_loadbalancer_target.go new file mode 100644 index 0000000..37a4dea --- /dev/null +++ b/cmd/create_loadbalancer_target.go @@ -0,0 +1,118 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "net/netip" + "os" + + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func CreateLoadBalancerTarget( + dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, +) *cobra.Command { + var ( + opts CreateLoadBalancerTargetOptions + ) + + cmd := &cobra.Command{ + Use: "lbtarget [flags]", + Short: "Create a loadbalancer target", + Args: cobra.ExactArgs(1), + Aliases: LoadBalancerTargetAliases, + RunE: func(cmd *cobra.Command, args []string) error { + ip, err := netip.ParseAddr(args[0]) + if err != nil { + return fmt.Errorf("error parsing ip: %w", err) + } + + return RunCreateLoadBalancerTarget( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + ip, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type CreateLoadBalancerTargetOptions struct { + LoadBalancerID string +} + +func (o *CreateLoadBalancerTargetOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.LoadBalancerID, "lb-id", o.LoadBalancerID, "ID of the loadbalancer to create the target for.") +} + +func (o *CreateLoadBalancerTargetOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"lb-id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunCreateLoadBalancerTarget( + ctx context.Context, + dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, + ip netip.Addr, + opts CreateLoadBalancerTargetOptions, +) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + renderer, err := rendererFactory.NewRenderer("created", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + + targetIP := api.ProtoLbipToLbip(*api.LbipToProtoLbip(ip)) + res, err := client.CreateLoadBalancerTarget(ctx, &api.LoadBalancerTarget{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, + LoadBalancerTargetMeta: api.LoadBalancerTargetMeta{ID: opts.LoadBalancerID}, + Spec: api.LoadBalancerTargetSpec{TargetIP: *targetIP}, + }) + if err != nil { + return fmt.Errorf("error creating loadbalancer target: %w", err) + } + + if err := renderer.Render(res); err != nil { + return fmt.Errorf("error rendering loadbalancer target: %w", err) + } + return nil +} diff --git a/cmd/delete_loadblanacer_prefix.go b/cmd/delete_loadblanacer_prefix.go index b47ecce..11ed28d 100644 --- a/cmd/delete_loadblanacer_prefix.go +++ b/cmd/delete_loadblanacer_prefix.go @@ -32,7 +32,7 @@ func DeleteLoadBalancerPrefix(factory DPDKClientFactory) *cobra.Command { cmd := &cobra.Command{ Use: "lbprefix [...]", Short: "Delete a loadbalancer prefix", - Aliases: PrefixAliases, + Aliases: LoadBalancerPrefixAliases, Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { prefixes, err := ParsePrefixArgs(args) @@ -56,7 +56,7 @@ type DeleteLoadBalancerPrefixOptions struct { } func (o *DeleteLoadBalancerPrefixOptions) AddFlags(fs *pflag.FlagSet) { - fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID of the prefix.") + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID of the loadbalancer prefix.") } func (o *DeleteLoadBalancerPrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { @@ -81,9 +81,9 @@ func RunDeleteLoadBalancerPrefix(ctx context.Context, factory DPDKClientFactory, for _, prefix := range prefixes { if err := client.DeleteLoadBalancerPrefix(ctx, opts.InterfaceID, prefix); err != nil { - fmt.Printf("Error deleting prefix %s/%v: %v\n", opts.InterfaceID, prefix, err) + fmt.Printf("Error deleting loadbalancer prefix %s/%v: %v\n", opts.InterfaceID, prefix, err) } - fmt.Printf("Deleted prefix %s/%v\n", opts.InterfaceID, prefix) + fmt.Printf("Deleted loadbalancer prefix %s/%v\n", opts.InterfaceID, prefix) } return nil } diff --git a/cmd/get.go b/cmd/get.go index a9d5634..58ad13b 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -38,6 +38,7 @@ func Get(factory DPDKClientFactory) *cobra.Command { GetVirtualIP(factory, rendererOptions), GetLoadBalancer(factory, rendererOptions), GetLoadBalancerPrefix(factory, rendererOptions), + GetLoadBalancerTargets(factory, rendererOptions), } cmd.Short = fmt.Sprintf("Gets/Lists one of %v", CommandNames(subcommands)) diff --git a/cmd/get_loadbalancer_target.go b/cmd/get_loadbalancer_target.go new file mode 100644 index 0000000..a2b5f04 --- /dev/null +++ b/cmd/get_loadbalancer_target.go @@ -0,0 +1,102 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func GetLoadBalancerTargets(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + var ( + opts GetLoadBalancerTargetOptions + ) + + cmd := &cobra.Command{ + Use: "lbtarget [...]", + Short: "Get or list LoadBalancerTarget(s)", + Aliases: LoadBalancerTargetAliases, + RunE: func(cmd *cobra.Command, args []string) error { + interfaceIDs := args + return RunGetLoadBalancerTargets( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + interfaceIDs, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type GetLoadBalancerTargetOptions struct { +} + +func (o *GetLoadBalancerTargetOptions) AddFlags(fs *pflag.FlagSet) { +} + +func (o *GetLoadBalancerTargetOptions) MarkRequiredFlags(cmd *cobra.Command) error { + return nil +} + +func RunGetLoadBalancerTargets( + ctx context.Context, + dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, + interfaceIDs []string, + opts GetLoadBalancerTargetOptions, +) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error getting dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + renderer, err := rendererFactory.NewRenderer("", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + + if len(interfaceIDs) == 0 { + return fmt.Errorf("listing loadbalancer targets is not implemented") + } + + for _, interfaceID := range interfaceIDs { + lbtarget, err := client.GetLoadBalancerTargets(ctx, interfaceID) + if err != nil { + fmt.Printf("Error getting loadbalancer target for interface %s: %v\n", interfaceID, err) + } + + if err := renderer.Render(lbtarget); err != nil { + return fmt.Errorf("error rendering loadbalancer target: %w", err) + } + } + return nil +} diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index f4d0bee..903d853 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -73,6 +73,18 @@ func LbipToProtoLbip(lbip netip.Addr) *proto.LBIP { return &proto.LBIP{IpVersion: NetIPAddrToProtoIPVersion(lbip), Address: []byte(lbip.String())} } +func ProtoLbipToLbip(protolbip proto.LBIP) *LBIP { + var ip netip.Addr + if lbipString := string(protolbip.Address); lbipString != "" { + var err error + ip, err = netip.ParseAddr(string(protolbip.Address)) + if err != nil { + return nil + } + } + return &LBIP{Address: ip, IpVersion: proto.IPVersion_name[int32(protolbip.IpVersion)]} +} + func StringLbportToLbport(lbport string) (LBPort, error) { p := strings.Split(lbport, "/") protocolName := p[0] diff --git a/dpdk/api/types.go b/dpdk/api/types.go index a757722..53ce0b3 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -153,8 +153,8 @@ type LoadBalancerStatus struct { } type LBIP struct { - IpVersion bool `json:"ipVersion"` - Address []byte `json:"address"` + IpVersion string `json:"ipVersion"` + Address netip.Addr `json:"address"` } type LBPort struct { @@ -175,7 +175,28 @@ func (l *LoadBalancerList) GetItems() []Object { return res } -// InterfaceLoadBalancerPrefix section +type LoadBalancerTarget struct { + TypeMeta `json:",inline"` + LoadBalancerTargetMeta `json:"metadata"` + Spec LoadBalancerTargetSpec `json:"spec"` +} + +type LoadBalancerTargetMeta struct { + ID string `json:"id"` +} + +func (m *LoadBalancerTargetMeta) GetName() string { + return m.ID +} + +type LoadBalancerTargetSpec struct { + TargetIP LBIP `json:"targetIP"` +} + +type LoadBalancerTargetList struct { + TypeMeta `json:",inline"` + Items []LoadBalancerTarget `json:"items"` +} // Interface section type Interface struct { @@ -217,13 +238,14 @@ func (l *InterfaceList) GetItems() []Object { } var ( - InterfaceKind = reflect.TypeOf(Interface{}).Name() - InterfaceListKind = reflect.TypeOf(InterfaceList{}).Name() - LoadBalancerKind = reflect.TypeOf(LoadBalancer{}).Name() - LoadBalancerListKind = reflect.TypeOf(LoadBalancerList{}).Name() - PrefixKind = reflect.TypeOf(Prefix{}).Name() - PrefixListKind = reflect.TypeOf(PrefixList{}).Name() - VirtualIPKind = reflect.TypeOf(VirtualIP{}).Name() - RouteKind = reflect.TypeOf(Route{}).Name() - RouteListKind = reflect.TypeOf(RouteList{}).Name() + InterfaceKind = reflect.TypeOf(Interface{}).Name() + InterfaceListKind = reflect.TypeOf(InterfaceList{}).Name() + LoadBalancerKind = reflect.TypeOf(LoadBalancer{}).Name() + LoadBalancerTargetKind = reflect.TypeOf(LoadBalancerTarget{}).Name() + LoadBalancerTargetListKind = reflect.TypeOf(LoadBalancerTargetList{}).Name() + PrefixKind = reflect.TypeOf(Prefix{}).Name() + PrefixListKind = reflect.TypeOf(PrefixList{}).Name() + VirtualIPKind = reflect.TypeOf(VirtualIP{}).Name() + RouteKind = reflect.TypeOf(Route{}).Name() + RouteListKind = reflect.TypeOf(RouteList{}).Name() ) diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 743efb7..1f84bb4 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -34,6 +34,9 @@ type Client interface { CreateLoadBalancerPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix, error) DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) error + GetLoadBalancerTargets(ctx context.Context, interfaceID string) (*api.LoadBalancerTargetList, error) + CreateLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBalancerTarget) (*api.LoadBalancerTarget, error) + GetInterface(ctx context.Context, id string) (*api.Interface, error) ListInterfaces(ctx context.Context) (*api.InterfaceList, error) CreateInterface(ctx context.Context, iface *api.Interface) (*api.Interface, error) @@ -187,6 +190,52 @@ func (c *client) DeleteLoadBalancerPrefix(ctx context.Context, interfaceID strin return nil } +func (c *client) GetLoadBalancerTargets(ctx context.Context, loadBalancerID string) (*api.LoadBalancerTargetList, error) { + res, err := c.DPDKonmetalClient.GetLoadBalancerTargets(ctx, &dpdkproto.GetLoadBalancerTargetsRequest{ + LoadBalancerID: []byte(loadBalancerID), + }) + if err != nil { + return nil, err + } + if errorCode := res.GetStatus().GetError(); errorCode != 0 { + return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + } + + lbtargets := make([]api.LoadBalancerTarget, len(res.GetTargetIPs())) + for i, dpdkLBtarget := range res.GetTargetIPs() { + var lbtarget api.LoadBalancerTarget + lbtarget.TypeMeta.Kind = api.LoadBalancerTargetKind + lbtarget.Spec.TargetIP = *api.ProtoLbipToLbip(*dpdkLBtarget) + lbtarget.LoadBalancerTargetMeta.ID = loadBalancerID + + lbtargets[i] = lbtarget + } + + return &api.LoadBalancerTargetList{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetListKind}, + Items: lbtargets, + }, nil +} + +func (c *client) CreateLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBalancerTarget) (*api.LoadBalancerTarget, error) { + res, err := c.DPDKonmetalClient.AddLoadBalancerTarget(ctx, &dpdkproto.AddLoadBalancerTargetRequest{ + LoadBalancerID: []byte(lbtarget.LoadBalancerTargetMeta.ID), + TargetIP: api.LbipToProtoLbip(lbtarget.Spec.TargetIP.Address), + }) + if err != nil { + return nil, err + } + if errorCode := res.GetError(); errorCode != 0 { + return nil, apierrors.NewStatusError(errorCode, res.GetMessage()) + } + + return &api.LoadBalancerTarget{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, + LoadBalancerTargetMeta: lbtarget.LoadBalancerTargetMeta, + Spec: lbtarget.Spec, + }, nil +} + func (c *client) GetInterface(ctx context.Context, name string) (*api.Interface, error) { res, err := c.DPDKonmetalClient.GetInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(name)}) if err != nil { diff --git a/renderer/renderer.go b/renderer/renderer.go index 8b7db74..bc8ded3 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -151,7 +151,11 @@ 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(*obj) + case *api.LoadBalancerTarget: + return t.loadBalancerTargetTable([]api.LoadBalancerTarget{*obj}) + case *api.LoadBalancerTargetList: + return t.loadBalancerTargetTable(obj.Items) case *api.Interface: return t.interfaceTable([]api.Interface{*obj}) case *api.InterfaceList: @@ -171,7 +175,7 @@ func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) { } } -func (t defaultTableConverter) loadbalancerTable(lbs api.LoadBalancer) (*TableData, error) { +func (t defaultTableConverter) loadBalancerTable(lbs api.LoadBalancer) (*TableData, error) { headers := []any{"ID", "VNI", "LbVipIP", "Lbports", "UnderlayRoute"} columns := make([][]any, 1) @@ -189,6 +193,24 @@ func (t defaultTableConverter) loadbalancerTable(lbs api.LoadBalancer) (*TableDa }, nil } +func (t defaultTableConverter) loadBalancerTargetTable(lbtargets []api.LoadBalancerTarget) (*TableData, error) { + headers := []any{"LoadBalancerID", "IpVersion", "Address"} + + columns := make([][]any, len(lbtargets)) + for i, lbtarget := range lbtargets { + columns[i] = []any{ + lbtarget.LoadBalancerTargetMeta.ID, + lbtarget.Spec.TargetIP.IpVersion, + lbtarget.Spec.TargetIP.Address, + } + } + + return &TableData{ + Headers: headers, + Columns: columns, + }, nil +} + func (t defaultTableConverter) interfaceTable(ifaces []api.Interface) (*TableData, error) { headers := []any{"ID", "VNI", "Device", "IPs", "UnderlayIP"} From 751861aaaecf83b1182599f5c24d8b930c4b2ab9 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 4 May 2023 11:23:44 +0200 Subject: [PATCH 07/65] add Delete LoadBalancerTarget --- cmd/create_loadbalancer.go | 2 +- cmd/create_loadbalancer_target.go | 3 +- cmd/delete.go | 1 + cmd/delete_loadblanacer_target.go | 91 +++++++++++++++++++++++++++++++ cmd/get_loadbalancer_target.go | 3 +- dpdk/api/conversion.go | 10 +++- dpdk/client/client.go | 15 +++++ 7 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 cmd/delete_loadblanacer_target.go diff --git a/cmd/create_loadbalancer.go b/cmd/create_loadbalancer.go index 9108acc..53d4532 100644 --- a/cmd/create_loadbalancer.go +++ b/cmd/create_loadbalancer.go @@ -35,7 +35,7 @@ func CreateLoadBalancer(dpdkClientFactory DPDKClientFactory, rendererFactory Ren cmd := &cobra.Command{ Use: "loadbalancer ", Short: "Create a loadbalancer", - Example: "dpservice-cli create lb 4 --vni 100 --vip 10.20.30.40 --lbports TCP/443,UDP/53", + Example: "dpservice-cli create lb 4 --vni=100 --vip=10.20.30.40 --lbports=TCP/443,UDP/53", Aliases: LoadBalancerAliases, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/create_loadbalancer_target.go b/cmd/create_loadbalancer_target.go index 37a4dea..52f04cb 100644 --- a/cmd/create_loadbalancer_target.go +++ b/cmd/create_loadbalancer_target.go @@ -35,8 +35,9 @@ func CreateLoadBalancerTarget( ) cmd := &cobra.Command{ - Use: "lbtarget [flags]", + Use: "lbtarget [flags]", Short: "Create a loadbalancer target", + Example: "dpservice-cli create lbtarget ff80::5 --lb-id 2", Args: cobra.ExactArgs(1), Aliases: LoadBalancerTargetAliases, RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/delete.go b/cmd/delete.go index ddd7f42..f74f9d2 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -48,6 +48,7 @@ func Delete(factory DPDKClientFactory) *cobra.Command { DeleteVirtualIP(factory), DeleteLoadBalancer(factory), DeleteLoadBalancerPrefix(factory), + DeleteLoadBalancerTarget(factory), } cmd.Short = fmt.Sprintf("Deletes one of %v", CommandNames(subcommands)) diff --git a/cmd/delete_loadblanacer_target.go b/cmd/delete_loadblanacer_target.go new file mode 100644 index 0000000..a76be93 --- /dev/null +++ b/cmd/delete_loadblanacer_target.go @@ -0,0 +1,91 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "net/netip" + + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func DeleteLoadBalancerTarget(factory DPDKClientFactory) *cobra.Command { + var ( + opts DeleteLoadBalancerTargetOptions + ) + + cmd := &cobra.Command{ + Use: "lbtarget [] --lb-id ", + Short: "Delete a loadbalancer target", + Example: "dpservice-cli delete lbtarget ff80::4 ff80::5 --lb-id=2", + Aliases: LoadBalancerTargetAliases, + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + targets := args + + return RunDeleteLoadBalancerTarget(cmd.Context(), factory, targets, opts) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type DeleteLoadBalancerTargetOptions struct { + LoadBalancerID string +} + +func (o *DeleteLoadBalancerTargetOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.LoadBalancerID, "lb-id", o.LoadBalancerID, "LoadBalancerID where to delete target.") +} + +func (o *DeleteLoadBalancerTargetOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"lb-id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunDeleteLoadBalancerTarget(ctx context.Context, factory DPDKClientFactory, targets []string, opts DeleteLoadBalancerTargetOptions) error { + client, cleanup, err := factory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error deleting dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + for _, target := range targets { + targetIP, err := netip.ParseAddr(target) + if err != nil { + return fmt.Errorf("not valid IP Address: %w", err) + } + if err := client.DeleteLoadBalancerTarget(ctx, opts.LoadBalancerID, targetIP); err != nil { + fmt.Printf("Error deleting loadbalancer target %s/%v: %v\n", opts.LoadBalancerID, target, err) + } + fmt.Printf("Deleted loadbalancer target %s/%v\n", opts.LoadBalancerID, target) + } + return nil +} diff --git a/cmd/get_loadbalancer_target.go b/cmd/get_loadbalancer_target.go index a2b5f04..7af4be3 100644 --- a/cmd/get_loadbalancer_target.go +++ b/cmd/get_loadbalancer_target.go @@ -30,8 +30,9 @@ func GetLoadBalancerTargets(dpdkClientFactory DPDKClientFactory, rendererFactory ) cmd := &cobra.Command{ - Use: "lbtarget [...]", + Use: "lbtarget [...]", Short: "Get or list LoadBalancerTarget(s)", + Example: "dpservice-cli get lbtarget 1 2", Aliases: LoadBalancerTargetAliases, RunE: func(cmd *cobra.Command, args []string) error { interfaceIDs := args diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index 903d853..53833ec 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -87,7 +87,15 @@ func ProtoLbipToLbip(protolbip proto.LBIP) *LBIP { func StringLbportToLbport(lbport string) (LBPort, error) { p := strings.Split(lbport, "/") - protocolName := p[0] + protocolName := strings.ToLower(p[0]) + switch protocolName { + case "icmp", "tcp", "udp", "sctp": + protocolName = strings.ToUpper(protocolName) + case "icmpv6": + protocolName = "ICMPv6" + default: + return LBPort{}, fmt.Errorf("unsupported protocol") + } protocol := proto.Protocol_value[protocolName] port, err := strconv.Atoi(p[1]) if err != nil { diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 1f84bb4..86af0cb 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -36,6 +36,7 @@ type Client interface { GetLoadBalancerTargets(ctx context.Context, interfaceID string) (*api.LoadBalancerTargetList, error) CreateLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBalancerTarget) (*api.LoadBalancerTarget, error) + DeleteLoadBalancerTarget(ctx context.Context, id string, targetIP netip.Addr) error GetInterface(ctx context.Context, id string) (*api.Interface, error) ListInterfaces(ctx context.Context) (*api.InterfaceList, error) @@ -236,6 +237,20 @@ func (c *client) CreateLoadBalancerTarget(ctx context.Context, lbtarget *api.Loa }, nil } +func (c *client) DeleteLoadBalancerTarget(ctx context.Context, id string, targetIP netip.Addr) error { + res, err := c.DPDKonmetalClient.DeleteLoadBalancerTarget(ctx, &dpdkproto.DeleteLoadBalancerTargetRequest{ + LoadBalancerID: []byte(id), + TargetIP: api.LbipToProtoLbip(targetIP), + }) + if err != nil { + return err + } + if errorCode := res.GetError(); errorCode != 0 { + return apierrors.NewStatusError(errorCode, res.GetMessage()) + } + return nil +} + func (c *client) GetInterface(ctx context.Context, name string) (*api.Interface, error) { res, err := c.DPDKonmetalClient.GetInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(name)}) if err != nil { From 19b1308c36b963df07529e7c7f8c30dfc87aaec7 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 4 May 2023 14:03:22 +0200 Subject: [PATCH 08/65] add CreateNat, Nat type --- cmd/common.go | 1 + cmd/create.go | 1 + cmd/create_nat.go | 115 ++++++++++++++++++++++++++++++++++ dpdk/api/errors/errors.go | 126 +++++++++++++++++++++++++------------- dpdk/api/register.go | 1 + dpdk/api/types.go | 29 +++++++++ dpdk/client/client.go | 37 +++++++++++ renderer/renderer.go | 16 +++++ 8 files changed, 284 insertions(+), 42 deletions(-) create mode 100644 cmd/create_nat.go diff --git a/cmd/common.go b/cmd/common.go index e10ab8d..57b7fe0 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -220,4 +220,5 @@ var ( LoadBalancerAliases = []string{"loadbalancers", "loadbalancer", "lbs", "lb"} LoadBalancerPrefixAliases = []string{"loadbalancer-prefixes", "lbprfx", "lbprfxs"} LoadBalancerTargetAliases = []string{"loadbalancer-targets", "lbtrgt", "lbtrgts"} + NatAliases = []string{"translation"} ) diff --git a/cmd/create.go b/cmd/create.go index 5f0beca..3ed0357 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -50,6 +50,7 @@ func Create(dpdkClientFactory DPDKClientFactory) *cobra.Command { CreateLoadBalancer(dpdkClientFactory, rendererOptions), CreateLoadBalancerPrefix(dpdkClientFactory, rendererOptions), CreateLoadBalancerTarget(dpdkClientFactory, rendererOptions), + CreateNat(dpdkClientFactory, rendererOptions), } cmd.Short = fmt.Sprintf("Creates one of %v", CommandNames(subcommands)) diff --git a/cmd/create_nat.go b/cmd/create_nat.go new file mode 100644 index 0000000..170d34e --- /dev/null +++ b/cmd/create_nat.go @@ -0,0 +1,115 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "net/netip" + "os" + + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/flag" + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func CreateNat(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + var ( + opts CreateNatOptions + ) + + cmd := &cobra.Command{ + Use: "nat [flags]", + Short: "Create a NAT", + Example: "dpservice-cli create nat vm1 --natip=10.20.30.40 --minport=30000 --maxport=30100", + Aliases: NatAliases, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + interfaceID := args[0] + return RunCreateNat( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + interfaceID, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type CreateNatOptions struct { + NATVipIP netip.Addr + MinPort uint32 + MaxPort uint32 +} + +func (o *CreateNatOptions) AddFlags(fs *pflag.FlagSet) { + fs.Uint32Var(&o.MinPort, "minport", o.MinPort, "MinPort of NAT.") + fs.Uint32Var(&o.MaxPort, "maxport", o.MaxPort, "MaxPort of NAT.") + flag.AddrVar(fs, &o.NATVipIP, "natip", o.NATVipIP, "NAT IP to assign to the interface.") +} + +func (o *CreateNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"minport", "maxport", "natip"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunCreateNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, interfaceID string, opts CreateNatOptions) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + renderer, err := rendererFactory.NewRenderer("created", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + + lb, err := client.CreateNat(ctx, &api.Nat{ + NatMeta: api.NatMeta{ + InterfaceID: interfaceID, + }, + Spec: api.NatSpec{ + NatVIPIP: opts.NATVipIP, + MinPort: opts.MinPort, + MaxPort: opts.MaxPort, + }, + }) + if err != nil { + return fmt.Errorf("error creating nat: %w", err) + } + + if err := renderer.Render(lb); err != nil { + return fmt.Errorf("error rendering nat: %w", err) + } + return nil +} diff --git a/dpdk/api/errors/errors.go b/dpdk/api/errors/errors.go index afa3a6d..3ff4e9b 100644 --- a/dpdk/api/errors/errors.go +++ b/dpdk/api/errors/errors.go @@ -20,48 +20,90 @@ import ( ) const ( - ADD = 100 - ADD_IPV6_FORMAT = 101 - ADD_VM_NAME_ERR = 102 - ADD_VM_LPM4 = 104 - ADD_VM_LPM6 = 105 - ADD_VM_ADD_ROUT4 = 106 - ADD_VM_ADD_ROUT6 = 107 - ADD_VM_NO_VFS = 108 - ALREADY_ALLOCATED = 109 - CANT_GET_NAME = 110 - DEL = 150 - DEL_VM_NOT_FND = 151 - GET_VM_NOT_FND = 171 - LIST = 200 - ADD_RT = 250 - ADD_RT_FAIL4 = 251 - ADD_RT_FAIL6 = 252 - DEL_RT = 300 - ADD_NAT = 350 - ADD_NAT_IP_EXISTS = 351 - ADD_NAT_ALLOC = 352 - ADD_NAT_ADD_KEY = 353 - ADD_NAT_ADD_DATA = 354 - ADD_DNAT = 400 - ADD_DNAT_IP_EXISTS = 401 - ADD_DNAT_ALLOC = 402 - ADD_DNAT_ADD_KEY = 403 - ADD_DNAT_ADD_DATA = 404 - DEL_NAT = 450 - GET_NAT = 500 - GET_NAT_NO_IP_SET = 501 - ADD_LB_VIP = 550 - ADD_LB_NO_VNI_EXIST = 551 - ADD_LB_UNSUPP_IP = 552 - DEL_LB_VIP = 600 - DEL_LB_NO_VNI_EXIST = 601 - DEL_LB_UNSUPP_IP = 602 - ADD_PFX = 650 - ADD_PFX_NO_VM = 651 - ADD_PFX_ROUTE = 652 - DEL_PFX = 700 - DEL_PFX_NO_VM = 701 + ADD = 100 + ADD_IPV6_FORMAT = 101 + ADD_VM_NAME_ERR = 102 + ADD_VM_LPM4 = 104 + ADD_VM_LPM6 = 105 + ADD_VM_ADD_ROUT4 = 106 + ADD_VM_ADD_ROUT6 = 107 + ADD_VM_NO_VFS = 108 + ALREADY_ALLOCATED = 109 + CANT_GET_NAME = 110 + ADD_VM_VNF_ERROR = 111 + DEL = 150 + DEL_VM_NOT_FND = 151 + GET_VM_NOT_FND = 171 + LIST = 200 + ADD_RT = 250 + ADD_RT_FAIL4 = 251 + ADD_RT_FAIL6 = 252 + ADD_RT_NO_VM = 253 + DEL_RT = 300 + GET_NETNAT_ITER_ERROR = 349 + ADD_NAT = 350 + ADD_NAT_IP_EXISTS = 351 + ADD_NAT_ALLOC = 352 + ADD_NAT_ADD_KEY = 353 + ADD_NET_NAT_DATA = 354 + ADD_NETWORK_NAT = 355 + DEL_NETWORK_NAT = 356 + ADD_NETNAT_NONLOCAL = 357 + ADD_NETNAT_INVALID_PORT = 358 + ADD_NETNAT_DATA_NOT_FOUND = 359 + DEL_NETNAT_NONLOCAL = 360 + DEL_NETNAT_INVALID_PORT = 361 + DEL_NETNAT_ENTRY_NOT_FOUND = 362 + ADD_NETNAT_IP_EXISTS = 363 + ADD_NETNAT_KEY = 364 + ADD_NETNAT_ALLO_DATA = 365 + ADD_NETNAT_ADD_DATA = 366 + DEL_NETNAT_KEY_DELETED = 367 + GET_NETNAT_IPV6_UNSUPPORTED = 369 + ADD_NEIGHNAT_WRONGTYPE = 370 + DEL_NEIGHNAT_WRONGTYPE = 371 + ADD_NEIGHNAT_ENTRY_EXIST = 372 + ADD_NEIGHNAT_ALLOC = 373 + DEL_NEIGHNAT_ENTRY_NOFOUND = 374 + GET_NEIGHNAT_UNDER_IPV6 = 375 + GET_NETNAT_INFO_TYPE_UNKNOWN = 376 + ADD_NAT_VNF_ERR = 377 + ADD_DNAT = 400 + ADD_DNAT_IP_EXISTS = 401 + ADD_DNAT_ALLOC = 402 + ADD_DNAT_ADD_KEY = 403 + ADD_DNAT_ADD_DATA = 404 + DEL_NAT = 450 + DEL_NAT_NO_SNAT = 451 + GET_NAT = 500 + GET_NAT_NO_IP_SET = 501 + ADD_LB_VIP = 550 + ADD_LB_NO_VNI_EXIST = 551 + ADD_LB_UNSUPP_IP = 552 + DEL_LB_VIP = 600 + DEL_LB_NO_VNI_EXIST = 601 + DEL_LB_UNSUPP_IP = 602 + ADD_PFX = 650 + ADD_PFX_NO_VM = 651 + ADD_PFX_ROUTE = 652 + ADD_PFX_VNF_ERR = 653 + DEL_PFX = 700 + DEL_PFX_NO_VM = 701 + CREATE_LB_UNSUPP_IP = 750 + CREATE_LB_ERR = 751 + CREATE_LB_VNF_ERR = 752 + DEL_LB_ID_ERR = 755 + DEL_LB_BACK_IP_ERR = 756 + GET_LB_ID_ERR = 760 + GET_LB_BACK_IP_ERR = 761 + ADD_FWALL_ERR = 800 + ADD_FWALL_RULE_ERR = 801 + ADD_FWALL_NO_DROP_SUPPORT = 802 + ADD_FWALL_ID_EXISTS = 803 + GET_FWALL_ERR = 810 + GET_NO_FWALL_RULE_ERR = 811 + DEL_FWALL_ERR = 820 + DEL_NO_FWALL_RULE_ERR = 821 ) type StatusError struct { diff --git a/dpdk/api/register.go b/dpdk/api/register.go index a00c455..3a3ffc2 100644 --- a/dpdk/api/register.go +++ b/dpdk/api/register.go @@ -30,6 +30,7 @@ func init() { &RouteList{}, &VirtualIP{}, &LoadBalancer{}, + &Nat{}, ); err != nil { panic(err) } diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 53ce0b3..5a9b8da 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -237,6 +237,34 @@ func (l *InterfaceList) GetItems() []Object { return res } +// NAT section +type Nat struct { + TypeMeta `json:",inline"` + NatMeta `json:"metadata"` + Spec NatSpec `json:"spec"` + Status NatStatus `json:"status"` +} + +type NatMeta struct { + InterfaceID string `json:"id"` +} + +func (m *NatMeta) GetName() string { + return m.InterfaceID +} + +type NatSpec struct { + NatVIPIP netip.Addr `json:"natVIPIP"` + MinPort uint32 `json:"device"` + MaxPort uint32 `json:"ips"` + UnderlayRoute netip.Addr `json:"underplayRoute"` +} + +type NatStatus struct { + Error int32 `json:"error,omitempty"` + Message string `json:"message,omitempty"` +} + var ( InterfaceKind = reflect.TypeOf(Interface{}).Name() InterfaceListKind = reflect.TypeOf(InterfaceList{}).Name() @@ -248,4 +276,5 @@ var ( VirtualIPKind = reflect.TypeOf(VirtualIP{}).Name() RouteKind = reflect.TypeOf(Route{}).Name() RouteListKind = reflect.TypeOf(RouteList{}).Name() + NatKind = reflect.TypeOf(Nat{}).Name() ) diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 86af0cb..b4492af 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -54,6 +54,8 @@ type Client interface { ListRoutes(ctx context.Context, vni uint32) (*api.RouteList, error) CreateRoute(ctx context.Context, route *api.Route) (*api.Route, error) DeleteRoute(ctx context.Context, vni uint32, prefix netip.Prefix, nextHopVNI uint32, nextHopIP netip.Addr) error + + CreateNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) } type client struct { @@ -492,6 +494,7 @@ func (c *client) DeleteRoute(ctx context.Context, vni uint32, prefix netip.Prefi } return nil } + func (c *client) ListRoutes(ctx context.Context, vni uint32) (*api.RouteList, error) { res, err := c.DPDKonmetalClient.ListRoutes(ctx, &dpdkproto.VNIMsg{ Vni: vni, @@ -515,3 +518,37 @@ func (c *client) ListRoutes(ctx context.Context, vni uint32) (*api.RouteList, er Items: routes, }, nil } + +func (c *client) CreateNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) { + res, err := c.DPDKonmetalClient.AddNAT(ctx, &dpdkproto.AddNATRequest{ + InterfaceID: []byte(nat.NatMeta.InterfaceID), + NatVIPIP: &dpdkproto.NATIP{ + IpVersion: api.NetIPAddrToProtoIPVersion(nat.Spec.NatVIPIP), + Address: []byte(nat.Spec.NatVIPIP.String()), + }, + MinPort: nat.Spec.MinPort, + MaxPort: nat.Spec.MaxPort, + }) + if err != nil { + return nil, err + } + if errorCode := res.GetStatus().GetError(); errorCode != 0 { + return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + } + + underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) + if err != nil { + return nil, fmt.Errorf("error parsing underlay route: %w", err) + } + nat.Spec.UnderlayRoute = underlayRoute + + return &api.Nat{ + TypeMeta: api.TypeMeta{Kind: api.NatKind}, + NatMeta: nat.NatMeta, + Spec: nat.Spec, + Status: api.NatStatus{ + Error: res.Status.Error, + Message: res.Status.Message, + }, + }, nil +} diff --git a/renderer/renderer.go b/renderer/renderer.go index bc8ded3..e149447 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -170,6 +170,8 @@ func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) { return t.routeTable(obj.Items) case *api.VirtualIP: return t.virtualIPTable([]api.VirtualIP{*obj}) + case *api.Nat: + return t.natTable([]api.Nat{*obj}) default: return nil, fmt.Errorf("unsupported type %T", v) } @@ -267,6 +269,20 @@ func (t defaultTableConverter) virtualIPTable(virtualIPs []api.VirtualIP) (*Tabl }, nil } +func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { + headers := []any{"interfaceID", "NatIP", "minPort", "maxPort", "underlayRoute"} + + columns := make([][]any, len(nats)) + for i, nat := range nats { + columns[i] = []any{nat.NatMeta.InterfaceID, nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute} + } + + return &TableData{ + Headers: headers, + Columns: columns, + }, nil +} + var ( lightBoxStyle = table.BoxStyle{ BottomLeft: "", From 1c7ce4b756b402ebc1a61934a488bfbfb1226551 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 4 May 2023 15:43:08 +0200 Subject: [PATCH 09/65] add GetNat --- cmd/get.go | 1 + cmd/get_nat.go | 102 +++++++++++++++++++++++++++++++++++++++++ dpdk/api/conversion.go | 38 +++++++++++++++ dpdk/api/types.go | 8 ++-- dpdk/client/client.go | 13 ++++++ 5 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 cmd/get_nat.go diff --git a/cmd/get.go b/cmd/get.go index 58ad13b..3198da6 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -39,6 +39,7 @@ func Get(factory DPDKClientFactory) *cobra.Command { GetLoadBalancer(factory, rendererOptions), GetLoadBalancerPrefix(factory, rendererOptions), GetLoadBalancerTargets(factory, rendererOptions), + GetNat(factory, rendererOptions), } cmd.Short = fmt.Sprintf("Gets/Lists one of %v", CommandNames(subcommands)) diff --git a/cmd/get_nat.go b/cmd/get_nat.go new file mode 100644 index 0000000..3625f8f --- /dev/null +++ b/cmd/get_nat.go @@ -0,0 +1,102 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func GetNat(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + var ( + opts GetNatOptions + ) + + cmd := &cobra.Command{ + Use: "nat [...]", + Short: "Get or list nat(s)", + Aliases: NatAliases, + RunE: func(cmd *cobra.Command, args []string) error { + interfaceIDs := args + return RunGetNat( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + interfaceIDs, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type GetNatOptions struct { +} + +func (o *GetNatOptions) AddFlags(fs *pflag.FlagSet) { +} + +func (o *GetNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { + return nil +} + +func RunGetNat( + ctx context.Context, + dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, + interfaceIDs []string, + opts GetNatOptions, +) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error getting dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + renderer, err := rendererFactory.NewRenderer("", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + + if len(interfaceIDs) == 0 { + return fmt.Errorf("listing all nats is not implemented") + } + + for _, interfaceID := range interfaceIDs { + nat, err := client.GetNat(ctx, interfaceID) + if err != nil { + fmt.Printf("Error getting nat for interface %s: %v\n", interfaceID, err) + } + + if err := renderer.Render(nat); err != nil { + return fmt.Errorf("error rendering nat: %w", err) + } + } + return nil +} diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index 53833ec..d8580df 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -252,3 +252,41 @@ func ProtoLBPrefixToProtoPrefix(lbprefix proto.LBPrefix) *proto.Prefix { UnderlayRoute: lbprefix.UnderlayRoute, } } + +func ProtoNatToNat(dpdkNat *proto.GetNATResponse, interfaceID string) (*Nat, error) { + var underlayRoute netip.Addr + if underlayRouteString := string(dpdkNat.GetUnderlayRoute()); underlayRouteString != "" { + var err error + underlayRoute, err = netip.ParseAddr(string(dpdkNat.GetUnderlayRoute())) + if err != nil { + return nil, fmt.Errorf("error parsing underlay ip: %w", err) + } + } + var natvipip netip.Addr + if natvipipString := string(dpdkNat.GetNatVIPIP().Address); natvipipString != "" { + var err error + natvipip, err = netip.ParseAddr(string(dpdkNat.GetNatVIPIP().Address)) + if err != nil { + return nil, fmt.Errorf("error parsing lb ip: %w", err) + } + } + + return &Nat{ + TypeMeta: TypeMeta{ + Kind: NatKind, + }, + NatMeta: NatMeta{ + InterfaceID: interfaceID, + }, + Spec: NatSpec{ + NatVIPIP: natvipip, + MinPort: dpdkNat.MinPort, + MaxPort: dpdkNat.MaxPort, + UnderlayRoute: underlayRoute, + }, + Status: NatStatus{ + Error: dpdkNat.Status.Error, + Message: dpdkNat.Status.Message, + }, + }, nil +} diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 5a9b8da..9e2594c 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -246,7 +246,7 @@ type Nat struct { } type NatMeta struct { - InterfaceID string `json:"id"` + InterfaceID string `json:"interfaceID"` } func (m *NatMeta) GetName() string { @@ -255,9 +255,9 @@ func (m *NatMeta) GetName() string { type NatSpec struct { NatVIPIP netip.Addr `json:"natVIPIP"` - MinPort uint32 `json:"device"` - MaxPort uint32 `json:"ips"` - UnderlayRoute netip.Addr `json:"underplayRoute"` + MinPort uint32 `json:"minPort"` + MaxPort uint32 `json:"maxPort"` + UnderlayRoute netip.Addr `json:"underlayRoute"` } type NatStatus struct { diff --git a/dpdk/client/client.go b/dpdk/client/client.go index b4492af..8a66dd3 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -55,6 +55,7 @@ type Client interface { CreateRoute(ctx context.Context, route *api.Route) (*api.Route, error) DeleteRoute(ctx context.Context, vni uint32, prefix netip.Prefix, nextHopVNI uint32, nextHopIP netip.Addr) error + GetNat(ctx context.Context, interfaceID string) (*api.Nat, error) CreateNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) } @@ -519,6 +520,18 @@ func (c *client) ListRoutes(ctx context.Context, vni uint32) (*api.RouteList, er }, nil } +func (c *client) GetNat(ctx context.Context, interfaceID string) (*api.Nat, error) { + res, err := c.DPDKonmetalClient.GetNAT(ctx, &dpdkproto.GetNATRequest{InterfaceID: []byte(interfaceID)}) + if err != nil { + return nil, err + } + if errorCode := res.GetStatus().GetError(); errorCode != 0 { + return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + } + nat, err := api.ProtoNatToNat(res, interfaceID) + return nat, err +} + func (c *client) CreateNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) { res, err := c.DPDKonmetalClient.AddNAT(ctx, &dpdkproto.AddNATRequest{ InterfaceID: []byte(nat.NatMeta.InterfaceID), From a9967ddfc7ab379f0f53b89e719f707166cccf58 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Fri, 5 May 2023 09:03:41 +0200 Subject: [PATCH 10/65] add DeleteNat --- cmd/create_nat.go | 4 +-- cmd/delete.go | 1 + cmd/delete_nat.go | 78 +++++++++++++++++++++++++++++++++++++++++++ cmd/get_nat.go | 2 +- dpdk/client/client.go | 14 ++++++++ 5 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 cmd/delete_nat.go diff --git a/cmd/create_nat.go b/cmd/create_nat.go index 170d34e..fe03238 100644 --- a/cmd/create_nat.go +++ b/cmd/create_nat.go @@ -94,7 +94,7 @@ func RunCreateNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rend return fmt.Errorf("error creating renderer: %w", err) } - lb, err := client.CreateNat(ctx, &api.Nat{ + nat, err := client.CreateNat(ctx, &api.Nat{ NatMeta: api.NatMeta{ InterfaceID: interfaceID, }, @@ -108,7 +108,7 @@ func RunCreateNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rend return fmt.Errorf("error creating nat: %w", err) } - if err := renderer.Render(lb); err != nil { + if err := renderer.Render(nat); err != nil { return fmt.Errorf("error rendering nat: %w", err) } return nil diff --git a/cmd/delete.go b/cmd/delete.go index f74f9d2..26919d9 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -49,6 +49,7 @@ func Delete(factory DPDKClientFactory) *cobra.Command { DeleteLoadBalancer(factory), DeleteLoadBalancerPrefix(factory), DeleteLoadBalancerTarget(factory), + DeleteNat(factory), } cmd.Short = fmt.Sprintf("Deletes one of %v", CommandNames(subcommands)) diff --git a/cmd/delete_nat.go b/cmd/delete_nat.go new file mode 100644 index 0000000..43bb2b8 --- /dev/null +++ b/cmd/delete_nat.go @@ -0,0 +1,78 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func DeleteNat(factory DPDKClientFactory) *cobra.Command { + var ( + opts DeleteNatOptions + ) + + cmd := &cobra.Command{ + Use: "nat [...]", + Short: "Delete nat(s)", + Aliases: NatAliases, + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + interfaceIDs := args + return RunDeleteNat(cmd.Context(), factory, interfaceIDs, opts) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type DeleteNatOptions struct { +} + +func (o *DeleteNatOptions) AddFlags(fs *pflag.FlagSet) { +} + +func (o *DeleteNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { + return nil +} + +func RunDeleteNat(ctx context.Context, factory DPDKClientFactory, interfaceIDs []string, opts DeleteNatOptions) error { + client, cleanup, err := factory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + for _, interfaceID := range interfaceIDs { + if err := client.DeleteNat(ctx, interfaceID); err != nil { + fmt.Printf("Error deleting nat of interface %s: %v\n", interfaceID, err) + } + + fmt.Printf("Deleted nat of interface %s\n", interfaceID) + } + return nil +} diff --git a/cmd/get_nat.go b/cmd/get_nat.go index 3625f8f..849d179 100644 --- a/cmd/get_nat.go +++ b/cmd/get_nat.go @@ -91,7 +91,7 @@ func RunGetNat( for _, interfaceID := range interfaceIDs { nat, err := client.GetNat(ctx, interfaceID) if err != nil { - fmt.Printf("Error getting nat for interface %s: %v\n", interfaceID, err) + return fmt.Errorf("error getting nat for interface %s: %v", interfaceID, err) } if err := renderer.Render(nat); err != nil { diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 8a66dd3..f90668e 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -57,6 +57,7 @@ type Client interface { GetNat(ctx context.Context, interfaceID string) (*api.Nat, error) CreateNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) + DeleteNat(ctx context.Context, interfaceID string) error } type client struct { @@ -565,3 +566,16 @@ func (c *client) CreateNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) }, }, nil } + +func (c *client) DeleteNat(ctx context.Context, interfaceID string) error { + res, err := c.DPDKonmetalClient.DeleteNAT(ctx, &dpdkproto.DeleteNATRequest{ + InterfaceID: []byte(interfaceID), + }) + if err != nil { + return err + } + if errorCode := res.GetError(); errorCode != 0 { + return apierrors.NewStatusError(errorCode, res.GetMessage()) + } + return nil +} From a56455b80d034efdc7d8244f77a54cb97b5a1330 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Fri, 5 May 2023 15:34:35 +0200 Subject: [PATCH 11/65] add CreateNeighborNat and NeighborNat type --- cmd/common.go | 1 + cmd/create.go | 1 + cmd/create_neighbor_nat.go | 115 +++++++++++++++++++++++++++++++++++++ dpdk/api/conversion.go | 2 +- dpdk/api/register.go | 3 + dpdk/api/types.go | 28 +++++++++ dpdk/client/client.go | 24 ++++++++ 7 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 cmd/create_neighbor_nat.go diff --git a/cmd/common.go b/cmd/common.go index 57b7fe0..acbf6d4 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -221,4 +221,5 @@ var ( LoadBalancerPrefixAliases = []string{"loadbalancer-prefixes", "lbprfx", "lbprfxs"} LoadBalancerTargetAliases = []string{"loadbalancer-targets", "lbtrgt", "lbtrgts"} NatAliases = []string{"translation"} + NeighborNatAliases = []string{"nnat", "ngbnat", "neighnat"} ) diff --git a/cmd/create.go b/cmd/create.go index 3ed0357..f9a5911 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -51,6 +51,7 @@ func Create(dpdkClientFactory DPDKClientFactory) *cobra.Command { CreateLoadBalancerPrefix(dpdkClientFactory, rendererOptions), CreateLoadBalancerTarget(dpdkClientFactory, rendererOptions), CreateNat(dpdkClientFactory, rendererOptions), + CreateNeighborNat(dpdkClientFactory, rendererOptions), } cmd.Short = fmt.Sprintf("Creates one of %v", CommandNames(subcommands)) diff --git a/cmd/create_neighbor_nat.go b/cmd/create_neighbor_nat.go new file mode 100644 index 0000000..bf65c23 --- /dev/null +++ b/cmd/create_neighbor_nat.go @@ -0,0 +1,115 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "net/netip" + + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/flag" + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func CreateNeighborNat(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + var ( + opts CreateNeighborNatOptions + ) + + cmd := &cobra.Command{ + Use: "neighbornat [flags]", + Short: "Create a Neighbor NAT", + Example: "dpservice-cli create neighbornat 10.20.30.40 --vni=100 --minport=30000 --maxport=30100 --underlayroute=ff80::1", + Aliases: NeighborNatAliases, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + natVIPIP, err := netip.ParseAddr(args[0]) + if err != nil { + return fmt.Errorf("error parsing nat vip ip: %w", err) + } + + return RunCreateNeighborNat( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + natVIPIP, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type CreateNeighborNatOptions struct { + Vni uint32 + MinPort uint32 + MaxPort uint32 + UnderlayRoute netip.Addr +} + +func (o *CreateNeighborNatOptions) AddFlags(fs *pflag.FlagSet) { + fs.Uint32Var(&o.Vni, "vni", o.Vni, "VNI of neighbor NAT.") + fs.Uint32Var(&o.MinPort, "minport", o.MinPort, "MinPort of neighbor NAT.") + fs.Uint32Var(&o.MaxPort, "maxport", o.MaxPort, "MaxPort of neighbor NAT.") + flag.AddrVar(fs, &o.UnderlayRoute, "underlayroute", o.UnderlayRoute, "Underlay route of neighbor NAT.") +} + +func (o *CreateNeighborNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"vni", "minport", "maxport", "underlayroute"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunCreateNeighborNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, natVIPIP netip.Addr, opts CreateNeighborNatOptions) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + err = client.CreateNeighborNat(ctx, &api.NeighborNat{ + NeighborNatMeta: api.NeighborNatMeta{ + NatVIPIP: natVIPIP, + }, + Spec: api.NeighborNatSpec{ + Vni: opts.Vni, + MinPort: opts.MinPort, + MaxPort: opts.MaxPort, + UnderlayRoute: opts.UnderlayRoute, + }, + }) + + if err != nil { + return fmt.Errorf("error creating neighbor nat: %w", err) + } + fmt.Printf("Neighbor NAT with IP: %s created\n", natVIPIP.String()) + + return nil +} diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index d8580df..9fc6b7e 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -267,7 +267,7 @@ func ProtoNatToNat(dpdkNat *proto.GetNATResponse, interfaceID string) (*Nat, err var err error natvipip, err = netip.ParseAddr(string(dpdkNat.GetNatVIPIP().Address)) if err != nil { - return nil, fmt.Errorf("error parsing lb ip: %w", err) + return nil, fmt.Errorf("error parsing nat ip: %w", err) } } diff --git a/dpdk/api/register.go b/dpdk/api/register.go index 3a3ffc2..47cdfd6 100644 --- a/dpdk/api/register.go +++ b/dpdk/api/register.go @@ -30,7 +30,10 @@ func init() { &RouteList{}, &VirtualIP{}, &LoadBalancer{}, + &LoadBalancerTarget{}, + &LoadBalancerTargetList{}, &Nat{}, + &NeighborNat{}, ); err != nil { panic(err) } diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 9e2594c..b87b8a5 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -265,6 +265,33 @@ type NatStatus struct { Message string `json:"message,omitempty"` } +type NeighborNat struct { + TypeMeta `json:",inline"` + NeighborNatMeta `json:"metadata"` + Spec NeighborNatSpec `json:"spec"` + Status NeighborNatStatus `json:"status"` +} + +type NeighborNatMeta struct { + NatVIPIP netip.Addr `json:"natVIPIP"` +} + +func (m *NeighborNatMeta) GetName() string { + return m.NatVIPIP.String() +} + +type NeighborNatSpec struct { + Vni uint32 `json:"vni"` + MinPort uint32 `json:"minPort"` + MaxPort uint32 `json:"maxPort"` + UnderlayRoute netip.Addr `json:"underlayRoute"` +} + +type NeighborNatStatus struct { + Error int32 `json:"error,omitempty"` + Message string `json:"message,omitempty"` +} + var ( InterfaceKind = reflect.TypeOf(Interface{}).Name() InterfaceListKind = reflect.TypeOf(InterfaceList{}).Name() @@ -277,4 +304,5 @@ var ( RouteKind = reflect.TypeOf(Route{}).Name() RouteListKind = reflect.TypeOf(RouteList{}).Name() NatKind = reflect.TypeOf(Nat{}).Name() + NeighborNatKind = reflect.TypeOf(NeighborNat{}).Name() ) diff --git a/dpdk/client/client.go b/dpdk/client/client.go index f90668e..a711d67 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -58,6 +58,8 @@ type Client interface { GetNat(ctx context.Context, interfaceID string) (*api.Nat, error) CreateNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) DeleteNat(ctx context.Context, interfaceID string) error + + CreateNeighborNat(ctx context.Context, nat *api.NeighborNat) error } type client struct { @@ -579,3 +581,25 @@ func (c *client) DeleteNat(ctx context.Context, interfaceID string) error { } return nil } + +func (c *client) CreateNeighborNat(ctx context.Context, nNat *api.NeighborNat) error { + + res, err := c.DPDKonmetalClient.AddNeighborNAT(ctx, &dpdkproto.AddNeighborNATRequest{ + NatVIPIP: &dpdkproto.NATIP{ + IpVersion: api.NetIPAddrToProtoIPVersion(nNat.NeighborNatMeta.NatVIPIP), + Address: []byte(nNat.NeighborNatMeta.NatVIPIP.String()), + }, + Vni: nNat.Spec.Vni, + MinPort: nNat.Spec.MinPort, + MaxPort: nNat.Spec.MaxPort, + UnderlayRoute: []byte(nNat.Spec.UnderlayRoute.String()), + }) + if err != nil { + return err + } + + if res.Error == 0 { + return nil + } + return fmt.Errorf("%d", res.Error) +} From a286fae00d16bdcf11d96d96ddf9e0012d1c739c Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 9 May 2023 12:21:22 +0200 Subject: [PATCH 12/65] add GetNATInfo --- .vscode/launch.json | 2 +- cmd/get.go | 1 + cmd/get_nat_info.go | 109 ++++++++++++++++++++++++++++++++++++++++++ dpdk/api/register.go | 1 + dpdk/api/types.go | 14 ++++++ dpdk/client/client.go | 43 +++++++++++++++++ renderer/renderer.go | 2 + 7 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 cmd/get_nat_info.go diff --git a/.vscode/launch.json b/.vscode/launch.json index d675d60..c9cc756 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -15,7 +15,7 @@ //"args": ["get", "route", "0.0.0.0/24", "0", "fc00:2::64:0:1", "--vni", "100"] //"args": ["create", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] //"args": ["create", "lbtarget", "ff80::1", "--lb-id", "1"] - "args": ["get", "lbtarget", "1"] + "args": ["get", "natinfo", "10.20.30.40", "--nat-type=2"] } ] } \ No newline at end of file diff --git a/cmd/get.go b/cmd/get.go index 3198da6..72c65d6 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -40,6 +40,7 @@ func Get(factory DPDKClientFactory) *cobra.Command { GetLoadBalancerPrefix(factory, rendererOptions), GetLoadBalancerTargets(factory, rendererOptions), GetNat(factory, rendererOptions), + GetNatInfo(factory, rendererOptions), } cmd.Short = fmt.Sprintf("Gets/Lists one of %v", CommandNames(subcommands)) diff --git a/cmd/get_nat_info.go b/cmd/get_nat_info.go new file mode 100644 index 0000000..0359699 --- /dev/null +++ b/cmd/get_nat_info.go @@ -0,0 +1,109 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "net/netip" + "os" + + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func GetNatInfo(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + var ( + opts GetNatInfoOptions + ) + + cmd := &cobra.Command{ + Use: "natinfo <--nat-type>", + Short: "List all local machines that are behind this IP", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + natVIPIP, err := netip.ParseAddr(args[0]) + if err != nil { + return fmt.Errorf("error parsing nat vip ip: %w", err) + } + + return RunGetNatInfo( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + natVIPIP, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type GetNatInfoOptions struct { + NatType int32 +} + +func (o *GetNatInfoOptions) AddFlags(fs *pflag.FlagSet) { + fs.Int32Var(&o.NatType, "nat-type", o.NatType, "NAT Info type: NATInfoTypeZero = 0/NATInfoLocal = 1/NATInfoNeigh = 2") +} + +func (o *GetNatInfoOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"nat-type"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunGetNatInfo( + ctx context.Context, + dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, + natVIPIP netip.Addr, + opts GetNatInfoOptions, +) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error getting dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + renderer, err := rendererFactory.NewRenderer("", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + + natinfo, err := client.GetNATInfo(ctx, natVIPIP, opts.NatType) + if err != nil { + return fmt.Errorf("error getting nat info for interface %s: %v", natVIPIP, err) + } + + if err := renderer.Render(natinfo); err != nil { + return fmt.Errorf("error rendering nat info: %w", err) + } + + return nil +} diff --git a/dpdk/api/register.go b/dpdk/api/register.go index 47cdfd6..eea9332 100644 --- a/dpdk/api/register.go +++ b/dpdk/api/register.go @@ -33,6 +33,7 @@ func init() { &LoadBalancerTarget{}, &LoadBalancerTargetList{}, &Nat{}, + &NatList{}, &NeighborNat{}, ); err != nil { panic(err) diff --git a/dpdk/api/types.go b/dpdk/api/types.go index b87b8a5..b30eec2 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -265,6 +265,19 @@ type NatStatus struct { Message string `json:"message,omitempty"` } +type NatList struct { + TypeMeta `json:",inline"` + Items []Nat `json:"items"` +} + +func (l *NatList) GetItems() []Object { + res := make([]Object, len(l.Items)) + for i := range l.Items { + res[i] = &l.Items[i] + } + return res +} + type NeighborNat struct { TypeMeta `json:",inline"` NeighborNatMeta `json:"metadata"` @@ -304,5 +317,6 @@ var ( RouteKind = reflect.TypeOf(Route{}).Name() RouteListKind = reflect.TypeOf(RouteList{}).Name() NatKind = reflect.TypeOf(Nat{}).Name() + NatListKind = reflect.TypeOf(NatList{}).Name() NeighborNatKind = reflect.TypeOf(NeighborNat{}).Name() ) diff --git a/dpdk/client/client.go b/dpdk/client/client.go index a711d67..1364370 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -60,6 +60,7 @@ type Client interface { DeleteNat(ctx context.Context, interfaceID string) error CreateNeighborNat(ctx context.Context, nat *api.NeighborNat) error + GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType int32) (*api.NatList, error) } type client struct { @@ -603,3 +604,45 @@ func (c *client) CreateNeighborNat(ctx context.Context, nNat *api.NeighborNat) e } return fmt.Errorf("%d", res.Error) } + +func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType int32) (*api.NatList, error) { + res, err := c.DPDKonmetalClient.GetNATInfo(ctx, &dpdkproto.GetNATInfoRequest{ + NatVIPIP: &dpdkproto.NATIP{IpVersion: api.NetIPAddrToProtoIPVersion(natVIPIP), + Address: []byte(natVIPIP.String()), + }, + NatInfoType: dpdkproto.NATInfoType(natType), + }) + if err != nil { + return nil, err + } + + var nats = make([]api.Nat, len(res.NatInfoEntries)) + var nat api.Nat + for i, natInfoEntry := range res.GetNatInfoEntries() { + + var underlayRoute, vipIP netip.Addr + if res.NatInfoType == 2 { + underlayRoute, err = netip.ParseAddr(string(natInfoEntry.GetUnderlayRoute())) + if err != nil { + return nil, fmt.Errorf("error parsing underlay route: %w", err) + } + nat.Spec.UnderlayRoute = underlayRoute + } else if res.NatInfoType == 1 { + vipIP, err = netip.ParseAddr(string(natInfoEntry.GetAddress())) + if err != nil { + return nil, fmt.Errorf("error parsing vip ip: %w", err) + } + nat.Spec.NatVIPIP = vipIP + } + nat.InterfaceID = res.NatInfoType.String() + " " + res.NatVIPIP.String() + nat.Kind = api.NatKind + nat.Spec.MinPort = natInfoEntry.MinPort + nat.Spec.MaxPort = natInfoEntry.MaxPort + + nats[i] = nat + } + return &api.NatList{ + TypeMeta: api.TypeMeta{Kind: api.NatListKind}, + Items: nats, + }, nil +} diff --git a/renderer/renderer.go b/renderer/renderer.go index e149447..fc04b9c 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -172,6 +172,8 @@ func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) { return t.virtualIPTable([]api.VirtualIP{*obj}) case *api.Nat: return t.natTable([]api.Nat{*obj}) + case *api.NatList: + return t.natTable(obj.Items) default: return nil, fmt.Errorf("unsupported type %T", v) } From 6fe1f7c961d118573fa6f1f9a0f41a78974b4320 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 9 May 2023 12:59:07 +0200 Subject: [PATCH 13/65] add DeleteNeighborNat --- cmd/delete.go | 1 + cmd/delete_neighbor_nat.go | 103 +++++++++++++++++++++++++++++++++++++ dpdk/client/client.go | 20 +++++++ 3 files changed, 124 insertions(+) create mode 100644 cmd/delete_neighbor_nat.go diff --git a/cmd/delete.go b/cmd/delete.go index 26919d9..be65471 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -50,6 +50,7 @@ func Delete(factory DPDKClientFactory) *cobra.Command { DeleteLoadBalancerPrefix(factory), DeleteLoadBalancerTarget(factory), DeleteNat(factory), + DeleteNeighborNat(factory), } cmd.Short = fmt.Sprintf("Deletes one of %v", CommandNames(subcommands)) diff --git a/cmd/delete_neighbor_nat.go b/cmd/delete_neighbor_nat.go new file mode 100644 index 0000000..8500772 --- /dev/null +++ b/cmd/delete_neighbor_nat.go @@ -0,0 +1,103 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "net/netip" + + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func DeleteNeighborNat(factory DPDKClientFactory) *cobra.Command { + var ( + opts DeleteNeighborNatOptions + ) + + cmd := &cobra.Command{ + Use: "neighbornat [flags]", + Short: "Delete neighbor nat(s)", + Aliases: NeighborNatAliases, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + natVIPIP, err := netip.ParseAddr(args[0]) + if err != nil { + return fmt.Errorf("error parsing nat vip ip: %w", err) + } + + return RunDeleteNeighborNat(cmd.Context(), factory, natVIPIP, opts) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type DeleteNeighborNatOptions struct { + Vni uint32 + MinPort uint32 + MaxPort uint32 +} + +func (o *DeleteNeighborNatOptions) AddFlags(fs *pflag.FlagSet) { + fs.Uint32Var(&o.Vni, "vni", o.Vni, "VNI of neighbor NAT.") + fs.Uint32Var(&o.MinPort, "minport", o.MinPort, "MinPort of neighbor NAT.") + fs.Uint32Var(&o.MaxPort, "maxport", o.MaxPort, "MaxPort of neighbor NAT.") +} + +func (o *DeleteNeighborNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"vni", "minport", "maxport"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunDeleteNeighborNat(ctx context.Context, factory DPDKClientFactory, natVIPIP netip.Addr, opts DeleteNeighborNatOptions) error { + client, cleanup, err := factory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + neigbhorNat := api.NeighborNat{ + TypeMeta: api.TypeMeta{Kind: api.NatKind}, + NeighborNatMeta: api.NeighborNatMeta{NatVIPIP: natVIPIP}, + Spec: api.NeighborNatSpec{ + Vni: opts.Vni, + MinPort: opts.MinPort, + MaxPort: opts.MaxPort, + }, + } + if err := client.DeleteNeighborNat(ctx, neigbhorNat); err != nil { + fmt.Printf("Error deleting neighbor nat of interface %s: %v\n", natVIPIP, err) + } + + fmt.Printf("Deleted neighbor nat of interface %s\n", natVIPIP) + + return nil +} diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 1364370..7c83473 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -61,6 +61,7 @@ type Client interface { CreateNeighborNat(ctx context.Context, nat *api.NeighborNat) error GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType int32) (*api.NatList, error) + DeleteNeighborNat(ctx context.Context, neigbhorNat api.NeighborNat) error } type client struct { @@ -646,3 +647,22 @@ func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType in Items: nats, }, nil } + +func (c *client) DeleteNeighborNat(ctx context.Context, neigbhorNat api.NeighborNat) error { + res, err := c.DPDKonmetalClient.DeleteNeighborNAT(ctx, &dpdkproto.DeleteNeighborNATRequest{ + NatVIPIP: &dpdkproto.NATIP{ + IpVersion: api.NetIPAddrToProtoIPVersion(neigbhorNat.NatVIPIP), + Address: []byte(neigbhorNat.NatVIPIP.String()), + }, + Vni: neigbhorNat.Spec.Vni, + MinPort: neigbhorNat.Spec.MinPort, + MaxPort: neigbhorNat.Spec.MaxPort, + }) + if err != nil { + return err + } + if errorCode := res.GetError(); errorCode != 0 { + return apierrors.NewStatusError(errorCode, res.GetMessage()) + } + return nil +} From 8b1325aa90a837dbf7367dfcc3f22012d63885f7 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 9 May 2023 13:49:41 +0200 Subject: [PATCH 14/65] bump net-dpservice-go, add FirewallRule type --- dpdk/api/types.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/dpdk/api/types.go b/dpdk/api/types.go index b30eec2..e7e1a94 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -305,6 +305,49 @@ type NeighborNatStatus struct { Message string `json:"message,omitempty"` } +// FirewallRule section +type FirewallRule struct { + TypeMeta `json:",inline"` + FirewallRuleMeta `json:"metadata"` + Spec FirewallRuleSpec `json:"spec"` + Status FirewallRuleStatus `json:"status"` +} + +type FirewallRuleMeta struct { + RuleID string `json:"ruleID"` +} + +func (m *FirewallRuleMeta) GetName() string { + return m.RuleID +} + +type FirewallRuleSpec struct { + TrafficDirection uint8 `json:"trafficeDirection"` + FirewallAction uint8 `json:"firewallAction"` + Priority uint32 `json:"priority"` + SourcePrefix netip.Addr `json:"sourcePrefix"` + DestinationPrefix netip.Addr `json:"destinationPrefix"` + //ProtocolFilter proto.ProtocolFilter `json:"protocolFilter"` +} + +type FirewallRuleStatus struct { + Error int32 `json:"error,omitempty"` + Message string `json:"message,omitempty"` +} + +type FirewallRuleList struct { + TypeMeta `json:",inline"` + Items []FirewallRule `json:"items"` +} + +func (l *FirewallRuleList) GetItems() []Object { + res := make([]Object, len(l.Items)) + for i := range l.Items { + res[i] = &l.Items[i] + } + return res +} + var ( InterfaceKind = reflect.TypeOf(Interface{}).Name() InterfaceListKind = reflect.TypeOf(InterfaceList{}).Name() @@ -319,4 +362,6 @@ var ( NatKind = reflect.TypeOf(Nat{}).Name() NatListKind = reflect.TypeOf(NatList{}).Name() NeighborNatKind = reflect.TypeOf(NeighborNat{}).Name() + FirewallRuleKind = reflect.TypeOf(FirewallRule{}).Name() + FirewallRuleListKind = reflect.TypeOf(FirewallRuleList{}).Name() ) diff --git a/go.mod b/go.mod index af61ca0..0605b77 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/ghodss/yaml v1.0.0 github.com/google/addlicense v1.1.1 github.com/jedib0t/go-pretty/v6 v6.3.9 - github.com/onmetal/net-dpservice-go v0.1.9 + github.com/onmetal/net-dpservice-go v0.1.10 github.com/onsi/ginkgo/v2 v2.2.0 github.com/onsi/gomega v1.20.2 github.com/spf13/cobra v1.4.0 diff --git a/go.sum b/go.sum index 3e07247..3bf249b 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/onmetal/net-dpservice-go v0.1.9 h1:PXJDl5SFe/BnOeDCAnTJJlRTRuj33YgsEkifrHb+0DA= -github.com/onmetal/net-dpservice-go v0.1.9/go.mod h1:dbItLY1P+nYl7/c04JdJTEaMCG6GLXgAZsMP/OuZBmg= +github.com/onmetal/net-dpservice-go v0.1.10 h1:yzVMj+jylw7UGhJhLCP46gxNBn2GOfwunlxe0t/7/P0= +github.com/onmetal/net-dpservice-go v0.1.10/go.mod h1:dbItLY1P+nYl7/c04JdJTEaMCG6GLXgAZsMP/OuZBmg= github.com/onsi/ginkgo/v2 v2.2.0 h1:3ZNA3L1c5FYDFTTxbFeVGGD8jYvjYauHD30YgLxVsNI= github.com/onsi/ginkgo/v2 v2.2.0/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk= github.com/onsi/gomega v1.20.2 h1:8uQq0zMgLEfa0vRrrBgaJF2gyW9Da9BmfGV+OyUzfkY= From 01829a439e2673f04fb96b103eb6be4a9621a19a Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 9 May 2023 16:27:05 +0200 Subject: [PATCH 15/65] add CreateFirewallRule --- cmd/common.go | 1 + cmd/create.go | 1 + cmd/create_firewall_rule.go | 176 ++++++++++++++++++++++++++++++++++++ dpdk/api/register.go | 1 + dpdk/api/types.go | 20 ++-- dpdk/client/client.go | 34 +++++++ renderer/renderer.go | 17 ++++ 7 files changed, 242 insertions(+), 8 deletions(-) create mode 100644 cmd/create_firewall_rule.go diff --git a/cmd/common.go b/cmd/common.go index acbf6d4..ccc8b59 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -222,4 +222,5 @@ var ( LoadBalancerTargetAliases = []string{"loadbalancer-targets", "lbtrgt", "lbtrgts"} NatAliases = []string{"translation"} NeighborNatAliases = []string{"nnat", "ngbnat", "neighnat"} + FirewallRuleAliases = []string{"fwrule", "fw-rule", "firewallrule"} ) diff --git a/cmd/create.go b/cmd/create.go index f9a5911..6cd30c4 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -52,6 +52,7 @@ func Create(dpdkClientFactory DPDKClientFactory) *cobra.Command { CreateLoadBalancerTarget(dpdkClientFactory, rendererOptions), CreateNat(dpdkClientFactory, rendererOptions), CreateNeighborNat(dpdkClientFactory, rendererOptions), + CreateFirewallRule(dpdkClientFactory, rendererOptions), } cmd.Short = fmt.Sprintf("Creates one of %v", CommandNames(subcommands)) diff --git a/cmd/create_firewall_rule.go b/cmd/create_firewall_rule.go new file mode 100644 index 0000000..74d4eb5 --- /dev/null +++ b/cmd/create_firewall_rule.go @@ -0,0 +1,176 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "net/netip" + "os" + + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/flag" + "github.com/onmetal/dpservice-cli/util" + dpdkproto "github.com/onmetal/net-dpservice-go/proto" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func CreateFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + var ( + opts CreateFirewallRuleOptions + ) + + cmd := &cobra.Command{ + Use: "firewallrule [flags]", + Short: "Create a FirewallRule", + //Example: "dpservice-cli create nat vm1 --natip=10.20.30.40 --minport=30000 --maxport=30100", + Aliases: FirewallRuleAliases, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + interfaceID := args[0] + return RunCreateFirewallRule( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + interfaceID, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type CreateFirewallRuleOptions struct { + RuleID string + TrafficDirection uint8 + FirewallAction uint8 + Priority uint32 + IpVersion uint8 + SourcePrefix netip.Prefix + DestinationPrefix netip.Prefix + ProtocolFilter string + SrcPortLower int32 + SrcPortUpper int32 + DstPortLower int32 + DstPortUpper int32 + IcmpType int32 + IcmpCode int32 +} + +func (o *CreateFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.RuleID, "rule-id", o.RuleID, "RuleID of FW Rule.") + fs.Uint8Var(&o.TrafficDirection, "direction", o.TrafficDirection, "Traffic direction of FW Rule: Ingress = 0/Egress = 1") + fs.Uint8Var(&o.FirewallAction, "action", o.FirewallAction, "Firewall action: Drop = 0/Accept = 1 // Can be only \"accept\" at the moment.") + fs.Uint32Var(&o.Priority, "priority", o.Priority, "Priority of FW Rule. // For future use. No effect at the moment.") + fs.Uint8Var(&o.IpVersion, "ipv", o.IpVersion, "IpVersion of FW Rule IPv4 = 0/IPv6 = 1.") + flag.PrefixVar(fs, &o.SourcePrefix, "src", o.SourcePrefix, "Source prefix // 0.0.0.0 with prefix length 0 matches all source IPs.") + flag.PrefixVar(fs, &o.DestinationPrefix, "dst", o.DestinationPrefix, "Destination prefix // 0.0.0.0 with prefix length 0 matches all destination IPs.") + fs.StringVar(&o.ProtocolFilter, "protocol", o.ProtocolFilter, "Protocol used icmp/tcp/udp // Not defining a protocol filter matches all protocols.") + fs.Int32Var(&o.SrcPortLower, "srcPortLower", o.SrcPortLower, "Source Ports start // -1 matches all source ports.") + fs.Int32Var(&o.SrcPortUpper, "srcPortUpper", o.SrcPortUpper, "Source Ports end.") + fs.Int32Var(&o.DstPortLower, "dstPortLower", o.DstPortLower, "Destination Ports start // -1 matches all destination ports.") + fs.Int32Var(&o.DstPortUpper, "dstPortUpper", o.DstPortUpper, "Destination Ports end.") + fs.Int32Var(&o.IcmpType, "icmpType", o.IcmpType, "ICMP type // -1 matches all ICMP Types.") + fs.Int32Var(&o.IcmpCode, "icmpCode", o.IcmpCode, "ICMP code // -1 matches all ICMP Codes.") + +} + +func (o *CreateFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"rule-id", "direction", "action", "ipv", "src", "dst"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunCreateFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, interfaceID string, opts CreateFirewallRuleOptions) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + renderer, err := rendererFactory.NewRenderer("created", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + + srcPfx, err := netip.ParsePrefix(opts.SourcePrefix.String()) + if err != nil { + return fmt.Errorf("error parsing src prefix: %w", err) + } + dstPfx, err := netip.ParsePrefix(opts.DestinationPrefix.String()) + if err != nil { + return fmt.Errorf("error parsing dst prefix: %w", err) + } + var protocolFilter dpdkproto.ProtocolFilter + switch opts.ProtocolFilter { + case "icmp": + protocolFilter.Filter = &dpdkproto.ProtocolFilter_Icmp{Icmp: &dpdkproto.ICMPFilter{ + IcmpType: opts.IcmpType, + IcmpCode: opts.IcmpCode}} + case "tcp": + protocolFilter.Filter = &dpdkproto.ProtocolFilter_Tcp{Tcp: &dpdkproto.TCPFilter{ + SrcPortLower: opts.SrcPortLower, + SrcPortUpper: opts.DstPortUpper, + DstPortLower: opts.DstPortLower, + DstPortUpper: opts.DstPortUpper, + }} + case "udp": + protocolFilter.Filter = &dpdkproto.ProtocolFilter_Udp{Udp: &dpdkproto.UDPFilter{ + SrcPortLower: opts.SrcPortLower, + SrcPortUpper: opts.DstPortUpper, + DstPortLower: opts.DstPortLower, + DstPortUpper: opts.DstPortUpper, + }} + } + + fwrule, err := client.CreateFirewallRule(ctx, &api.FirewallRule{ + TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, + FirewallRuleMeta: api.FirewallRuleMeta{ + RuleID: opts.RuleID, + InterfaceID: interfaceID, + }, + Spec: api.FirewallRuleSpec{ + TrafficDirection: opts.TrafficDirection, + FirewallAction: opts.FirewallAction, + Priority: opts.Priority, + SourcePrefix: srcPfx, + DestinationPrefix: dstPfx, + ProtocolFilter: dpdkproto.ProtocolFilter{ + Filter: protocolFilter.Filter}, + }, + }, + ) + if err != nil { + return fmt.Errorf("error creating firewall rule: %w", err) + } + + if err := renderer.Render(fwrule); err != nil { + return fmt.Errorf("error rendering firewall rule: %w", err) + } + return nil +} diff --git a/dpdk/api/register.go b/dpdk/api/register.go index eea9332..926f1eb 100644 --- a/dpdk/api/register.go +++ b/dpdk/api/register.go @@ -35,6 +35,7 @@ func init() { &Nat{}, &NatList{}, &NeighborNat{}, + &FirewallRule{}, ); err != nil { panic(err) } diff --git a/dpdk/api/types.go b/dpdk/api/types.go index e7e1a94..e5a4196 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -18,6 +18,8 @@ import ( "fmt" "net/netip" "reflect" + + proto "github.com/onmetal/net-dpservice-go/proto" ) type Object interface { @@ -314,20 +316,22 @@ type FirewallRule struct { } type FirewallRuleMeta struct { - RuleID string `json:"ruleID"` + InterfaceID string `json:"interfaceID"` + RuleID string `json:"ruleID"` } func (m *FirewallRuleMeta) GetName() string { - return m.RuleID + return m.InterfaceID + "/" + m.RuleID } type FirewallRuleSpec struct { - TrafficDirection uint8 `json:"trafficeDirection"` - FirewallAction uint8 `json:"firewallAction"` - Priority uint32 `json:"priority"` - SourcePrefix netip.Addr `json:"sourcePrefix"` - DestinationPrefix netip.Addr `json:"destinationPrefix"` - //ProtocolFilter proto.ProtocolFilter `json:"protocolFilter"` + TrafficDirection uint8 `json:"trafficeDirection"` + FirewallAction uint8 `json:"firewallAction"` + Priority uint32 `json:"priority"` + IpVersion uint8 `json:"ipVersion"` + SourcePrefix netip.Prefix `json:"sourcePrefix"` + DestinationPrefix netip.Prefix `json:"destinationPrefix"` + ProtocolFilter proto.ProtocolFilter `json:"protocolFilter"` } type FirewallRuleStatus struct { diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 7c83473..3405ac2 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -62,6 +62,8 @@ type Client interface { CreateNeighborNat(ctx context.Context, nat *api.NeighborNat) error GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType int32) (*api.NatList, error) DeleteNeighborNat(ctx context.Context, neigbhorNat api.NeighborNat) error + + CreateFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) } type client struct { @@ -666,3 +668,35 @@ func (c *client) DeleteNeighborNat(ctx context.Context, neigbhorNat api.Neighbor } return nil } + +func (c *client) CreateFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) { + res, err := c.DPDKonmetalClient.AddFirewallRule(ctx, &dpdkproto.AddFirewallRuleRequest{ + InterfaceID: []byte(fwRule.FirewallRuleMeta.InterfaceID), + Rule: &dpdkproto.FirewallRule{ + RuleID: []byte(fwRule.FirewallRuleMeta.RuleID), + Direction: dpdkproto.TrafficDirection(fwRule.Spec.TrafficDirection), + Action: dpdkproto.FirewallAction(fwRule.Spec.FirewallAction), + Priority: fwRule.Spec.Priority, + IpVersion: dpdkproto.IPVersion(fwRule.Spec.IpVersion), + SourcePrefix: &dpdkproto.Prefix{ + IpVersion: dpdkproto.IPVersion(fwRule.Spec.IpVersion), + Address: []byte(fwRule.Spec.SourcePrefix.Addr().String()), + PrefixLength: uint32(fwRule.Spec.SourcePrefix.Bits()), + }, + DestinationPrefix: &dpdkproto.Prefix{ + IpVersion: dpdkproto.IPVersion(fwRule.Spec.IpVersion), + Address: []byte(fwRule.Spec.DestinationPrefix.Addr().String()), + PrefixLength: uint32(fwRule.Spec.DestinationPrefix.Bits()), + }, + ProtocolFilter: &fwRule.Spec.ProtocolFilter, + }, + }) + if err != nil { + return nil, err + } + + if res.Status.Error == 0 { + return nil, err + } + return &api.FirewallRule{FirewallRuleMeta: api.FirewallRuleMeta{RuleID: string(res.RuleID), InterfaceID: fwRule.InterfaceID}}, nil +} diff --git a/renderer/renderer.go b/renderer/renderer.go index fc04b9c..5170fec 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -174,6 +174,8 @@ func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) { return t.natTable([]api.Nat{*obj}) case *api.NatList: return t.natTable(obj.Items) + case *api.FirewallRule: + return t.fwruleTable([]api.FirewallRule{*obj}) default: return nil, fmt.Errorf("unsupported type %T", v) } @@ -285,6 +287,21 @@ func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { }, nil } +func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableData, error) { + // TODO add all fields + headers := []any{"interfaceID", "ruleID", "src", "dst", "action"} + + columns := make([][]any, len(fwrules)) + for i, fwrule := range fwrules { + columns[i] = []any{fwrule.FirewallRuleMeta.InterfaceID, fwrule.FirewallRuleMeta.RuleID, fwrule.Spec.SourcePrefix, fwrule.Spec.DestinationPrefix, fwrule.Spec.FirewallAction} + } + + return &TableData{ + Headers: headers, + Columns: columns, + }, nil +} + var ( lightBoxStyle = table.BoxStyle{ BottomLeft: "", From 427d00c05da4ffc817dfdeb53d29a951dfbf134b Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Wed, 10 May 2023 15:45:47 +0200 Subject: [PATCH 16/65] add GetFirewallRule --- .vscode/launch.json | 5 +- cmd/create_firewall_rule.go | 25 ++++++++- cmd/get.go | 1 + cmd/get_firewall_rule.go | 109 ++++++++++++++++++++++++++++++++++++ dpdk/api/conversion.go | 34 +++++++++++ dpdk/client/client.go | 25 ++++++++- renderer/renderer.go | 15 ++++- 7 files changed, 206 insertions(+), 8 deletions(-) create mode 100644 cmd/get_firewall_rule.go diff --git a/.vscode/launch.json b/.vscode/launch.json index c9cc756..6bfdc11 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -15,7 +15,10 @@ //"args": ["get", "route", "0.0.0.0/24", "0", "fc00:2::64:0:1", "--vni", "100"] //"args": ["create", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] //"args": ["create", "lbtarget", "ff80::1", "--lb-id", "1"] - "args": ["get", "natinfo", "10.20.30.40", "--nat-type=2"] + //"args": ["get", "natinfo", "10.20.30.40", "--nat-type=2"] + //"args": ["get", "fwrule", "vm1", "--rule-id=4"] + "args": ["create", "fwrule", "vm1", "--action=1", "--direction=1", "--dst=5.5.5.0/24", "--ipv=0", + "--priority=100", "--rule-id=9", "--src=1.1.1.1/32", "--protocol=tcp"] } ] } \ No newline at end of file diff --git a/cmd/create_firewall_rule.go b/cmd/create_firewall_rule.go index 74d4eb5..9fa673b 100644 --- a/cmd/create_firewall_rule.go +++ b/cmd/create_firewall_rule.go @@ -94,7 +94,8 @@ func (o *CreateFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { } func (o *CreateFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"rule-id", "direction", "action", "ipv", "src", "dst"} { + // TODO if protocol is not specified it should match all protocols + for _, name := range []string{"rule-id", "direction", "action", "ipv", "src", "dst", "protocol"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -126,6 +127,8 @@ func RunCreateFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFact if err != nil { return fmt.Errorf("error parsing dst prefix: %w", err) } + + // TODO add cases if icmp type or code is -1 var protocolFilter dpdkproto.ProtocolFilter switch opts.ProtocolFilter { case "icmp": @@ -133,16 +136,32 @@ func RunCreateFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFact IcmpType: opts.IcmpType, IcmpCode: opts.IcmpCode}} case "tcp": + if opts.SrcPortLower == -1 { + opts.SrcPortLower = 1 + opts.SrcPortUpper = 65535 + } + if opts.DstPortLower == -1 { + opts.DstPortLower = 1 + opts.DstPortUpper = 65535 + } protocolFilter.Filter = &dpdkproto.ProtocolFilter_Tcp{Tcp: &dpdkproto.TCPFilter{ SrcPortLower: opts.SrcPortLower, - SrcPortUpper: opts.DstPortUpper, + SrcPortUpper: opts.SrcPortUpper, DstPortLower: opts.DstPortLower, DstPortUpper: opts.DstPortUpper, }} case "udp": + if opts.SrcPortLower == -1 { + opts.SrcPortLower = 1 + opts.SrcPortUpper = 65535 + } + if opts.DstPortLower == -1 { + opts.DstPortLower = 1 + opts.DstPortUpper = 65535 + } protocolFilter.Filter = &dpdkproto.ProtocolFilter_Udp{Udp: &dpdkproto.UDPFilter{ SrcPortLower: opts.SrcPortLower, - SrcPortUpper: opts.DstPortUpper, + SrcPortUpper: opts.SrcPortUpper, DstPortLower: opts.DstPortLower, DstPortUpper: opts.DstPortUpper, }} diff --git a/cmd/get.go b/cmd/get.go index 72c65d6..9eecdc7 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -41,6 +41,7 @@ func Get(factory DPDKClientFactory) *cobra.Command { GetLoadBalancerTargets(factory, rendererOptions), GetNat(factory, rendererOptions), GetNatInfo(factory, rendererOptions), + GetFirewallRule(factory, rendererOptions), } cmd.Short = fmt.Sprintf("Gets/Lists one of %v", CommandNames(subcommands)) diff --git a/cmd/get_firewall_rule.go b/cmd/get_firewall_rule.go new file mode 100644 index 0000000..957df6d --- /dev/null +++ b/cmd/get_firewall_rule.go @@ -0,0 +1,109 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func GetFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + var ( + opts GetFirewallRuleOptions + ) + + cmd := &cobra.Command{ + Use: "firewallrule <--rule-id>", + Short: "Get firewall rule", + Example: "dpservice-cli get fwrule vm1 --rule-id=1", + Aliases: FirewallRuleAliases, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + interfaceID := args[0] + return RunGetFirewallRule( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + interfaceID, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type GetFirewallRuleOptions struct { + RuleID string +} + +func (o *GetFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.RuleID, "rule-id", o.RuleID, "Rule ID of Firewall Rule.") +} + +func (o *GetFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"rule-id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunGetFirewallRule( + ctx context.Context, + dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, + interfaceID string, + opts GetFirewallRuleOptions, +) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error getting dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + renderer, err := rendererFactory.NewRenderer("", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + + if len(interfaceID) == 0 { + return fmt.Errorf("need to specify interface id") + } + + fwrule, err := client.GetFirewallRule(ctx, interfaceID, opts.RuleID) + if err != nil { + return fmt.Errorf("error getting firewall rule: %w", err) + } + + if err := renderer.Render(fwrule); err != nil { + return fmt.Errorf("error rendering firewall rule %s/%s: %w", interfaceID, opts.RuleID, err) + } + return nil +} diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index 9fc6b7e..b5497cf 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -290,3 +290,37 @@ func ProtoNatToNat(dpdkNat *proto.GetNATResponse, interfaceID string) (*Nat, err }, }, nil } + +func ProtoFwRuleToFwRule(dpdkFwRule *proto.GetFirewallRuleResponse, interfaceID string) (*FirewallRule, error) { + + srcPrefix, err := ProtoPrefixToPrefix(interfaceID, dpdkFwRule.Rule.SourcePrefix) + if err != nil { + return nil, fmt.Errorf("error converting prefix: %w", err) + } + + dstPrefix, err := ProtoPrefixToPrefix(interfaceID, dpdkFwRule.Rule.DestinationPrefix) + if err != nil { + return nil, fmt.Errorf("error converting prefix: %w", err) + } + + return &FirewallRule{ + TypeMeta: TypeMeta{Kind: FirewallRuleKind}, + FirewallRuleMeta: FirewallRuleMeta{ + InterfaceID: interfaceID, + RuleID: string(dpdkFwRule.Rule.RuleID), + }, + Spec: FirewallRuleSpec{ + TrafficDirection: uint8(dpdkFwRule.Rule.Direction), + FirewallAction: uint8(dpdkFwRule.Rule.Action), + Priority: dpdkFwRule.Rule.Priority, + IpVersion: uint8(dpdkFwRule.Rule.IpVersion), + SourcePrefix: srcPrefix.Prefix, + DestinationPrefix: dstPrefix.Prefix, + ProtocolFilter: *dpdkFwRule.Rule.ProtocolFilter, + }, + Status: FirewallRuleStatus{ + Error: dpdkFwRule.Status.Error, + Message: dpdkFwRule.Status.Message, + }, + }, nil +} diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 3405ac2..71fff36 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -64,6 +64,7 @@ type Client interface { DeleteNeighborNat(ctx context.Context, neigbhorNat api.NeighborNat) error CreateFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) + GetFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) } type client struct { @@ -602,7 +603,7 @@ func (c *client) CreateNeighborNat(ctx context.Context, nNat *api.NeighborNat) e return err } - if res.Error == 0 { + if res.Error != 0 { return nil } return fmt.Errorf("%d", res.Error) @@ -695,8 +696,28 @@ func (c *client) CreateFirewallRule(ctx context.Context, fwRule *api.FirewallRul return nil, err } - if res.Status.Error == 0 { + if res.Status.Error != 0 { return nil, err } return &api.FirewallRule{FirewallRuleMeta: api.FirewallRuleMeta{RuleID: string(res.RuleID), InterfaceID: fwRule.InterfaceID}}, nil } + +func (c *client) GetFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) { + res, err := c.DPDKonmetalClient.GetFirewallRule(ctx, &dpdkproto.GetFirewallRuleRequest{ + InterfaceID: []byte(interfaceID), + RuleID: []byte(ruleID), + }) + if err != nil { + return nil, err + } + if errorCode := res.GetStatus().GetError(); errorCode != 0 { + return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + } + + fwrule, err := api.ProtoFwRuleToFwRule(res, interfaceID) + if err != nil { + return nil, err + } + + return fwrule, err +} diff --git a/renderer/renderer.go b/renderer/renderer.go index 5170fec..288570e 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -176,6 +176,8 @@ func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) { return t.natTable(obj.Items) case *api.FirewallRule: return t.fwruleTable([]api.FirewallRule{*obj}) + case *api.FirewallRuleList: + return t.fwruleTable(obj.Items) default: return nil, fmt.Errorf("unsupported type %T", v) } @@ -289,11 +291,20 @@ func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableData, error) { // TODO add all fields - headers := []any{"interfaceID", "ruleID", "src", "dst", "action"} + headers := []any{"interfaceID", "ruleID", "direction", "src", "dst", "action", "protocol", "priority"} columns := make([][]any, len(fwrules)) for i, fwrule := range fwrules { - columns[i] = []any{fwrule.FirewallRuleMeta.InterfaceID, fwrule.FirewallRuleMeta.RuleID, fwrule.Spec.SourcePrefix, fwrule.Spec.DestinationPrefix, fwrule.Spec.FirewallAction} + columns[i] = []any{ + fwrule.FirewallRuleMeta.InterfaceID, + fwrule.FirewallRuleMeta.RuleID, + dpdkproto.TrafficDirection(fwrule.Spec.TrafficDirection).String(), + fwrule.Spec.SourcePrefix, + fwrule.Spec.DestinationPrefix, + dpdkproto.FirewallAction(fwrule.Spec.FirewallAction).String(), + fwrule.Spec.ProtocolFilter.String(), + fwrule.Spec.Priority, + } } return &TableData{ From c540db10f9c0d4cc52344d129802b419da3d6530 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 11 May 2023 10:16:09 +0200 Subject: [PATCH 17/65] add DeleteFirewallRule --- cmd/create_firewall_rule.go | 6 +-- cmd/delete.go | 1 + cmd/delete_firewall_rule.go | 85 +++++++++++++++++++++++++++++++++++++ cmd/get_firewall_rule.go | 24 +++++------ dpdk/client/client.go | 17 +++++++- 5 files changed, 117 insertions(+), 16 deletions(-) create mode 100644 cmd/delete_firewall_rule.go diff --git a/cmd/create_firewall_rule.go b/cmd/create_firewall_rule.go index 9fa673b..0d246e4 100644 --- a/cmd/create_firewall_rule.go +++ b/cmd/create_firewall_rule.go @@ -34,9 +34,9 @@ func CreateFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory Ren ) cmd := &cobra.Command{ - Use: "firewallrule [flags]", - Short: "Create a FirewallRule", - //Example: "dpservice-cli create nat vm1 --natip=10.20.30.40 --minport=30000 --maxport=30100", + Use: "firewallrule [flags]", + Short: "Create a FirewallRule", + Example: "dpservice-cli create fwrule vm1 --action 1 --direction 1 --dst 5.5.5.0/24 --ipv 0 --priority 100 --rule-id 12 --src 1.1.1.1/32 --protocol tcp --srcPortLower 1 --srcPortUpper 1000 --dstPortLower 500 --dstPortUpper 600", Aliases: FirewallRuleAliases, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/delete.go b/cmd/delete.go index be65471..4e30229 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -51,6 +51,7 @@ func Delete(factory DPDKClientFactory) *cobra.Command { DeleteLoadBalancerTarget(factory), DeleteNat(factory), DeleteNeighborNat(factory), + DeleteFirewallRule(factory), } cmd.Short = fmt.Sprintf("Deletes one of %v", CommandNames(subcommands)) diff --git a/cmd/delete_firewall_rule.go b/cmd/delete_firewall_rule.go new file mode 100644 index 0000000..38cf75a --- /dev/null +++ b/cmd/delete_firewall_rule.go @@ -0,0 +1,85 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func DeleteFirewallRule(factory DPDKClientFactory) *cobra.Command { + var ( + opts DeleteFirewallRuleOptions + ) + + cmd := &cobra.Command{ + Use: "firewallrule [ ...] <--interface-id>", + Short: "Delete firewall rule(s)", + Aliases: FirewallRuleAliases, + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + ruleIDs := args + return RunDeleteFirewallRule(cmd.Context(), factory, ruleIDs, opts) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type DeleteFirewallRuleOptions struct { + InrerfaceID string +} + +func (o *DeleteFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.InrerfaceID, "interface-id", o.InrerfaceID, "Intreface ID where to delete firewall rule(s).") +} + +func (o *DeleteFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"interface-id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, ruleIDs []string, opts DeleteFirewallRuleOptions) error { + client, cleanup, err := factory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + for _, ruleID := range ruleIDs { + if err := client.DeleteFirewallRule(ctx, opts.InrerfaceID, ruleID); err != nil { + fmt.Printf("Error deleting firewall rule %s: %v\n", ruleID, err) + } + + fmt.Println("Deleted firewall rule", ruleID, "on interface", opts.InrerfaceID) + } + return nil +} diff --git a/cmd/get_firewall_rule.go b/cmd/get_firewall_rule.go index 957df6d..eb40c56 100644 --- a/cmd/get_firewall_rule.go +++ b/cmd/get_firewall_rule.go @@ -30,18 +30,18 @@ func GetFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory Render ) cmd := &cobra.Command{ - Use: "firewallrule <--rule-id>", + Use: "firewallrule <--interface-id>", Short: "Get firewall rule", - Example: "dpservice-cli get fwrule vm1 --rule-id=1", + Example: "dpservice-cli get fwrule 1 --interface-id=vm1", Aliases: FirewallRuleAliases, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - interfaceID := args[0] + ruleID := args[0] return RunGetFirewallRule( cmd.Context(), dpdkClientFactory, rendererFactory, - interfaceID, + ruleID, opts, ) }, @@ -55,15 +55,15 @@ func GetFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory Render } type GetFirewallRuleOptions struct { - RuleID string + InterfaceID string } func (o *GetFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { - fs.StringVar(&o.RuleID, "rule-id", o.RuleID, "Rule ID of Firewall Rule.") + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Rule ID of Firewall Rule.") } func (o *GetFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"rule-id"} { + for _, name := range []string{"interface-id"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -75,7 +75,7 @@ func RunGetFirewallRule( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - interfaceID string, + ruleID string, opts GetFirewallRuleOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) @@ -93,17 +93,17 @@ func RunGetFirewallRule( return fmt.Errorf("error creating renderer: %w", err) } - if len(interfaceID) == 0 { - return fmt.Errorf("need to specify interface id") + if len(ruleID) == 0 { + return fmt.Errorf("need to specify rule id") } - fwrule, err := client.GetFirewallRule(ctx, interfaceID, opts.RuleID) + fwrule, err := client.GetFirewallRule(ctx, ruleID, opts.InterfaceID) if err != nil { return fmt.Errorf("error getting firewall rule: %w", err) } if err := renderer.Render(fwrule); err != nil { - return fmt.Errorf("error rendering firewall rule %s/%s: %w", interfaceID, opts.RuleID, err) + return fmt.Errorf("error rendering firewall rule %s/%s: %w", ruleID, opts.InterfaceID, err) } return nil } diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 71fff36..af76eaa 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -65,6 +65,7 @@ type Client interface { CreateFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) GetFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) + DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) error } type client struct { @@ -702,7 +703,7 @@ func (c *client) CreateFirewallRule(ctx context.Context, fwRule *api.FirewallRul return &api.FirewallRule{FirewallRuleMeta: api.FirewallRuleMeta{RuleID: string(res.RuleID), InterfaceID: fwRule.InterfaceID}}, nil } -func (c *client) GetFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) { +func (c *client) GetFirewallRule(ctx context.Context, ruleID string, interfaceID string) (*api.FirewallRule, error) { res, err := c.DPDKonmetalClient.GetFirewallRule(ctx, &dpdkproto.GetFirewallRuleRequest{ InterfaceID: []byte(interfaceID), RuleID: []byte(ruleID), @@ -721,3 +722,17 @@ func (c *client) GetFirewallRule(ctx context.Context, interfaceID string, ruleID return fwrule, err } + +func (c *client) DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) error { + res, err := c.DPDKonmetalClient.DeleteFirewallRule(ctx, &dpdkproto.DeleteFirewallRuleRequest{ + InterfaceID: []byte(interfaceID), + RuleID: []byte(ruleID), + }) + if err != nil { + return err + } + if errorCode := res.GetError(); errorCode != 0 { + return apierrors.NewStatusError(errorCode, res.GetMessage()) + } + return nil +} From fdd4e3c43cdbb6f3ca1b8f98ef113c0a7907f93a Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 11 May 2023 11:49:36 +0200 Subject: [PATCH 18/65] add list command and ListFirewallRules --- cmd/command.go | 1 + cmd/common.go | 2 +- cmd/get_firewall_rule.go | 6 +-- cmd/list.go | 46 +++++++++++++++++ cmd/list_firewall_rule.go | 102 ++++++++++++++++++++++++++++++++++++++ dpdk/api/conversion.go | 22 ++++---- dpdk/client/client.go | 26 +++++++++- 7 files changed, 185 insertions(+), 20 deletions(-) create mode 100644 cmd/list.go create mode 100644 cmd/list_firewall_rule.go diff --git a/cmd/command.go b/cmd/command.go index 0aa1dc0..00cd3d0 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -32,6 +32,7 @@ func Command() *cobra.Command { cmd.AddCommand( Create(dpdkClientOptions), Get(dpdkClientOptions), + List(dpdkClientOptions), Delete(dpdkClientOptions), completionCmd, ) diff --git a/cmd/common.go b/cmd/common.go index ccc8b59..503b22f 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -222,5 +222,5 @@ var ( LoadBalancerTargetAliases = []string{"loadbalancer-targets", "lbtrgt", "lbtrgts"} NatAliases = []string{"translation"} NeighborNatAliases = []string{"nnat", "ngbnat", "neighnat"} - FirewallRuleAliases = []string{"fwrule", "fw-rule", "firewallrule"} + FirewallRuleAliases = []string{"fwrule", "fw-rule", "firewallrules", "fwrules", "fw-rules"} ) diff --git a/cmd/get_firewall_rule.go b/cmd/get_firewall_rule.go index eb40c56..d5b3010 100644 --- a/cmd/get_firewall_rule.go +++ b/cmd/get_firewall_rule.go @@ -59,7 +59,7 @@ type GetFirewallRuleOptions struct { } func (o *GetFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { - fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Rule ID of Firewall Rule.") + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "InterfaceID where is firewall rule.") } func (o *GetFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error { @@ -93,10 +93,6 @@ func RunGetFirewallRule( return fmt.Errorf("error creating renderer: %w", err) } - if len(ruleID) == 0 { - return fmt.Errorf("need to specify rule id") - } - fwrule, err := client.GetFirewallRule(ctx, ruleID, opts.InterfaceID) if err != nil { return fmt.Errorf("error getting firewall rule: %w", err) diff --git a/cmd/list.go b/cmd/list.go new file mode 100644 index 0000000..d6430fb --- /dev/null +++ b/cmd/list.go @@ -0,0 +1,46 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +func List(factory DPDKClientFactory) *cobra.Command { + rendererOptions := &RendererOptions{} + + cmd := &cobra.Command{ + Use: "list", + Args: cobra.NoArgs, + RunE: SubcommandRequired, + } + + rendererOptions.AddFlags(cmd.PersistentFlags()) + + subcommands := []*cobra.Command{ + ListFirewallRules(factory, rendererOptions), + } + + cmd.Short = fmt.Sprintf("Lists one of %v", CommandNames(subcommands)) + cmd.Long = fmt.Sprintf("Lists one of %v", CommandNames(subcommands)) + + cmd.AddCommand( + subcommands..., + ) + + return cmd +} diff --git a/cmd/list_firewall_rule.go b/cmd/list_firewall_rule.go new file mode 100644 index 0000000..4348c91 --- /dev/null +++ b/cmd/list_firewall_rule.go @@ -0,0 +1,102 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func ListFirewallRules(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + var ( + opts ListFirewallRulesOptions + ) + + cmd := &cobra.Command{ + Use: "firewallrules <--interface-id>", + Short: "List firewall rules on interface", + Example: "dpservice-cli list firewallrules --interface-id=vm1", + Aliases: FirewallRuleAliases, + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + return RunListFirewallRules( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type ListFirewallRulesOptions struct { + InterfaceID string +} + +func (o *ListFirewallRulesOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "InterfaceID from which to list firewall rules.") +} + +func (o *ListFirewallRulesOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"interface-id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunListFirewallRules( + ctx context.Context, + dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, + opts ListFirewallRulesOptions, +) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error getting dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + renderer, err := rendererFactory.NewRenderer("", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + + fwrules, err := client.ListFirewallRules(ctx, opts.InterfaceID) + if err != nil { + return fmt.Errorf("error listing firewall rules: %w", err) + } + + if err := renderer.Render(fwrules); err != nil { + return fmt.Errorf("error rendering firewall rules on interface %s: %w", opts.InterfaceID, err) + } + return nil +} diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index b5497cf..443787a 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -291,14 +291,14 @@ func ProtoNatToNat(dpdkNat *proto.GetNATResponse, interfaceID string) (*Nat, err }, nil } -func ProtoFwRuleToFwRule(dpdkFwRule *proto.GetFirewallRuleResponse, interfaceID string) (*FirewallRule, error) { +func ProtoFwRuleToFwRule(dpdkFwRule *proto.FirewallRule, interfaceID string) (*FirewallRule, error) { - srcPrefix, err := ProtoPrefixToPrefix(interfaceID, dpdkFwRule.Rule.SourcePrefix) + srcPrefix, err := ProtoPrefixToPrefix(interfaceID, dpdkFwRule.SourcePrefix) if err != nil { return nil, fmt.Errorf("error converting prefix: %w", err) } - dstPrefix, err := ProtoPrefixToPrefix(interfaceID, dpdkFwRule.Rule.DestinationPrefix) + dstPrefix, err := ProtoPrefixToPrefix(interfaceID, dpdkFwRule.DestinationPrefix) if err != nil { return nil, fmt.Errorf("error converting prefix: %w", err) } @@ -307,20 +307,16 @@ func ProtoFwRuleToFwRule(dpdkFwRule *proto.GetFirewallRuleResponse, interfaceID TypeMeta: TypeMeta{Kind: FirewallRuleKind}, FirewallRuleMeta: FirewallRuleMeta{ InterfaceID: interfaceID, - RuleID: string(dpdkFwRule.Rule.RuleID), + RuleID: string(dpdkFwRule.RuleID), }, Spec: FirewallRuleSpec{ - TrafficDirection: uint8(dpdkFwRule.Rule.Direction), - FirewallAction: uint8(dpdkFwRule.Rule.Action), - Priority: dpdkFwRule.Rule.Priority, - IpVersion: uint8(dpdkFwRule.Rule.IpVersion), + TrafficDirection: uint8(dpdkFwRule.Direction), + FirewallAction: uint8(dpdkFwRule.Action), + Priority: dpdkFwRule.Priority, + IpVersion: uint8(dpdkFwRule.IpVersion), SourcePrefix: srcPrefix.Prefix, DestinationPrefix: dstPrefix.Prefix, - ProtocolFilter: *dpdkFwRule.Rule.ProtocolFilter, - }, - Status: FirewallRuleStatus{ - Error: dpdkFwRule.Status.Error, - Message: dpdkFwRule.Status.Message, + ProtocolFilter: *dpdkFwRule.ProtocolFilter, }, }, nil } diff --git a/dpdk/client/client.go b/dpdk/client/client.go index af76eaa..15b51bf 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -63,6 +63,7 @@ type Client interface { GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType int32) (*api.NatList, error) DeleteNeighborNat(ctx context.Context, neigbhorNat api.NeighborNat) error + ListFirewallRules(ctx context.Context, interfaceID string) (*api.FirewallRuleList, error) CreateFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) GetFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) error @@ -671,6 +672,29 @@ func (c *client) DeleteNeighborNat(ctx context.Context, neigbhorNat api.Neighbor return nil } +func (c *client) ListFirewallRules(ctx context.Context, interfaceID string) (*api.FirewallRuleList, error) { + res, err := c.DPDKonmetalClient.ListFirewallRules(ctx, &dpdkproto.ListFirewallRulesRequest{ + InterfaceID: []byte(interfaceID), + }) + if err != nil { + return nil, err + } + + fwRules := make([]api.FirewallRule, len(res.GetRules())) + for i, dpdkFwRule := range res.GetRules() { + fwRule, err := api.ProtoFwRuleToFwRule(dpdkFwRule, interfaceID) + if err != nil { + return nil, err + } + fwRules[i] = *fwRule + } + + return &api.FirewallRuleList{ + TypeMeta: api.TypeMeta{Kind: api.FirewallRuleListKind}, + Items: fwRules, + }, nil +} + func (c *client) CreateFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) { res, err := c.DPDKonmetalClient.AddFirewallRule(ctx, &dpdkproto.AddFirewallRuleRequest{ InterfaceID: []byte(fwRule.FirewallRuleMeta.InterfaceID), @@ -715,7 +739,7 @@ func (c *client) GetFirewallRule(ctx context.Context, ruleID string, interfaceID return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) } - fwrule, err := api.ProtoFwRuleToFwRule(res, interfaceID) + fwrule, err := api.ProtoFwRuleToFwRule(res.Rule, interfaceID) if err != nil { return nil, err } From efdac42a7ec88d9297329ecdf05e2d16cd419a6f Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 11 May 2023 14:03:40 +0200 Subject: [PATCH 19/65] add Initialized --- cmd/command.go | 1 + cmd/initialized.go | 61 +++++++++++++++++++++++++++++++++++++++++++ dpdk/client/client.go | 10 +++++++ 3 files changed, 72 insertions(+) create mode 100644 cmd/initialized.go diff --git a/cmd/command.go b/cmd/command.go index 00cd3d0..b862316 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -34,6 +34,7 @@ func Command() *cobra.Command { Get(dpdkClientOptions), List(dpdkClientOptions), Delete(dpdkClientOptions), + Initialized(dpdkClientOptions), completionCmd, ) diff --git a/cmd/initialized.go b/cmd/initialized.go new file mode 100644 index 0000000..17d45dd --- /dev/null +++ b/cmd/initialized.go @@ -0,0 +1,61 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + + "github.com/spf13/cobra" +) + +func Initialized(factory DPDKClientFactory) *cobra.Command { + cmd := &cobra.Command{ + Use: "initialized", + Short: "Indicates if the DPDK app has been initialized already", + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + + return RunInitialized( + cmd.Context(), + factory, + ) + }, + } + + return cmd +} + +func RunInitialized( + ctx context.Context, + dpdkClientFactory DPDKClientFactory, +) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + uuid, err := client.Initialized(ctx) + if err != nil { + return fmt.Errorf("error: %w", err) + } + fmt.Println("UUID of dp-service:", uuid) + return nil +} diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 15b51bf..ab91e37 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -67,6 +67,8 @@ type Client interface { CreateFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) GetFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) error + + Initialized(ctx context.Context) (string, error) } type client struct { @@ -760,3 +762,11 @@ func (c *client) DeleteFirewallRule(ctx context.Context, interfaceID string, rul } return nil } + +func (c *client) Initialized(ctx context.Context) (string, error) { + res, err := c.DPDKonmetalClient.Initialized(ctx, &dpdkproto.Empty{}) + if err != nil { + return "", err + } + return res.Uuid, nil +} From 4c62c6fef1367a3deafc3b30116cd458396348a8 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 11 May 2023 15:15:21 +0200 Subject: [PATCH 20/65] add Init func --- cmd/command.go | 1 + cmd/init.go | 122 ++++++++++++++++++++++++++++++++++++++++++ dpdk/client/client.go | 12 +++++ 3 files changed, 135 insertions(+) create mode 100644 cmd/init.go diff --git a/cmd/command.go b/cmd/command.go index b862316..fbc9d37 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -35,6 +35,7 @@ func Command() *cobra.Command { List(dpdkClientOptions), Delete(dpdkClientOptions), Initialized(dpdkClientOptions), + Init(dpdkClientOptions), completionCmd, ) diff --git a/cmd/init.go b/cmd/init.go new file mode 100644 index 0000000..96241b8 --- /dev/null +++ b/cmd/init.go @@ -0,0 +1,122 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "net/netip" + "strings" + + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/util" + dpdkproto "github.com/onmetal/net-dpservice-go/proto" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func Init(factory DPDKClientFactory) *cobra.Command { + var ( + opts InitOptions + ) + + cmd := &cobra.Command{ + Use: "init [flags]", + Short: "Initial set up of the DPDK app", + Long: "To add multiple values to flags, use \",\" (comma) between values", + Example: "dpservice-cli init ff80::1/64 --pfnames=a,b,c --uplink-ports=e,f,g", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + prefix, err := netip.ParsePrefix(args[0]) + if err != nil { + return fmt.Errorf("error parsing prefix: %w", err) + } + + return RunInit( + cmd.Context(), + factory, + prefix, + opts, + ) + }, + } + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type InitOptions struct { + UplinkPorts string + PfNames string +} + +func (o *InitOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.UplinkPorts, "uplink-ports", o.UplinkPorts, "Linux name of the NICs that are connected to the Leaf Switches.") + fs.StringVar(&o.PfNames, "pfnames", o.PfNames, "Linux name of the Physical Functions, that Virtual Functions will be derived from.") +} + +func (o *InitOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"uplink-ports", "pfnames"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunInit( + ctx context.Context, + dpdkClientFactory DPDKClientFactory, + prefix netip.Prefix, + opts InitOptions, +) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + uuid, err := client.Initialized(ctx) + if err != nil { + return fmt.Errorf("error: %w", err) + } + if uuid != "" { + return fmt.Errorf("error dp-service already initialized, uuid: %s", uuid) + } + + uplinkPorts := strings.Split(opts.UplinkPorts, ",") + pfNames := strings.Split(opts.PfNames, ",") + + err = client.Init(ctx, dpdkproto.InitConfig{ + UnderlayIPv6Prefix: &dpdkproto.Prefix{ + IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Addr()), + Address: []byte(prefix.Addr().String()), + PrefixLength: uint32(prefix.Bits()), + }, + UplinkPorts: uplinkPorts, + PfNames: pfNames, + }) + if err != nil { + return fmt.Errorf("error: %w", err) + } + + return nil +} diff --git a/dpdk/client/client.go b/dpdk/client/client.go index ab91e37..509feba 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -69,6 +69,7 @@ type Client interface { DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) error Initialized(ctx context.Context) (string, error) + Init(ctx context.Context, initConfig dpdkproto.InitConfig) error } type client struct { @@ -770,3 +771,14 @@ func (c *client) Initialized(ctx context.Context) (string, error) { } return res.Uuid, nil } + +func (c *client) Init(ctx context.Context, initConfig dpdkproto.InitConfig) error { + res, err := c.DPDKonmetalClient.Init(ctx, &initConfig) + if err != nil { + return err + } + if errorCode := res.GetError(); errorCode != 0 { + return apierrors.NewStatusError(errorCode, res.GetMessage()) + } + return nil +} From c24cf3b94b4ed3d90da6990dc98ac3bf180443c7 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 11 May 2023 16:06:41 +0200 Subject: [PATCH 21/65] add Examples to create commands --- cmd/create_interface.go | 1 + cmd/create_loadbalancer_prefix.go | 3 ++- cmd/create_prefix.go | 1 + cmd/create_route.go | 1 + cmd/create_virtualip.go | 1 + cmd/get_virtualip.go | 2 +- cmd/init.go | 20 ++++++++------------ dpdk/client/client.go | 2 +- 8 files changed, 16 insertions(+), 15 deletions(-) diff --git a/cmd/create_interface.go b/cmd/create_interface.go index 83ff374..fa9455f 100644 --- a/cmd/create_interface.go +++ b/cmd/create_interface.go @@ -35,6 +35,7 @@ func CreateInterface(dpdkClientFactory DPDKClientFactory, rendererFactory Render cmd := &cobra.Command{ Use: "interface ", Short: "Create an interface", + Example: "dpservice-cli create interface vm4 --ips=10.200.1.4 --ips=2000:200:1::4 --vni=200 --device=net_tap5", Aliases: InterfaceAliases, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/create_loadbalancer_prefix.go b/cmd/create_loadbalancer_prefix.go index 5f3c4ec..6c3a3fe 100644 --- a/cmd/create_loadbalancer_prefix.go +++ b/cmd/create_loadbalancer_prefix.go @@ -35,8 +35,9 @@ func CreateLoadBalancerPrefix( ) cmd := &cobra.Command{ - Use: "lbprefix ", + Use: "lbprefix <--interface-id>", Short: "Create a loadbalancer prefix", + Example: "dpservice-cli create lbprefix ff80::1/64 --interface-id=vm1", Args: cobra.ExactArgs(1), Aliases: PrefixAliases, RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/create_prefix.go b/cmd/create_prefix.go index 604ebb9..8f7df44 100644 --- a/cmd/create_prefix.go +++ b/cmd/create_prefix.go @@ -37,6 +37,7 @@ func CreatePrefix( cmd := &cobra.Command{ Use: "prefix ", Short: "Create a prefix", + Example: "dpservice-cli create prefix 10.20.30.0/24 --interface-id=vm1", Args: cobra.ExactArgs(1), Aliases: PrefixAliases, RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/create_route.go b/cmd/create_route.go index dd47bc3..24b6dcb 100644 --- a/cmd/create_route.go +++ b/cmd/create_route.go @@ -35,6 +35,7 @@ func CreateRoute(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFa cmd := &cobra.Command{ Use: "route ", Short: "Create a route", + Example: "dpservice-cli create route 10.100.3.0/24 0 fc00:2::64:0:1 --vni=100", Aliases: RouteAliases, Args: cobra.ExactArgs(3), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/create_virtualip.go b/cmd/create_virtualip.go index 36c4c7f..fc9d3e0 100644 --- a/cmd/create_virtualip.go +++ b/cmd/create_virtualip.go @@ -34,6 +34,7 @@ func CreateVirtualIP(dpdkClientFactory DPDKClientFactory, rendererFactory Render cmd := &cobra.Command{ Use: "virtualip ", Short: "Create a virtual ip", + Example: "dpservice-cli create virtualip 20.20.20.20 --interface-id=vm1", Aliases: VirtualIPAliases, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/get_virtualip.go b/cmd/get_virtualip.go index 642fd75..45136f7 100644 --- a/cmd/get_virtualip.go +++ b/cmd/get_virtualip.go @@ -91,7 +91,7 @@ func RunGetVirtualIP( for _, interfaceID := range interfaceIDs { virtualIP, err := client.GetVirtualIP(ctx, interfaceID) if err != nil { - fmt.Printf("Error getting virtual ip for interface %s: %v\n", interfaceID, err) + return fmt.Errorf("error getting virtual ip for interface %s: %v", interfaceID, err) } if err := renderer.Render(virtualIP); err != nil { diff --git a/cmd/init.go b/cmd/init.go index 96241b8..6038ca2 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -18,7 +18,6 @@ import ( "context" "fmt" "net/netip" - "strings" "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/util" @@ -35,8 +34,8 @@ func Init(factory DPDKClientFactory) *cobra.Command { cmd := &cobra.Command{ Use: "init [flags]", Short: "Initial set up of the DPDK app", - Long: "To add multiple values to flags, use \",\" (comma) between values", - Example: "dpservice-cli init ff80::1/64 --pfnames=a,b,c --uplink-ports=e,f,g", + Long: "To add multiple values use flag multiple times", + Example: "dpservice-cli init ff80::1/64 --pfnames=a --pfnames=b --uplink-ports=c --uplink-ports=d", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { prefix, err := netip.ParsePrefix(args[0]) @@ -60,13 +59,13 @@ func Init(factory DPDKClientFactory) *cobra.Command { } type InitOptions struct { - UplinkPorts string - PfNames string + UplinkPorts []string + PfNames []string } func (o *InitOptions) AddFlags(fs *pflag.FlagSet) { - fs.StringVar(&o.UplinkPorts, "uplink-ports", o.UplinkPorts, "Linux name of the NICs that are connected to the Leaf Switches.") - fs.StringVar(&o.PfNames, "pfnames", o.PfNames, "Linux name of the Physical Functions, that Virtual Functions will be derived from.") + fs.StringSliceVar(&o.UplinkPorts, "uplink-ports", o.UplinkPorts, "Linux name of the NICs that are connected to the Leaf Switches.") + fs.StringSliceVar(&o.PfNames, "pfnames", o.PfNames, "Linux name of the Physical Functions, that Virtual Functions will be derived from.") } func (o *InitOptions) MarkRequiredFlags(cmd *cobra.Command) error { @@ -102,17 +101,14 @@ func RunInit( return fmt.Errorf("error dp-service already initialized, uuid: %s", uuid) } - uplinkPorts := strings.Split(opts.UplinkPorts, ",") - pfNames := strings.Split(opts.PfNames, ",") - err = client.Init(ctx, dpdkproto.InitConfig{ UnderlayIPv6Prefix: &dpdkproto.Prefix{ IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Addr()), Address: []byte(prefix.Addr().String()), PrefixLength: uint32(prefix.Bits()), }, - UplinkPorts: uplinkPorts, - PfNames: pfNames, + UplinkPorts: opts.UplinkPorts, + PfNames: opts.PfNames, }) if err != nil { return fmt.Errorf("error: %w", err) diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 509feba..f575cfe 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -466,7 +466,7 @@ func (c *client) CreateRoute(ctx context.Context, route *api.Route) (*api.Route, Weight: 100, Prefix: &dpdkproto.Prefix{ IpVersion: api.NetIPAddrToProtoIPVersion(route.Prefix.Addr()), - Address: []byte(route.Prefix.String()), + Address: []byte(route.Prefix.Addr().String()), PrefixLength: uint32(route.Prefix.Bits()), }, NexthopVNI: route.NextHop.VNI, From a7b8f5e3eab3001b2e29e4c177a7005a8fb4318b Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Fri, 12 May 2023 12:40:59 +0200 Subject: [PATCH 22/65] add ListInterfaces, ListPrefixes --- cmd/list.go | 2 + ...irewall_rule.go => list_firewall_rules.go} | 0 cmd/list_interfaces.go | 84 +++++++++++++++ cmd/list_prefixes.go | 100 ++++++++++++++++++ 4 files changed, 186 insertions(+) rename cmd/{list_firewall_rule.go => list_firewall_rules.go} (100%) create mode 100644 cmd/list_interfaces.go create mode 100644 cmd/list_prefixes.go diff --git a/cmd/list.go b/cmd/list.go index d6430fb..deeedb8 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -33,6 +33,8 @@ func List(factory DPDKClientFactory) *cobra.Command { subcommands := []*cobra.Command{ ListFirewallRules(factory, rendererOptions), + ListInterfaces(factory, rendererOptions), + ListPrefixes(factory, rendererOptions), } cmd.Short = fmt.Sprintf("Lists one of %v", CommandNames(subcommands)) diff --git a/cmd/list_firewall_rule.go b/cmd/list_firewall_rules.go similarity index 100% rename from cmd/list_firewall_rule.go rename to cmd/list_firewall_rules.go diff --git a/cmd/list_interfaces.go b/cmd/list_interfaces.go new file mode 100644 index 0000000..1a540b4 --- /dev/null +++ b/cmd/list_interfaces.go @@ -0,0 +1,84 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func ListInterfaces(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + cmd := &cobra.Command{ + Use: "interfaces", + Short: "List all interfaces", + Example: "dpservice-cli list interfaces", + Aliases: InterfaceAliases, + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + return RunListInterfaces( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + ) + }, + } + + return cmd +} + +type ListInterfacesOptions struct { +} + +func (o *ListInterfacesOptions) AddFlags(fs *pflag.FlagSet) { +} + +func (o *ListInterfacesOptions) MarkRequiredFlags(cmd *cobra.Command) error { + return nil +} + +func RunListInterfaces( + ctx context.Context, + dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, +) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error getting dpdk client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + renderer, err := rendererFactory.NewRenderer("", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + + interfaceList, err := client.ListInterfaces(ctx) + if err != nil { + return fmt.Errorf("error listing firewall rules: %w", err) + } + + if err := renderer.Render(interfaceList); err != nil { + return fmt.Errorf("error rendering interfaces: %w", err) + } + return nil +} diff --git a/cmd/list_prefixes.go b/cmd/list_prefixes.go new file mode 100644 index 0000000..aad6c89 --- /dev/null +++ b/cmd/list_prefixes.go @@ -0,0 +1,100 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func ListPrefixes(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + var ( + opts ListPrefixesOptions + ) + + cmd := &cobra.Command{ + Use: "prefixes <--interface-id>", + Short: "List prefix(es) on interface.", + 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 +} + +type ListPrefixesOptions struct { + InterfaceID string +} + +func (o *ListPrefixesOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID of the prefix.") +} + +func (o *ListPrefixesOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"interface-id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunListPrefixes( + ctx context.Context, + factory DPDKClientFactory, + rendererFactory RendererFactory, + opts ListPrefixesOptions, +) error { + client, cleanup, err := factory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating client: %w", err) + } + defer func() { + if err := cleanup(); err != nil { + fmt.Printf("Error cleaning up client: %v\n", err) + } + }() + + renderer, err := rendererFactory.NewRenderer("", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + + prefixList, err := client.ListPrefixes(ctx, opts.InterfaceID) + if err != nil { + return fmt.Errorf("error listing prefixes: %w", err) + } + + if err := renderer.Render(prefixList); err != nil { + return fmt.Errorf("error rendering prefix list: %w", err) + } + return nil +} From 3bd6288aded0aafb992e97a3ccf5e22238dc4407 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Fri, 12 May 2023 14:37:23 +0200 Subject: [PATCH 23/65] add List LoadBalancerPrefixes, Prefixes, Routes --- cmd/get.go | 7 +- cmd/get_loadbalancer_prefix.go | 112 ------------------ cmd/list.go | 2 + ...refix.go => list_loadbalancer_prefixes.go} | 49 ++++---- cmd/list_prefixes.go | 2 + cmd/{get_route.go => list_routes.go} | 42 +++---- 6 files changed, 43 insertions(+), 171 deletions(-) delete mode 100644 cmd/get_loadbalancer_prefix.go rename cmd/{get_prefix.go => list_loadbalancer_prefixes.go} (63%) rename cmd/{get_route.go => list_routes.go} (66%) diff --git a/cmd/get.go b/cmd/get.go index 9eecdc7..9c42387 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -33,19 +33,16 @@ func Get(factory DPDKClientFactory) *cobra.Command { subcommands := []*cobra.Command{ GetInterface(factory, rendererOptions), - GetPrefix(factory, rendererOptions), - GetRoute(factory, rendererOptions), GetVirtualIP(factory, rendererOptions), GetLoadBalancer(factory, rendererOptions), - GetLoadBalancerPrefix(factory, rendererOptions), GetLoadBalancerTargets(factory, rendererOptions), GetNat(factory, rendererOptions), GetNatInfo(factory, rendererOptions), GetFirewallRule(factory, rendererOptions), } - cmd.Short = fmt.Sprintf("Gets/Lists one of %v", CommandNames(subcommands)) - cmd.Long = fmt.Sprintf("Gets/Lists one of %v", CommandNames(subcommands)) + cmd.Short = fmt.Sprintf("Gets one of %v", CommandNames(subcommands)) + cmd.Long = fmt.Sprintf("Gets one of %v", CommandNames(subcommands)) cmd.AddCommand( subcommands..., diff --git a/cmd/get_loadbalancer_prefix.go b/cmd/get_loadbalancer_prefix.go deleted file mode 100644 index 91231a8..0000000 --- a/cmd/get_loadbalancer_prefix.go +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright 2022 OnMetal authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package cmd - -import ( - "context" - "fmt" - "net/netip" - "os" - - "github.com/onmetal/dpservice-cli/util" - "github.com/spf13/cobra" - "github.com/spf13/pflag" -) - -func GetLoadBalancerPrefix(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { - var ( - opts GetPrefixOptions - ) - - cmd := &cobra.Command{ - Use: "lbprefix [...]", - Short: "Get or list loadbalancer prefix(es)", - Aliases: LoadBalancerPrefixAliases, - RunE: func(cmd *cobra.Command, args []string) error { - prefixes, err := ParsePrefixArgs(args) - if err != nil { - return err - } - - return RunGetLoadBalancerPrefix( - cmd.Context(), - dpdkClientFactory, - rendererFactory, - prefixes, - opts, - ) - }, - } - - opts.AddFlags(cmd.Flags()) - - util.Must(opts.MarkRequiredFlags(cmd)) - - return cmd -} - -type GetLoadBalancerPrefixOptions struct { - InterfaceID string -} - -func (o *GetLoadBalancerPrefixOptions) AddFlags(fs *pflag.FlagSet) { - fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID of the prefix.") -} - -func (o *GetLoadBalancerPrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"interface-id"} { - if err := cmd.MarkFlagRequired(name); err != nil { - return err - } - } - return nil -} - -func RunGetLoadBalancerPrefix( - ctx context.Context, - factory DPDKClientFactory, - rendererFactory RendererFactory, - prefixes []netip.Prefix, - opts GetPrefixOptions, -) error { - client, cleanup, err := factory.NewClient(ctx) - if err != nil { - return fmt.Errorf("error creating client: %w", err) - } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } - - if len(prefixes) == 0 { - prefixList, err := client.ListLoadBalancerPrefixes(ctx, opts.InterfaceID) - if err != nil { - return fmt.Errorf("error listing loadbalancer prefixes: %w", err) - } - - if err := renderer.Render(prefixList); err != nil { - return fmt.Errorf("error rendering list: %w", err) - } - return nil - } - - return fmt.Errorf("getting individual loadbalancer prefixes is not implemented") -} diff --git a/cmd/list.go b/cmd/list.go index deeedb8..dc6f189 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -35,6 +35,8 @@ func List(factory DPDKClientFactory) *cobra.Command { ListFirewallRules(factory, rendererOptions), ListInterfaces(factory, rendererOptions), ListPrefixes(factory, rendererOptions), + ListLoadBalancerPrefixes(factory, rendererOptions), + ListRoutes(factory, rendererOptions), } cmd.Short = fmt.Sprintf("Lists one of %v", CommandNames(subcommands)) diff --git a/cmd/get_prefix.go b/cmd/list_loadbalancer_prefixes.go similarity index 63% rename from cmd/get_prefix.go rename to cmd/list_loadbalancer_prefixes.go index c2d46ac..acf7c9f 100644 --- a/cmd/get_prefix.go +++ b/cmd/list_loadbalancer_prefixes.go @@ -17,7 +17,6 @@ package cmd import ( "context" "fmt" - "net/netip" "os" "github.com/onmetal/dpservice-cli/util" @@ -25,26 +24,23 @@ import ( "github.com/spf13/pflag" ) -func GetPrefix(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func ListLoadBalancerPrefixes(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( - opts GetPrefixOptions + opts ListLoadBalancerPrefixesOptions ) cmd := &cobra.Command{ - Use: "prefix [...]", - Short: "Get or list prefix(es)", - Aliases: PrefixAliases, + Use: "lbprefixes <--interface-id>", + Short: "List loadbalancer prefixes on interface.", + Example: "dpservice-cli list lbprefixes --interface-id=vm1", + Aliases: LoadBalancerPrefixAliases, + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - prefixes, err := ParsePrefixArgs(args) - if err != nil { - return err - } - return RunGetPrefix( + return RunListLoadBalancerPrefixes( cmd.Context(), dpdkClientFactory, rendererFactory, - prefixes, opts, ) }, @@ -57,15 +53,15 @@ func GetPrefix(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFact return cmd } -type GetPrefixOptions struct { +type ListLoadBalancerPrefixesOptions struct { InterfaceID string } -func (o *GetPrefixOptions) AddFlags(fs *pflag.FlagSet) { +func (o *ListLoadBalancerPrefixesOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID of the prefix.") } -func (o *GetPrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { +func (o *ListLoadBalancerPrefixesOptions) MarkRequiredFlags(cmd *cobra.Command) error { for _, name := range []string{"interface-id"} { if err := cmd.MarkFlagRequired(name); err != nil { return err @@ -74,12 +70,11 @@ func (o *GetPrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunGetPrefix( +func RunListLoadBalancerPrefixes( ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, - prefixes []netip.Prefix, - opts GetPrefixOptions, + opts ListLoadBalancerPrefixesOptions, ) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { @@ -96,17 +91,13 @@ func RunGetPrefix( return fmt.Errorf("error creating renderer: %w", err) } - if len(prefixes) == 0 { - prefixList, err := client.ListPrefixes(ctx, opts.InterfaceID) - if err != nil { - return fmt.Errorf("error listing prefixes: %w", err) - } - - if err := renderer.Render(prefixList); err != nil { - return fmt.Errorf("error rendering list: %w", err) - } - return nil + prefixList, err := client.ListLoadBalancerPrefixes(ctx, opts.InterfaceID) + if err != nil { + return fmt.Errorf("error listing loadbalancer prefixes: %w", err) } - return fmt.Errorf("getting individual prefixes is not implemented") + if err := renderer.Render(prefixList); err != nil { + return fmt.Errorf("error rendering list: %w", err) + } + return nil } diff --git a/cmd/list_prefixes.go b/cmd/list_prefixes.go index aad6c89..289805c 100644 --- a/cmd/list_prefixes.go +++ b/cmd/list_prefixes.go @@ -32,6 +32,8 @@ func ListPrefixes(dpdkClientFactory DPDKClientFactory, rendererFactory RendererF cmd := &cobra.Command{ Use: "prefixes <--interface-id>", Short: "List prefix(es) on interface.", + Example: "dpservice-cli list prefixes --interface-id=vm1", + Args: cobra.ExactArgs(0), Aliases: PrefixAliases, RunE: func(cmd *cobra.Command, args []string) error { return RunListPrefixes( diff --git a/cmd/get_route.go b/cmd/list_routes.go similarity index 66% rename from cmd/get_route.go rename to cmd/list_routes.go index d6711c3..563794c 100644 --- a/cmd/get_route.go +++ b/cmd/list_routes.go @@ -24,27 +24,23 @@ import ( "github.com/spf13/pflag" ) -func GetRoute(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func ListRoutes(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( - opts GetRouteOptions + opts ListRoutesOptions ) cmd := &cobra.Command{ - Use: "route [ ...]", - Short: "Get or list route(s)", + Use: "routes <--vni>", + Short: "List routes of specified VNI", + Example: "dpservice-cli list routes --vni=100", Aliases: RouteAliases, - Args: MultipleOfArgs(3), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - keys, err := ParseRouteKeyArgs(args) - if err != nil { - return err - } return RunGetRoute( cmd.Context(), dpdkClientFactory, rendererFactory, - keys, opts, ) }, @@ -57,15 +53,15 @@ func GetRoute(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFacto return cmd } -type GetRouteOptions struct { +type ListRoutesOptions struct { VNI uint32 } -func (o *GetRouteOptions) AddFlags(fs *pflag.FlagSet) { +func (o *ListRoutesOptions) AddFlags(fs *pflag.FlagSet) { fs.Uint32Var(&o.VNI, "vni", o.VNI, "VNI to get the routes from.") } -func (o *GetRouteOptions) MarkRequiredFlags(cmd *cobra.Command) error { +func (o *ListRoutesOptions) MarkRequiredFlags(cmd *cobra.Command) error { for _, name := range []string{"vni"} { if err := cmd.MarkFlagRequired(name); err != nil { return err @@ -78,8 +74,7 @@ func RunGetRoute( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - keys []RouteKey, - opts GetRouteOptions, + opts ListRoutesOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { @@ -96,16 +91,13 @@ func RunGetRoute( return fmt.Errorf("error creating renderer: %w", err) } - if len(keys) == 0 { - routeList, err := client.ListRoutes(ctx, opts.VNI) - if err != nil { - return fmt.Errorf("error listing routes: %w", err) - } + routeList, err := client.ListRoutes(ctx, opts.VNI) + if err != nil { + return fmt.Errorf("error listing routes: %w", err) + } - if err := renderer.Render(routeList); err != nil { - return fmt.Errorf("error rendering list: %w", err) - } - return nil + if err := renderer.Render(routeList); err != nil { + return fmt.Errorf("error rendering list: %w", err) } - return fmt.Errorf("getting individual routes is not implemented") + return nil } From 3e7c25d8cf3f10c1d0f7ee016dd14703c1dfc674 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Fri, 12 May 2023 15:50:05 +0200 Subject: [PATCH 24/65] add Example of delete commands --- cmd/delete_firewall_rule.go | 1 + cmd/delete_interface.go | 3 ++- cmd/delete_loadbalnacer.go | 3 ++- cmd/delete_loadblanacer_prefix.go | 1 + cmd/delete_nat.go | 1 + cmd/delete_neighbor_nat.go | 5 +++-- cmd/delete_prefix.go | 1 + cmd/delete_route.go | 3 ++- cmd/delete_virtualip.go | 1 + 9 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cmd/delete_firewall_rule.go b/cmd/delete_firewall_rule.go index 38cf75a..2d39b74 100644 --- a/cmd/delete_firewall_rule.go +++ b/cmd/delete_firewall_rule.go @@ -31,6 +31,7 @@ func DeleteFirewallRule(factory DPDKClientFactory) *cobra.Command { cmd := &cobra.Command{ Use: "firewallrule [ ...] <--interface-id>", Short: "Delete firewall rule(s)", + Example: "dpservice-cli delete firewallrule 1 --interface-id=vm1", Aliases: FirewallRuleAliases, Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/delete_interface.go b/cmd/delete_interface.go index f329dec..79e7293 100644 --- a/cmd/delete_interface.go +++ b/cmd/delete_interface.go @@ -29,8 +29,9 @@ func DeleteInterface(factory DPDKClientFactory) *cobra.Command { ) cmd := &cobra.Command{ - Use: "interface [ ...]", + Use: "interface [ ...]", Short: "Delete interface(s)", + Example: "dpservice-cli delete interface vm1", Aliases: InterfaceAliases, Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/delete_loadbalnacer.go b/cmd/delete_loadbalnacer.go index 8c8fd27..0e85e71 100644 --- a/cmd/delete_loadbalnacer.go +++ b/cmd/delete_loadbalnacer.go @@ -29,8 +29,9 @@ func DeleteLoadBalancer(factory DPDKClientFactory) *cobra.Command { ) cmd := &cobra.Command{ - Use: "loadbalancer [ ...]", + Use: "loadbalancer [ ...]", Short: "Delete loadbalancer(s)", + Example: "dpservice-cli delete loadbalancer 1", Aliases: LoadBalancerAliases, Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/delete_loadblanacer_prefix.go b/cmd/delete_loadblanacer_prefix.go index 11ed28d..7043a68 100644 --- a/cmd/delete_loadblanacer_prefix.go +++ b/cmd/delete_loadblanacer_prefix.go @@ -32,6 +32,7 @@ func DeleteLoadBalancerPrefix(factory DPDKClientFactory) *cobra.Command { cmd := &cobra.Command{ Use: "lbprefix [...]", Short: "Delete a loadbalancer prefix", + Example: "dpservice-cli delete lbprefix ff80::1/64 --interface-id=vm1", Aliases: LoadBalancerPrefixAliases, Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/delete_nat.go b/cmd/delete_nat.go index 43bb2b8..2bab8c7 100644 --- a/cmd/delete_nat.go +++ b/cmd/delete_nat.go @@ -31,6 +31,7 @@ func DeleteNat(factory DPDKClientFactory) *cobra.Command { cmd := &cobra.Command{ Use: "nat [...]", Short: "Delete nat(s)", + Example: "dpservice-cli delete nat vm1", Aliases: NatAliases, Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/delete_neighbor_nat.go b/cmd/delete_neighbor_nat.go index 8500772..8d9f24e 100644 --- a/cmd/delete_neighbor_nat.go +++ b/cmd/delete_neighbor_nat.go @@ -33,6 +33,7 @@ func DeleteNeighborNat(factory DPDKClientFactory) *cobra.Command { cmd := &cobra.Command{ Use: "neighbornat [flags]", Short: "Delete neighbor nat(s)", + Example: "dpservice-cli delete neighbornat 10.20.30.40 --vni=100 --minport=30000 --maxport=30100", Aliases: NeighborNatAliases, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { @@ -94,10 +95,10 @@ func RunDeleteNeighborNat(ctx context.Context, factory DPDKClientFactory, natVIP }, } if err := client.DeleteNeighborNat(ctx, neigbhorNat); err != nil { - fmt.Printf("Error deleting neighbor nat of interface %s: %v\n", natVIPIP, err) + return fmt.Errorf("error deleting neighbor nat with ip %s: %v", natVIPIP, err) } - fmt.Printf("Deleted neighbor nat of interface %s\n", natVIPIP) + fmt.Printf("Deleted neighbor NAT with IP %s\n", natVIPIP) return nil } diff --git a/cmd/delete_prefix.go b/cmd/delete_prefix.go index 5998299..cf1add6 100644 --- a/cmd/delete_prefix.go +++ b/cmd/delete_prefix.go @@ -32,6 +32,7 @@ func DeletePrefix(factory DPDKClientFactory) *cobra.Command { cmd := &cobra.Command{ Use: "prefix [...]", Short: "Delete a prefix", + Example: "dpservice-cli delete prefix 10.20.30.0/24 --interface-id=vm1", Aliases: PrefixAliases, Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/delete_route.go b/cmd/delete_route.go index bb3c8c0..4a64132 100644 --- a/cmd/delete_route.go +++ b/cmd/delete_route.go @@ -31,6 +31,7 @@ func DeleteRoute(factory DPDKClientFactory) *cobra.Command { cmd := &cobra.Command{ Use: "route [ ...]", Short: "Delete a route", + Example: "dpservice-cli delete route 10.100.2.0/24 0 fc00:2::64:0:1 --vni=100", Aliases: RouteAliases, Args: MultipleOfArgs(3), RunE: func(cmd *cobra.Command, args []string) error { @@ -80,7 +81,7 @@ func RunDeleteRoute(ctx context.Context, factory DPDKClientFactory, keys []Route for _, key := range keys { if err := client.DeleteRoute(ctx, opts.VNI, key.Prefix, key.NextHopVNI, key.NextHopIP); err != nil { - fmt.Printf("Error deleting route %d-%v:%d-%v: %v\n", opts.VNI, key.Prefix, key.NextHopVNI, key.NextHopIP, err) + return fmt.Errorf("error deleting route %d-%v:%d-%v: %v", opts.VNI, key.Prefix, key.NextHopVNI, key.NextHopIP, err) } fmt.Printf("Deleted route %d-%v:%d-%v\n", opts.VNI, key.Prefix, key.NextHopVNI, key.NextHopIP) } diff --git a/cmd/delete_virtualip.go b/cmd/delete_virtualip.go index 90f0c94..66c05df 100644 --- a/cmd/delete_virtualip.go +++ b/cmd/delete_virtualip.go @@ -31,6 +31,7 @@ func DeleteVirtualIP(factory DPDKClientFactory) *cobra.Command { cmd := &cobra.Command{ Use: "virtualip [...]", Short: "Delete virtual ip(s)", + Example: "dpservice-cli delete virtualip vm1", Aliases: VirtualIPAliases, Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { From e8c403bc2d54229a7294be5debcb3bd94de90145 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Mon, 15 May 2023 11:03:24 +0200 Subject: [PATCH 25/65] add Example of Get commands --- cmd/get_interface.go | 3 ++- cmd/get_loadbalancer.go | 3 ++- cmd/get_loadbalancer_target.go | 1 + cmd/get_nat.go | 2 ++ cmd/get_nat_info.go | 7 ++++--- cmd/get_virtualip.go | 4 +++- cmd/initialized.go | 7 ++++--- renderer/renderer.go | 4 ++-- 8 files changed, 20 insertions(+), 11 deletions(-) diff --git a/cmd/get_interface.go b/cmd/get_interface.go index ae4f9b7..d00f1c4 100644 --- a/cmd/get_interface.go +++ b/cmd/get_interface.go @@ -30,8 +30,9 @@ func GetInterface(dpdkClientFactory DPDKClientFactory, rendererFactory RendererF ) cmd := &cobra.Command{ - Use: "interface", + Use: "interface [...]", Short: "Get or list interface(s)", + Example: "dpservice-cli get interface vm1 vm2", Aliases: InterfaceAliases, RunE: func(cmd *cobra.Command, args []string) error { interfaceIDs := args diff --git a/cmd/get_loadbalancer.go b/cmd/get_loadbalancer.go index f46426e..0cf8f33 100644 --- a/cmd/get_loadbalancer.go +++ b/cmd/get_loadbalancer.go @@ -30,10 +30,11 @@ func GetLoadBalancer(dpdkClientFactory DPDKClientFactory, rendererFactory Render ) cmd := &cobra.Command{ - Use: "loadbalancer ", + Use: "loadbalancer [...]", Short: "Get or list loadbalancer(s)", Example: "dpservice-cli get lb 4", Aliases: LoadBalancerAliases, + Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { loadbalancerIDs := args return RunGetLoadBalancer( diff --git a/cmd/get_loadbalancer_target.go b/cmd/get_loadbalancer_target.go index 7af4be3..68bc05a 100644 --- a/cmd/get_loadbalancer_target.go +++ b/cmd/get_loadbalancer_target.go @@ -34,6 +34,7 @@ func GetLoadBalancerTargets(dpdkClientFactory DPDKClientFactory, rendererFactory Short: "Get or list LoadBalancerTarget(s)", Example: "dpservice-cli get lbtarget 1 2", Aliases: LoadBalancerTargetAliases, + Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { interfaceIDs := args return RunGetLoadBalancerTargets( diff --git a/cmd/get_nat.go b/cmd/get_nat.go index 849d179..5e69855 100644 --- a/cmd/get_nat.go +++ b/cmd/get_nat.go @@ -32,7 +32,9 @@ func GetNat(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory cmd := &cobra.Command{ Use: "nat [...]", Short: "Get or list nat(s)", + Example: "dpservice-cli get nat vm1", Aliases: NatAliases, + Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { interfaceIDs := args return RunGetNat( diff --git a/cmd/get_nat_info.go b/cmd/get_nat_info.go index 0359699..3289ad4 100644 --- a/cmd/get_nat_info.go +++ b/cmd/get_nat_info.go @@ -31,9 +31,10 @@ func GetNatInfo(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFac ) cmd := &cobra.Command{ - Use: "natinfo <--nat-type>", - Short: "List all local machines that are behind this IP", - Args: cobra.ExactArgs(1), + Use: "natinfo <--nat-type>", + Short: "List all local machines that are behind this IP", + Example: "dpservice-cli get natinfo 10.20.30.40 --nat-type=1", + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { natVIPIP, err := netip.ParseAddr(args[0]) if err != nil { diff --git a/cmd/get_virtualip.go b/cmd/get_virtualip.go index 45136f7..84e4160 100644 --- a/cmd/get_virtualip.go +++ b/cmd/get_virtualip.go @@ -30,9 +30,11 @@ func GetVirtualIP(dpdkClientFactory DPDKClientFactory, rendererFactory RendererF ) cmd := &cobra.Command{ - Use: "virtualip [...]", + Use: "virtualip [...]", Short: "Get or list virtualip(s)", + Example: "dpservice-cli get virtualip vm1", Aliases: VirtualIPAliases, + Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { interfaceIDs := args return RunGetVirtualIP( diff --git a/cmd/initialized.go b/cmd/initialized.go index 17d45dd..beab681 100644 --- a/cmd/initialized.go +++ b/cmd/initialized.go @@ -23,9 +23,10 @@ import ( func Initialized(factory DPDKClientFactory) *cobra.Command { cmd := &cobra.Command{ - Use: "initialized", - Short: "Indicates if the DPDK app has been initialized already", - Args: cobra.ExactArgs(0), + Use: "initialized", + Short: "Indicates if the DPDK app has been initialized already", + Example: "dpservice-cli initialized", + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { return RunInitialized( diff --git a/renderer/renderer.go b/renderer/renderer.go index 288570e..c2d0f81 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -262,11 +262,11 @@ func (t defaultTableConverter) routeTable(routes []api.Route) (*TableData, error } func (t defaultTableConverter) virtualIPTable(virtualIPs []api.VirtualIP) (*TableData, error) { - headers := []any{"IP"} + headers := []any{"interfaceID", "virtualIP"} columns := make([][]any, len(virtualIPs)) for i, virtualIP := range virtualIPs { - columns[i] = []any{virtualIP.IP} + columns[i] = []any{virtualIP.InterfaceID, virtualIP.IP} } return &TableData{ From 90f5e16b855808dcd0ec18972a0edeb01b04ac43 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Mon, 15 May 2023 15:08:48 +0200 Subject: [PATCH 26/65] fix error handling and various output messages --- .vscode/launch.json | 2 ++ cmd/create_loadbalancer_prefix.go | 2 +- cmd/delete_firewall_rule.go | 2 +- cmd/delete_interface.go | 2 +- cmd/delete_loadblanacer_prefix.go | 2 +- cmd/delete_loadblanacer_target.go | 2 +- cmd/delete_nat.go | 2 +- cmd/delete_prefix.go | 2 +- cmd/delete_virtualip.go | 4 ++-- cmd/get_loadbalancer_target.go | 2 +- dpdk/client/client.go | 23 +++++++++++++++++------ renderer/renderer.go | 1 - 12 files changed, 29 insertions(+), 17 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 6bfdc11..bc06686 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,12 +13,14 @@ //"program": "${fileDirname}", //"args": ["create", "loadbalancer", "7", "--lbports", "389", "--vip", "10.3.4.5", "--vni", "100"] //"args": ["get", "route", "0.0.0.0/24", "0", "fc00:2::64:0:1", "--vni", "100"] + //"args": ["delete", "route", "10.200.2.0/24", "0", "ff80::1", "--vni", "100"] //"args": ["create", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] //"args": ["create", "lbtarget", "ff80::1", "--lb-id", "1"] //"args": ["get", "natinfo", "10.20.30.40", "--nat-type=2"] //"args": ["get", "fwrule", "vm1", "--rule-id=4"] "args": ["create", "fwrule", "vm1", "--action=1", "--direction=1", "--dst=5.5.5.0/24", "--ipv=0", "--priority=100", "--rule-id=9", "--src=1.1.1.1/32", "--protocol=tcp"] + //"args": ["create", "nnat", "10.20.30.40", "--vni", "100", "--minport", "30000", "--maxport", "30100", "--underlayroute", "ff80::1"] } ] } \ No newline at end of file diff --git a/cmd/create_loadbalancer_prefix.go b/cmd/create_loadbalancer_prefix.go index 6c3a3fe..3ed4ee2 100644 --- a/cmd/create_loadbalancer_prefix.go +++ b/cmd/create_loadbalancer_prefix.go @@ -37,7 +37,7 @@ func CreateLoadBalancerPrefix( cmd := &cobra.Command{ Use: "lbprefix <--interface-id>", Short: "Create a loadbalancer prefix", - Example: "dpservice-cli create lbprefix ff80::1/64 --interface-id=vm1", + Example: "dpservice-cli create lbprefix 10.10.10.0/24 --interface-id=vm1", Args: cobra.ExactArgs(1), Aliases: PrefixAliases, RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/delete_firewall_rule.go b/cmd/delete_firewall_rule.go index 2d39b74..3609fc8 100644 --- a/cmd/delete_firewall_rule.go +++ b/cmd/delete_firewall_rule.go @@ -77,7 +77,7 @@ func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, ruleI for _, ruleID := range ruleIDs { if err := client.DeleteFirewallRule(ctx, opts.InrerfaceID, ruleID); err != nil { - fmt.Printf("Error deleting firewall rule %s: %v\n", ruleID, err) + return fmt.Errorf("error deleting firewall rule %s: %v", ruleID, err) } fmt.Println("Deleted firewall rule", ruleID, "on interface", opts.InrerfaceID) diff --git a/cmd/delete_interface.go b/cmd/delete_interface.go index 79e7293..efd042b 100644 --- a/cmd/delete_interface.go +++ b/cmd/delete_interface.go @@ -70,7 +70,7 @@ func RunDeleteInterface(ctx context.Context, factory DPDKClientFactory, interfac for _, interfaceID := range interfaceIDs { if err := client.DeleteInterface(ctx, interfaceID); err != nil { - fmt.Printf("Error deleting interface %s: %v\n", interfaceID, err) + return fmt.Errorf("error deleting interface %s: %v", interfaceID, err) } fmt.Println("Deleted interface", interfaceID) diff --git a/cmd/delete_loadblanacer_prefix.go b/cmd/delete_loadblanacer_prefix.go index 7043a68..c35fc8b 100644 --- a/cmd/delete_loadblanacer_prefix.go +++ b/cmd/delete_loadblanacer_prefix.go @@ -82,7 +82,7 @@ func RunDeleteLoadBalancerPrefix(ctx context.Context, factory DPDKClientFactory, for _, prefix := range prefixes { if err := client.DeleteLoadBalancerPrefix(ctx, opts.InterfaceID, prefix); err != nil { - fmt.Printf("Error deleting loadbalancer prefix %s/%v: %v\n", opts.InterfaceID, prefix, err) + return fmt.Errorf("error deleting loadbalancer prefix %s/%v: %v", opts.InterfaceID, prefix, err) } fmt.Printf("Deleted loadbalancer prefix %s/%v\n", opts.InterfaceID, prefix) } diff --git a/cmd/delete_loadblanacer_target.go b/cmd/delete_loadblanacer_target.go index a76be93..c77292c 100644 --- a/cmd/delete_loadblanacer_target.go +++ b/cmd/delete_loadblanacer_target.go @@ -83,7 +83,7 @@ func RunDeleteLoadBalancerTarget(ctx context.Context, factory DPDKClientFactory, return fmt.Errorf("not valid IP Address: %w", err) } if err := client.DeleteLoadBalancerTarget(ctx, opts.LoadBalancerID, targetIP); err != nil { - fmt.Printf("Error deleting loadbalancer target %s/%v: %v\n", opts.LoadBalancerID, target, err) + return fmt.Errorf("error deleting loadbalancer target %s/%v: %v", opts.LoadBalancerID, target, err) } fmt.Printf("Deleted loadbalancer target %s/%v\n", opts.LoadBalancerID, target) } diff --git a/cmd/delete_nat.go b/cmd/delete_nat.go index 2bab8c7..a63bdca 100644 --- a/cmd/delete_nat.go +++ b/cmd/delete_nat.go @@ -70,7 +70,7 @@ func RunDeleteNat(ctx context.Context, factory DPDKClientFactory, interfaceIDs [ for _, interfaceID := range interfaceIDs { if err := client.DeleteNat(ctx, interfaceID); err != nil { - fmt.Printf("Error deleting nat of interface %s: %v\n", interfaceID, err) + return fmt.Errorf("error deleting nat of interface %s: %v", interfaceID, err) } fmt.Printf("Deleted nat of interface %s\n", interfaceID) diff --git a/cmd/delete_prefix.go b/cmd/delete_prefix.go index cf1add6..9dd6b41 100644 --- a/cmd/delete_prefix.go +++ b/cmd/delete_prefix.go @@ -82,7 +82,7 @@ func RunDeletePrefix(ctx context.Context, factory DPDKClientFactory, prefixes [] for _, prefix := range prefixes { if err := client.DeletePrefix(ctx, opts.InterfaceID, prefix); err != nil { - fmt.Printf("Error deleting prefix %s/%v: %v\n", opts.InterfaceID, prefix, err) + return fmt.Errorf("error deleting prefix %s/%v: %v", opts.InterfaceID, prefix, err) } fmt.Printf("Deleted prefix %s/%v\n", opts.InterfaceID, prefix) } diff --git a/cmd/delete_virtualip.go b/cmd/delete_virtualip.go index 66c05df..fc886f3 100644 --- a/cmd/delete_virtualip.go +++ b/cmd/delete_virtualip.go @@ -29,7 +29,7 @@ func DeleteVirtualIP(factory DPDKClientFactory) *cobra.Command { ) cmd := &cobra.Command{ - Use: "virtualip [...]", + Use: "virtualip [...]", Short: "Delete virtual ip(s)", Example: "dpservice-cli delete virtualip vm1", Aliases: VirtualIPAliases, @@ -70,7 +70,7 @@ func RunDeleteVirtualIP(ctx context.Context, factory DPDKClientFactory, interfac for _, interfaceID := range interfaceIDs { if err := client.DeleteVirtualIP(ctx, interfaceID); err != nil { - fmt.Printf("Error deleting virtual ip of interface %s: %v\n", interfaceID, err) + return fmt.Errorf("error deleting virtual ip of interface %s: %v", interfaceID, err) } fmt.Printf("Deleted virtual ip of interface %s\n", interfaceID) diff --git a/cmd/get_loadbalancer_target.go b/cmd/get_loadbalancer_target.go index 68bc05a..1bd4693 100644 --- a/cmd/get_loadbalancer_target.go +++ b/cmd/get_loadbalancer_target.go @@ -93,7 +93,7 @@ func RunGetLoadBalancerTargets( for _, interfaceID := range interfaceIDs { lbtarget, err := client.GetLoadBalancerTargets(ctx, interfaceID) if err != nil { - fmt.Printf("Error getting loadbalancer target for interface %s: %v\n", interfaceID, err) + return fmt.Errorf("error getting loadbalancer target for interface %s: %v", interfaceID, err) } if err := renderer.Render(lbtarget); err != nil { diff --git a/dpdk/client/client.go b/dpdk/client/client.go index f575cfe..0b10641 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -480,8 +480,9 @@ func (c *client) CreateRoute(ctx context.Context, route *api.Route) (*api.Route, return nil, apierrors.NewStatusError(errorCode, res.GetMessage()) } return &api.Route{ - TypeMeta: api.TypeMeta{Kind: api.RouteKind}, - Spec: route.Spec, + TypeMeta: api.TypeMeta{Kind: api.RouteKind}, + RouteMeta: route.RouteMeta, + Spec: route.Spec, }, nil } @@ -493,7 +494,7 @@ func (c *client) DeleteRoute(ctx context.Context, vni uint32, prefix netip.Prefi Weight: 100, Prefix: &dpdkproto.Prefix{ IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Addr()), - Address: []byte(prefix.String()), + Address: []byte(prefix.Addr().String()), PrefixLength: uint32(prefix.Bits()), }, NexthopVNI: nextHopVNI, @@ -608,7 +609,7 @@ func (c *client) CreateNeighborNat(ctx context.Context, nNat *api.NeighborNat) e return err } - if res.Error != 0 { + if res.Error == 0 { return nil } return fmt.Errorf("%d", res.Error) @@ -636,6 +637,11 @@ func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType in return nil, fmt.Errorf("error parsing underlay route: %w", err) } nat.Spec.UnderlayRoute = underlayRoute + vipIP, err = netip.ParseAddr(string(res.NatVIPIP.Address)) + if err != nil { + return nil, fmt.Errorf("error parsing vip ip: %w", err) + } + nat.Spec.NatVIPIP = vipIP } else if res.NatInfoType == 1 { vipIP, err = netip.ParseAddr(string(natInfoEntry.GetAddress())) if err != nil { @@ -643,7 +649,6 @@ func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType in } nat.Spec.NatVIPIP = vipIP } - nat.InterfaceID = res.NatInfoType.String() + " " + res.NatVIPIP.String() nat.Kind = api.NatKind nat.Spec.MinPort = natInfoEntry.MinPort nat.Spec.MaxPort = natInfoEntry.MaxPort @@ -727,7 +732,13 @@ func (c *client) CreateFirewallRule(ctx context.Context, fwRule *api.FirewallRul if res.Status.Error != 0 { return nil, err } - return &api.FirewallRule{FirewallRuleMeta: api.FirewallRuleMeta{RuleID: string(res.RuleID), InterfaceID: fwRule.InterfaceID}}, nil + return &api.FirewallRule{ + TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, + FirewallRuleMeta: api.FirewallRuleMeta{ + RuleID: fwRule.RuleID, + InterfaceID: fwRule.InterfaceID, + }, + }, nil } func (c *client) GetFirewallRule(ctx context.Context, ruleID string, interfaceID string) (*api.FirewallRule, error) { diff --git a/renderer/renderer.go b/renderer/renderer.go index c2d0f81..5a10f64 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -290,7 +290,6 @@ func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { } func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableData, error) { - // TODO add all fields headers := []any{"interfaceID", "ruleID", "direction", "src", "dst", "action", "protocol", "priority"} columns := make([][]any, len(fwrules)) From 7bc7eed239d11d5712005d94333665192c4b9854 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Mon, 15 May 2023 15:39:44 +0200 Subject: [PATCH 27/65] change func Init to be used without parameters --- cmd/init.go | 35 +++++------------------------------ 1 file changed, 5 insertions(+), 30 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 6038ca2..fb2e169 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -17,36 +17,29 @@ package cmd import ( "context" "fmt" - "net/netip" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/util" dpdkproto "github.com/onmetal/net-dpservice-go/proto" "github.com/spf13/cobra" "github.com/spf13/pflag" ) +// func Init is not up to dpdk.proto spec, but is implemented to comply with current dpservice implementation func Init(factory DPDKClientFactory) *cobra.Command { var ( opts InitOptions ) cmd := &cobra.Command{ - Use: "init [flags]", + Use: "init", Short: "Initial set up of the DPDK app", - Long: "To add multiple values use flag multiple times", - Example: "dpservice-cli init ff80::1/64 --pfnames=a --pfnames=b --uplink-ports=c --uplink-ports=d", - Args: cobra.ExactArgs(1), + Example: "dpservice-cli init", + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - prefix, err := netip.ParsePrefix(args[0]) - if err != nil { - return fmt.Errorf("error parsing prefix: %w", err) - } return RunInit( cmd.Context(), factory, - prefix, opts, ) }, @@ -59,28 +52,18 @@ func Init(factory DPDKClientFactory) *cobra.Command { } type InitOptions struct { - UplinkPorts []string - PfNames []string } func (o *InitOptions) AddFlags(fs *pflag.FlagSet) { - fs.StringSliceVar(&o.UplinkPorts, "uplink-ports", o.UplinkPorts, "Linux name of the NICs that are connected to the Leaf Switches.") - fs.StringSliceVar(&o.PfNames, "pfnames", o.PfNames, "Linux name of the Physical Functions, that Virtual Functions will be derived from.") } func (o *InitOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"uplink-ports", "pfnames"} { - if err := cmd.MarkFlagRequired(name); err != nil { - return err - } - } return nil } func RunInit( ctx context.Context, dpdkClientFactory DPDKClientFactory, - prefix netip.Prefix, opts InitOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) @@ -101,15 +84,7 @@ func RunInit( return fmt.Errorf("error dp-service already initialized, uuid: %s", uuid) } - err = client.Init(ctx, dpdkproto.InitConfig{ - UnderlayIPv6Prefix: &dpdkproto.Prefix{ - IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Addr()), - Address: []byte(prefix.Addr().String()), - PrefixLength: uint32(prefix.Bits()), - }, - UplinkPorts: opts.UplinkPorts, - PfNames: opts.PfNames, - }) + err = client.Init(ctx, dpdkproto.InitConfig{}) if err != nil { return fmt.Errorf("error: %w", err) } From e92df068ee3c45a196af8092668e8b9bd16c0cd5 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Mon, 15 May 2023 16:14:36 +0200 Subject: [PATCH 28/65] change initialization check in func Init --- cmd/init.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index fb2e169..38e0014 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -77,10 +77,7 @@ func RunInit( }() uuid, err := client.Initialized(ctx) - if err != nil { - return fmt.Errorf("error: %w", err) - } - if uuid != "" { + if err == nil && uuid != "" { return fmt.Errorf("error dp-service already initialized, uuid: %s", uuid) } From fe3e1a373ecdb2d7e5bdd0a299fda0f73242c100 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 16 May 2023 13:59:22 +0200 Subject: [PATCH 29/65] change args to flags of create commands --- cmd/{create.go => add.go} | 22 +++--- ..._firewall_rule.go => add_firewall_rule.go} | 37 +++++----- ...r_target.go => add_loadbalancer_target.go} | 46 ++++++------ cmd/{create_nat.go => add_nat.go} | 43 ++++++------ ...te_neighbor_nat.go => add_neighbor_nat.go} | 40 +++++------ cmd/{create_prefix.go => add_prefix.go} | 45 ++++++------ cmd/{create_route.go => add_route.go} | 70 ++++++++----------- cmd/{create_virtualip.go => add_virtualip.go} | 45 ++++++------ cmd/command.go | 2 +- cmd/create_interface.go | 22 +++--- cmd/create_loadbalancer.go | 18 ++--- cmd/create_loadbalancer_prefix.go | 19 +++-- dpdk/api/conversion.go | 4 +- dpdk/api/types.go | 2 +- dpdk/client/client.go | 35 +++++----- dpdk/client/dynamic/dynamic.go | 6 +- renderer/renderer.go | 4 +- 17 files changed, 217 insertions(+), 243 deletions(-) rename cmd/{create.go => add.go} (82%) rename cmd/{create_firewall_rule.go => add_firewall_rule.go} (80%) rename cmd/{create_loadbalancer_target.go => add_loadbalancer_target.go} (64%) rename cmd/{create_nat.go => add_nat.go} (64%) rename cmd/{create_neighbor_nat.go => add_neighbor_nat.go} (64%) rename cmd/{create_prefix.go => add_prefix.go} (68%) rename cmd/{create_route.go => add_route.go} (58%) rename cmd/{create_virtualip.go => add_virtualip.go} (65%) diff --git a/cmd/create.go b/cmd/add.go similarity index 82% rename from cmd/create.go rename to cmd/add.go index 6cd30c4..9814e89 100644 --- a/cmd/create.go +++ b/cmd/add.go @@ -25,16 +25,16 @@ import ( "github.com/spf13/cobra" ) -func Create(dpdkClientFactory DPDKClientFactory) *cobra.Command { +func Add(dpdkClientFactory DPDKClientFactory) *cobra.Command { rendererOptions := &RendererOptions{Output: "name"} sourcesOptions := &SourcesOptions{} cmd := &cobra.Command{ - Use: "create", + Use: "add", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() - return RunCreate(ctx, dpdkClientFactory, rendererOptions, sourcesOptions) + return RunAdd(ctx, dpdkClientFactory, rendererOptions, sourcesOptions) }, } @@ -44,15 +44,15 @@ func Create(dpdkClientFactory DPDKClientFactory) *cobra.Command { subcommands := []*cobra.Command{ CreateInterface(dpdkClientFactory, rendererOptions), - CreatePrefix(dpdkClientFactory, rendererOptions), - CreateRoute(dpdkClientFactory, rendererOptions), - CreateVirtualIP(dpdkClientFactory, rendererOptions), + AddPrefix(dpdkClientFactory, rendererOptions), + AddRoute(dpdkClientFactory, rendererOptions), + AddVirtualIP(dpdkClientFactory, rendererOptions), CreateLoadBalancer(dpdkClientFactory, rendererOptions), CreateLoadBalancerPrefix(dpdkClientFactory, rendererOptions), - CreateLoadBalancerTarget(dpdkClientFactory, rendererOptions), - CreateNat(dpdkClientFactory, rendererOptions), - CreateNeighborNat(dpdkClientFactory, rendererOptions), - CreateFirewallRule(dpdkClientFactory, rendererOptions), + AddLoadBalancerTarget(dpdkClientFactory, rendererOptions), + AddNat(dpdkClientFactory, rendererOptions), + AddNeighborNat(dpdkClientFactory, rendererOptions), + AddFirewallRule(dpdkClientFactory, rendererOptions), } cmd.Short = fmt.Sprintf("Creates one of %v", CommandNames(subcommands)) @@ -65,7 +65,7 @@ func Create(dpdkClientFactory DPDKClientFactory) *cobra.Command { return cmd } -func RunCreate( +func RunAdd( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, diff --git a/cmd/create_firewall_rule.go b/cmd/add_firewall_rule.go similarity index 80% rename from cmd/create_firewall_rule.go rename to cmd/add_firewall_rule.go index 0d246e4..2abae63 100644 --- a/cmd/create_firewall_rule.go +++ b/cmd/add_firewall_rule.go @@ -28,24 +28,23 @@ import ( "github.com/spf13/pflag" ) -func CreateFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func AddFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( - opts CreateFirewallRuleOptions + opts AddFirewallRuleOptions ) cmd := &cobra.Command{ - Use: "firewallrule [flags]", - Short: "Create a FirewallRule", - Example: "dpservice-cli create fwrule vm1 --action 1 --direction 1 --dst 5.5.5.0/24 --ipv 0 --priority 100 --rule-id 12 --src 1.1.1.1/32 --protocol tcp --srcPortLower 1 --srcPortUpper 1000 --dstPortLower 500 --dstPortUpper 600", + Use: "firewallrule <--interface-id> [flags]", + Short: "Add a FirewallRule to interface", + Example: "dpservice-cli add fwrule --interface-id=vm1 --action=1 --direction=1 --dst=5.5.5.0/24 --ipv=0 --priority=100 --rule-id=12 --src=1.1.1.1/32 --protocol=tcp --srcPortLower=1 --srcPortUpper=1000 --dstPortLower=500 --dstPortUpper=600", Aliases: FirewallRuleAliases, - Args: cobra.ExactArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - interfaceID := args[0] - return RunCreateFirewallRule( + + return RunAddFirewallRule( cmd.Context(), dpdkClientFactory, rendererFactory, - interfaceID, opts, ) }, @@ -58,7 +57,8 @@ func CreateFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory Ren return cmd } -type CreateFirewallRuleOptions struct { +type AddFirewallRuleOptions struct { + InterfaceID string RuleID string TrafficDirection uint8 FirewallAction uint8 @@ -75,7 +75,8 @@ type CreateFirewallRuleOptions struct { IcmpCode int32 } -func (o *CreateFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { +func (o *AddFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "InterfaceID of FW Rule.") fs.StringVar(&o.RuleID, "rule-id", o.RuleID, "RuleID of FW Rule.") fs.Uint8Var(&o.TrafficDirection, "direction", o.TrafficDirection, "Traffic direction of FW Rule: Ingress = 0/Egress = 1") fs.Uint8Var(&o.FirewallAction, "action", o.FirewallAction, "Firewall action: Drop = 0/Accept = 1 // Can be only \"accept\" at the moment.") @@ -93,9 +94,9 @@ func (o *CreateFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { } -func (o *CreateFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error { +func (o *AddFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error { // TODO if protocol is not specified it should match all protocols - for _, name := range []string{"rule-id", "direction", "action", "ipv", "src", "dst", "protocol"} { + for _, name := range []string{"interface-id", "rule-id", "direction", "action", "ipv", "src", "dst", "protocol"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -103,7 +104,7 @@ func (o *CreateFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error return nil } -func RunCreateFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, interfaceID string, opts CreateFirewallRuleOptions) error { +func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts AddFirewallRuleOptions) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -114,7 +115,7 @@ func RunCreateFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFact } }() - renderer, err := rendererFactory.NewRenderer("created", os.Stdout) + renderer, err := rendererFactory.NewRenderer("added", os.Stdout) if err != nil { return fmt.Errorf("error creating renderer: %w", err) } @@ -167,11 +168,11 @@ func RunCreateFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFact }} } - fwrule, err := client.CreateFirewallRule(ctx, &api.FirewallRule{ + fwrule, err := client.AddFirewallRule(ctx, &api.FirewallRule{ TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, FirewallRuleMeta: api.FirewallRuleMeta{ RuleID: opts.RuleID, - InterfaceID: interfaceID, + InterfaceID: opts.InterfaceID, }, Spec: api.FirewallRuleSpec{ TrafficDirection: opts.TrafficDirection, @@ -185,7 +186,7 @@ func RunCreateFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFact }, ) if err != nil { - return fmt.Errorf("error creating firewall rule: %w", err) + return fmt.Errorf("error adding firewall rule: %w", err) } if err := renderer.Render(fwrule); err != nil { diff --git a/cmd/create_loadbalancer_target.go b/cmd/add_loadbalancer_target.go similarity index 64% rename from cmd/create_loadbalancer_target.go rename to cmd/add_loadbalancer_target.go index 52f04cb..d9ab97f 100644 --- a/cmd/create_loadbalancer_target.go +++ b/cmd/add_loadbalancer_target.go @@ -21,36 +21,32 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func CreateLoadBalancerTarget( +func AddLoadBalancerTarget( dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, ) *cobra.Command { var ( - opts CreateLoadBalancerTargetOptions + opts AddLoadBalancerTargetOptions ) cmd := &cobra.Command{ - Use: "lbtarget [flags]", - Short: "Create a loadbalancer target", - Example: "dpservice-cli create lbtarget ff80::5 --lb-id 2", - Args: cobra.ExactArgs(1), + Use: "lbtarget <--lb-id>", + Short: "Add a loadbalancer target", + Example: "dpservice-cli add lbtarget --target-ip=ff80::5 --lb-id=2", + Args: cobra.ExactArgs(0), Aliases: LoadBalancerTargetAliases, RunE: func(cmd *cobra.Command, args []string) error { - ip, err := netip.ParseAddr(args[0]) - if err != nil { - return fmt.Errorf("error parsing ip: %w", err) - } - return RunCreateLoadBalancerTarget( + return RunAddLoadBalancerTarget( cmd.Context(), dpdkClientFactory, rendererFactory, - ip, opts, ) }, @@ -63,16 +59,18 @@ func CreateLoadBalancerTarget( return cmd } -type CreateLoadBalancerTargetOptions struct { +type AddLoadBalancerTargetOptions struct { + TargetIP netip.Addr LoadBalancerID string } -func (o *CreateLoadBalancerTargetOptions) AddFlags(fs *pflag.FlagSet) { - fs.StringVar(&o.LoadBalancerID, "lb-id", o.LoadBalancerID, "ID of the loadbalancer to create the target for.") +func (o *AddLoadBalancerTargetOptions) AddFlags(fs *pflag.FlagSet) { + flag.AddrVar(fs, &o.TargetIP, "target-ip", o.TargetIP, "Loadbalancer Target IP.") + fs.StringVar(&o.LoadBalancerID, "lb-id", o.LoadBalancerID, "ID of the loadbalancer to add the target for.") } -func (o *CreateLoadBalancerTargetOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"lb-id"} { +func (o *AddLoadBalancerTargetOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"target-ip", "lb-id"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -80,12 +78,11 @@ func (o *CreateLoadBalancerTargetOptions) MarkRequiredFlags(cmd *cobra.Command) return nil } -func RunCreateLoadBalancerTarget( +func RunAddLoadBalancerTarget( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - ip netip.Addr, - opts CreateLoadBalancerTargetOptions, + opts AddLoadBalancerTargetOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { @@ -97,19 +94,18 @@ func RunCreateLoadBalancerTarget( } }() - renderer, err := rendererFactory.NewRenderer("created", os.Stdout) + renderer, err := rendererFactory.NewRenderer("added", os.Stdout) if err != nil { return fmt.Errorf("error creating renderer: %w", err) } - targetIP := api.ProtoLbipToLbip(*api.LbipToProtoLbip(ip)) - res, err := client.CreateLoadBalancerTarget(ctx, &api.LoadBalancerTarget{ + res, err := client.AddLoadBalancerTarget(ctx, &api.LoadBalancerTarget{ TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, LoadBalancerTargetMeta: api.LoadBalancerTargetMeta{ID: opts.LoadBalancerID}, - Spec: api.LoadBalancerTargetSpec{TargetIP: *targetIP}, + Spec: api.LoadBalancerTargetSpec{TargetIP: opts.TargetIP}, }) if err != nil { - return fmt.Errorf("error creating loadbalancer target: %w", err) + return fmt.Errorf("error adding loadbalancer target: %w", err) } if err := renderer.Render(res); err != nil { diff --git a/cmd/create_nat.go b/cmd/add_nat.go similarity index 64% rename from cmd/create_nat.go rename to cmd/add_nat.go index fe03238..8907187 100644 --- a/cmd/create_nat.go +++ b/cmd/add_nat.go @@ -27,24 +27,23 @@ import ( "github.com/spf13/pflag" ) -func CreateNat(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func AddNat(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( - opts CreateNatOptions + opts AddNatOptions ) cmd := &cobra.Command{ - Use: "nat [flags]", - Short: "Create a NAT", - Example: "dpservice-cli create nat vm1 --natip=10.20.30.40 --minport=30000 --maxport=30100", + Use: "nat <--natip> <--minport> <--maxport>", + Short: "Add a NAT to interface", + Example: "dpservice-cli add nat --interface-id=vm1 --natip=10.20.30.40 --minport=30000 --maxport=30100", Aliases: NatAliases, - Args: cobra.ExactArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - interfaceID := args[0] - return RunCreateNat( + + return RunAddNat( cmd.Context(), dpdkClientFactory, rendererFactory, - interfaceID, opts, ) }, @@ -57,20 +56,22 @@ func CreateNat(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFact return cmd } -type CreateNatOptions struct { - NATVipIP netip.Addr - MinPort uint32 - MaxPort uint32 +type AddNatOptions struct { + InterfaceID string + NATVipIP netip.Addr + MinPort uint32 + MaxPort uint32 } -func (o *CreateNatOptions) AddFlags(fs *pflag.FlagSet) { +func (o *AddNatOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID where to add NAT.") fs.Uint32Var(&o.MinPort, "minport", o.MinPort, "MinPort of NAT.") fs.Uint32Var(&o.MaxPort, "maxport", o.MaxPort, "MaxPort of NAT.") flag.AddrVar(fs, &o.NATVipIP, "natip", o.NATVipIP, "NAT IP to assign to the interface.") } -func (o *CreateNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"minport", "maxport", "natip"} { +func (o *AddNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"interface-id", "minport", "maxport", "natip"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -78,7 +79,7 @@ func (o *CreateNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunCreateNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, interfaceID string, opts CreateNatOptions) error { +func RunAddNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts AddNatOptions) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -89,14 +90,14 @@ func RunCreateNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rend } }() - renderer, err := rendererFactory.NewRenderer("created", os.Stdout) + renderer, err := rendererFactory.NewRenderer("added", os.Stdout) if err != nil { return fmt.Errorf("error creating renderer: %w", err) } - nat, err := client.CreateNat(ctx, &api.Nat{ + nat, err := client.AddNat(ctx, &api.Nat{ NatMeta: api.NatMeta{ - InterfaceID: interfaceID, + InterfaceID: opts.InterfaceID, }, Spec: api.NatSpec{ NatVIPIP: opts.NATVipIP, @@ -105,7 +106,7 @@ func RunCreateNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rend }, }) if err != nil { - return fmt.Errorf("error creating nat: %w", err) + return fmt.Errorf("error adding nat: %w", err) } if err := renderer.Render(nat); err != nil { diff --git a/cmd/create_neighbor_nat.go b/cmd/add_neighbor_nat.go similarity index 64% rename from cmd/create_neighbor_nat.go rename to cmd/add_neighbor_nat.go index bf65c23..b9c948f 100644 --- a/cmd/create_neighbor_nat.go +++ b/cmd/add_neighbor_nat.go @@ -26,28 +26,23 @@ import ( "github.com/spf13/pflag" ) -func CreateNeighborNat(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func AddNeighborNat(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( - opts CreateNeighborNatOptions + opts AddNeighborNatOptions ) cmd := &cobra.Command{ - Use: "neighbornat [flags]", - Short: "Create a Neighbor NAT", - Example: "dpservice-cli create neighbornat 10.20.30.40 --vni=100 --minport=30000 --maxport=30100 --underlayroute=ff80::1", + Use: "neighbornat <--natip> <--vni> <--minport> <--maxport> <--underlayroute>", + Short: "Add a Neighbor NAT", + Example: "dpservice-cli add neighbornat --natip=10.20.30.40 --vni=100 --minport=30000 --maxport=30100 --underlayroute=ff80::1", Aliases: NeighborNatAliases, - Args: cobra.ExactArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - natVIPIP, err := netip.ParseAddr(args[0]) - if err != nil { - return fmt.Errorf("error parsing nat vip ip: %w", err) - } - return RunCreateNeighborNat( + return RunAddNeighborNat( cmd.Context(), dpdkClientFactory, rendererFactory, - natVIPIP, opts, ) }, @@ -60,22 +55,24 @@ func CreateNeighborNat(dpdkClientFactory DPDKClientFactory, rendererFactory Rend return cmd } -type CreateNeighborNatOptions struct { +type AddNeighborNatOptions struct { + NatIP netip.Addr Vni uint32 MinPort uint32 MaxPort uint32 UnderlayRoute netip.Addr } -func (o *CreateNeighborNatOptions) AddFlags(fs *pflag.FlagSet) { +func (o *AddNeighborNatOptions) AddFlags(fs *pflag.FlagSet) { + flag.AddrVar(fs, &o.NatIP, "natip", o.NatIP, "Neighbor NAT IP.") fs.Uint32Var(&o.Vni, "vni", o.Vni, "VNI of neighbor NAT.") fs.Uint32Var(&o.MinPort, "minport", o.MinPort, "MinPort of neighbor NAT.") fs.Uint32Var(&o.MaxPort, "maxport", o.MaxPort, "MaxPort of neighbor NAT.") flag.AddrVar(fs, &o.UnderlayRoute, "underlayroute", o.UnderlayRoute, "Underlay route of neighbor NAT.") } -func (o *CreateNeighborNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"vni", "minport", "maxport", "underlayroute"} { +func (o *AddNeighborNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"natip", "vni", "minport", "maxport", "underlayroute"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -83,7 +80,7 @@ func (o *CreateNeighborNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunCreateNeighborNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, natVIPIP netip.Addr, opts CreateNeighborNatOptions) error { +func RunAddNeighborNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts AddNeighborNatOptions) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -94,9 +91,9 @@ func RunCreateNeighborNat(ctx context.Context, dpdkClientFactory DPDKClientFacto } }() - err = client.CreateNeighborNat(ctx, &api.NeighborNat{ + err = client.AddNeighborNat(ctx, &api.NeighborNat{ NeighborNatMeta: api.NeighborNatMeta{ - NatVIPIP: natVIPIP, + NatVIPIP: opts.NatIP, }, Spec: api.NeighborNatSpec{ Vni: opts.Vni, @@ -107,9 +104,10 @@ func RunCreateNeighborNat(ctx context.Context, dpdkClientFactory DPDKClientFacto }) if err != nil { - return fmt.Errorf("error creating neighbor nat: %w", err) + return fmt.Errorf("error adding neighbor nat: %w", err) } - fmt.Printf("Neighbor NAT with IP: %s created\n", natVIPIP.String()) + + fmt.Printf("Neighbor NAT with IP: %s added\n", opts.NatIP.String()) return nil } diff --git a/cmd/create_prefix.go b/cmd/add_prefix.go similarity index 68% rename from cmd/create_prefix.go rename to cmd/add_prefix.go index 8f7df44..4002313 100644 --- a/cmd/create_prefix.go +++ b/cmd/add_prefix.go @@ -21,36 +21,32 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func CreatePrefix( +func AddPrefix( dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, ) *cobra.Command { var ( - opts CreatePrefixOptions + opts AddPrefixOptions ) cmd := &cobra.Command{ - Use: "prefix ", - Short: "Create a prefix", - Example: "dpservice-cli create prefix 10.20.30.0/24 --interface-id=vm1", - Args: cobra.ExactArgs(1), + Use: "prefix <--prefix> <--interface-id>", + Short: "Add a prefix to interface.", + Example: "dpservice-cli add prefix --prefix=10.20.30.0/24 --interface-id=vm1", + Args: cobra.ExactArgs(0), Aliases: PrefixAliases, RunE: func(cmd *cobra.Command, args []string) error { - prefix, err := netip.ParsePrefix(args[0]) - if err != nil { - return fmt.Errorf("error parsing prefix: %w", err) - } - return RunCreatePrefix( + return RunAddPrefix( cmd.Context(), dpdkClientFactory, rendererFactory, - prefix, opts, ) }, @@ -63,16 +59,18 @@ func CreatePrefix( return cmd } -type CreatePrefixOptions struct { +type AddPrefixOptions struct { + Prefix netip.Prefix InterfaceID string } -func (o *CreatePrefixOptions) AddFlags(fs *pflag.FlagSet) { - fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "ID of the interface to create the prefix for.") +func (o *AddPrefixOptions) AddFlags(fs *pflag.FlagSet) { + flag.PrefixVar(fs, &o.Prefix, "prefix", o.Prefix, "Prefix to add to the interface.") + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "ID of the interface to add the prefix for.") } -func (o *CreatePrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"interface-id"} { +func (o *AddPrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"prefix", "interface-id"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -80,12 +78,11 @@ func (o *CreatePrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunCreatePrefix( +func RunAddPrefix( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - prefix netip.Prefix, - opts CreatePrefixOptions, + opts AddPrefixOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { @@ -97,20 +94,20 @@ func RunCreatePrefix( } }() - renderer, err := rendererFactory.NewRenderer("created", os.Stdout) + renderer, err := rendererFactory.NewRenderer("added", os.Stdout) if err != nil { return fmt.Errorf("error creating renderer: %w", err) } - res, err := client.CreatePrefix(ctx, &api.Prefix{ + res, err := client.AddPrefix(ctx, &api.Prefix{ PrefixMeta: api.PrefixMeta{ InterfaceID: opts.InterfaceID, - Prefix: prefix, + Prefix: opts.Prefix, }, Spec: api.PrefixSpec{}, }) if err != nil { - return fmt.Errorf("error creating prefix: %w", err) + return fmt.Errorf("error adding prefix: %w", err) } if err := renderer.Render(res); err != nil { diff --git a/cmd/create_route.go b/cmd/add_route.go similarity index 58% rename from cmd/create_route.go rename to cmd/add_route.go index 24b6dcb..0f3f688 100644 --- a/cmd/create_route.go +++ b/cmd/add_route.go @@ -19,48 +19,31 @@ import ( "fmt" "net/netip" "os" - "strconv" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func CreateRoute(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func AddRoute(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( - opts CreateRouteOptions + opts AddRouteOptions ) cmd := &cobra.Command{ - Use: "route ", - Short: "Create a route", - Example: "dpservice-cli create route 10.100.3.0/24 0 fc00:2::64:0:1 --vni=100", + Use: "route <--prefix> <--next-hop-vni> <--next-hop-ip> <--vni>", + Short: "Add a route", + Example: "dpservice-cli add route --prefix=10.100.3.0/24 --next-hop-vni=0 --next-hop-ip=fc00:2::64:0:1 --vni=100", Aliases: RouteAliases, - Args: cobra.ExactArgs(3), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - prefix, err := netip.ParsePrefix(args[0]) - if err != nil { - return fmt.Errorf("error parsing prefix: %w", err) - } - - nextHopVNI, err := strconv.ParseUint(args[1], 10, 32) - if err != nil { - return fmt.Errorf("error parsing next hop vni: %w", err) - } - - nextHopIP, err := netip.ParseAddr(args[2]) - if err != nil { - return fmt.Errorf("error parsing next hop ip: %w", err) - } - - return RunCreateRoute( + + return RunAddRoute( cmd.Context(), dpdkClientFactory, rendererFactory, - prefix, - uint32(nextHopVNI), - nextHopIP, opts, ) }, @@ -73,16 +56,22 @@ func CreateRoute(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFa return cmd } -type CreateRouteOptions struct { - VNI uint32 +type AddRouteOptions struct { + Prefix netip.Prefix + NextHopVNI uint32 + NextHopIP netip.Addr + VNI uint32 } -func (o *CreateRouteOptions) AddFlags(fs *pflag.FlagSet) { +func (o *AddRouteOptions) AddFlags(fs *pflag.FlagSet) { + flag.PrefixVar(fs, &o.Prefix, "prefix", o.Prefix, "Prefix for the route.") + fs.Uint32Var(&o.NextHopVNI, "next-hop-vni", o.NextHopVNI, "Next hop VNI for the route.") + flag.AddrVar(fs, &o.NextHopIP, "next-hop-ip", o.NextHopIP, "Next hop IP for the route.") fs.Uint32Var(&o.VNI, "vni", o.VNI, "Source VNI for the route.") } -func (o *CreateRouteOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"vni"} { +func (o *AddRouteOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"prefix", "next-hop-vni", "next-hop-ip", "vni"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -90,14 +79,11 @@ func (o *CreateRouteOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunCreateRoute( +func RunAddRoute( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - prefix netip.Prefix, - nextHopVNI uint32, - nextHopIP netip.Addr, - opts CreateRouteOptions, + opts AddRouteOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { @@ -109,24 +95,24 @@ func RunCreateRoute( } }() - renderer, err := rendererFactory.NewRenderer("created", os.Stdout) + renderer, err := rendererFactory.NewRenderer("added", os.Stdout) if err != nil { return fmt.Errorf("error creating renderer: %w", err) } - route, err := client.CreateRoute(ctx, &api.Route{ + route, err := client.AddRoute(ctx, &api.Route{ RouteMeta: api.RouteMeta{ VNI: opts.VNI, - Prefix: prefix, + Prefix: opts.Prefix, NextHop: api.RouteNextHop{ - VNI: nextHopVNI, - IP: nextHopIP, + VNI: opts.NextHopVNI, + IP: opts.NextHopIP, }, }, Spec: api.RouteSpec{}, }) if err != nil { - return fmt.Errorf("error creating route: %w", err) + return fmt.Errorf("error adding route: %w", err) } if err := renderer.Render(route); err != nil { diff --git a/cmd/create_virtualip.go b/cmd/add_virtualip.go similarity index 65% rename from cmd/create_virtualip.go rename to cmd/add_virtualip.go index fc9d3e0..8cca039 100644 --- a/cmd/create_virtualip.go +++ b/cmd/add_virtualip.go @@ -21,33 +21,29 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func CreateVirtualIP(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func AddVirtualIP(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( - opts CreateVirtualIPOptions + opts AddVirtualIPOptions ) cmd := &cobra.Command{ - Use: "virtualip ", - Short: "Create a virtual ip", - Example: "dpservice-cli create virtualip 20.20.20.20 --interface-id=vm1", + Use: "virtualip <--vip> <--interface-id>", + Short: "Add a virtual IP to interface.", + Example: "dpservice-cli add virtualip --vip=20.20.20.20 --interface-id=vm1", Aliases: VirtualIPAliases, - Args: cobra.ExactArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - ip, err := netip.ParseAddr(args[0]) - if err != nil { - return fmt.Errorf("error parsing ip: %w", err) - } - return RunCreateVirtualIP( + return RunAddVirtualIP( cmd.Context(), dpdkClientFactory, rendererFactory, - ip, opts, ) }, @@ -60,16 +56,18 @@ func CreateVirtualIP(dpdkClientFactory DPDKClientFactory, rendererFactory Render return cmd } -type CreateVirtualIPOptions struct { +type AddVirtualIPOptions struct { + Vip netip.Addr InterfaceID string } -func (o *CreateVirtualIPOptions) AddFlags(fs *pflag.FlagSet) { - fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID to create the virtual ip for.") +func (o *AddVirtualIPOptions) AddFlags(fs *pflag.FlagSet) { + flag.AddrVar(fs, &o.Vip, "vip", o.Vip, "Virtual IP to add on interface.") + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID to add the virtual ip for.") } -func (o *CreateVirtualIPOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"interface-id"} { +func (o *AddVirtualIPOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"vip", "interface-id"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -77,12 +75,11 @@ func (o *CreateVirtualIPOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunCreateVirtualIP( +func RunAddVirtualIP( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - ip netip.Addr, - opts CreateVirtualIPOptions, + opts AddVirtualIPOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { @@ -94,20 +91,20 @@ func RunCreateVirtualIP( } }() - renderer, err := rendererFactory.NewRenderer("created", os.Stdout) + renderer, err := rendererFactory.NewRenderer("added", os.Stdout) if err != nil { return fmt.Errorf("error creating renderer: %w", err) } - virtualIP, err := client.CreateVirtualIP(ctx, &api.VirtualIP{ + virtualIP, err := client.AddVirtualIP(ctx, &api.VirtualIP{ VirtualIPMeta: api.VirtualIPMeta{ InterfaceID: opts.InterfaceID, - IP: ip, + IP: opts.Vip, }, Spec: api.VirtualIPSpec{}, }) if err != nil { - return fmt.Errorf("error creating virtual ip: %w", err) + return fmt.Errorf("error adding virtual ip: %w", err) } if err := renderer.Render(virtualIP); err != nil { diff --git a/cmd/command.go b/cmd/command.go index fbc9d37..4b1f9a7 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -30,7 +30,7 @@ func Command() *cobra.Command { dpdkClientOptions.AddFlags(cmd.PersistentFlags()) cmd.AddCommand( - Create(dpdkClientOptions), + Add(dpdkClientOptions), Get(dpdkClientOptions), List(dpdkClientOptions), Delete(dpdkClientOptions), diff --git a/cmd/create_interface.go b/cmd/create_interface.go index fa9455f..35ea1f3 100644 --- a/cmd/create_interface.go +++ b/cmd/create_interface.go @@ -33,18 +33,16 @@ func CreateInterface(dpdkClientFactory DPDKClientFactory, rendererFactory Render ) cmd := &cobra.Command{ - Use: "interface ", + Use: "interface <--id> [<--ip>] <--vni> <--device>", Short: "Create an interface", - Example: "dpservice-cli create interface vm4 --ips=10.200.1.4 --ips=2000:200:1::4 --vni=200 --device=net_tap5", + Example: "dpservice-cli add interface --id=vm4 --ip=10.200.1.4 --ip=2000:200:1::4 --vni=200 --device=net_tap5", Aliases: InterfaceAliases, - Args: cobra.ExactArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - interfaceID := args[0] return RunCreateInterface( cmd.Context(), dpdkClientFactory, rendererFactory, - interfaceID, opts, ) }, @@ -58,19 +56,21 @@ func CreateInterface(dpdkClientFactory DPDKClientFactory, rendererFactory Render } type CreateInterfaceOptions struct { + ID string VNI uint32 - IPs []netip.Addr + IP []netip.Addr Device string } func (o *CreateInterfaceOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.ID, "id", o.ID, "ID of the interface.") fs.Uint32Var(&o.VNI, "vni", o.VNI, "VNI to add the interface to.") - flag.AddrSliceVar(fs, &o.IPs, "ips", o.IPs, "IPs to assign to the interface.") + flag.AddrSliceVar(fs, &o.IP, "ip", o.IP, "IP to assign to the interface.") fs.StringVar(&o.Device, "device", o.Device, "Device to allocate.") } func (o *CreateInterfaceOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"vni", "ips"} { + for _, name := range []string{"id", "vni", "ip", "device"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -78,7 +78,7 @@ func (o *CreateInterfaceOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunCreateInterface(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, interfaceID string, opts CreateInterfaceOptions) error { +func RunCreateInterface(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts CreateInterfaceOptions) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -96,12 +96,12 @@ func RunCreateInterface(ctx context.Context, dpdkClientFactory DPDKClientFactory iface, err := client.CreateInterface(ctx, &api.Interface{ InterfaceMeta: api.InterfaceMeta{ - ID: interfaceID, + ID: opts.ID, }, Spec: api.InterfaceSpec{ VNI: opts.VNI, Device: opts.Device, - IPs: opts.IPs, + IPs: opts.IP, }, }) if err != nil { diff --git a/cmd/create_loadbalancer.go b/cmd/create_loadbalancer.go index 53d4532..ced8cdf 100644 --- a/cmd/create_loadbalancer.go +++ b/cmd/create_loadbalancer.go @@ -33,18 +33,16 @@ func CreateLoadBalancer(dpdkClientFactory DPDKClientFactory, rendererFactory Ren ) cmd := &cobra.Command{ - Use: "loadbalancer ", + Use: "loadbalancer <--id> <--vni> <--vip> <--lbports>", Short: "Create a loadbalancer", - Example: "dpservice-cli create lb 4 --vni=100 --vip=10.20.30.40 --lbports=TCP/443,UDP/53", + Example: "dpservice-cli create lb --id=4 --vni=100 --vip=10.20.30.40 --lbports=TCP/443,UDP/53", Aliases: LoadBalancerAliases, - Args: cobra.ExactArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - loadbalancerID := args[0] return RunCreateLoadBalancer( cmd.Context(), dpdkClientFactory, rendererFactory, - loadbalancerID, opts, ) }, @@ -58,19 +56,21 @@ func CreateLoadBalancer(dpdkClientFactory DPDKClientFactory, rendererFactory Ren } type CreateLoadBalancerOptions struct { + Id string VNI uint32 LbVipIP netip.Addr Lbports []string } func (o *CreateLoadBalancerOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.Id, "id", o.Id, "Loadbalancer ID to add.") fs.Uint32Var(&o.VNI, "vni", o.VNI, "VNI to add the loadbalancer to.") flag.AddrVar(fs, &o.LbVipIP, "vip", o.LbVipIP, "VIP to assign to the loadbalancer.") - fs.StringSliceVar(&o.Lbports, "lbports", o.Lbports, "LB ports to assign to the loadbalancer") + fs.StringSliceVar(&o.Lbports, "lbports", o.Lbports, "LB ports to assign to the loadbalancer.") } func (o *CreateLoadBalancerOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"vni", "vip", "lbports"} { + for _, name := range []string{"id", "vni", "vip", "lbports"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -78,7 +78,7 @@ func (o *CreateLoadBalancerOptions) MarkRequiredFlags(cmd *cobra.Command) error return nil } -func RunCreateLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, loadbalancerID string, opts CreateLoadBalancerOptions) error { +func RunCreateLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts CreateLoadBalancerOptions) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -105,7 +105,7 @@ func RunCreateLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFact lb, err := client.CreateLoadBalancer(ctx, &api.LoadBalancer{ LoadBalancerMeta: api.LoadBalancerMeta{ - ID: loadbalancerID, + ID: opts.Id, }, Spec: api.LoadBalancerSpec{ VNI: opts.VNI, diff --git a/cmd/create_loadbalancer_prefix.go b/cmd/create_loadbalancer_prefix.go index 3ed4ee2..fabd88d 100644 --- a/cmd/create_loadbalancer_prefix.go +++ b/cmd/create_loadbalancer_prefix.go @@ -21,6 +21,7 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -35,22 +36,17 @@ func CreateLoadBalancerPrefix( ) cmd := &cobra.Command{ - Use: "lbprefix <--interface-id>", + Use: "lbprefix <--prefix> <--interface-id>", Short: "Create a loadbalancer prefix", - Example: "dpservice-cli create lbprefix 10.10.10.0/24 --interface-id=vm1", - Args: cobra.ExactArgs(1), + Example: "dpservice-cli add lbprefix --prefix=10.10.10.0/24 --interface-id=vm1", + Args: cobra.ExactArgs(0), Aliases: PrefixAliases, RunE: func(cmd *cobra.Command, args []string) error { - prefix, err := netip.ParsePrefix(args[0]) - if err != nil { - return fmt.Errorf("error parsing prefix: %w", err) - } return RunCreateLoadBalancerPrefix( cmd.Context(), dpdkClientFactory, rendererFactory, - prefix, opts, ) }, @@ -64,15 +60,17 @@ func CreateLoadBalancerPrefix( } type CreateLoadBalancerPrefixOptions struct { + Prefix netip.Prefix InterfaceID string } func (o *CreateLoadBalancerPrefixOptions) AddFlags(fs *pflag.FlagSet) { + flag.PrefixVar(fs, &o.Prefix, "prefix", o.Prefix, "Prefix to add to the interface.") fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "ID of the interface to create the prefix for.") } func (o *CreateLoadBalancerPrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"interface-id"} { + for _, name := range []string{"prefix", "interface-id"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -84,7 +82,6 @@ func RunCreateLoadBalancerPrefix( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - prefix netip.Prefix, opts CreateLoadBalancerPrefixOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) @@ -105,7 +102,7 @@ func RunCreateLoadBalancerPrefix( res, err := client.CreateLoadBalancerPrefix(ctx, &api.Prefix{ PrefixMeta: api.PrefixMeta{ InterfaceID: opts.InterfaceID, - Prefix: prefix, + Prefix: opts.Prefix, }, Spec: api.PrefixSpec{}, }) diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index 443787a..bda9c4e 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -73,7 +73,7 @@ func LbipToProtoLbip(lbip netip.Addr) *proto.LBIP { return &proto.LBIP{IpVersion: NetIPAddrToProtoIPVersion(lbip), Address: []byte(lbip.String())} } -func ProtoLbipToLbip(protolbip proto.LBIP) *LBIP { +func ProtoLbipToLbip(protolbip proto.LBIP) *netip.Addr { var ip netip.Addr if lbipString := string(protolbip.Address); lbipString != "" { var err error @@ -82,7 +82,7 @@ func ProtoLbipToLbip(protolbip proto.LBIP) *LBIP { return nil } } - return &LBIP{Address: ip, IpVersion: proto.IPVersion_name[int32(protolbip.IpVersion)]} + return &ip } func StringLbportToLbport(lbport string) (LBPort, error) { diff --git a/dpdk/api/types.go b/dpdk/api/types.go index e5a4196..5cea767 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -192,7 +192,7 @@ func (m *LoadBalancerTargetMeta) GetName() string { } type LoadBalancerTargetSpec struct { - TargetIP LBIP `json:"targetIP"` + TargetIP netip.Addr `json:"targetIP"` } type LoadBalancerTargetList struct { diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 0b10641..64bf770 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -35,7 +35,7 @@ type Client interface { DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) error GetLoadBalancerTargets(ctx context.Context, interfaceID string) (*api.LoadBalancerTargetList, error) - CreateLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBalancerTarget) (*api.LoadBalancerTarget, error) + AddLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBalancerTarget) (*api.LoadBalancerTarget, error) DeleteLoadBalancerTarget(ctx context.Context, id string, targetIP netip.Addr) error GetInterface(ctx context.Context, id string) (*api.Interface, error) @@ -44,27 +44,27 @@ type Client interface { DeleteInterface(ctx context.Context, id string) error GetVirtualIP(ctx context.Context, interfaceID string) (*api.VirtualIP, error) - CreateVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*api.VirtualIP, error) + AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*api.VirtualIP, error) DeleteVirtualIP(ctx context.Context, interfaceID string) error ListPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) - CreatePrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix, error) + AddPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix, error) DeletePrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) error ListRoutes(ctx context.Context, vni uint32) (*api.RouteList, error) - CreateRoute(ctx context.Context, route *api.Route) (*api.Route, error) + AddRoute(ctx context.Context, route *api.Route) (*api.Route, error) DeleteRoute(ctx context.Context, vni uint32, prefix netip.Prefix, nextHopVNI uint32, nextHopIP netip.Addr) error GetNat(ctx context.Context, interfaceID string) (*api.Nat, error) - CreateNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) + AddNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) DeleteNat(ctx context.Context, interfaceID string) error - CreateNeighborNat(ctx context.Context, nat *api.NeighborNat) error + AddNeighborNat(ctx context.Context, nat *api.NeighborNat) error GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType int32) (*api.NatList, error) DeleteNeighborNat(ctx context.Context, neigbhorNat api.NeighborNat) error ListFirewallRules(ctx context.Context, interfaceID string) (*api.FirewallRuleList, error) - CreateFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) + AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) GetFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) error @@ -234,10 +234,10 @@ func (c *client) GetLoadBalancerTargets(ctx context.Context, loadBalancerID stri }, nil } -func (c *client) CreateLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBalancerTarget) (*api.LoadBalancerTarget, error) { +func (c *client) AddLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBalancerTarget) (*api.LoadBalancerTarget, error) { res, err := c.DPDKonmetalClient.AddLoadBalancerTarget(ctx, &dpdkproto.AddLoadBalancerTargetRequest{ LoadBalancerID: []byte(lbtarget.LoadBalancerTargetMeta.ID), - TargetIP: api.LbipToProtoLbip(lbtarget.Spec.TargetIP.Address), + TargetIP: api.LbipToProtoLbip(lbtarget.Spec.TargetIP), }) if err != nil { return nil, err @@ -356,7 +356,7 @@ func (c *client) GetVirtualIP(ctx context.Context, interfaceName string) (*api.V return api.ProtoVirtualIPToVirtualIP(interfaceName, res) } -func (c *client) CreateVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*api.VirtualIP, error) { +func (c *client) AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*api.VirtualIP, error) { res, err := c.DPDKonmetalClient.AddInterfaceVIP(ctx, &dpdkproto.InterfaceVIPMsg{ InterfaceID: []byte(virtualIP.InterfaceID), InterfaceVIPIP: &dpdkproto.InterfaceVIPIP{ @@ -372,8 +372,9 @@ func (c *client) CreateVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) } return &api.VirtualIP{ - TypeMeta: api.TypeMeta{Kind: api.VirtualIPKind}, - Spec: virtualIP.Spec, + TypeMeta: api.TypeMeta{Kind: api.VirtualIPKind}, + VirtualIPMeta: virtualIP.VirtualIPMeta, + Spec: virtualIP.Spec, }, nil } @@ -414,7 +415,7 @@ func (c *client) ListPrefixes(ctx context.Context, interfaceID string) (*api.Pre }, nil } -func (c *client) CreatePrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix, error) { +func (c *client) AddPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix, error) { res, err := c.DPDKonmetalClient.AddInterfacePrefix(ctx, &dpdkproto.InterfacePrefixMsg{ InterfaceID: &dpdkproto.InterfaceIDMsg{ InterfaceID: []byte(prefix.InterfaceID), @@ -458,7 +459,7 @@ func (c *client) DeletePrefix(ctx context.Context, interfaceID string, prefix ne return nil } -func (c *client) CreateRoute(ctx context.Context, route *api.Route) (*api.Route, error) { +func (c *client) AddRoute(ctx context.Context, route *api.Route) (*api.Route, error) { res, err := c.DPDKonmetalClient.AddRoute(ctx, &dpdkproto.VNIRouteMsg{ Vni: &dpdkproto.VNIMsg{Vni: route.VNI}, Route: &dpdkproto.Route{ @@ -546,7 +547,7 @@ func (c *client) GetNat(ctx context.Context, interfaceID string) (*api.Nat, erro return nat, err } -func (c *client) CreateNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) { +func (c *client) AddNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) { res, err := c.DPDKonmetalClient.AddNAT(ctx, &dpdkproto.AddNATRequest{ InterfaceID: []byte(nat.NatMeta.InterfaceID), NatVIPIP: &dpdkproto.NATIP{ @@ -593,7 +594,7 @@ func (c *client) DeleteNat(ctx context.Context, interfaceID string) error { return nil } -func (c *client) CreateNeighborNat(ctx context.Context, nNat *api.NeighborNat) error { +func (c *client) AddNeighborNat(ctx context.Context, nNat *api.NeighborNat) error { res, err := c.DPDKonmetalClient.AddNeighborNAT(ctx, &dpdkproto.AddNeighborNATRequest{ NatVIPIP: &dpdkproto.NATIP{ @@ -703,7 +704,7 @@ func (c *client) ListFirewallRules(ctx context.Context, interfaceID string) (*ap }, nil } -func (c *client) CreateFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) { +func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) { res, err := c.DPDKonmetalClient.AddFirewallRule(ctx, &dpdkproto.AddFirewallRuleRequest{ InterfaceID: []byte(fwRule.FirewallRuleMeta.InterfaceID), Rule: &dpdkproto.FirewallRule{ diff --git a/dpdk/client/dynamic/dynamic.go b/dpdk/client/dynamic/dynamic.go index 13d848d..4462562 100644 --- a/dpdk/client/dynamic/dynamic.go +++ b/dpdk/client/dynamic/dynamic.go @@ -137,7 +137,7 @@ func (c *client) Create(ctx context.Context, obj any) error { *obj = *res return nil case *api.Prefix: - res, err := c.structured.CreatePrefix(ctx, obj) + res, err := c.structured.AddPrefix(ctx, obj) if err != nil { return err } @@ -145,7 +145,7 @@ func (c *client) Create(ctx context.Context, obj any) error { *obj = *res return nil case *api.Route: - res, err := c.structured.CreateRoute(ctx, obj) + res, err := c.structured.AddRoute(ctx, obj) if err != nil { return err } @@ -153,7 +153,7 @@ func (c *client) Create(ctx context.Context, obj any) error { *obj = *res return nil case *api.VirtualIP: - res, err := c.structured.CreateVirtualIP(ctx, obj) + res, err := c.structured.AddVirtualIP(ctx, obj) if err != nil { return err } diff --git a/renderer/renderer.go b/renderer/renderer.go index 5a10f64..08036fd 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -208,8 +208,8 @@ func (t defaultTableConverter) loadBalancerTargetTable(lbtargets []api.LoadBalan for i, lbtarget := range lbtargets { columns[i] = []any{ lbtarget.LoadBalancerTargetMeta.ID, - lbtarget.Spec.TargetIP.IpVersion, - lbtarget.Spec.TargetIP.Address, + api.NetIPAddrToProtoIPVersion(lbtarget.Spec.TargetIP), + lbtarget.Spec.TargetIP, } } From 00089b9e98adce177a4f74bd93d8ca8b4ca7b24f Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 16 May 2023 15:59:58 +0200 Subject: [PATCH 30/65] change args to flags of delete commands --- cmd/delete_firewall_rule.go | 33 +++++++++--------- cmd/delete_interface.go | 32 ++++++++++------- ...loadbalnacer.go => delete_loadbalancer.go} | 32 ++++++++++------- ...refix.go => delete_loadbalancer_prefix.go} | 28 +++++++-------- ...arget.go => delete_loadbalancer_target.go} | 29 +++++++--------- cmd/delete_nat.go | 32 ++++++++++------- cmd/delete_neighbor_nat.go | 27 +++++++-------- cmd/delete_prefix.go | 28 +++++++-------- cmd/delete_route.go | 34 ++++++++++--------- cmd/delete_virtualip.go | 32 ++++++++++------- 10 files changed, 163 insertions(+), 144 deletions(-) rename cmd/{delete_loadbalnacer.go => delete_loadbalancer.go} (71%) rename cmd/{delete_loadblanacer_prefix.go => delete_loadbalancer_prefix.go} (70%) rename cmd/{delete_loadblanacer_target.go => delete_loadbalancer_target.go} (69%) diff --git a/cmd/delete_firewall_rule.go b/cmd/delete_firewall_rule.go index 3609fc8..b27074e 100644 --- a/cmd/delete_firewall_rule.go +++ b/cmd/delete_firewall_rule.go @@ -29,14 +29,14 @@ func DeleteFirewallRule(factory DPDKClientFactory) *cobra.Command { ) cmd := &cobra.Command{ - Use: "firewallrule [ ...] <--interface-id>", - Short: "Delete firewall rule(s)", - Example: "dpservice-cli delete firewallrule 1 --interface-id=vm1", + Use: "firewallrule <--rule-id> <--interface-id>", + Short: "Delete firewall rule from interface", + Example: "dpservice-cli delete firewallrule --rule-id=1 --interface-id=vm1", Aliases: FirewallRuleAliases, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - ruleIDs := args - return RunDeleteFirewallRule(cmd.Context(), factory, ruleIDs, opts) + + return RunDeleteFirewallRule(cmd.Context(), factory, opts) }, } @@ -48,15 +48,17 @@ func DeleteFirewallRule(factory DPDKClientFactory) *cobra.Command { } type DeleteFirewallRuleOptions struct { - InrerfaceID string + RuleID string + InterfaceID string } func (o *DeleteFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { - fs.StringVar(&o.InrerfaceID, "interface-id", o.InrerfaceID, "Intreface ID where to delete firewall rule(s).") + fs.StringVar(&o.RuleID, "rule-id", o.RuleID, "Rule ID to delete.") + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Intreface ID where to delete firewall rule.") } func (o *DeleteFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"interface-id"} { + for _, name := range []string{"rule-id", "interface-id"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -64,7 +66,7 @@ func (o *DeleteFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error return nil } -func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, ruleIDs []string, opts DeleteFirewallRuleOptions) error { +func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, opts DeleteFirewallRuleOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -75,12 +77,11 @@ func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, ruleI } }() - for _, ruleID := range ruleIDs { - if err := client.DeleteFirewallRule(ctx, opts.InrerfaceID, ruleID); err != nil { - return fmt.Errorf("error deleting firewall rule %s: %v", ruleID, err) - } - - fmt.Println("Deleted firewall rule", ruleID, "on interface", opts.InrerfaceID) + if err := client.DeleteFirewallRule(ctx, opts.InterfaceID, opts.RuleID); err != nil { + return fmt.Errorf("error deleting firewall rule %s/%s: %v", opts.RuleID, opts.InterfaceID, err) } + + fmt.Printf("Deleted firewall rule %s on interface %s\n", opts.RuleID, opts.InterfaceID) + return nil } diff --git a/cmd/delete_interface.go b/cmd/delete_interface.go index efd042b..f74a845 100644 --- a/cmd/delete_interface.go +++ b/cmd/delete_interface.go @@ -29,14 +29,14 @@ func DeleteInterface(factory DPDKClientFactory) *cobra.Command { ) cmd := &cobra.Command{ - Use: "interface [ ...]", - Short: "Delete interface(s)", - Example: "dpservice-cli delete interface vm1", + Use: "interface <--id>", + Short: "Delete interface", + Example: "dpservice-cli delete interface --id=vm1", Aliases: InterfaceAliases, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - interfaceIDs := args - return RunDeleteInterface(cmd.Context(), factory, interfaceIDs, opts) + + return RunDeleteInterface(cmd.Context(), factory, opts) }, } @@ -48,16 +48,23 @@ func DeleteInterface(factory DPDKClientFactory) *cobra.Command { } type DeleteInterfaceOptions struct { + ID string } func (o *DeleteInterfaceOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.ID, "id", o.ID, "Interface ID to delete.") } func (o *DeleteInterfaceOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } return nil } -func RunDeleteInterface(ctx context.Context, factory DPDKClientFactory, interfaceIDs []string, opts DeleteInterfaceOptions) error { +func RunDeleteInterface(ctx context.Context, factory DPDKClientFactory, opts DeleteInterfaceOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -68,12 +75,11 @@ func RunDeleteInterface(ctx context.Context, factory DPDKClientFactory, interfac } }() - for _, interfaceID := range interfaceIDs { - if err := client.DeleteInterface(ctx, interfaceID); err != nil { - return fmt.Errorf("error deleting interface %s: %v", interfaceID, err) - } - - fmt.Println("Deleted interface", interfaceID) + if err := client.DeleteInterface(ctx, opts.ID); err != nil { + return fmt.Errorf("error deleting interface %s: %v", opts.ID, err) } + + fmt.Println("Deleted interface", opts.ID) + return nil } diff --git a/cmd/delete_loadbalnacer.go b/cmd/delete_loadbalancer.go similarity index 71% rename from cmd/delete_loadbalnacer.go rename to cmd/delete_loadbalancer.go index 0e85e71..39f1216 100644 --- a/cmd/delete_loadbalnacer.go +++ b/cmd/delete_loadbalancer.go @@ -29,14 +29,14 @@ func DeleteLoadBalancer(factory DPDKClientFactory) *cobra.Command { ) cmd := &cobra.Command{ - Use: "loadbalancer [ ...]", - Short: "Delete loadbalancer(s)", - Example: "dpservice-cli delete loadbalancer 1", + Use: "loadbalancer <--id>", + Short: "Delete loadbalancer", + Example: "dpservice-cli delete loadbalancer --id=1", Aliases: LoadBalancerAliases, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - loadbalancerIDs := args - return RunDeleteLoadBalancer(cmd.Context(), factory, loadbalancerIDs, opts) + + return RunDeleteLoadBalancer(cmd.Context(), factory, opts) }, } @@ -48,16 +48,23 @@ func DeleteLoadBalancer(factory DPDKClientFactory) *cobra.Command { } type DeleteLoadBalancerOptions struct { + ID string } func (o *DeleteLoadBalancerOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.ID, "id", o.ID, "LoadBalancer ID to delete.") } func (o *DeleteLoadBalancerOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } return nil } -func RunDeleteLoadBalancer(ctx context.Context, factory DPDKClientFactory, loadbalancerIDs []string, opts DeleteLoadBalancerOptions) error { +func RunDeleteLoadBalancer(ctx context.Context, factory DPDKClientFactory, opts DeleteLoadBalancerOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -68,12 +75,11 @@ func RunDeleteLoadBalancer(ctx context.Context, factory DPDKClientFactory, loadb } }() - for _, loadbalancerID := range loadbalancerIDs { - if err := client.DeleteLoadBalancer(ctx, loadbalancerID); err != nil { - return fmt.Errorf("error deleting loadbalancer %s: %v", loadbalancerID, err) - } - - fmt.Println("Deleted loadbalancer", loadbalancerID) + if err := client.DeleteLoadBalancer(ctx, opts.ID); err != nil { + return fmt.Errorf("error deleting loadbalancer %s: %v", opts.ID, err) } + + fmt.Println("Deleted loadbalancer", opts.ID) + return nil } diff --git a/cmd/delete_loadblanacer_prefix.go b/cmd/delete_loadbalancer_prefix.go similarity index 70% rename from cmd/delete_loadblanacer_prefix.go rename to cmd/delete_loadbalancer_prefix.go index c35fc8b..f9f7cc8 100644 --- a/cmd/delete_loadblanacer_prefix.go +++ b/cmd/delete_loadbalancer_prefix.go @@ -19,6 +19,7 @@ import ( "fmt" "net/netip" + "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -30,18 +31,14 @@ func DeleteLoadBalancerPrefix(factory DPDKClientFactory) *cobra.Command { ) cmd := &cobra.Command{ - Use: "lbprefix [...]", + Use: "lbprefix <--prefix> <--interface-id>", Short: "Delete a loadbalancer prefix", - Example: "dpservice-cli delete lbprefix ff80::1/64 --interface-id=vm1", + Example: "dpservice-cli delete lbprefix --prefix=ff80::1/64 --interface-id=vm1", Aliases: LoadBalancerPrefixAliases, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - prefixes, err := ParsePrefixArgs(args) - if err != nil { - return err - } - return RunDeleteLoadBalancerPrefix(cmd.Context(), factory, prefixes, opts) + return RunDeleteLoadBalancerPrefix(cmd.Context(), factory, opts) }, } @@ -53,15 +50,17 @@ func DeleteLoadBalancerPrefix(factory DPDKClientFactory) *cobra.Command { } type DeleteLoadBalancerPrefixOptions struct { + Prefix netip.Prefix InterfaceID string } func (o *DeleteLoadBalancerPrefixOptions) AddFlags(fs *pflag.FlagSet) { + flag.PrefixVar(fs, &o.Prefix, "prefix", o.Prefix, "Loadbalancer prefix to delete.") fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID of the loadbalancer prefix.") } func (o *DeleteLoadBalancerPrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"interface-id"} { + for _, name := range []string{"prefix", "interface-id"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -69,7 +68,7 @@ func (o *DeleteLoadBalancerPrefixOptions) MarkRequiredFlags(cmd *cobra.Command) return nil } -func RunDeleteLoadBalancerPrefix(ctx context.Context, factory DPDKClientFactory, prefixes []netip.Prefix, opts DeleteLoadBalancerPrefixOptions) error { +func RunDeleteLoadBalancerPrefix(ctx context.Context, factory DPDKClientFactory, opts DeleteLoadBalancerPrefixOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error deleting dpdk client: %w", err) @@ -80,11 +79,10 @@ func RunDeleteLoadBalancerPrefix(ctx context.Context, factory DPDKClientFactory, } }() - for _, prefix := range prefixes { - if err := client.DeleteLoadBalancerPrefix(ctx, opts.InterfaceID, prefix); err != nil { - return fmt.Errorf("error deleting loadbalancer prefix %s/%v: %v", opts.InterfaceID, prefix, err) - } - fmt.Printf("Deleted loadbalancer prefix %s/%v\n", opts.InterfaceID, prefix) + if err := client.DeleteLoadBalancerPrefix(ctx, opts.InterfaceID, opts.Prefix); err != nil { + return fmt.Errorf("error deleting loadbalancer prefix %s/%v: %v", opts.InterfaceID, opts.Prefix, err) } + fmt.Printf("Deleted loadbalancer prefix %s/%v\n", opts.InterfaceID, opts.Prefix) + return nil } diff --git a/cmd/delete_loadblanacer_target.go b/cmd/delete_loadbalancer_target.go similarity index 69% rename from cmd/delete_loadblanacer_target.go rename to cmd/delete_loadbalancer_target.go index c77292c..7a8f39a 100644 --- a/cmd/delete_loadblanacer_target.go +++ b/cmd/delete_loadbalancer_target.go @@ -19,6 +19,7 @@ import ( "fmt" "net/netip" + "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -30,15 +31,14 @@ func DeleteLoadBalancerTarget(factory DPDKClientFactory) *cobra.Command { ) cmd := &cobra.Command{ - Use: "lbtarget [] --lb-id ", + Use: "lbtarget <--target-ip> <--lb-id>", Short: "Delete a loadbalancer target", - Example: "dpservice-cli delete lbtarget ff80::4 ff80::5 --lb-id=2", + Example: "dpservice-cli delete lbtarget --target-ip=ff80::1 --lb-id=1", Aliases: LoadBalancerTargetAliases, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - targets := args - return RunDeleteLoadBalancerTarget(cmd.Context(), factory, targets, opts) + return RunDeleteLoadBalancerTarget(cmd.Context(), factory, opts) }, } @@ -50,15 +50,17 @@ func DeleteLoadBalancerTarget(factory DPDKClientFactory) *cobra.Command { } type DeleteLoadBalancerTargetOptions struct { + TargetIP netip.Addr LoadBalancerID string } func (o *DeleteLoadBalancerTargetOptions) AddFlags(fs *pflag.FlagSet) { + flag.AddrVar(fs, &o.TargetIP, "target-ip", o.TargetIP, "LoadBalancer target IP to delete.") fs.StringVar(&o.LoadBalancerID, "lb-id", o.LoadBalancerID, "LoadBalancerID where to delete target.") } func (o *DeleteLoadBalancerTargetOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"lb-id"} { + for _, name := range []string{"target-ip", "lb-id"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -66,7 +68,7 @@ func (o *DeleteLoadBalancerTargetOptions) MarkRequiredFlags(cmd *cobra.Command) return nil } -func RunDeleteLoadBalancerTarget(ctx context.Context, factory DPDKClientFactory, targets []string, opts DeleteLoadBalancerTargetOptions) error { +func RunDeleteLoadBalancerTarget(ctx context.Context, factory DPDKClientFactory, opts DeleteLoadBalancerTargetOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error deleting dpdk client: %w", err) @@ -77,15 +79,10 @@ func RunDeleteLoadBalancerTarget(ctx context.Context, factory DPDKClientFactory, } }() - for _, target := range targets { - targetIP, err := netip.ParseAddr(target) - if err != nil { - return fmt.Errorf("not valid IP Address: %w", err) - } - if err := client.DeleteLoadBalancerTarget(ctx, opts.LoadBalancerID, targetIP); err != nil { - return fmt.Errorf("error deleting loadbalancer target %s/%v: %v", opts.LoadBalancerID, target, err) - } - fmt.Printf("Deleted loadbalancer target %s/%v\n", opts.LoadBalancerID, target) + if err := client.DeleteLoadBalancerTarget(ctx, opts.LoadBalancerID, opts.TargetIP); err != nil { + return fmt.Errorf("error deleting loadbalancer target %s/%v: %v", opts.LoadBalancerID, opts.TargetIP, err) } + fmt.Printf("Deleted loadbalancer target %s/%v\n", opts.LoadBalancerID, opts.TargetIP) + return nil } diff --git a/cmd/delete_nat.go b/cmd/delete_nat.go index a63bdca..a791396 100644 --- a/cmd/delete_nat.go +++ b/cmd/delete_nat.go @@ -29,14 +29,14 @@ func DeleteNat(factory DPDKClientFactory) *cobra.Command { ) cmd := &cobra.Command{ - Use: "nat [...]", - Short: "Delete nat(s)", - Example: "dpservice-cli delete nat vm1", + Use: "nat <--interface-id>", + Short: "Delete nat from interface", + Example: "dpservice-cli delete nat --interface-id=vm1", Aliases: NatAliases, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - interfaceIDs := args - return RunDeleteNat(cmd.Context(), factory, interfaceIDs, opts) + + return RunDeleteNat(cmd.Context(), factory, opts) }, } @@ -48,16 +48,23 @@ func DeleteNat(factory DPDKClientFactory) *cobra.Command { } type DeleteNatOptions struct { + InterfaceID string } func (o *DeleteNatOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID of the Virtual IP.") } func (o *DeleteNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"interface-id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } return nil } -func RunDeleteNat(ctx context.Context, factory DPDKClientFactory, interfaceIDs []string, opts DeleteNatOptions) error { +func RunDeleteNat(ctx context.Context, factory DPDKClientFactory, opts DeleteNatOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -68,12 +75,11 @@ func RunDeleteNat(ctx context.Context, factory DPDKClientFactory, interfaceIDs [ } }() - for _, interfaceID := range interfaceIDs { - if err := client.DeleteNat(ctx, interfaceID); err != nil { - return fmt.Errorf("error deleting nat of interface %s: %v", interfaceID, err) - } - - fmt.Printf("Deleted nat of interface %s\n", interfaceID) + if err := client.DeleteNat(ctx, opts.InterfaceID); err != nil { + return fmt.Errorf("error deleting nat of interface %s: %v", opts.InterfaceID, err) } + + fmt.Printf("Deleted NAT of interface %s\n", opts.InterfaceID) + return nil } diff --git a/cmd/delete_neighbor_nat.go b/cmd/delete_neighbor_nat.go index 8d9f24e..2a81f95 100644 --- a/cmd/delete_neighbor_nat.go +++ b/cmd/delete_neighbor_nat.go @@ -20,6 +20,7 @@ import ( "net/netip" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -31,18 +32,14 @@ func DeleteNeighborNat(factory DPDKClientFactory) *cobra.Command { ) cmd := &cobra.Command{ - Use: "neighbornat [flags]", - Short: "Delete neighbor nat(s)", - Example: "dpservice-cli delete neighbornat 10.20.30.40 --vni=100 --minport=30000 --maxport=30100", + Use: "neighbornat <--natip> <--vni> <--minport> <--maxport>", + Short: "Delete neighbor nat", + Example: "dpservice-cli delete neighbornat --natip=10.20.30.40 --vni=100 --minport=30000 --maxport=30100", Aliases: NeighborNatAliases, - Args: cobra.ExactArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - natVIPIP, err := netip.ParseAddr(args[0]) - if err != nil { - return fmt.Errorf("error parsing nat vip ip: %w", err) - } - return RunDeleteNeighborNat(cmd.Context(), factory, natVIPIP, opts) + return RunDeleteNeighborNat(cmd.Context(), factory, opts) }, } @@ -54,19 +51,21 @@ func DeleteNeighborNat(factory DPDKClientFactory) *cobra.Command { } type DeleteNeighborNatOptions struct { + NatIP netip.Addr Vni uint32 MinPort uint32 MaxPort uint32 } func (o *DeleteNeighborNatOptions) AddFlags(fs *pflag.FlagSet) { + flag.AddrVar(fs, &o.NatIP, "natip", o.NatIP, "Neighbor NAT IP.") fs.Uint32Var(&o.Vni, "vni", o.Vni, "VNI of neighbor NAT.") fs.Uint32Var(&o.MinPort, "minport", o.MinPort, "MinPort of neighbor NAT.") fs.Uint32Var(&o.MaxPort, "maxport", o.MaxPort, "MaxPort of neighbor NAT.") } func (o *DeleteNeighborNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"vni", "minport", "maxport"} { + for _, name := range []string{"natip", "vni", "minport", "maxport"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -74,7 +73,7 @@ func (o *DeleteNeighborNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeleteNeighborNat(ctx context.Context, factory DPDKClientFactory, natVIPIP netip.Addr, opts DeleteNeighborNatOptions) error { +func RunDeleteNeighborNat(ctx context.Context, factory DPDKClientFactory, opts DeleteNeighborNatOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -87,7 +86,7 @@ func RunDeleteNeighborNat(ctx context.Context, factory DPDKClientFactory, natVIP neigbhorNat := api.NeighborNat{ TypeMeta: api.TypeMeta{Kind: api.NatKind}, - NeighborNatMeta: api.NeighborNatMeta{NatVIPIP: natVIPIP}, + NeighborNatMeta: api.NeighborNatMeta{NatVIPIP: opts.NatIP}, Spec: api.NeighborNatSpec{ Vni: opts.Vni, MinPort: opts.MinPort, @@ -95,10 +94,10 @@ func RunDeleteNeighborNat(ctx context.Context, factory DPDKClientFactory, natVIP }, } if err := client.DeleteNeighborNat(ctx, neigbhorNat); err != nil { - return fmt.Errorf("error deleting neighbor nat with ip %s: %v", natVIPIP, err) + return fmt.Errorf("error deleting neighbor nat with ip %s: %v", opts.NatIP, err) } - fmt.Printf("Deleted neighbor NAT with IP %s\n", natVIPIP) + fmt.Printf("Deleted neighbor NAT with IP %s\n", opts.NatIP) return nil } diff --git a/cmd/delete_prefix.go b/cmd/delete_prefix.go index 9dd6b41..b8516e1 100644 --- a/cmd/delete_prefix.go +++ b/cmd/delete_prefix.go @@ -19,6 +19,7 @@ import ( "fmt" "net/netip" + "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -30,18 +31,14 @@ func DeletePrefix(factory DPDKClientFactory) *cobra.Command { ) cmd := &cobra.Command{ - Use: "prefix [...]", + Use: "prefix <--prefix> <--interface-id>", Short: "Delete a prefix", - Example: "dpservice-cli delete prefix 10.20.30.0/24 --interface-id=vm1", + Example: "dpservice-cli delete prefix --prefix=10.20.30.0/24 --interface-id=vm1", Aliases: PrefixAliases, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - prefixes, err := ParsePrefixArgs(args) - if err != nil { - return err - } - return RunDeletePrefix(cmd.Context(), factory, prefixes, opts) + return RunDeletePrefix(cmd.Context(), factory, opts) }, } @@ -53,15 +50,17 @@ func DeletePrefix(factory DPDKClientFactory) *cobra.Command { } type DeletePrefixOptions struct { + Prefix netip.Prefix InterfaceID string } func (o *DeletePrefixOptions) AddFlags(fs *pflag.FlagSet) { + flag.PrefixVar(fs, &o.Prefix, "prefix", o.Prefix, "Prefix to delete.") fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID of the prefix.") } func (o *DeletePrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"interface-id"} { + for _, name := range []string{"prefix", "interface-id"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -69,7 +68,7 @@ func (o *DeletePrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeletePrefix(ctx context.Context, factory DPDKClientFactory, prefixes []netip.Prefix, opts DeletePrefixOptions) error { +func RunDeletePrefix(ctx context.Context, factory DPDKClientFactory, opts DeletePrefixOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error deleting dpdk client: %w", err) @@ -80,11 +79,10 @@ func RunDeletePrefix(ctx context.Context, factory DPDKClientFactory, prefixes [] } }() - for _, prefix := range prefixes { - if err := client.DeletePrefix(ctx, opts.InterfaceID, prefix); err != nil { - return fmt.Errorf("error deleting prefix %s/%v: %v", opts.InterfaceID, prefix, err) - } - fmt.Printf("Deleted prefix %s/%v\n", opts.InterfaceID, prefix) + if err := client.DeletePrefix(ctx, opts.InterfaceID, opts.Prefix); err != nil { + return fmt.Errorf("error deleting prefix %s/%v: %v", opts.InterfaceID, opts.Prefix, err) } + fmt.Printf("Deleted prefix %s/%v\n", opts.InterfaceID, opts.Prefix) + return nil } diff --git a/cmd/delete_route.go b/cmd/delete_route.go index 4a64132..daa877d 100644 --- a/cmd/delete_route.go +++ b/cmd/delete_route.go @@ -17,7 +17,9 @@ package cmd import ( "context" "fmt" + "net/netip" + "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -29,18 +31,14 @@ func DeleteRoute(factory DPDKClientFactory) *cobra.Command { ) cmd := &cobra.Command{ - Use: "route [ ...]", + Use: "route <--prefix> <--next-hop-vni> <--next-hop-ip> <--vni>", Short: "Delete a route", - Example: "dpservice-cli delete route 10.100.2.0/24 0 fc00:2::64:0:1 --vni=100", + Example: "dpservice-cli delete route --prefix=10.100.2.0/24 --next-hop-vni=0 --next-hop-ip=fc00:2::64:0:1 --vni=100", Aliases: RouteAliases, - Args: MultipleOfArgs(3), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - keys, err := ParseRouteKeyArgs(args) - if err != nil { - return err - } - return RunDeleteRoute(cmd.Context(), factory, keys, opts) + return RunDeleteRoute(cmd.Context(), factory, opts) }, } @@ -52,15 +50,21 @@ func DeleteRoute(factory DPDKClientFactory) *cobra.Command { } type DeleteRouteOptions struct { - VNI uint32 + Prefix netip.Prefix + NextHopVNI uint32 + NextHopIP netip.Addr + VNI uint32 } func (o *DeleteRouteOptions) AddFlags(fs *pflag.FlagSet) { + flag.PrefixVar(fs, &o.Prefix, "prefix", o.Prefix, "Prefix of the route.") + fs.Uint32Var(&o.NextHopVNI, "next-hop-vni", o.NextHopVNI, "Next hop VNI of the route.") + flag.AddrVar(fs, &o.NextHopIP, "next-hop-ip", o.NextHopIP, "Next hop IP of the route.") fs.Uint32Var(&o.VNI, "vni", o.VNI, "VNI of the route.") } func (o *DeleteRouteOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"vni"} { + for _, name := range []string{"prefix", "next-hop-vni", "next-hop-ip", "vni"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -68,7 +72,7 @@ func (o *DeleteRouteOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeleteRoute(ctx context.Context, factory DPDKClientFactory, keys []RouteKey, opts DeleteRouteOptions) error { +func RunDeleteRoute(ctx context.Context, factory DPDKClientFactory, opts DeleteRouteOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error deleting dpdk client: %w", err) @@ -79,12 +83,10 @@ func RunDeleteRoute(ctx context.Context, factory DPDKClientFactory, keys []Route } }() - for _, key := range keys { - if err := client.DeleteRoute(ctx, opts.VNI, key.Prefix, key.NextHopVNI, key.NextHopIP); err != nil { - return fmt.Errorf("error deleting route %d-%v:%d-%v: %v", opts.VNI, key.Prefix, key.NextHopVNI, key.NextHopIP, err) - } - fmt.Printf("Deleted route %d-%v:%d-%v\n", opts.VNI, key.Prefix, key.NextHopVNI, key.NextHopIP) + if err := client.DeleteRoute(ctx, opts.VNI, opts.Prefix, opts.NextHopVNI, opts.NextHopIP); err != nil { + return fmt.Errorf("error deleting route %d-%v:%d-%v: %v", opts.VNI, opts.Prefix, opts.NextHopVNI, opts.NextHopIP, err) } + fmt.Printf("Deleted route %d-%v:%d-%v\n", opts.VNI, opts.Prefix, opts.NextHopVNI, opts.NextHopIP) return nil } diff --git a/cmd/delete_virtualip.go b/cmd/delete_virtualip.go index fc886f3..549d25d 100644 --- a/cmd/delete_virtualip.go +++ b/cmd/delete_virtualip.go @@ -29,14 +29,14 @@ func DeleteVirtualIP(factory DPDKClientFactory) *cobra.Command { ) cmd := &cobra.Command{ - Use: "virtualip [...]", - Short: "Delete virtual ip(s)", - Example: "dpservice-cli delete virtualip vm1", + Use: "virtualip <--interface-id>", + Short: "Delete virtual IP from interface", + Example: "dpservice-cli delete virtualip --interface-id=vm1", Aliases: VirtualIPAliases, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - interfaceIDs := args - return RunDeleteVirtualIP(cmd.Context(), factory, interfaceIDs, opts) + + return RunDeleteVirtualIP(cmd.Context(), factory, opts) }, } @@ -48,16 +48,23 @@ func DeleteVirtualIP(factory DPDKClientFactory) *cobra.Command { } type DeleteVirtualIPOptions struct { + InterfaceID string } func (o *DeleteVirtualIPOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID of the Virtual IP.") } func (o *DeleteVirtualIPOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"interface-id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } return nil } -func RunDeleteVirtualIP(ctx context.Context, factory DPDKClientFactory, interfaceIDs []string, opts DeleteVirtualIPOptions) error { +func RunDeleteVirtualIP(ctx context.Context, factory DPDKClientFactory, opts DeleteVirtualIPOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -68,12 +75,11 @@ func RunDeleteVirtualIP(ctx context.Context, factory DPDKClientFactory, interfac } }() - for _, interfaceID := range interfaceIDs { - if err := client.DeleteVirtualIP(ctx, interfaceID); err != nil { - return fmt.Errorf("error deleting virtual ip of interface %s: %v", interfaceID, err) - } - - fmt.Printf("Deleted virtual ip of interface %s\n", interfaceID) + if err := client.DeleteVirtualIP(ctx, opts.InterfaceID); err != nil { + return fmt.Errorf("error deleting virtual ip of interface %s: %v", opts.InterfaceID, err) } + + fmt.Printf("Deleted virtual ip of interface %s\n", opts.InterfaceID) + return nil } From 5e3bdc020af093fb93cd93f4d7850fbaba4da8bf Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Wed, 17 May 2023 11:17:48 +0200 Subject: [PATCH 31/65] change args to flags of get commands --- cmd/get_firewall_rule.go | 20 ++++++++--------- cmd/get_interface.go | 41 +++++++++++++--------------------- cmd/get_loadbalancer.go | 35 ++++++++++++++--------------- cmd/get_loadbalancer_target.go | 36 ++++++++++++++--------------- cmd/get_nat.go | 36 ++++++++++++++--------------- cmd/get_nat_info.go | 23 +++++++++---------- cmd/get_virtualip.go | 35 ++++++++++++++--------------- 7 files changed, 106 insertions(+), 120 deletions(-) diff --git a/cmd/get_firewall_rule.go b/cmd/get_firewall_rule.go index d5b3010..05cfd44 100644 --- a/cmd/get_firewall_rule.go +++ b/cmd/get_firewall_rule.go @@ -30,18 +30,17 @@ func GetFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory Render ) cmd := &cobra.Command{ - Use: "firewallrule <--interface-id>", + Use: "firewallrule <--rule-id> <--interface-id>", Short: "Get firewall rule", - Example: "dpservice-cli get fwrule 1 --interface-id=vm1", + Example: "dpservice-cli get fwrule --rule-id=1 --interface-id=vm1", Aliases: FirewallRuleAliases, - Args: cobra.ExactArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - ruleID := args[0] + return RunGetFirewallRule( cmd.Context(), dpdkClientFactory, rendererFactory, - ruleID, opts, ) }, @@ -55,15 +54,17 @@ func GetFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory Render } type GetFirewallRuleOptions struct { + RuleID string InterfaceID string } func (o *GetFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { - fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "InterfaceID where is firewall rule.") + fs.StringVar(&o.RuleID, "rule-id", o.RuleID, "Rule ID to get.") + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID where is firewall rule.") } func (o *GetFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"interface-id"} { + for _, name := range []string{"rule-id", "interface-id"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -75,7 +76,6 @@ func RunGetFirewallRule( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - ruleID string, opts GetFirewallRuleOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) @@ -93,13 +93,13 @@ func RunGetFirewallRule( return fmt.Errorf("error creating renderer: %w", err) } - fwrule, err := client.GetFirewallRule(ctx, ruleID, opts.InterfaceID) + fwrule, err := client.GetFirewallRule(ctx, opts.RuleID, opts.InterfaceID) if err != nil { return fmt.Errorf("error getting firewall rule: %w", err) } if err := renderer.Render(fwrule); err != nil { - return fmt.Errorf("error rendering firewall rule %s/%s: %w", ruleID, opts.InterfaceID, err) + return fmt.Errorf("error rendering firewall rule %s/%s: %w", opts.RuleID, opts.InterfaceID, err) } return nil } diff --git a/cmd/get_interface.go b/cmd/get_interface.go index d00f1c4..911f1ec 100644 --- a/cmd/get_interface.go +++ b/cmd/get_interface.go @@ -30,17 +30,16 @@ func GetInterface(dpdkClientFactory DPDKClientFactory, rendererFactory RendererF ) cmd := &cobra.Command{ - Use: "interface [...]", - Short: "Get or list interface(s)", - Example: "dpservice-cli get interface vm1 vm2", + Use: "interface <--id>", + Short: "Get interface", + Example: "dpservice-cli get interface --id=vm1", Aliases: InterfaceAliases, RunE: func(cmd *cobra.Command, args []string) error { - interfaceIDs := args + return RunGetInterface( cmd.Context(), dpdkClientFactory, rendererFactory, - interfaceIDs, opts, ) }, @@ -54,12 +53,19 @@ func GetInterface(dpdkClientFactory DPDKClientFactory, rendererFactory RendererF } type GetInterfaceOptions struct { + ID string } func (o *GetInterfaceOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.ID, "id", o.ID, "ID of the interface.") } func (o *GetInterfaceOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } return nil } @@ -67,7 +73,6 @@ func RunGetInterface( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - interfaceIDs []string, opts GetInterfaceOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) @@ -85,27 +90,13 @@ func RunGetInterface( return fmt.Errorf("error creating renderer: %w", err) } - if len(interfaceIDs) == 0 { - ifaceList, err := client.ListInterfaces(ctx) - if err != nil { - return fmt.Errorf("error listing interfaces: %w", err) - } - - if err := renderer.Render(ifaceList); err != nil { - return fmt.Errorf("error rendering list: %w", err) - } - return nil + iface, err := client.GetInterface(ctx, opts.ID) + if err != nil { + return fmt.Errorf("error getting interface: %w", err) } - for _, interfaceID := range interfaceIDs { - iface, err := client.GetInterface(ctx, interfaceID) - if err != nil { - return fmt.Errorf("error getting interface: %w", err) - } - - if err := renderer.Render(iface); err != nil { - return fmt.Errorf("error rendering interface %s: %w", interfaceID, err) - } + if err := renderer.Render(iface); err != nil { + return fmt.Errorf("error rendering interface %s: %w", opts.ID, err) } return nil } diff --git a/cmd/get_loadbalancer.go b/cmd/get_loadbalancer.go index 0cf8f33..66c5b53 100644 --- a/cmd/get_loadbalancer.go +++ b/cmd/get_loadbalancer.go @@ -30,18 +30,17 @@ func GetLoadBalancer(dpdkClientFactory DPDKClientFactory, rendererFactory Render ) cmd := &cobra.Command{ - Use: "loadbalancer [...]", - Short: "Get or list loadbalancer(s)", - Example: "dpservice-cli get lb 4", + Use: "loadbalancer <--id>", + Short: "Get loadbalancer", + Example: "dpservice-cli get loadbalancer --id=4", Aliases: LoadBalancerAliases, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - loadbalancerIDs := args + return RunGetLoadBalancer( cmd.Context(), dpdkClientFactory, rendererFactory, - loadbalancerIDs, opts, ) }, @@ -55,12 +54,19 @@ func GetLoadBalancer(dpdkClientFactory DPDKClientFactory, rendererFactory Render } type GetLoadBalancerOptions struct { + ID string } func (o *GetLoadBalancerOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.ID, "id", o.ID, "ID of the LoadBalancer.") } func (o *GetLoadBalancerOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } return nil } @@ -68,7 +74,6 @@ func RunGetLoadBalancer( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - loadbalancerIDs []string, opts GetLoadBalancerOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) @@ -86,19 +91,13 @@ func RunGetLoadBalancer( return fmt.Errorf("error creating renderer: %w", err) } - if len(loadbalancerIDs) == 0 { - return fmt.Errorf("need to specify loadbalancer id") + lb, err := client.GetLoadBalancer(ctx, opts.ID) + if err != nil { + return fmt.Errorf("error getting loadbalancer: %w", err) } - for _, loadbalancerID := range loadbalancerIDs { - lb, err := client.GetLoadBalancer(ctx, loadbalancerID) - if err != nil { - return fmt.Errorf("error getting loadbalancer: %w", err) - } - - if err := renderer.Render(lb); err != nil { - return fmt.Errorf("error rendering loadbalancer %s: %w", loadbalancerID, err) - } + if err := renderer.Render(lb); err != nil { + return fmt.Errorf("error rendering loadbalancer %s: %w", opts.ID, err) } return nil } diff --git a/cmd/get_loadbalancer_target.go b/cmd/get_loadbalancer_target.go index 1bd4693..ec942a7 100644 --- a/cmd/get_loadbalancer_target.go +++ b/cmd/get_loadbalancer_target.go @@ -30,18 +30,17 @@ func GetLoadBalancerTargets(dpdkClientFactory DPDKClientFactory, rendererFactory ) cmd := &cobra.Command{ - Use: "lbtarget [...]", - Short: "Get or list LoadBalancerTarget(s)", - Example: "dpservice-cli get lbtarget 1 2", + Use: "lbtarget <--lb-id>", + Short: "Get LoadBalancer Targets", + Example: "dpservice-cli get lbtarget --lb-id=1", Aliases: LoadBalancerTargetAliases, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - interfaceIDs := args + return RunGetLoadBalancerTargets( cmd.Context(), dpdkClientFactory, rendererFactory, - interfaceIDs, opts, ) }, @@ -55,12 +54,19 @@ func GetLoadBalancerTargets(dpdkClientFactory DPDKClientFactory, rendererFactory } type GetLoadBalancerTargetOptions struct { + LoadBalancerID string } func (o *GetLoadBalancerTargetOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.LoadBalancerID, "lb-id", o.LoadBalancerID, "ID of the loadbalancer to get the targets for.") } func (o *GetLoadBalancerTargetOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"lb-id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } return nil } @@ -68,7 +74,6 @@ func RunGetLoadBalancerTargets( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - interfaceIDs []string, opts GetLoadBalancerTargetOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) @@ -86,19 +91,14 @@ func RunGetLoadBalancerTargets( return fmt.Errorf("error creating renderer: %w", err) } - if len(interfaceIDs) == 0 { - return fmt.Errorf("listing loadbalancer targets is not implemented") + lbtarget, err := client.GetLoadBalancerTargets(ctx, opts.LoadBalancerID) + if err != nil { + return fmt.Errorf("error getting loadbalancer target for interface %s: %v", opts.LoadBalancerID, err) } - for _, interfaceID := range interfaceIDs { - lbtarget, err := client.GetLoadBalancerTargets(ctx, interfaceID) - if err != nil { - return fmt.Errorf("error getting loadbalancer target for interface %s: %v", interfaceID, err) - } - - if err := renderer.Render(lbtarget); err != nil { - return fmt.Errorf("error rendering loadbalancer target: %w", err) - } + if err := renderer.Render(lbtarget); err != nil { + return fmt.Errorf("error rendering loadbalancer target: %w", err) } + return nil } diff --git a/cmd/get_nat.go b/cmd/get_nat.go index 5e69855..41697d5 100644 --- a/cmd/get_nat.go +++ b/cmd/get_nat.go @@ -30,18 +30,17 @@ func GetNat(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory ) cmd := &cobra.Command{ - Use: "nat [...]", - Short: "Get or list nat(s)", - Example: "dpservice-cli get nat vm1", + Use: "nat <--interface-id>", + Short: "Get NAT on interface", + Example: "dpservice-cli get nat --interface-id=vm1", Aliases: NatAliases, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - interfaceIDs := args + return RunGetNat( cmd.Context(), dpdkClientFactory, rendererFactory, - interfaceIDs, opts, ) }, @@ -55,12 +54,19 @@ func GetNat(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory } type GetNatOptions struct { + InterfaceID string } func (o *GetNatOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID of the NAT.") } 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 } @@ -68,7 +74,6 @@ func RunGetNat( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - interfaceIDs []string, opts GetNatOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) @@ -86,19 +91,14 @@ func RunGetNat( return fmt.Errorf("error creating renderer: %w", err) } - if len(interfaceIDs) == 0 { - return fmt.Errorf("listing all nats is not implemented") + nat, err := client.GetNat(ctx, opts.InterfaceID) + if err != nil { + return fmt.Errorf("error getting nat for interface %s: %v", opts.InterfaceID, err) } - for _, interfaceID := range interfaceIDs { - nat, err := client.GetNat(ctx, interfaceID) - if err != nil { - return fmt.Errorf("error getting nat for interface %s: %v", interfaceID, err) - } - - if err := renderer.Render(nat); err != nil { - return fmt.Errorf("error rendering nat: %w", err) - } + if err := renderer.Render(nat); err != nil { + return fmt.Errorf("error rendering nat: %w", err) } + return nil } diff --git a/cmd/get_nat_info.go b/cmd/get_nat_info.go index 3289ad4..46ae3cc 100644 --- a/cmd/get_nat_info.go +++ b/cmd/get_nat_info.go @@ -20,6 +20,7 @@ import ( "net/netip" "os" + "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -31,21 +32,16 @@ func GetNatInfo(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFac ) cmd := &cobra.Command{ - Use: "natinfo <--nat-type>", - Short: "List all local machines that are behind this IP", - Example: "dpservice-cli get natinfo 10.20.30.40 --nat-type=1", - Args: cobra.ExactArgs(1), + Use: "natinfo <--nat-ip> <--nat-type>", + Short: "List all machines that are behind this IP", + Example: "dpservice-cli get natinfo --nat-ip=10.20.30.40 --nat-type=1", + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - natVIPIP, err := netip.ParseAddr(args[0]) - if err != nil { - return fmt.Errorf("error parsing nat vip ip: %w", err) - } return RunGetNatInfo( cmd.Context(), dpdkClientFactory, rendererFactory, - natVIPIP, opts, ) }, @@ -59,15 +55,17 @@ func GetNatInfo(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFac } type GetNatInfoOptions struct { + NatIP netip.Addr NatType int32 } func (o *GetNatInfoOptions) AddFlags(fs *pflag.FlagSet) { + flag.AddrVar(fs, &o.NatIP, "nat-ip", o.NatIP, "NAT IP to get info for") fs.Int32Var(&o.NatType, "nat-type", o.NatType, "NAT Info type: NATInfoTypeZero = 0/NATInfoLocal = 1/NATInfoNeigh = 2") } func (o *GetNatInfoOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"nat-type"} { + for _, name := range []string{"nat-ip", "nat-type"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -79,7 +77,6 @@ func RunGetNatInfo( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - natVIPIP netip.Addr, opts GetNatInfoOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) @@ -97,9 +94,9 @@ func RunGetNatInfo( return fmt.Errorf("error creating renderer: %w", err) } - natinfo, err := client.GetNATInfo(ctx, natVIPIP, opts.NatType) + natinfo, err := client.GetNATInfo(ctx, opts.NatIP, opts.NatType) if err != nil { - return fmt.Errorf("error getting nat info for interface %s: %v", natVIPIP, err) + return fmt.Errorf("error getting nat info for ip %s: %v", opts.NatIP, err) } if err := renderer.Render(natinfo); err != nil { diff --git a/cmd/get_virtualip.go b/cmd/get_virtualip.go index 84e4160..b9d1b9e 100644 --- a/cmd/get_virtualip.go +++ b/cmd/get_virtualip.go @@ -30,18 +30,17 @@ func GetVirtualIP(dpdkClientFactory DPDKClientFactory, rendererFactory RendererF ) cmd := &cobra.Command{ - Use: "virtualip [...]", - Short: "Get or list virtualip(s)", - Example: "dpservice-cli get virtualip vm1", + Use: "virtualip <--interface-id>", + Short: "Get Virtual IP on interface", + Example: "dpservice-cli get virtualip --interface-id=vm1", Aliases: VirtualIPAliases, - Args: cobra.MinimumNArgs(1), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - interfaceIDs := args + return RunGetVirtualIP( cmd.Context(), dpdkClientFactory, rendererFactory, - interfaceIDs, opts, ) }, @@ -55,12 +54,19 @@ func GetVirtualIP(dpdkClientFactory DPDKClientFactory, rendererFactory RendererF } type GetVirtualIPOptions struct { + InterfaceID string } func (o *GetVirtualIPOptions) AddFlags(fs *pflag.FlagSet) { + fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID of the Virtual IP.") } func (o *GetVirtualIPOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"interface-id"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } return nil } @@ -68,7 +74,6 @@ func RunGetVirtualIP( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - interfaceIDs []string, opts GetVirtualIPOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) @@ -86,19 +91,13 @@ func RunGetVirtualIP( return fmt.Errorf("error creating renderer: %w", err) } - if len(interfaceIDs) == 0 { - return fmt.Errorf("listing virtual ips is not implemented") + virtualIP, err := client.GetVirtualIP(ctx, opts.InterfaceID) + if err != nil { + return fmt.Errorf("error getting virtual ip for interface %s: %v", opts.InterfaceID, err) } - for _, interfaceID := range interfaceIDs { - virtualIP, err := client.GetVirtualIP(ctx, interfaceID) - if err != nil { - return fmt.Errorf("error getting virtual ip for interface %s: %v", interfaceID, err) - } - - if err := renderer.Render(virtualIP); err != nil { - return fmt.Errorf("error rendering virtual ip: %w", err) - } + if err := renderer.Render(virtualIP); err != nil { + return fmt.Errorf("error rendering virtual ip: %w", err) } return nil } From 6b2f0ba80a63425dfda9ea3515fcbde81aa8908b Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Wed, 17 May 2023 12:19:52 +0200 Subject: [PATCH 32/65] add docs folder and update of README.md --- README.md | 33 +++++++++-- docs/dpservice-cli.md | 27 +++++++++ docs/dpservice-cli_add.md | 43 ++++++++++++++ docs/dpservice-cli_add_firewallrule.md | 49 ++++++++++++++++ docs/dpservice-cli_add_interface.md | 38 +++++++++++++ docs/dpservice-cli_add_lbprefix.md | 36 ++++++++++++ docs/dpservice-cli_add_lbtarget.md | 36 ++++++++++++ docs/dpservice-cli_add_loadbalancer.md | 38 +++++++++++++ docs/dpservice-cli_add_nat.md | 38 +++++++++++++ docs/dpservice-cli_add_neighbornat.md | 39 +++++++++++++ docs/dpservice-cli_add_prefix.md | 36 ++++++++++++ docs/dpservice-cli_add_route.md | 38 +++++++++++++ docs/dpservice-cli_add_virtualip.md | 36 ++++++++++++ docs/dpservice-cli_completion.md | 68 +++++++++++++++++++++++ docs/dpservice-cli_delete.md | 41 ++++++++++++++ docs/dpservice-cli_delete_firewallrule.md | 34 ++++++++++++ docs/dpservice-cli_delete_interface.md | 33 +++++++++++ docs/dpservice-cli_delete_lbprefix.md | 34 ++++++++++++ docs/dpservice-cli_delete_lbtarget.md | 34 ++++++++++++ docs/dpservice-cli_delete_loadbalancer.md | 33 +++++++++++ docs/dpservice-cli_delete_nat.md | 33 +++++++++++ docs/dpservice-cli_delete_neighbornat.md | 36 ++++++++++++ docs/dpservice-cli_delete_prefix.md | 34 ++++++++++++ docs/dpservice-cli_delete_route.md | 36 ++++++++++++ docs/dpservice-cli_delete_virtualip.md | 33 +++++++++++ docs/dpservice-cli_get.md | 39 +++++++++++++ docs/dpservice-cli_get_firewallrule.md | 36 ++++++++++++ docs/dpservice-cli_get_interface.md | 35 ++++++++++++ docs/dpservice-cli_get_lbtarget.md | 35 ++++++++++++ docs/dpservice-cli_get_loadbalancer.md | 35 ++++++++++++ docs/dpservice-cli_get_nat.md | 35 ++++++++++++ docs/dpservice-cli_get_natinfo.md | 36 ++++++++++++ docs/dpservice-cli_get_virtualip.md | 35 ++++++++++++ docs/dpservice-cli_init.md | 32 +++++++++++ docs/dpservice-cli_initialized.md | 32 +++++++++++ docs/dpservice-cli_list.md | 37 ++++++++++++ docs/dpservice-cli_list_firewallrules.md | 35 ++++++++++++ docs/dpservice-cli_list_interfaces.md | 34 ++++++++++++ docs/dpservice-cli_list_lbprefixes.md | 35 ++++++++++++ docs/dpservice-cli_list_prefixes.md | 35 ++++++++++++ docs/dpservice-cli_list_routes.md | 35 ++++++++++++ go.mod | 2 + go.sum | 2 + 43 files changed, 1496 insertions(+), 5 deletions(-) create mode 100644 docs/dpservice-cli.md create mode 100644 docs/dpservice-cli_add.md create mode 100644 docs/dpservice-cli_add_firewallrule.md create mode 100644 docs/dpservice-cli_add_interface.md create mode 100644 docs/dpservice-cli_add_lbprefix.md create mode 100644 docs/dpservice-cli_add_lbtarget.md create mode 100644 docs/dpservice-cli_add_loadbalancer.md create mode 100644 docs/dpservice-cli_add_nat.md create mode 100644 docs/dpservice-cli_add_neighbornat.md create mode 100644 docs/dpservice-cli_add_prefix.md create mode 100644 docs/dpservice-cli_add_route.md create mode 100644 docs/dpservice-cli_add_virtualip.md create mode 100644 docs/dpservice-cli_completion.md create mode 100644 docs/dpservice-cli_delete.md create mode 100644 docs/dpservice-cli_delete_firewallrule.md create mode 100644 docs/dpservice-cli_delete_interface.md create mode 100644 docs/dpservice-cli_delete_lbprefix.md create mode 100644 docs/dpservice-cli_delete_lbtarget.md create mode 100644 docs/dpservice-cli_delete_loadbalancer.md create mode 100644 docs/dpservice-cli_delete_nat.md create mode 100644 docs/dpservice-cli_delete_neighbornat.md create mode 100644 docs/dpservice-cli_delete_prefix.md create mode 100644 docs/dpservice-cli_delete_route.md create mode 100644 docs/dpservice-cli_delete_virtualip.md create mode 100644 docs/dpservice-cli_get.md create mode 100644 docs/dpservice-cli_get_firewallrule.md create mode 100644 docs/dpservice-cli_get_interface.md create mode 100644 docs/dpservice-cli_get_lbtarget.md create mode 100644 docs/dpservice-cli_get_loadbalancer.md create mode 100644 docs/dpservice-cli_get_nat.md create mode 100644 docs/dpservice-cli_get_natinfo.md create mode 100644 docs/dpservice-cli_get_virtualip.md create mode 100644 docs/dpservice-cli_init.md create mode 100644 docs/dpservice-cli_initialized.md create mode 100644 docs/dpservice-cli_list.md create mode 100644 docs/dpservice-cli_list_firewallrules.md create mode 100644 docs/dpservice-cli_list_interfaces.md create mode 100644 docs/dpservice-cli_list_lbprefixes.md create mode 100644 docs/dpservice-cli_list_prefixes.md create mode 100644 docs/dpservice-cli_list_routes.md diff --git a/README.md b/README.md index c0056a5..f28b480 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # dpservice-cli CLI for [net-dpservice](https://github.com/onmetal/net-dpservice). +
## Installation @@ -18,6 +19,22 @@ run ```shell make install ``` +
+ +## Autocompletion + +To generate autocompletion use: + +```shell +dpservice-cli completion [bash|zsh|fish|powershell] +``` + +Or use -h to get more info and examples for specific shell: + +```shell +dpservice-cli completion -h +``` +
## Usage @@ -27,11 +44,14 @@ Usage: dpservice-cli [command] Available Commands: - completion Generate the autocompletion script for the specified shell - create Creates one of [interface prefix route virtualip] - delete Deletes one of [interface prefix route virtualip] - get Gets/Lists one of [interface prefix route virtualip] + add Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + completion Generate completion script + delete Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + get Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] help Help about any command + init Initial set up of the DPDK app + initialized Indicates if the DPDK app has been initialized already + list Lists one of [firewallrules interfaces prefixes lbprefixes routes] Flags: --address string net-dpservice address. (default "localhost:1337") @@ -39,4 +59,7 @@ Flags: -h, --help help for dpservice-cli Use "dpservice-cli [command] --help" for more information about a command. -``` \ No newline at end of file +``` +All commands are in [docs](/docs/dpservice-cli.md) + +
diff --git a/docs/dpservice-cli.md b/docs/dpservice-cli.md new file mode 100644 index 0000000..b456396 --- /dev/null +++ b/docs/dpservice-cli.md @@ -0,0 +1,27 @@ +## dpservice-cli + + + +``` +dpservice-cli [flags] +``` + +### Options + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -h, --help help for dpservice-cli +``` + +### SEE ALSO + +* [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] +* [dpservice-cli completion](dpservice-cli_completion.md) - Generate completion script +* [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] +* [dpservice-cli init](dpservice-cli_init.md) - Initial set up of the DPDK app +* [dpservice-cli initialized](dpservice-cli_initialized.md) - Indicates if the DPDK app has been initialized already +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_add.md b/docs/dpservice-cli_add.md new file mode 100644 index 0000000..4cc831c --- /dev/null +++ b/docs/dpservice-cli_add.md @@ -0,0 +1,43 @@ +## dpservice-cli add + +Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +### Synopsis + +Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +``` +dpservice-cli add [flags] +``` + +### Options + +``` + -f, --filename strings Filename, directory, or URL to file to use to create the resource + -h, --help help for add + -o, --output string Output format. (default "name") + --pretty Whether to render pretty output. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli](dpservice-cli.md) - +* [dpservice-cli add firewallrule](dpservice-cli_add_firewallrule.md) - Add a FirewallRule to interface +* [dpservice-cli add interface](dpservice-cli_add_interface.md) - Create an interface +* [dpservice-cli add lbprefix](dpservice-cli_add_lbprefix.md) - Create a loadbalancer prefix +* [dpservice-cli add lbtarget](dpservice-cli_add_lbtarget.md) - Add a loadbalancer target +* [dpservice-cli add loadbalancer](dpservice-cli_add_loadbalancer.md) - Create a loadbalancer +* [dpservice-cli add nat](dpservice-cli_add_nat.md) - Add a NAT to interface +* [dpservice-cli add neighbornat](dpservice-cli_add_neighbornat.md) - Add a Neighbor NAT +* [dpservice-cli add prefix](dpservice-cli_add_prefix.md) - Add a prefix to interface. +* [dpservice-cli add route](dpservice-cli_add_route.md) - Add a route +* [dpservice-cli add virtualip](dpservice-cli_add_virtualip.md) - Add a virtual IP to interface. + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_add_firewallrule.md b/docs/dpservice-cli_add_firewallrule.md new file mode 100644 index 0000000..6a9c0cf --- /dev/null +++ b/docs/dpservice-cli_add_firewallrule.md @@ -0,0 +1,49 @@ +## dpservice-cli add firewallrule + +Add a FirewallRule to interface + +``` +dpservice-cli add firewallrule <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli add fwrule --interface-id=vm1 --action=1 --direction=1 --dst=5.5.5.0/24 --ipv=0 --priority=100 --rule-id=12 --src=1.1.1.1/32 --protocol=tcp --srcPortLower=1 --srcPortUpper=1000 --dstPortLower=500 --dstPortUpper=600 +``` + +### Options + +``` + --action uint8 Firewall action: Drop = 0/Accept = 1 // Can be only "accept" at the moment. + --direction uint8 Traffic direction of FW Rule: Ingress = 0/Egress = 1 + --dst ipprefix Destination prefix // 0.0.0.0 with prefix length 0 matches all destination IPs. (default invalid Prefix) + --dstPortLower int32 Destination Ports start // -1 matches all destination ports. + --dstPortUpper int32 Destination Ports end. + -h, --help help for firewallrule + --icmpCode int32 ICMP code // -1 matches all ICMP Codes. + --icmpType int32 ICMP type // -1 matches all ICMP Types. + --interface-id string InterfaceID of FW Rule. + --ipv uint8 IpVersion of FW Rule IPv4 = 0/IPv6 = 1. + --priority uint32 Priority of FW Rule. // For future use. No effect at the moment. + --protocol string Protocol used icmp/tcp/udp // Not defining a protocol filter matches all protocols. + --rule-id string RuleID of FW Rule. + --src ipprefix Source prefix // 0.0.0.0 with prefix length 0 matches all source IPs. (default invalid Prefix) + --srcPortLower int32 Source Ports start // -1 matches all source ports. + --srcPortUpper int32 Source Ports end. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. (default "name") + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_add_interface.md b/docs/dpservice-cli_add_interface.md new file mode 100644 index 0000000..1e6c5bd --- /dev/null +++ b/docs/dpservice-cli_add_interface.md @@ -0,0 +1,38 @@ +## dpservice-cli add interface + +Create an interface + +``` +dpservice-cli add interface <--id> [<--ip>] <--vni> <--device> [flags] +``` + +### Examples + +``` +dpservice-cli add interface --id=vm4 --ip=10.200.1.4 --ip=2000:200:1::4 --vni=200 --device=net_tap5 +``` + +### Options + +``` + --device string Device to allocate. + -h, --help help for interface + --id string ID of the interface. + --ip addrSlice IP to assign to the interface. (default []) + --vni uint32 VNI to add the interface to. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. (default "name") + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_add_lbprefix.md b/docs/dpservice-cli_add_lbprefix.md new file mode 100644 index 0000000..e54d83f --- /dev/null +++ b/docs/dpservice-cli_add_lbprefix.md @@ -0,0 +1,36 @@ +## dpservice-cli add lbprefix + +Create a loadbalancer prefix + +``` +dpservice-cli add lbprefix <--prefix> <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli add lbprefix --prefix=10.10.10.0/24 --interface-id=vm1 +``` + +### Options + +``` + -h, --help help for lbprefix + --interface-id string ID of the interface to create the prefix for. + --prefix ipprefix Prefix to add to the interface. (default invalid Prefix) +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. (default "name") + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_add_lbtarget.md b/docs/dpservice-cli_add_lbtarget.md new file mode 100644 index 0000000..e704773 --- /dev/null +++ b/docs/dpservice-cli_add_lbtarget.md @@ -0,0 +1,36 @@ +## dpservice-cli add lbtarget + +Add a loadbalancer target + +``` +dpservice-cli add lbtarget <--lb-id> [flags] +``` + +### Examples + +``` +dpservice-cli add lbtarget --target-ip=ff80::5 --lb-id=2 +``` + +### Options + +``` + -h, --help help for lbtarget + --lb-id string ID of the loadbalancer to add the target for. + --target-ip ip Loadbalancer Target IP. (default invalid IP) +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. (default "name") + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_add_loadbalancer.md b/docs/dpservice-cli_add_loadbalancer.md new file mode 100644 index 0000000..92d7d15 --- /dev/null +++ b/docs/dpservice-cli_add_loadbalancer.md @@ -0,0 +1,38 @@ +## dpservice-cli add loadbalancer + +Create a loadbalancer + +``` +dpservice-cli add loadbalancer <--id> <--vni> <--vip> <--lbports> [flags] +``` + +### Examples + +``` +dpservice-cli create lb --id=4 --vni=100 --vip=10.20.30.40 --lbports=TCP/443,UDP/53 +``` + +### Options + +``` + -h, --help help for loadbalancer + --id string Loadbalancer ID to add. + --lbports strings LB ports to assign to the loadbalancer. + --vip ip VIP to assign to the loadbalancer. (default invalid IP) + --vni uint32 VNI to add the loadbalancer to. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. (default "name") + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_add_nat.md b/docs/dpservice-cli_add_nat.md new file mode 100644 index 0000000..27439a2 --- /dev/null +++ b/docs/dpservice-cli_add_nat.md @@ -0,0 +1,38 @@ +## dpservice-cli add nat + +Add a NAT to interface + +``` +dpservice-cli add nat <--natip> <--minport> <--maxport> [flags] +``` + +### Examples + +``` +dpservice-cli add nat --interface-id=vm1 --natip=10.20.30.40 --minport=30000 --maxport=30100 +``` + +### Options + +``` + -h, --help help for nat + --interface-id string Interface ID where to add NAT. + --maxport uint32 MaxPort of NAT. + --minport uint32 MinPort of NAT. + --natip ip NAT IP to assign to the interface. (default invalid IP) +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. (default "name") + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_add_neighbornat.md b/docs/dpservice-cli_add_neighbornat.md new file mode 100644 index 0000000..bbfcf78 --- /dev/null +++ b/docs/dpservice-cli_add_neighbornat.md @@ -0,0 +1,39 @@ +## dpservice-cli add neighbornat + +Add a Neighbor NAT + +``` +dpservice-cli add neighbornat <--natip> <--vni> <--minport> <--maxport> <--underlayroute> [flags] +``` + +### Examples + +``` +dpservice-cli add neighbornat --natip=10.20.30.40 --vni=100 --minport=30000 --maxport=30100 --underlayroute=ff80::1 +``` + +### Options + +``` + -h, --help help for neighbornat + --maxport uint32 MaxPort of neighbor NAT. + --minport uint32 MinPort of neighbor NAT. + --natip ip Neighbor NAT IP. (default invalid IP) + --underlayroute ip Underlay route of neighbor NAT. (default invalid IP) + --vni uint32 VNI of neighbor NAT. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. (default "name") + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_add_prefix.md b/docs/dpservice-cli_add_prefix.md new file mode 100644 index 0000000..cd05a61 --- /dev/null +++ b/docs/dpservice-cli_add_prefix.md @@ -0,0 +1,36 @@ +## dpservice-cli add prefix + +Add a prefix to interface. + +``` +dpservice-cli add prefix <--prefix> <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli add prefix --prefix=10.20.30.0/24 --interface-id=vm1 +``` + +### Options + +``` + -h, --help help for prefix + --interface-id string ID of the interface to add the prefix for. + --prefix ipprefix Prefix to add to the interface. (default invalid Prefix) +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. (default "name") + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_add_route.md b/docs/dpservice-cli_add_route.md new file mode 100644 index 0000000..91cf4db --- /dev/null +++ b/docs/dpservice-cli_add_route.md @@ -0,0 +1,38 @@ +## dpservice-cli add route + +Add a route + +``` +dpservice-cli add route <--prefix> <--next-hop-vni> <--next-hop-ip> <--vni> [flags] +``` + +### Examples + +``` +dpservice-cli add route --prefix=10.100.3.0/24 --next-hop-vni=0 --next-hop-ip=fc00:2::64:0:1 --vni=100 +``` + +### Options + +``` + -h, --help help for route + --next-hop-ip ip Next hop IP for the route. (default invalid IP) + --next-hop-vni uint32 Next hop VNI for the route. + --prefix ipprefix Prefix for the route. (default invalid Prefix) + --vni uint32 Source VNI for the route. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. (default "name") + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_add_virtualip.md b/docs/dpservice-cli_add_virtualip.md new file mode 100644 index 0000000..38ae9f0 --- /dev/null +++ b/docs/dpservice-cli_add_virtualip.md @@ -0,0 +1,36 @@ +## dpservice-cli add virtualip + +Add a virtual IP to interface. + +``` +dpservice-cli add virtualip <--vip> <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli add virtualip --vip=20.20.20.20 --interface-id=vm1 +``` + +### Options + +``` + -h, --help help for virtualip + --interface-id string Interface ID to add the virtual ip for. + --vip ip Virtual IP to add on interface. (default invalid IP) +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. (default "name") + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_completion.md b/docs/dpservice-cli_completion.md new file mode 100644 index 0000000..9a36414 --- /dev/null +++ b/docs/dpservice-cli_completion.md @@ -0,0 +1,68 @@ +## dpservice-cli completion + +Generate completion script + +### Synopsis + +To load completions: + +Bash: + + $ source <(dpservice-cli completion bash) + + # To load completions for each session, execute once: + # Linux: + $ dpservice-cli completion bash > /etc/bash_completion.d/dpservice-cli + # macOS: + $ dpservice-cli completion bash > $(brew --prefix)/etc/bash_completion.d/dpservice-cli + +Zsh: + + # If shell completion is not already enabled in your environment, + # you will need to enable it. You can execute the following once: + + $ echo "autoload -U compinit; compinit" >> ~/.zshrc + + # To load completions for each session, execute once: + $ dpservice-cli completion zsh > "${fpath[1]}/_dpservice-cli" + + # You will need to start a new shell for this setup to take effect. + +fish: + + $ dpservice-cli completion fish | source + + # To load completions for each session, execute once: + $ dpservice-cli completion fish > ~/.config/fish/completions/dpservice-cli.fish + +PowerShell: + + PS> dpservice-cli completion powershell | Out-String | Invoke-Expression + + # To load completions for every new session, run: + PS> dpservice-cli completion powershell > dpservice-cli.ps1 + # and source this file from your PowerShell profile. + + +``` +dpservice-cli completion [bash|zsh|fish|powershell] +``` + +### Options + +``` + -h, --help help for completion +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli](dpservice-cli.md) - + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_delete.md b/docs/dpservice-cli_delete.md new file mode 100644 index 0000000..cfa1cf4 --- /dev/null +++ b/docs/dpservice-cli_delete.md @@ -0,0 +1,41 @@ +## dpservice-cli delete + +Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +### Synopsis + +Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +``` +dpservice-cli delete [flags] +``` + +### Options + +``` + -f, --filename strings Filename, directory, or URL to file to use to create the resource + -h, --help help for delete +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli](dpservice-cli.md) - +* [dpservice-cli delete firewallrule](dpservice-cli_delete_firewallrule.md) - Delete firewall rule from interface +* [dpservice-cli delete interface](dpservice-cli_delete_interface.md) - Delete interface +* [dpservice-cli delete lbprefix](dpservice-cli_delete_lbprefix.md) - Delete a loadbalancer prefix +* [dpservice-cli delete lbtarget](dpservice-cli_delete_lbtarget.md) - Delete a loadbalancer target +* [dpservice-cli delete loadbalancer](dpservice-cli_delete_loadbalancer.md) - Delete loadbalancer +* [dpservice-cli delete nat](dpservice-cli_delete_nat.md) - Delete nat from interface +* [dpservice-cli delete neighbornat](dpservice-cli_delete_neighbornat.md) - Delete neighbor nat +* [dpservice-cli delete prefix](dpservice-cli_delete_prefix.md) - Delete a prefix +* [dpservice-cli delete route](dpservice-cli_delete_route.md) - Delete a route +* [dpservice-cli delete virtualip](dpservice-cli_delete_virtualip.md) - Delete virtual IP from interface + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_delete_firewallrule.md b/docs/dpservice-cli_delete_firewallrule.md new file mode 100644 index 0000000..76a5553 --- /dev/null +++ b/docs/dpservice-cli_delete_firewallrule.md @@ -0,0 +1,34 @@ +## dpservice-cli delete firewallrule + +Delete firewall rule from interface + +``` +dpservice-cli delete firewallrule <--rule-id> <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli delete firewallrule --rule-id=1 --interface-id=vm1 +``` + +### Options + +``` + -h, --help help for firewallrule + --interface-id string Intreface ID where to delete firewall rule. + --rule-id string Rule ID to delete. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_delete_interface.md b/docs/dpservice-cli_delete_interface.md new file mode 100644 index 0000000..2f52f3a --- /dev/null +++ b/docs/dpservice-cli_delete_interface.md @@ -0,0 +1,33 @@ +## dpservice-cli delete interface + +Delete interface + +``` +dpservice-cli delete interface <--id> [flags] +``` + +### Examples + +``` +dpservice-cli delete interface --id=vm1 +``` + +### Options + +``` + -h, --help help for interface + --id string Interface ID to delete. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_delete_lbprefix.md b/docs/dpservice-cli_delete_lbprefix.md new file mode 100644 index 0000000..66d700d --- /dev/null +++ b/docs/dpservice-cli_delete_lbprefix.md @@ -0,0 +1,34 @@ +## dpservice-cli delete lbprefix + +Delete a loadbalancer prefix + +``` +dpservice-cli delete lbprefix <--prefix> <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli delete lbprefix --prefix=ff80::1/64 --interface-id=vm1 +``` + +### Options + +``` + -h, --help help for lbprefix + --interface-id string Interface ID of the loadbalancer prefix. + --prefix ipprefix Loadbalancer prefix to delete. (default invalid Prefix) +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_delete_lbtarget.md b/docs/dpservice-cli_delete_lbtarget.md new file mode 100644 index 0000000..43ab24f --- /dev/null +++ b/docs/dpservice-cli_delete_lbtarget.md @@ -0,0 +1,34 @@ +## dpservice-cli delete lbtarget + +Delete a loadbalancer target + +``` +dpservice-cli delete lbtarget <--target-ip> <--lb-id> [flags] +``` + +### Examples + +``` +dpservice-cli delete lbtarget --target-ip=ff80::1 --lb-id=1 +``` + +### Options + +``` + -h, --help help for lbtarget + --lb-id string LoadBalancerID where to delete target. + --target-ip ip LoadBalancer target IP to delete. (default invalid IP) +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_delete_loadbalancer.md b/docs/dpservice-cli_delete_loadbalancer.md new file mode 100644 index 0000000..28ad1f3 --- /dev/null +++ b/docs/dpservice-cli_delete_loadbalancer.md @@ -0,0 +1,33 @@ +## dpservice-cli delete loadbalancer + +Delete loadbalancer + +``` +dpservice-cli delete loadbalancer <--id> [flags] +``` + +### Examples + +``` +dpservice-cli delete loadbalancer --id=1 +``` + +### Options + +``` + -h, --help help for loadbalancer + --id string LoadBalancer ID to delete. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_delete_nat.md b/docs/dpservice-cli_delete_nat.md new file mode 100644 index 0000000..09585c6 --- /dev/null +++ b/docs/dpservice-cli_delete_nat.md @@ -0,0 +1,33 @@ +## dpservice-cli delete nat + +Delete nat from interface + +``` +dpservice-cli delete nat <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli delete nat --interface-id=vm1 +``` + +### Options + +``` + -h, --help help for nat + --interface-id string Interface ID of the Virtual IP. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_delete_neighbornat.md b/docs/dpservice-cli_delete_neighbornat.md new file mode 100644 index 0000000..0cc4c21 --- /dev/null +++ b/docs/dpservice-cli_delete_neighbornat.md @@ -0,0 +1,36 @@ +## dpservice-cli delete neighbornat + +Delete neighbor nat + +``` +dpservice-cli delete neighbornat <--natip> <--vni> <--minport> <--maxport> [flags] +``` + +### Examples + +``` +dpservice-cli delete neighbornat --natip=10.20.30.40 --vni=100 --minport=30000 --maxport=30100 +``` + +### Options + +``` + -h, --help help for neighbornat + --maxport uint32 MaxPort of neighbor NAT. + --minport uint32 MinPort of neighbor NAT. + --natip ip Neighbor NAT IP. (default invalid IP) + --vni uint32 VNI of neighbor NAT. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_delete_prefix.md b/docs/dpservice-cli_delete_prefix.md new file mode 100644 index 0000000..b90c1d5 --- /dev/null +++ b/docs/dpservice-cli_delete_prefix.md @@ -0,0 +1,34 @@ +## dpservice-cli delete prefix + +Delete a prefix + +``` +dpservice-cli delete prefix <--prefix> <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli delete prefix --prefix=10.20.30.0/24 --interface-id=vm1 +``` + +### Options + +``` + -h, --help help for prefix + --interface-id string Interface ID of the prefix. + --prefix ipprefix Prefix to delete. (default invalid Prefix) +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_delete_route.md b/docs/dpservice-cli_delete_route.md new file mode 100644 index 0000000..71d8889 --- /dev/null +++ b/docs/dpservice-cli_delete_route.md @@ -0,0 +1,36 @@ +## dpservice-cli delete route + +Delete a route + +``` +dpservice-cli delete route <--prefix> <--next-hop-vni> <--next-hop-ip> <--vni> [flags] +``` + +### Examples + +``` +dpservice-cli delete route --prefix=10.100.2.0/24 --next-hop-vni=0 --next-hop-ip=fc00:2::64:0:1 --vni=100 +``` + +### Options + +``` + -h, --help help for route + --next-hop-ip ip Next hop IP of the route. (default invalid IP) + --next-hop-vni uint32 Next hop VNI of the route. + --prefix ipprefix Prefix of the route. (default invalid Prefix) + --vni uint32 VNI of the route. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_delete_virtualip.md b/docs/dpservice-cli_delete_virtualip.md new file mode 100644 index 0000000..9920f63 --- /dev/null +++ b/docs/dpservice-cli_delete_virtualip.md @@ -0,0 +1,33 @@ +## dpservice-cli delete virtualip + +Delete virtual IP from interface + +``` +dpservice-cli delete virtualip <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli delete virtualip --interface-id=vm1 +``` + +### Options + +``` + -h, --help help for virtualip + --interface-id string Interface ID of the Virtual IP. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_get.md b/docs/dpservice-cli_get.md new file mode 100644 index 0000000..8cacf5a --- /dev/null +++ b/docs/dpservice-cli_get.md @@ -0,0 +1,39 @@ +## dpservice-cli get + +Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] + +### Synopsis + +Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] + +``` +dpservice-cli get [flags] +``` + +### Options + +``` + -h, --help help for get + -o, --output string Output format. + --pretty Whether to render pretty output. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli](dpservice-cli.md) - +* [dpservice-cli get firewallrule](dpservice-cli_get_firewallrule.md) - Get firewall rule +* [dpservice-cli get interface](dpservice-cli_get_interface.md) - Get interface +* [dpservice-cli get lbtarget](dpservice-cli_get_lbtarget.md) - Get LoadBalancer Targets +* [dpservice-cli get loadbalancer](dpservice-cli_get_loadbalancer.md) - Get loadbalancer +* [dpservice-cli get nat](dpservice-cli_get_nat.md) - Get NAT on interface +* [dpservice-cli get natinfo](dpservice-cli_get_natinfo.md) - List all machines that are behind this IP +* [dpservice-cli get virtualip](dpservice-cli_get_virtualip.md) - Get Virtual IP on interface + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_get_firewallrule.md b/docs/dpservice-cli_get_firewallrule.md new file mode 100644 index 0000000..84e11ae --- /dev/null +++ b/docs/dpservice-cli_get_firewallrule.md @@ -0,0 +1,36 @@ +## dpservice-cli get firewallrule + +Get firewall rule + +``` +dpservice-cli get firewallrule <--rule-id> <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli get fwrule --rule-id=1 --interface-id=vm1 +``` + +### Options + +``` + -h, --help help for firewallrule + --interface-id string Interface ID where is firewall rule. + --rule-id string Rule ID to get. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_get_interface.md b/docs/dpservice-cli_get_interface.md new file mode 100644 index 0000000..c6fc160 --- /dev/null +++ b/docs/dpservice-cli_get_interface.md @@ -0,0 +1,35 @@ +## dpservice-cli get interface + +Get interface + +``` +dpservice-cli get interface <--id> [flags] +``` + +### Examples + +``` +dpservice-cli get interface --id=vm1 +``` + +### Options + +``` + -h, --help help for interface + --id string ID of the interface. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_get_lbtarget.md b/docs/dpservice-cli_get_lbtarget.md new file mode 100644 index 0000000..29a3e64 --- /dev/null +++ b/docs/dpservice-cli_get_lbtarget.md @@ -0,0 +1,35 @@ +## dpservice-cli get lbtarget + +Get LoadBalancer Targets + +``` +dpservice-cli get lbtarget <--lb-id> [flags] +``` + +### Examples + +``` +dpservice-cli get lbtarget --lb-id=1 +``` + +### Options + +``` + -h, --help help for lbtarget + --lb-id string ID of the loadbalancer to get the targets for. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_get_loadbalancer.md b/docs/dpservice-cli_get_loadbalancer.md new file mode 100644 index 0000000..5216938 --- /dev/null +++ b/docs/dpservice-cli_get_loadbalancer.md @@ -0,0 +1,35 @@ +## dpservice-cli get loadbalancer + +Get loadbalancer + +``` +dpservice-cli get loadbalancer <--id> [flags] +``` + +### Examples + +``` +dpservice-cli get loadbalancer --id=4 +``` + +### Options + +``` + -h, --help help for loadbalancer + --id string ID of the LoadBalancer. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_get_nat.md b/docs/dpservice-cli_get_nat.md new file mode 100644 index 0000000..4fc8d10 --- /dev/null +++ b/docs/dpservice-cli_get_nat.md @@ -0,0 +1,35 @@ +## dpservice-cli get nat + +Get NAT on interface + +``` +dpservice-cli get nat <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli get nat --interface-id=vm1 +``` + +### Options + +``` + -h, --help help for nat + --interface-id string Interface ID of the NAT. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_get_natinfo.md b/docs/dpservice-cli_get_natinfo.md new file mode 100644 index 0000000..459da64 --- /dev/null +++ b/docs/dpservice-cli_get_natinfo.md @@ -0,0 +1,36 @@ +## dpservice-cli get natinfo + +List all machines that are behind this IP + +``` +dpservice-cli get natinfo <--nat-ip> <--nat-type> [flags] +``` + +### Examples + +``` +dpservice-cli get natinfo --nat-ip=10.20.30.40 --nat-type=1 +``` + +### Options + +``` + -h, --help help for natinfo + --nat-ip ip NAT IP to get info for (default invalid IP) + --nat-type int32 NAT Info type: NATInfoTypeZero = 0/NATInfoLocal = 1/NATInfoNeigh = 2 +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_get_virtualip.md b/docs/dpservice-cli_get_virtualip.md new file mode 100644 index 0000000..17ec2d7 --- /dev/null +++ b/docs/dpservice-cli_get_virtualip.md @@ -0,0 +1,35 @@ +## dpservice-cli get virtualip + +Get Virtual IP on interface + +``` +dpservice-cli get virtualip <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli get virtualip --interface-id=vm1 +``` + +### Options + +``` + -h, --help help for virtualip + --interface-id string Interface ID of the Virtual IP. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_init.md b/docs/dpservice-cli_init.md new file mode 100644 index 0000000..efc1a0f --- /dev/null +++ b/docs/dpservice-cli_init.md @@ -0,0 +1,32 @@ +## dpservice-cli init + +Initial set up of the DPDK app + +``` +dpservice-cli init [flags] +``` + +### Examples + +``` +dpservice-cli init +``` + +### Options + +``` + -h, --help help for init +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli](dpservice-cli.md) - + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_initialized.md b/docs/dpservice-cli_initialized.md new file mode 100644 index 0000000..77abd2c --- /dev/null +++ b/docs/dpservice-cli_initialized.md @@ -0,0 +1,32 @@ +## dpservice-cli initialized + +Indicates if the DPDK app has been initialized already + +``` +dpservice-cli initialized [flags] +``` + +### Examples + +``` +dpservice-cli initialized +``` + +### Options + +``` + -h, --help help for initialized +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli](dpservice-cli.md) - + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_list.md b/docs/dpservice-cli_list.md new file mode 100644 index 0000000..f310830 --- /dev/null +++ b/docs/dpservice-cli_list.md @@ -0,0 +1,37 @@ +## dpservice-cli list + +Lists one of [firewallrules interfaces prefixes lbprefixes routes] + +### Synopsis + +Lists one of [firewallrules interfaces prefixes lbprefixes routes] + +``` +dpservice-cli list [flags] +``` + +### Options + +``` + -h, --help help for list + -o, --output string Output format. + --pretty Whether to render pretty output. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) +``` + +### SEE ALSO + +* [dpservice-cli](dpservice-cli.md) - +* [dpservice-cli list firewallrules](dpservice-cli_list_firewallrules.md) - List firewall rules on interface +* [dpservice-cli list interfaces](dpservice-cli_list_interfaces.md) - List all interfaces +* [dpservice-cli list lbprefixes](dpservice-cli_list_lbprefixes.md) - List loadbalancer prefixes on interface. +* [dpservice-cli list prefixes](dpservice-cli_list_prefixes.md) - List prefix(es) on interface. +* [dpservice-cli list routes](dpservice-cli_list_routes.md) - List routes of specified VNI + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_list_firewallrules.md b/docs/dpservice-cli_list_firewallrules.md new file mode 100644 index 0000000..5a56984 --- /dev/null +++ b/docs/dpservice-cli_list_firewallrules.md @@ -0,0 +1,35 @@ +## dpservice-cli list firewallrules + +List firewall rules on interface + +``` +dpservice-cli list firewallrules <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli list firewallrules --interface-id=vm1 +``` + +### Options + +``` + -h, --help help for firewallrules + --interface-id string InterfaceID from which to list firewall rules. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_list_interfaces.md b/docs/dpservice-cli_list_interfaces.md new file mode 100644 index 0000000..407a976 --- /dev/null +++ b/docs/dpservice-cli_list_interfaces.md @@ -0,0 +1,34 @@ +## dpservice-cli list interfaces + +List all interfaces + +``` +dpservice-cli list interfaces [flags] +``` + +### Examples + +``` +dpservice-cli list interfaces +``` + +### Options + +``` + -h, --help help for interfaces +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_list_lbprefixes.md b/docs/dpservice-cli_list_lbprefixes.md new file mode 100644 index 0000000..508e03e --- /dev/null +++ b/docs/dpservice-cli_list_lbprefixes.md @@ -0,0 +1,35 @@ +## dpservice-cli list lbprefixes + +List loadbalancer prefixes on interface. + +``` +dpservice-cli list lbprefixes <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli list lbprefixes --interface-id=vm1 +``` + +### Options + +``` + -h, --help help for lbprefixes + --interface-id string Interface ID of the prefix. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_list_prefixes.md b/docs/dpservice-cli_list_prefixes.md new file mode 100644 index 0000000..dd9d4de --- /dev/null +++ b/docs/dpservice-cli_list_prefixes.md @@ -0,0 +1,35 @@ +## dpservice-cli list prefixes + +List prefix(es) on interface. + +``` +dpservice-cli list prefixes <--interface-id> [flags] +``` + +### Examples + +``` +dpservice-cli list prefixes --interface-id=vm1 +``` + +### Options + +``` + -h, --help help for prefixes + --interface-id string Interface ID of the prefix. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/docs/dpservice-cli_list_routes.md b/docs/dpservice-cli_list_routes.md new file mode 100644 index 0000000..0d3b315 --- /dev/null +++ b/docs/dpservice-cli_list_routes.md @@ -0,0 +1,35 @@ +## dpservice-cli list routes + +List routes of specified VNI + +``` +dpservice-cli list routes <--vni> [flags] +``` + +### Examples + +``` +dpservice-cli list routes --vni=100 +``` + +### Options + +``` + -h, --help help for routes + --vni uint32 VNI to get the routes from. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes] + +###### Auto generated by spf13/cobra on 17-May-2023 diff --git a/go.mod b/go.mod index 0605b77..f0d3627 100644 --- a/go.mod +++ b/go.mod @@ -17,12 +17,14 @@ require ( require ( github.com/bmatcuk/doublestar/v4 v4.0.2 // indirect + github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-cmp v0.5.9 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/kr/pretty v0.2.0 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect github.com/rivo/uniseg v0.2.0 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect golang.org/x/net v0.5.0 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect golang.org/x/sys v0.4.0 // indirect diff --git a/go.sum b/go.sum index 3bf249b..3a564b1 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,6 @@ github.com/bmatcuk/doublestar/v4 v4.0.2 h1:X0krlUVAVmtr2cRoTqR8aDMrDqnB36ht8wpWTiQ3jsA= github.com/bmatcuk/doublestar/v4 v4.0.2/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= +github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU= github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -36,6 +37,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q= github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= From 64bed0348a94ef3f719a6f0b53b3e0d1ba52818a Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Wed, 17 May 2023 14:56:52 +0200 Subject: [PATCH 33/65] add output underlay route after object is created --- .vscode/launch.json | 13 +++++++------ cmd/add_nat.go | 1 + cmd/add_prefix.go | 2 +- cmd/add_virtualip.go | 2 +- cmd/create_interface.go | 1 + cmd/create_loadbalancer.go | 1 + cmd/create_loadbalancer_prefix.go | 6 +++--- dpdk/api/conversion.go | 7 ++++++- dpdk/api/types.go | 3 ++- dpdk/client/client.go | 18 +++++++++++++++--- go.mod | 2 -- go.sum | 2 -- renderer/renderer.go | 3 +-- 13 files changed, 39 insertions(+), 22 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index bc06686..e28d511 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,16 +11,17 @@ "mode": "auto", "program": "main.go", //"program": "${fileDirname}", - //"args": ["create", "loadbalancer", "7", "--lbports", "389", "--vip", "10.3.4.5", "--vni", "100"] + //"args": ["add", "loadbalancer", "7", "--lbports", "389", "--vip", "10.3.4.5", "--vni", "100"] //"args": ["get", "route", "0.0.0.0/24", "0", "fc00:2::64:0:1", "--vni", "100"] //"args": ["delete", "route", "10.200.2.0/24", "0", "ff80::1", "--vni", "100"] - //"args": ["create", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] - //"args": ["create", "lbtarget", "ff80::1", "--lb-id", "1"] + //"args": ["add", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] + //"args": ["add", "lbtarget", "ff80::1", "--lb-id", "1"] //"args": ["get", "natinfo", "10.20.30.40", "--nat-type=2"] //"args": ["get", "fwrule", "vm1", "--rule-id=4"] - "args": ["create", "fwrule", "vm1", "--action=1", "--direction=1", "--dst=5.5.5.0/24", "--ipv=0", - "--priority=100", "--rule-id=9", "--src=1.1.1.1/32", "--protocol=tcp"] - //"args": ["create", "nnat", "10.20.30.40", "--vni", "100", "--minport", "30000", "--maxport", "30100", "--underlayroute", "ff80::1"] + //"args": ["add", "fwrule", "vm1", "--action=1", "--direction=1", "--dst=5.5.5.0/24", "--ipv=0", + // "--priority=100", "--rule-id=9", "--src=1.1.1.1/32", "--protocol=tcp"] + //"args": ["add", "nnat", "10.20.30.40", "--vni", "100", "--minport", "30000", "--maxport", "30100", "--underlayroute", "ff80::1"] + "args": ["add", "interface", "--id=vm4", "--ip=10.200.1.4", "--ip=2000:200:1::4", "--vni=200", "--device=net_tap5"] } ] } \ No newline at end of file diff --git a/cmd/add_nat.go b/cmd/add_nat.go index 8907187..f9ff33c 100644 --- a/cmd/add_nat.go +++ b/cmd/add_nat.go @@ -112,5 +112,6 @@ func RunAddNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendere if err := renderer.Render(nat); err != nil { return fmt.Errorf("error rendering nat: %w", err) } + fmt.Println("Underlay route is:", nat.Spec.UnderlayRoute) return nil } diff --git a/cmd/add_prefix.go b/cmd/add_prefix.go index 4002313..ded4eb5 100644 --- a/cmd/add_prefix.go +++ b/cmd/add_prefix.go @@ -104,7 +104,6 @@ func RunAddPrefix( InterfaceID: opts.InterfaceID, Prefix: opts.Prefix, }, - Spec: api.PrefixSpec{}, }) if err != nil { return fmt.Errorf("error adding prefix: %w", err) @@ -113,5 +112,6 @@ func RunAddPrefix( if err := renderer.Render(res); err != nil { return fmt.Errorf("error rendering prefix: %w", err) } + fmt.Println("underlay route is:", res.Spec.UnderlayRoute) return nil } diff --git a/cmd/add_virtualip.go b/cmd/add_virtualip.go index 8cca039..722778b 100644 --- a/cmd/add_virtualip.go +++ b/cmd/add_virtualip.go @@ -101,7 +101,6 @@ func RunAddVirtualIP( InterfaceID: opts.InterfaceID, IP: opts.Vip, }, - Spec: api.VirtualIPSpec{}, }) if err != nil { return fmt.Errorf("error adding virtual ip: %w", err) @@ -110,5 +109,6 @@ func RunAddVirtualIP( if err := renderer.Render(virtualIP); err != nil { return fmt.Errorf("error rendering virtual ip: %w", err) } + fmt.Println("Underlay route is:", virtualIP.Spec.UnderlayRoute) return nil } diff --git a/cmd/create_interface.go b/cmd/create_interface.go index 35ea1f3..3197af6 100644 --- a/cmd/create_interface.go +++ b/cmd/create_interface.go @@ -111,5 +111,6 @@ func RunCreateInterface(ctx context.Context, dpdkClientFactory DPDKClientFactory if err := renderer.Render(iface); err != nil { return fmt.Errorf("error rendering interface: %w", err) } + fmt.Println("Underlay IP is:", iface.Status.UnderlayIP) return nil } diff --git a/cmd/create_loadbalancer.go b/cmd/create_loadbalancer.go index ced8cdf..8cd36e9 100644 --- a/cmd/create_loadbalancer.go +++ b/cmd/create_loadbalancer.go @@ -120,5 +120,6 @@ func RunCreateLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFact if err := renderer.Render(lb); err != nil { return fmt.Errorf("error rendering loadbalancer: %w", err) } + fmt.Println("Underlay route is:", lb.Spec.UnderlayRoute) return nil } diff --git a/cmd/create_loadbalancer_prefix.go b/cmd/create_loadbalancer_prefix.go index fabd88d..ec308d5 100644 --- a/cmd/create_loadbalancer_prefix.go +++ b/cmd/create_loadbalancer_prefix.go @@ -99,19 +99,19 @@ func RunCreateLoadBalancerPrefix( return fmt.Errorf("error creating renderer: %w", err) } - res, err := client.CreateLoadBalancerPrefix(ctx, &api.Prefix{ + lbprefix, err := client.CreateLoadBalancerPrefix(ctx, &api.Prefix{ PrefixMeta: api.PrefixMeta{ InterfaceID: opts.InterfaceID, Prefix: opts.Prefix, }, - Spec: api.PrefixSpec{}, }) if err != nil { return fmt.Errorf("error creating prefix: %w", err) } - if err := renderer.Render(res); err != nil { + if err := renderer.Render(lbprefix); err != nil { return fmt.Errorf("error rendering prefix: %w", err) } + fmt.Println("Underlay route is:", lbprefix.Spec.UnderlayRoute) return nil } diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index bda9c4e..bbc7f32 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -203,6 +203,11 @@ func ProtoPrefixToPrefix(interfaceID string, dpdkPrefix *proto.Prefix) (*Prefix, return nil, fmt.Errorf("invalid dpdk prefix length %d for address %s", dpdkPrefix.PrefixLength, addr) } + underlayRoute, err := netip.ParseAddr(string(dpdkPrefix.Address)) + if err != nil { + return nil, fmt.Errorf("error parsing underlay route: %w", err) + } + return &Prefix{ TypeMeta: TypeMeta{ Kind: PrefixKind, @@ -211,7 +216,7 @@ func ProtoPrefixToPrefix(interfaceID string, dpdkPrefix *proto.Prefix) (*Prefix, InterfaceID: interfaceID, Prefix: prefix, }, - Spec: PrefixSpec{UnderlayRoute: dpdkPrefix.UnderlayRoute}, + Spec: PrefixSpec{UnderlayRoute: underlayRoute}, }, nil } diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 5cea767..50b2c6b 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -105,7 +105,7 @@ func (m *PrefixMeta) GetName() string { } type PrefixSpec struct { - UnderlayRoute []byte `json:"underplayRoute"` + UnderlayRoute netip.Addr `json:"underplayRoute"` } type VirtualIP struct { @@ -124,6 +124,7 @@ func (m *VirtualIPMeta) GetName() string { } type VirtualIPSpec struct { + UnderlayRoute netip.Addr `json:"underlayRoute"` } // LoadBalancer section diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 64bf770..c87805d 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -180,10 +180,14 @@ func (c *client) CreateLoadBalancerPrefix(ctx context.Context, prefix *api.Prefi if errorCode := res.GetStatus().GetError(); errorCode != 0 { return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) } + underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) + if err != nil { + return nil, fmt.Errorf("error parsing underlay route: %w", err) + } return &api.Prefix{ TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, PrefixMeta: prefix.PrefixMeta, - Spec: prefix.Spec, + Spec: api.PrefixSpec{UnderlayRoute: underlayRoute}, }, nil } @@ -370,11 +374,15 @@ func (c *client) AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*a if errorCode := res.GetStatus().GetError(); errorCode != 0 { return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) } + underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) + if err != nil { + return nil, fmt.Errorf("error parsing underlay route: %w", err) + } return &api.VirtualIP{ TypeMeta: api.TypeMeta{Kind: api.VirtualIPKind}, VirtualIPMeta: virtualIP.VirtualIPMeta, - Spec: virtualIP.Spec, + Spec: api.VirtualIPSpec{UnderlayRoute: underlayRoute}, }, nil } @@ -432,10 +440,14 @@ func (c *client) AddPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix if errorCode := res.GetStatus().GetError(); errorCode != 0 { return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) } + underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) + if err != nil { + return nil, fmt.Errorf("error parsing underlay route: %w", err) + } return &api.Prefix{ TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, PrefixMeta: prefix.PrefixMeta, - Spec: prefix.Spec, + Spec: api.PrefixSpec{UnderlayRoute: underlayRoute}, }, nil } diff --git a/go.mod b/go.mod index f0d3627..0605b77 100644 --- a/go.mod +++ b/go.mod @@ -17,14 +17,12 @@ require ( require ( github.com/bmatcuk/doublestar/v4 v4.0.2 // indirect - github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-cmp v0.5.9 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/kr/pretty v0.2.0 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect github.com/rivo/uniseg v0.2.0 // indirect - github.com/russross/blackfriday/v2 v2.1.0 // indirect golang.org/x/net v0.5.0 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect golang.org/x/sys v0.4.0 // indirect diff --git a/go.sum b/go.sum index 3a564b1..3bf249b 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,5 @@ github.com/bmatcuk/doublestar/v4 v4.0.2 h1:X0krlUVAVmtr2cRoTqR8aDMrDqnB36ht8wpWTiQ3jsA= github.com/bmatcuk/doublestar/v4 v4.0.2/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= -github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU= github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -37,7 +36,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q= github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= diff --git a/renderer/renderer.go b/renderer/renderer.go index 08036fd..94018ee 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -19,7 +19,6 @@ import ( "encoding/json" "fmt" "io" - "net" "strconv" "strings" @@ -238,7 +237,7 @@ func (t defaultTableConverter) prefixTable(prefixes []api.Prefix) (*TableData, e columns := make([][]any, len(prefixes)) for i, prefix := range prefixes { - columns[i] = []any{prefix.Prefix, net.ParseIP(string(prefix.Spec.UnderlayRoute))} + columns[i] = []any{prefix.Prefix, prefix.Spec.UnderlayRoute} } return &TableData{ From d00e08db2b455f35d8bce5a791657fdc3dad158e Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Wed, 17 May 2023 15:47:51 +0200 Subject: [PATCH 34/65] change default renderer of add commands to table --- cmd/add.go | 2 +- cmd/add_nat.go | 1 - cmd/add_prefix.go | 1 - cmd/add_virtualip.go | 1 - cmd/common.go | 2 +- cmd/create_interface.go | 1 - cmd/create_loadbalancer.go | 1 - cmd/create_loadbalancer_prefix.go | 1 - 8 files changed, 2 insertions(+), 8 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 9814e89..d3e4040 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -26,7 +26,7 @@ import ( ) func Add(dpdkClientFactory DPDKClientFactory) *cobra.Command { - rendererOptions := &RendererOptions{Output: "name"} + rendererOptions := &RendererOptions{Output: "table"} sourcesOptions := &SourcesOptions{} cmd := &cobra.Command{ diff --git a/cmd/add_nat.go b/cmd/add_nat.go index f9ff33c..8907187 100644 --- a/cmd/add_nat.go +++ b/cmd/add_nat.go @@ -112,6 +112,5 @@ func RunAddNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendere if err := renderer.Render(nat); err != nil { return fmt.Errorf("error rendering nat: %w", err) } - fmt.Println("Underlay route is:", nat.Spec.UnderlayRoute) return nil } diff --git a/cmd/add_prefix.go b/cmd/add_prefix.go index ded4eb5..dadec79 100644 --- a/cmd/add_prefix.go +++ b/cmd/add_prefix.go @@ -112,6 +112,5 @@ func RunAddPrefix( if err := renderer.Render(res); err != nil { return fmt.Errorf("error rendering prefix: %w", err) } - fmt.Println("underlay route is:", res.Spec.UnderlayRoute) return nil } diff --git a/cmd/add_virtualip.go b/cmd/add_virtualip.go index 722778b..a4f6324 100644 --- a/cmd/add_virtualip.go +++ b/cmd/add_virtualip.go @@ -109,6 +109,5 @@ func RunAddVirtualIP( if err := renderer.Render(virtualIP); err != nil { return fmt.Errorf("error rendering virtual ip: %w", err) } - fmt.Println("Underlay route is:", virtualIP.Spec.UnderlayRoute) return nil } diff --git a/cmd/common.go b/cmd/common.go index 503b22f..e9c24e3 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -93,7 +93,7 @@ type RendererOptions struct { } func (o *RendererOptions) AddFlags(fs *pflag.FlagSet) { - fs.StringVarP(&o.Output, "output", "o", o.Output, "Output format.") + fs.StringVarP(&o.Output, "output", "o", o.Output, "Output format. [json|yaml|table|name]") fs.BoolVar(&o.Pretty, "pretty", o.Pretty, "Whether to render pretty output.") } diff --git a/cmd/create_interface.go b/cmd/create_interface.go index 3197af6..35ea1f3 100644 --- a/cmd/create_interface.go +++ b/cmd/create_interface.go @@ -111,6 +111,5 @@ func RunCreateInterface(ctx context.Context, dpdkClientFactory DPDKClientFactory if err := renderer.Render(iface); err != nil { return fmt.Errorf("error rendering interface: %w", err) } - fmt.Println("Underlay IP is:", iface.Status.UnderlayIP) return nil } diff --git a/cmd/create_loadbalancer.go b/cmd/create_loadbalancer.go index 8cd36e9..ced8cdf 100644 --- a/cmd/create_loadbalancer.go +++ b/cmd/create_loadbalancer.go @@ -120,6 +120,5 @@ func RunCreateLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFact if err := renderer.Render(lb); err != nil { return fmt.Errorf("error rendering loadbalancer: %w", err) } - fmt.Println("Underlay route is:", lb.Spec.UnderlayRoute) return nil } diff --git a/cmd/create_loadbalancer_prefix.go b/cmd/create_loadbalancer_prefix.go index ec308d5..18089f5 100644 --- a/cmd/create_loadbalancer_prefix.go +++ b/cmd/create_loadbalancer_prefix.go @@ -112,6 +112,5 @@ func RunCreateLoadBalancerPrefix( if err := renderer.Render(lbprefix); err != nil { return fmt.Errorf("error rendering prefix: %w", err) } - fmt.Println("Underlay route is:", lbprefix.Spec.UnderlayRoute) return nil } From 1b5124f2e587cd4120c3043a2d28f41493b587b8 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 18 May 2023 15:04:45 +0200 Subject: [PATCH 35/65] add development guide --- .vscode/launch.json | 3 +- README.md | 14 +++- docs/{ => commands}/dpservice-cli.md | 0 docs/{ => commands}/dpservice-cli_add.md | 0 .../dpservice-cli_add_firewallrule.md | 0 .../dpservice-cli_add_interface.md | 0 .../dpservice-cli_add_lbprefix.md | 0 .../dpservice-cli_add_lbtarget.md | 0 .../dpservice-cli_add_loadbalancer.md | 0 docs/{ => commands}/dpservice-cli_add_nat.md | 0 .../dpservice-cli_add_neighbornat.md | 0 .../dpservice-cli_add_prefix.md | 0 .../{ => commands}/dpservice-cli_add_route.md | 0 .../dpservice-cli_add_virtualip.md | 0 .../dpservice-cli_completion.md | 0 docs/{ => commands}/dpservice-cli_delete.md | 0 .../dpservice-cli_delete_firewallrule.md | 0 .../dpservice-cli_delete_interface.md | 0 .../dpservice-cli_delete_lbprefix.md | 0 .../dpservice-cli_delete_lbtarget.md | 0 .../dpservice-cli_delete_loadbalancer.md | 0 .../dpservice-cli_delete_nat.md | 0 .../dpservice-cli_delete_neighbornat.md | 0 .../dpservice-cli_delete_prefix.md | 0 .../dpservice-cli_delete_route.md | 0 .../dpservice-cli_delete_virtualip.md | 0 docs/{ => commands}/dpservice-cli_get.md | 0 .../dpservice-cli_get_firewallrule.md | 0 .../dpservice-cli_get_interface.md | 0 .../dpservice-cli_get_lbtarget.md | 0 .../dpservice-cli_get_loadbalancer.md | 0 docs/{ => commands}/dpservice-cli_get_nat.md | 0 .../dpservice-cli_get_natinfo.md | 0 .../dpservice-cli_get_virtualip.md | 0 docs/{ => commands}/dpservice-cli_init.md | 0 .../dpservice-cli_initialized.md | 0 docs/{ => commands}/dpservice-cli_list.md | 0 .../dpservice-cli_list_firewallrules.md | 0 .../dpservice-cli_list_interfaces.md | 0 .../dpservice-cli_list_lbprefixes.md | 0 .../dpservice-cli_list_prefixes.md | 0 .../dpservice-cli_list_routes.md | 0 docs/development/README.md | 68 +++++++++++++++++++ 43 files changed, 81 insertions(+), 4 deletions(-) rename docs/{ => commands}/dpservice-cli.md (100%) rename docs/{ => commands}/dpservice-cli_add.md (100%) rename docs/{ => commands}/dpservice-cli_add_firewallrule.md (100%) rename docs/{ => commands}/dpservice-cli_add_interface.md (100%) rename docs/{ => commands}/dpservice-cli_add_lbprefix.md (100%) rename docs/{ => commands}/dpservice-cli_add_lbtarget.md (100%) rename docs/{ => commands}/dpservice-cli_add_loadbalancer.md (100%) rename docs/{ => commands}/dpservice-cli_add_nat.md (100%) rename docs/{ => commands}/dpservice-cli_add_neighbornat.md (100%) rename docs/{ => commands}/dpservice-cli_add_prefix.md (100%) rename docs/{ => commands}/dpservice-cli_add_route.md (100%) rename docs/{ => commands}/dpservice-cli_add_virtualip.md (100%) rename docs/{ => commands}/dpservice-cli_completion.md (100%) rename docs/{ => commands}/dpservice-cli_delete.md (100%) rename docs/{ => commands}/dpservice-cli_delete_firewallrule.md (100%) rename docs/{ => commands}/dpservice-cli_delete_interface.md (100%) rename docs/{ => commands}/dpservice-cli_delete_lbprefix.md (100%) rename docs/{ => commands}/dpservice-cli_delete_lbtarget.md (100%) rename docs/{ => commands}/dpservice-cli_delete_loadbalancer.md (100%) rename docs/{ => commands}/dpservice-cli_delete_nat.md (100%) rename docs/{ => commands}/dpservice-cli_delete_neighbornat.md (100%) rename docs/{ => commands}/dpservice-cli_delete_prefix.md (100%) rename docs/{ => commands}/dpservice-cli_delete_route.md (100%) rename docs/{ => commands}/dpservice-cli_delete_virtualip.md (100%) rename docs/{ => commands}/dpservice-cli_get.md (100%) rename docs/{ => commands}/dpservice-cli_get_firewallrule.md (100%) rename docs/{ => commands}/dpservice-cli_get_interface.md (100%) rename docs/{ => commands}/dpservice-cli_get_lbtarget.md (100%) rename docs/{ => commands}/dpservice-cli_get_loadbalancer.md (100%) rename docs/{ => commands}/dpservice-cli_get_nat.md (100%) rename docs/{ => commands}/dpservice-cli_get_natinfo.md (100%) rename docs/{ => commands}/dpservice-cli_get_virtualip.md (100%) rename docs/{ => commands}/dpservice-cli_init.md (100%) rename docs/{ => commands}/dpservice-cli_initialized.md (100%) rename docs/{ => commands}/dpservice-cli_list.md (100%) rename docs/{ => commands}/dpservice-cli_list_firewallrules.md (100%) rename docs/{ => commands}/dpservice-cli_list_interfaces.md (100%) rename docs/{ => commands}/dpservice-cli_list_lbprefixes.md (100%) rename docs/{ => commands}/dpservice-cli_list_prefixes.md (100%) rename docs/{ => commands}/dpservice-cli_list_routes.md (100%) create mode 100644 docs/development/README.md diff --git a/.vscode/launch.json b/.vscode/launch.json index e28d511..3329cbf 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -21,7 +21,8 @@ //"args": ["add", "fwrule", "vm1", "--action=1", "--direction=1", "--dst=5.5.5.0/24", "--ipv=0", // "--priority=100", "--rule-id=9", "--src=1.1.1.1/32", "--protocol=tcp"] //"args": ["add", "nnat", "10.20.30.40", "--vni", "100", "--minport", "30000", "--maxport", "30100", "--underlayroute", "ff80::1"] - "args": ["add", "interface", "--id=vm4", "--ip=10.200.1.4", "--ip=2000:200:1::4", "--vni=200", "--device=net_tap5"] + //"args": ["add", "interface", "--id=vm4", "--ip=10.200.1.4", "--ip=2000:200:1::4", "--vni=200", "--device=net_tap5"] + "args": ["add", "-f", "/tmp/addint.json"] } ] } \ No newline at end of file diff --git a/README.md b/README.md index f28b480..667820d 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # dpservice-cli -CLI for [net-dpservice](https://github.com/onmetal/net-dpservice). +Command-line tool for debugging over gRPC for [net-dpservice](https://github.com/onmetal/net-dpservice). + +This tool connects directly to a running dp-service and communicates with it (orchestrates it).
-## Installation +## Installation and developing To build the CLI binary, run @@ -19,6 +21,8 @@ run ```shell make install ``` + +For more details about developing refer to documentation folder [docs](/docs/development/README.md)
## Autocompletion @@ -38,6 +42,10 @@ dpservice-cli completion -h ## Usage +Each command or subcommand has help that can be viewed with -h or --help flag. +```shell +dpservice-cli --help +``` ```bash Usage: dpservice-cli [flags] @@ -60,6 +68,6 @@ Flags: Use "dpservice-cli [command] --help" for more information about a command. ``` -All commands are in [docs](/docs/dpservice-cli.md) +All commands can be found in [docs](/docs/commands/dpservice-cli.md)
diff --git a/docs/dpservice-cli.md b/docs/commands/dpservice-cli.md similarity index 100% rename from docs/dpservice-cli.md rename to docs/commands/dpservice-cli.md diff --git a/docs/dpservice-cli_add.md b/docs/commands/dpservice-cli_add.md similarity index 100% rename from docs/dpservice-cli_add.md rename to docs/commands/dpservice-cli_add.md diff --git a/docs/dpservice-cli_add_firewallrule.md b/docs/commands/dpservice-cli_add_firewallrule.md similarity index 100% rename from docs/dpservice-cli_add_firewallrule.md rename to docs/commands/dpservice-cli_add_firewallrule.md diff --git a/docs/dpservice-cli_add_interface.md b/docs/commands/dpservice-cli_add_interface.md similarity index 100% rename from docs/dpservice-cli_add_interface.md rename to docs/commands/dpservice-cli_add_interface.md diff --git a/docs/dpservice-cli_add_lbprefix.md b/docs/commands/dpservice-cli_add_lbprefix.md similarity index 100% rename from docs/dpservice-cli_add_lbprefix.md rename to docs/commands/dpservice-cli_add_lbprefix.md diff --git a/docs/dpservice-cli_add_lbtarget.md b/docs/commands/dpservice-cli_add_lbtarget.md similarity index 100% rename from docs/dpservice-cli_add_lbtarget.md rename to docs/commands/dpservice-cli_add_lbtarget.md diff --git a/docs/dpservice-cli_add_loadbalancer.md b/docs/commands/dpservice-cli_add_loadbalancer.md similarity index 100% rename from docs/dpservice-cli_add_loadbalancer.md rename to docs/commands/dpservice-cli_add_loadbalancer.md diff --git a/docs/dpservice-cli_add_nat.md b/docs/commands/dpservice-cli_add_nat.md similarity index 100% rename from docs/dpservice-cli_add_nat.md rename to docs/commands/dpservice-cli_add_nat.md diff --git a/docs/dpservice-cli_add_neighbornat.md b/docs/commands/dpservice-cli_add_neighbornat.md similarity index 100% rename from docs/dpservice-cli_add_neighbornat.md rename to docs/commands/dpservice-cli_add_neighbornat.md diff --git a/docs/dpservice-cli_add_prefix.md b/docs/commands/dpservice-cli_add_prefix.md similarity index 100% rename from docs/dpservice-cli_add_prefix.md rename to docs/commands/dpservice-cli_add_prefix.md diff --git a/docs/dpservice-cli_add_route.md b/docs/commands/dpservice-cli_add_route.md similarity index 100% rename from docs/dpservice-cli_add_route.md rename to docs/commands/dpservice-cli_add_route.md diff --git a/docs/dpservice-cli_add_virtualip.md b/docs/commands/dpservice-cli_add_virtualip.md similarity index 100% rename from docs/dpservice-cli_add_virtualip.md rename to docs/commands/dpservice-cli_add_virtualip.md diff --git a/docs/dpservice-cli_completion.md b/docs/commands/dpservice-cli_completion.md similarity index 100% rename from docs/dpservice-cli_completion.md rename to docs/commands/dpservice-cli_completion.md diff --git a/docs/dpservice-cli_delete.md b/docs/commands/dpservice-cli_delete.md similarity index 100% rename from docs/dpservice-cli_delete.md rename to docs/commands/dpservice-cli_delete.md diff --git a/docs/dpservice-cli_delete_firewallrule.md b/docs/commands/dpservice-cli_delete_firewallrule.md similarity index 100% rename from docs/dpservice-cli_delete_firewallrule.md rename to docs/commands/dpservice-cli_delete_firewallrule.md diff --git a/docs/dpservice-cli_delete_interface.md b/docs/commands/dpservice-cli_delete_interface.md similarity index 100% rename from docs/dpservice-cli_delete_interface.md rename to docs/commands/dpservice-cli_delete_interface.md diff --git a/docs/dpservice-cli_delete_lbprefix.md b/docs/commands/dpservice-cli_delete_lbprefix.md similarity index 100% rename from docs/dpservice-cli_delete_lbprefix.md rename to docs/commands/dpservice-cli_delete_lbprefix.md diff --git a/docs/dpservice-cli_delete_lbtarget.md b/docs/commands/dpservice-cli_delete_lbtarget.md similarity index 100% rename from docs/dpservice-cli_delete_lbtarget.md rename to docs/commands/dpservice-cli_delete_lbtarget.md diff --git a/docs/dpservice-cli_delete_loadbalancer.md b/docs/commands/dpservice-cli_delete_loadbalancer.md similarity index 100% rename from docs/dpservice-cli_delete_loadbalancer.md rename to docs/commands/dpservice-cli_delete_loadbalancer.md diff --git a/docs/dpservice-cli_delete_nat.md b/docs/commands/dpservice-cli_delete_nat.md similarity index 100% rename from docs/dpservice-cli_delete_nat.md rename to docs/commands/dpservice-cli_delete_nat.md diff --git a/docs/dpservice-cli_delete_neighbornat.md b/docs/commands/dpservice-cli_delete_neighbornat.md similarity index 100% rename from docs/dpservice-cli_delete_neighbornat.md rename to docs/commands/dpservice-cli_delete_neighbornat.md diff --git a/docs/dpservice-cli_delete_prefix.md b/docs/commands/dpservice-cli_delete_prefix.md similarity index 100% rename from docs/dpservice-cli_delete_prefix.md rename to docs/commands/dpservice-cli_delete_prefix.md diff --git a/docs/dpservice-cli_delete_route.md b/docs/commands/dpservice-cli_delete_route.md similarity index 100% rename from docs/dpservice-cli_delete_route.md rename to docs/commands/dpservice-cli_delete_route.md diff --git a/docs/dpservice-cli_delete_virtualip.md b/docs/commands/dpservice-cli_delete_virtualip.md similarity index 100% rename from docs/dpservice-cli_delete_virtualip.md rename to docs/commands/dpservice-cli_delete_virtualip.md diff --git a/docs/dpservice-cli_get.md b/docs/commands/dpservice-cli_get.md similarity index 100% rename from docs/dpservice-cli_get.md rename to docs/commands/dpservice-cli_get.md diff --git a/docs/dpservice-cli_get_firewallrule.md b/docs/commands/dpservice-cli_get_firewallrule.md similarity index 100% rename from docs/dpservice-cli_get_firewallrule.md rename to docs/commands/dpservice-cli_get_firewallrule.md diff --git a/docs/dpservice-cli_get_interface.md b/docs/commands/dpservice-cli_get_interface.md similarity index 100% rename from docs/dpservice-cli_get_interface.md rename to docs/commands/dpservice-cli_get_interface.md diff --git a/docs/dpservice-cli_get_lbtarget.md b/docs/commands/dpservice-cli_get_lbtarget.md similarity index 100% rename from docs/dpservice-cli_get_lbtarget.md rename to docs/commands/dpservice-cli_get_lbtarget.md diff --git a/docs/dpservice-cli_get_loadbalancer.md b/docs/commands/dpservice-cli_get_loadbalancer.md similarity index 100% rename from docs/dpservice-cli_get_loadbalancer.md rename to docs/commands/dpservice-cli_get_loadbalancer.md diff --git a/docs/dpservice-cli_get_nat.md b/docs/commands/dpservice-cli_get_nat.md similarity index 100% rename from docs/dpservice-cli_get_nat.md rename to docs/commands/dpservice-cli_get_nat.md diff --git a/docs/dpservice-cli_get_natinfo.md b/docs/commands/dpservice-cli_get_natinfo.md similarity index 100% rename from docs/dpservice-cli_get_natinfo.md rename to docs/commands/dpservice-cli_get_natinfo.md diff --git a/docs/dpservice-cli_get_virtualip.md b/docs/commands/dpservice-cli_get_virtualip.md similarity index 100% rename from docs/dpservice-cli_get_virtualip.md rename to docs/commands/dpservice-cli_get_virtualip.md diff --git a/docs/dpservice-cli_init.md b/docs/commands/dpservice-cli_init.md similarity index 100% rename from docs/dpservice-cli_init.md rename to docs/commands/dpservice-cli_init.md diff --git a/docs/dpservice-cli_initialized.md b/docs/commands/dpservice-cli_initialized.md similarity index 100% rename from docs/dpservice-cli_initialized.md rename to docs/commands/dpservice-cli_initialized.md diff --git a/docs/dpservice-cli_list.md b/docs/commands/dpservice-cli_list.md similarity index 100% rename from docs/dpservice-cli_list.md rename to docs/commands/dpservice-cli_list.md diff --git a/docs/dpservice-cli_list_firewallrules.md b/docs/commands/dpservice-cli_list_firewallrules.md similarity index 100% rename from docs/dpservice-cli_list_firewallrules.md rename to docs/commands/dpservice-cli_list_firewallrules.md diff --git a/docs/dpservice-cli_list_interfaces.md b/docs/commands/dpservice-cli_list_interfaces.md similarity index 100% rename from docs/dpservice-cli_list_interfaces.md rename to docs/commands/dpservice-cli_list_interfaces.md diff --git a/docs/dpservice-cli_list_lbprefixes.md b/docs/commands/dpservice-cli_list_lbprefixes.md similarity index 100% rename from docs/dpservice-cli_list_lbprefixes.md rename to docs/commands/dpservice-cli_list_lbprefixes.md diff --git a/docs/dpservice-cli_list_prefixes.md b/docs/commands/dpservice-cli_list_prefixes.md similarity index 100% rename from docs/dpservice-cli_list_prefixes.md rename to docs/commands/dpservice-cli_list_prefixes.md diff --git a/docs/dpservice-cli_list_routes.md b/docs/commands/dpservice-cli_list_routes.md similarity index 100% rename from docs/dpservice-cli_list_routes.md rename to docs/commands/dpservice-cli_list_routes.md diff --git a/docs/development/README.md b/docs/development/README.md new file mode 100644 index 0000000..94893db --- /dev/null +++ b/docs/development/README.md @@ -0,0 +1,68 @@ +# dpservice-cli development guide + +This page is intended as a general overview for all development-oriented topics. +
+ + +## Running dpservice + +Before using dpservice-cli client, you need to have dpservice instance running. + +Please refer to this guide [net-dpservice](https://github.com/onmetal/net-dpservice/blob/osc/grpc_docs/docs/development/building.md) on how to build dpservice from source. + +You can then run python script /test/dp_service.py that will start the dpservice with preloaded config. +```bash +sudo ./test/dp_service.py +``` +If there is error about number of hugepages run this as root: +```bash +echo 2048 > /sys/devices/system/node/node0/hugepages/hugepages-2048kB/nr_hugepages +``` +
+ + +## Running dpservice-cli + +Go version 1.18 or newer is needed. \"make\" tool is also needed to utilize the Makefile. + +To run the dpservice-cli client build the binary first and then use it with commands and flags: +```bash +make build +./bin/dpservice-cli -h +``` +When you are running dpservice on the same VM you don't need to specify the address and defaults are used (localhost:1337). + +If dpservice is running on different machine or you changed the default settings, use --address flag: +```bash +./bin/dpservice-cli --address [command] [flags] +``` +
+ + +## Adding new type + +Basic steps when implementing new type (similar to Interface, Route, LoadBalancer, ...): +- Create new type in [/dpdk/api/types.go](/dpdk/api/types.go): + - create structs and methods + - at the bottom add new \Kind variable +- Create new [add|get|list|delete]\.go file in /cmd/ folder and implement the logic +- Add new command function to subcommands of matching parent command in /cmd/[add|get|list|delete].go +- If needed add aliases for \ at the bottom of [/cmd/common.go](/cmd/common.go) +- Add new function to [/dpdk/api/client.go](/dpdk/api/client.go): + - add function to Client interface + - implement the function +- Add new \ to DefaultScheme in [/dpdk/api/register.go](/dpdk/api/register.go) +- If needed create new conversion function(s) between dpdk struct and local struct in [/dpdk/api/conversion.go](/dpdk/api/conversion.go) +- Add new function to show \ as table in [/renderer/renderer.go](/renderer/renderer.go) + - add new \ to ConvertToTable method + - implement function to show new \ +
+ + +## gRPC + +This client uses golang bindings from repo [net-dpservice-go](https://github.com/onmetal/net-dpservice-go). + +Definition go files in [proto](https://github.com/onmetal/net-dpservice-go/tree/main/proto) folder are auto-generated from [dpdk.proto](https://github.com/onmetal/net-dpservice/blob/osc/main/proto/dpdk.proto) file in [net-dpservice](https://github.com/onmetal/net-dpservice/) repo. + +More info about gRPC can be found [here](https://grpc.io/docs/what-is-grpc/introduction/). \ No newline at end of file From 10f20a8bb3bc987b3e66d7666509a01d8d68c914 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Fri, 19 May 2023 08:26:45 +0200 Subject: [PATCH 36/65] change to allow --output flag globally --- cmd/add.go | 24 ++++++++++++------------ cmd/command.go | 10 +++++++--- cmd/delete.go | 4 +++- cmd/delete_interface.go | 25 +++++++++++++++++++++---- docs/README.md | 5 +++++ dpdk/api/conversion.go | 2 +- dpdk/api/types.go | 21 +++++++++++++++++---- dpdk/client/client.go | 9 ++++++++- renderer/renderer.go | 13 +++++++++++-- 9 files changed, 85 insertions(+), 28 deletions(-) create mode 100644 docs/README.md diff --git a/cmd/add.go b/cmd/add.go index d3e4040..d88b288 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -25,7 +25,7 @@ import ( "github.com/spf13/cobra" ) -func Add(dpdkClientFactory DPDKClientFactory) *cobra.Command { +func Add(factory DPDKClientFactory) *cobra.Command { rendererOptions := &RendererOptions{Output: "table"} sourcesOptions := &SourcesOptions{} @@ -34,7 +34,7 @@ func Add(dpdkClientFactory DPDKClientFactory) *cobra.Command { Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() - return RunAdd(ctx, dpdkClientFactory, rendererOptions, sourcesOptions) + return RunAdd(ctx, factory, rendererOptions, sourcesOptions) }, } @@ -43,16 +43,16 @@ func Add(dpdkClientFactory DPDKClientFactory) *cobra.Command { sourcesOptions.AddFlags(cmd.Flags()) subcommands := []*cobra.Command{ - CreateInterface(dpdkClientFactory, rendererOptions), - AddPrefix(dpdkClientFactory, rendererOptions), - AddRoute(dpdkClientFactory, rendererOptions), - AddVirtualIP(dpdkClientFactory, rendererOptions), - CreateLoadBalancer(dpdkClientFactory, rendererOptions), - CreateLoadBalancerPrefix(dpdkClientFactory, rendererOptions), - AddLoadBalancerTarget(dpdkClientFactory, rendererOptions), - AddNat(dpdkClientFactory, rendererOptions), - AddNeighborNat(dpdkClientFactory, rendererOptions), - AddFirewallRule(dpdkClientFactory, rendererOptions), + CreateInterface(factory, rendererOptions), + AddPrefix(factory, rendererOptions), + AddRoute(factory, rendererOptions), + AddVirtualIP(factory, rendererOptions), + CreateLoadBalancer(factory, rendererOptions), + CreateLoadBalancerPrefix(factory, rendererOptions), + AddLoadBalancerTarget(factory, rendererOptions), + AddNat(factory, rendererOptions), + AddNeighborNat(factory, rendererOptions), + AddFirewallRule(factory, rendererOptions), } cmd.Short = fmt.Sprintf("Creates one of %v", CommandNames(subcommands)) diff --git a/cmd/command.go b/cmd/command.go index 4b1f9a7..4c852c5 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -20,13 +20,17 @@ import ( func Command() *cobra.Command { dpdkClientOptions := &DPDKClientOptions{} + rendererOptions := &RendererOptions{} cmd := &cobra.Command{ - Use: "dpservice-cli", - Args: cobra.NoArgs, - RunE: SubcommandRequired, + Use: "dpservice-cli", + Args: cobra.NoArgs, + SilenceUsage: true, + SilenceErrors: true, + RunE: SubcommandRequired, } + rendererOptions.AddFlags(cmd.PersistentFlags()) dpdkClientOptions.AddFlags(cmd.PersistentFlags()) cmd.AddCommand( diff --git a/cmd/delete.go b/cmd/delete.go index 4e30229..24d5784 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -39,10 +39,12 @@ func Delete(factory DPDKClientFactory) *cobra.Command { }, } + rendererOptions.AddFlags(cmd.PersistentFlags()) + sourcesOptions.AddFlags(cmd.Flags()) subcommands := []*cobra.Command{ - DeleteInterface(factory), + DeleteInterface(factory, rendererOptions), DeletePrefix(factory), DeleteRoute(factory), DeleteVirtualIP(factory), diff --git a/cmd/delete_interface.go b/cmd/delete_interface.go index f74a845..808e2eb 100644 --- a/cmd/delete_interface.go +++ b/cmd/delete_interface.go @@ -17,13 +17,15 @@ package cmd import ( "context" "fmt" + "os" + "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteInterface(factory DPDKClientFactory) *cobra.Command { +func DeleteInterface(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteInterfaceOptions ) @@ -36,7 +38,12 @@ func DeleteInterface(factory DPDKClientFactory) *cobra.Command { Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - return RunDeleteInterface(cmd.Context(), factory, opts) + return RunDeleteInterface( + cmd.Context(), + factory, + rendererFactory, + opts, + ) }, } @@ -64,7 +71,7 @@ func (o *DeleteInterfaceOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeleteInterface(ctx context.Context, factory DPDKClientFactory, opts DeleteInterfaceOptions) error { +func RunDeleteInterface(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteInterfaceOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -75,11 +82,21 @@ func RunDeleteInterface(ctx context.Context, factory DPDKClientFactory, opts Del } }() + renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + if err := client.DeleteInterface(ctx, opts.ID); err != nil { return fmt.Errorf("error deleting interface %s: %v", opts.ID, err) } + iface := api.Interface{ + TypeMeta: api.TypeMeta{Kind: api.InterfaceKind}, + InterfaceMeta: api.InterfaceMeta{ID: opts.ID}} - fmt.Println("Deleted interface", opts.ID) + if err := renderer.Render(&iface); err != nil { + return fmt.Errorf("error rendering interface: %w", err) + } return nil } diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..c8c2170 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,5 @@ +# Documentation - dpservice-cli + +All available commands can be found [here](/docs/commands/). + +Development details can be found [here](/docs/development/) \ No newline at end of file diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index bbc7f32..27974af 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -147,7 +147,7 @@ func ProtoInterfaceToInterface(dpdkIface *proto.Interface) (*Interface, error) { IPs: ips, }, Status: InterfaceStatus{ - UnderlayIP: underlayIP, + UnderlayIP: &underlayIP, }, }, nil } diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 50b2c6b..02ab49a 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -218,13 +218,26 @@ func (m *InterfaceMeta) GetName() string { } type InterfaceSpec struct { - VNI uint32 `json:"vni"` - Device string `json:"device"` - IPs []netip.Addr `json:"ips"` + VNI uint32 `json:"vni,omitempty"` + Device string `json:"device,omitempty"` + IPs []netip.Addr `json:"ips,omitempty"` } type InterfaceStatus struct { - UnderlayIP netip.Addr `json:"underlayIP"` + UnderlayIP *netip.Addr `json:"underlayIP,omitempty"` + VirtualFunction *VirtualFunction `json:"virtualFunction,omitempty"` +} + +type VirtualFunction struct { + Name string `json:"vfName,omitempty"` + Domain uint32 `json:"vfDomain,omitempty"` + Bus uint32 `json:"vfBus,omitempty"` + Slot uint32 `json:"vfSlot,omitempty"` + Function uint32 `json:"vfFunction,omitempty"` +} + +func (vf *VirtualFunction) String() string { + return fmt.Sprintf("Name: %s, Domain: %d, Bus: %d, Slot: %d, Function: %d", vf.Name, vf.Domain, vf.Bus, vf.Slot, vf.Function) } type InterfaceList struct { diff --git a/dpdk/client/client.go b/dpdk/client/client.go index c87805d..785d6df 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -330,7 +330,14 @@ func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*ap InterfaceMeta: iface.InterfaceMeta, Spec: iface.Spec, // TODO: Enable dynamic device allocation Status: api.InterfaceStatus{ - UnderlayIP: underlayIP, + UnderlayIP: &underlayIP, + VirtualFunction: &api.VirtualFunction{ + Name: res.Vf.Name, + Domain: res.Vf.Domain, + Bus: res.Vf.Bus, + Slot: res.Vf.Slot, + Function: res.Vf.Function, + }, }, }, nil } diff --git a/renderer/renderer.go b/renderer/renderer.go index 94018ee..f0c7722 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -219,11 +219,20 @@ func (t defaultTableConverter) loadBalancerTargetTable(lbtargets []api.LoadBalan } func (t defaultTableConverter) interfaceTable(ifaces []api.Interface) (*TableData, error) { - headers := []any{"ID", "VNI", "Device", "IPs", "UnderlayIP"} + var headers []any + if ifaces[0].Status.VirtualFunction == nil { + headers = []any{"ID", "VNI", "Device", "IPs", "UnderlayIP"} + } else { + headers = []any{"ID", "VNI", "Device", "IPs", "UnderlayIP", "VirtualFunction"} + } columns := make([][]any, len(ifaces)) for i, iface := range ifaces { - columns[i] = []any{iface.ID, iface.Spec.VNI, iface.Spec.Device, iface.Spec.IPs, iface.Status.UnderlayIP} + if ifaces[0].Status.VirtualFunction == nil { + columns[i] = []any{iface.ID, iface.Spec.VNI, iface.Spec.Device, iface.Spec.IPs, iface.Status.UnderlayIP} + } else { + columns[i] = []any{iface.ID, iface.Spec.VNI, iface.Spec.Device, iface.Spec.IPs, iface.Status.UnderlayIP, iface.Status.VirtualFunction.String()} + } } return &TableData{ From ee1a72586400bce935917bb7f6c9b8d22a326f56 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Fri, 19 May 2023 13:38:16 +0200 Subject: [PATCH 37/65] add output option for delete commands --- README.md | 2 + cmd/add_firewall_rule.go | 6 +- cmd/add_loadbalancer_target.go | 2 +- cmd/add_nat.go | 2 +- cmd/add_neighbor_nat.go | 19 +++-- cmd/create_loadbalancer.go | 2 +- cmd/delete.go | 18 ++--- cmd/delete_firewall_rule.go | 31 ++++++-- cmd/delete_interface.go | 15 ++-- cmd/delete_loadbalancer.go | 30 ++++++-- cmd/delete_loadbalancer_prefix.go | 31 ++++++-- cmd/delete_loadbalancer_target.go | 33 +++++++-- cmd/delete_nat.go | 30 ++++++-- cmd/delete_neighbor_nat.go | 31 ++++++-- cmd/delete_prefix.go | 32 +++++++-- cmd/delete_route.go | 36 ++++++++-- cmd/delete_virtualip.go | 30 ++++++-- cmd/init.go | 2 + cmd/initialized.go | 2 +- docs/development/README.md | 16 +++-- dpdk/api/conversion.go | 36 +++++----- dpdk/api/types.go | 115 +++++++++++++----------------- dpdk/client/client.go | 55 +++++++------- renderer/renderer.go | 14 ++-- 24 files changed, 403 insertions(+), 187 deletions(-) diff --git a/README.md b/README.md index 667820d..86af161 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,8 @@ Flags: --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -h, --help help for dpservice-cli + -o, --output string Output format. [json|yaml|table|name] + --pretty Whether to render pretty output. Use "dpservice-cli [command] --help" for more information about a command. ``` diff --git a/cmd/add_firewall_rule.go b/cmd/add_firewall_rule.go index 2abae63..50b843e 100644 --- a/cmd/add_firewall_rule.go +++ b/cmd/add_firewall_rule.go @@ -178,9 +178,9 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory TrafficDirection: opts.TrafficDirection, FirewallAction: opts.FirewallAction, Priority: opts.Priority, - SourcePrefix: srcPfx, - DestinationPrefix: dstPfx, - ProtocolFilter: dpdkproto.ProtocolFilter{ + SourcePrefix: &srcPfx, + DestinationPrefix: &dstPfx, + ProtocolFilter: &dpdkproto.ProtocolFilter{ Filter: protocolFilter.Filter}, }, }, diff --git a/cmd/add_loadbalancer_target.go b/cmd/add_loadbalancer_target.go index d9ab97f..ce43658 100644 --- a/cmd/add_loadbalancer_target.go +++ b/cmd/add_loadbalancer_target.go @@ -102,7 +102,7 @@ func RunAddLoadBalancerTarget( res, err := client.AddLoadBalancerTarget(ctx, &api.LoadBalancerTarget{ TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, LoadBalancerTargetMeta: api.LoadBalancerTargetMeta{ID: opts.LoadBalancerID}, - Spec: api.LoadBalancerTargetSpec{TargetIP: opts.TargetIP}, + Spec: api.LoadBalancerTargetSpec{TargetIP: &opts.TargetIP}, }) if err != nil { return fmt.Errorf("error adding loadbalancer target: %w", err) diff --git a/cmd/add_nat.go b/cmd/add_nat.go index 8907187..215cabd 100644 --- a/cmd/add_nat.go +++ b/cmd/add_nat.go @@ -100,7 +100,7 @@ func RunAddNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendere InterfaceID: opts.InterfaceID, }, Spec: api.NatSpec{ - NatVIPIP: opts.NATVipIP, + NatVIPIP: &opts.NATVipIP, MinPort: opts.MinPort, MaxPort: opts.MaxPort, }, diff --git a/cmd/add_neighbor_nat.go b/cmd/add_neighbor_nat.go index b9c948f..9ffb15a 100644 --- a/cmd/add_neighbor_nat.go +++ b/cmd/add_neighbor_nat.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "net/netip" + "os" "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/flag" @@ -91,23 +92,31 @@ func RunAddNeighborNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, } }() - err = client.AddNeighborNat(ctx, &api.NeighborNat{ + nNat := &api.NeighborNat{ + TypeMeta: api.TypeMeta{Kind: api.NeighborNatKind}, NeighborNatMeta: api.NeighborNatMeta{ - NatVIPIP: opts.NatIP, + NatVIPIP: &opts.NatIP, }, Spec: api.NeighborNatSpec{ Vni: opts.Vni, MinPort: opts.MinPort, MaxPort: opts.MaxPort, - UnderlayRoute: opts.UnderlayRoute, + UnderlayRoute: &opts.UnderlayRoute, }, - }) + } + err = client.AddNeighborNat(ctx, nNat) if err != nil { return fmt.Errorf("error adding neighbor nat: %w", err) } - fmt.Printf("Neighbor NAT with IP: %s added\n", opts.NatIP.String()) + renderer, err := rendererFactory.NewRenderer("created", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + if err := renderer.Render(&nNat); err != nil { + return fmt.Errorf("error rendering neighbor nat: %w", err) + } return nil } diff --git a/cmd/create_loadbalancer.go b/cmd/create_loadbalancer.go index ced8cdf..36bb581 100644 --- a/cmd/create_loadbalancer.go +++ b/cmd/create_loadbalancer.go @@ -109,7 +109,7 @@ func RunCreateLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFact }, Spec: api.LoadBalancerSpec{ VNI: opts.VNI, - LbVipIP: opts.LbVipIP, + LbVipIP: &opts.LbVipIP, Lbports: ports, }, }) diff --git a/cmd/delete.go b/cmd/delete.go index 24d5784..4741b47 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -45,15 +45,15 @@ func Delete(factory DPDKClientFactory) *cobra.Command { subcommands := []*cobra.Command{ DeleteInterface(factory, rendererOptions), - DeletePrefix(factory), - DeleteRoute(factory), - DeleteVirtualIP(factory), - DeleteLoadBalancer(factory), - DeleteLoadBalancerPrefix(factory), - DeleteLoadBalancerTarget(factory), - DeleteNat(factory), - DeleteNeighborNat(factory), - DeleteFirewallRule(factory), + DeletePrefix(factory, rendererOptions), + DeleteRoute(factory, rendererOptions), + DeleteVirtualIP(factory, rendererOptions), + DeleteLoadBalancer(factory, rendererOptions), + DeleteLoadBalancerPrefix(factory, rendererOptions), + DeleteLoadBalancerTarget(factory, rendererOptions), + DeleteNat(factory, rendererOptions), + DeleteNeighborNat(factory, rendererOptions), + DeleteFirewallRule(factory, rendererOptions), } cmd.Short = fmt.Sprintf("Deletes one of %v", CommandNames(subcommands)) diff --git a/cmd/delete_firewall_rule.go b/cmd/delete_firewall_rule.go index b27074e..a404b9c 100644 --- a/cmd/delete_firewall_rule.go +++ b/cmd/delete_firewall_rule.go @@ -17,13 +17,15 @@ package cmd import ( "context" "fmt" + "os" + "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteFirewallRule(factory DPDKClientFactory) *cobra.Command { +func DeleteFirewallRule(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteFirewallRuleOptions ) @@ -36,7 +38,12 @@ func DeleteFirewallRule(factory DPDKClientFactory) *cobra.Command { Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - return RunDeleteFirewallRule(cmd.Context(), factory, opts) + return RunDeleteFirewallRule( + cmd.Context(), + factory, + rendererFactory, + opts, + ) }, } @@ -66,7 +73,7 @@ func (o *DeleteFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error return nil } -func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, opts DeleteFirewallRuleOptions) error { +func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteFirewallRuleOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -81,7 +88,23 @@ func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, opts return fmt.Errorf("error deleting firewall rule %s/%s: %v", opts.RuleID, opts.InterfaceID, err) } - fmt.Printf("Deleted firewall rule %s on interface %s\n", opts.RuleID, opts.InterfaceID) + renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + fwrule := api.FirewallRule{ + TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, + FirewallRuleMeta: api.FirewallRuleMeta{ + InterfaceID: opts.InterfaceID, + RuleID: opts.RuleID, + }, + Status: api.Status{ + Message: "Deleted", + }, + } + if err := renderer.Render(&fwrule); err != nil { + return fmt.Errorf("error rendering prefix: %w", err) + } return nil } diff --git a/cmd/delete_interface.go b/cmd/delete_interface.go index 808e2eb..69146c2 100644 --- a/cmd/delete_interface.go +++ b/cmd/delete_interface.go @@ -82,18 +82,21 @@ func RunDeleteInterface(ctx context.Context, factory DPDKClientFactory, renderer } }() + if err := client.DeleteInterface(ctx, opts.ID); err != nil { + return fmt.Errorf("error deleting interface %s: %v", opts.ID, err) + } + renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) if err != nil { return fmt.Errorf("error creating renderer: %w", err) } - - if err := client.DeleteInterface(ctx, opts.ID); err != nil { - return fmt.Errorf("error deleting interface %s: %v", opts.ID, err) - } iface := api.Interface{ TypeMeta: api.TypeMeta{Kind: api.InterfaceKind}, - InterfaceMeta: api.InterfaceMeta{ID: opts.ID}} - + InterfaceMeta: api.InterfaceMeta{ID: opts.ID}, + Status: api.Status{ + Message: "Deleted", + }, + } if err := renderer.Render(&iface); err != nil { return fmt.Errorf("error rendering interface: %w", err) } diff --git a/cmd/delete_loadbalancer.go b/cmd/delete_loadbalancer.go index 39f1216..498b478 100644 --- a/cmd/delete_loadbalancer.go +++ b/cmd/delete_loadbalancer.go @@ -17,13 +17,15 @@ package cmd import ( "context" "fmt" + "os" + "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteLoadBalancer(factory DPDKClientFactory) *cobra.Command { +func DeleteLoadBalancer(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteLoadBalancerOptions ) @@ -36,7 +38,12 @@ func DeleteLoadBalancer(factory DPDKClientFactory) *cobra.Command { Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - return RunDeleteLoadBalancer(cmd.Context(), factory, opts) + return RunDeleteLoadBalancer( + cmd.Context(), + factory, + rendererFactory, + opts, + ) }, } @@ -64,7 +71,7 @@ func (o *DeleteLoadBalancerOptions) MarkRequiredFlags(cmd *cobra.Command) error return nil } -func RunDeleteLoadBalancer(ctx context.Context, factory DPDKClientFactory, opts DeleteLoadBalancerOptions) error { +func RunDeleteLoadBalancer(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteLoadBalancerOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -79,7 +86,22 @@ func RunDeleteLoadBalancer(ctx context.Context, factory DPDKClientFactory, opts return fmt.Errorf("error deleting loadbalancer %s: %v", opts.ID, err) } - fmt.Println("Deleted loadbalancer", opts.ID) + renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + lb := api.LoadBalancer{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerKind}, + LoadBalancerMeta: api.LoadBalancerMeta{ + ID: opts.ID, + }, + Status: api.Status{ + Message: "Deleted", + }, + } + if err := renderer.Render(&lb); err != nil { + return fmt.Errorf("error rendering loadbalancer: %w", err) + } return nil } diff --git a/cmd/delete_loadbalancer_prefix.go b/cmd/delete_loadbalancer_prefix.go index f9f7cc8..11c99ae 100644 --- a/cmd/delete_loadbalancer_prefix.go +++ b/cmd/delete_loadbalancer_prefix.go @@ -18,14 +18,16 @@ import ( "context" "fmt" "net/netip" + "os" + "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteLoadBalancerPrefix(factory DPDKClientFactory) *cobra.Command { +func DeleteLoadBalancerPrefix(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteLoadBalancerPrefixOptions ) @@ -38,7 +40,12 @@ func DeleteLoadBalancerPrefix(factory DPDKClientFactory) *cobra.Command { Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - return RunDeleteLoadBalancerPrefix(cmd.Context(), factory, opts) + return RunDeleteLoadBalancerPrefix( + cmd.Context(), + factory, + rendererFactory, + opts, + ) }, } @@ -68,7 +75,7 @@ func (o *DeleteLoadBalancerPrefixOptions) MarkRequiredFlags(cmd *cobra.Command) return nil } -func RunDeleteLoadBalancerPrefix(ctx context.Context, factory DPDKClientFactory, opts DeleteLoadBalancerPrefixOptions) error { +func RunDeleteLoadBalancerPrefix(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteLoadBalancerPrefixOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error deleting dpdk client: %w", err) @@ -82,7 +89,23 @@ func RunDeleteLoadBalancerPrefix(ctx context.Context, factory DPDKClientFactory, if err := client.DeleteLoadBalancerPrefix(ctx, opts.InterfaceID, opts.Prefix); err != nil { return fmt.Errorf("error deleting loadbalancer prefix %s/%v: %v", opts.InterfaceID, opts.Prefix, err) } - fmt.Printf("Deleted loadbalancer prefix %s/%v\n", opts.InterfaceID, opts.Prefix) + renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + lbprefix := api.Prefix{ + TypeMeta: api.TypeMeta{Kind: "LoadBalancerPrefix"}, + PrefixMeta: api.PrefixMeta{ + InterfaceID: opts.InterfaceID, + Prefix: opts.Prefix, + }, + Status: api.Status{ + Message: "Deleted", + }, + } + if err := renderer.Render(&lbprefix); err != nil { + return fmt.Errorf("error rendering loadbalancer prefix: %w", err) + } return nil } diff --git a/cmd/delete_loadbalancer_target.go b/cmd/delete_loadbalancer_target.go index 7a8f39a..a4eb628 100644 --- a/cmd/delete_loadbalancer_target.go +++ b/cmd/delete_loadbalancer_target.go @@ -18,14 +18,16 @@ import ( "context" "fmt" "net/netip" + "os" + "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteLoadBalancerTarget(factory DPDKClientFactory) *cobra.Command { +func DeleteLoadBalancerTarget(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteLoadBalancerTargetOptions ) @@ -38,7 +40,12 @@ func DeleteLoadBalancerTarget(factory DPDKClientFactory) *cobra.Command { Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - return RunDeleteLoadBalancerTarget(cmd.Context(), factory, opts) + return RunDeleteLoadBalancerTarget( + cmd.Context(), + factory, + rendererFactory, + opts, + ) }, } @@ -68,7 +75,7 @@ func (o *DeleteLoadBalancerTargetOptions) MarkRequiredFlags(cmd *cobra.Command) return nil } -func RunDeleteLoadBalancerTarget(ctx context.Context, factory DPDKClientFactory, opts DeleteLoadBalancerTargetOptions) error { +func RunDeleteLoadBalancerTarget(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteLoadBalancerTargetOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error deleting dpdk client: %w", err) @@ -82,7 +89,25 @@ func RunDeleteLoadBalancerTarget(ctx context.Context, factory DPDKClientFactory, if err := client.DeleteLoadBalancerTarget(ctx, opts.LoadBalancerID, opts.TargetIP); err != nil { return fmt.Errorf("error deleting loadbalancer target %s/%v: %v", opts.LoadBalancerID, opts.TargetIP, err) } - fmt.Printf("Deleted loadbalancer target %s/%v\n", opts.LoadBalancerID, opts.TargetIP) + renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + lbtarget := api.LoadBalancerTarget{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, + LoadBalancerTargetMeta: api.LoadBalancerTargetMeta{ + ID: opts.LoadBalancerID, + }, + Spec: api.LoadBalancerTargetSpec{ + TargetIP: &opts.TargetIP, + }, + Status: api.Status{ + Message: "Deleted", + }, + } + if err := renderer.Render(&lbtarget); err != nil { + return fmt.Errorf("error rendering loadbalancer target: %w", err) + } return nil } diff --git a/cmd/delete_nat.go b/cmd/delete_nat.go index a791396..87d1c37 100644 --- a/cmd/delete_nat.go +++ b/cmd/delete_nat.go @@ -17,13 +17,15 @@ package cmd import ( "context" "fmt" + "os" + "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteNat(factory DPDKClientFactory) *cobra.Command { +func DeleteNat(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteNatOptions ) @@ -36,7 +38,12 @@ func DeleteNat(factory DPDKClientFactory) *cobra.Command { Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - return RunDeleteNat(cmd.Context(), factory, opts) + return RunDeleteNat( + cmd.Context(), + factory, + rendererFactory, + opts, + ) }, } @@ -64,7 +71,7 @@ func (o *DeleteNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeleteNat(ctx context.Context, factory DPDKClientFactory, opts DeleteNatOptions) error { +func RunDeleteNat(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteNatOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -79,7 +86,22 @@ func RunDeleteNat(ctx context.Context, factory DPDKClientFactory, opts DeleteNat return fmt.Errorf("error deleting nat of interface %s: %v", opts.InterfaceID, err) } - fmt.Printf("Deleted NAT of interface %s\n", opts.InterfaceID) + renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + nat := api.Nat{ + TypeMeta: api.TypeMeta{Kind: api.NatKind}, + NatMeta: api.NatMeta{ + InterfaceID: opts.InterfaceID, + }, + Status: api.Status{ + Message: "Deleted", + }, + } + if err := renderer.Render(&nat); err != nil { + return fmt.Errorf("error rendering prefix: %w", err) + } return nil } diff --git a/cmd/delete_neighbor_nat.go b/cmd/delete_neighbor_nat.go index 2a81f95..67c3b2f 100644 --- a/cmd/delete_neighbor_nat.go +++ b/cmd/delete_neighbor_nat.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "net/netip" + "os" "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/flag" @@ -26,7 +27,7 @@ import ( "github.com/spf13/pflag" ) -func DeleteNeighborNat(factory DPDKClientFactory) *cobra.Command { +func DeleteNeighborNat(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteNeighborNatOptions ) @@ -39,7 +40,12 @@ func DeleteNeighborNat(factory DPDKClientFactory) *cobra.Command { Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - return RunDeleteNeighborNat(cmd.Context(), factory, opts) + return RunDeleteNeighborNat( + cmd.Context(), + factory, + rendererFactory, + opts, + ) }, } @@ -73,7 +79,7 @@ func (o *DeleteNeighborNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeleteNeighborNat(ctx context.Context, factory DPDKClientFactory, opts DeleteNeighborNatOptions) error { +func RunDeleteNeighborNat(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteNeighborNatOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -86,7 +92,7 @@ func RunDeleteNeighborNat(ctx context.Context, factory DPDKClientFactory, opts D neigbhorNat := api.NeighborNat{ TypeMeta: api.TypeMeta{Kind: api.NatKind}, - NeighborNatMeta: api.NeighborNatMeta{NatVIPIP: opts.NatIP}, + NeighborNatMeta: api.NeighborNatMeta{NatVIPIP: &opts.NatIP}, Spec: api.NeighborNatSpec{ Vni: opts.Vni, MinPort: opts.MinPort, @@ -97,7 +103,22 @@ func RunDeleteNeighborNat(ctx context.Context, factory DPDKClientFactory, opts D return fmt.Errorf("error deleting neighbor nat with ip %s: %v", opts.NatIP, err) } - fmt.Printf("Deleted neighbor NAT with IP %s\n", opts.NatIP) + renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + nNat := api.NeighborNat{ + TypeMeta: api.TypeMeta{Kind: api.NeighborNatKind}, + NeighborNatMeta: api.NeighborNatMeta{ + NatVIPIP: &opts.NatIP, + }, + Status: api.Status{ + Message: "Deleted", + }, + } + if err := renderer.Render(&nNat); err != nil { + return fmt.Errorf("error rendering prefix: %w", err) + } return nil } diff --git a/cmd/delete_prefix.go b/cmd/delete_prefix.go index b8516e1..15beb23 100644 --- a/cmd/delete_prefix.go +++ b/cmd/delete_prefix.go @@ -18,14 +18,16 @@ import ( "context" "fmt" "net/netip" + "os" + "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeletePrefix(factory DPDKClientFactory) *cobra.Command { +func DeletePrefix(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeletePrefixOptions ) @@ -38,7 +40,12 @@ func DeletePrefix(factory DPDKClientFactory) *cobra.Command { Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - return RunDeletePrefix(cmd.Context(), factory, opts) + return RunDeletePrefix( + cmd.Context(), + factory, + rendererFactory, + opts, + ) }, } @@ -68,7 +75,7 @@ func (o *DeletePrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeletePrefix(ctx context.Context, factory DPDKClientFactory, opts DeletePrefixOptions) error { +func RunDeletePrefix(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeletePrefixOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error deleting dpdk client: %w", err) @@ -82,7 +89,24 @@ func RunDeletePrefix(ctx context.Context, factory DPDKClientFactory, opts Delete if err := client.DeletePrefix(ctx, opts.InterfaceID, opts.Prefix); err != nil { return fmt.Errorf("error deleting prefix %s/%v: %v", opts.InterfaceID, opts.Prefix, err) } - fmt.Printf("Deleted prefix %s/%v\n", opts.InterfaceID, opts.Prefix) + + renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + prefix := api.Prefix{ + TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, + PrefixMeta: api.PrefixMeta{ + InterfaceID: opts.InterfaceID, + Prefix: opts.Prefix, + }, + Status: api.Status{ + Message: "Deleted", + }, + } + if err := renderer.Render(&prefix); err != nil { + return fmt.Errorf("error rendering prefix: %w", err) + } return nil } diff --git a/cmd/delete_route.go b/cmd/delete_route.go index daa877d..5bf84bc 100644 --- a/cmd/delete_route.go +++ b/cmd/delete_route.go @@ -18,14 +18,16 @@ import ( "context" "fmt" "net/netip" + "os" + "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteRoute(factory DPDKClientFactory) *cobra.Command { +func DeleteRoute(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteRouteOptions ) @@ -38,7 +40,12 @@ func DeleteRoute(factory DPDKClientFactory) *cobra.Command { Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - return RunDeleteRoute(cmd.Context(), factory, opts) + return RunDeleteRoute( + cmd.Context(), + factory, + rendererFactory, + opts, + ) }, } @@ -72,7 +79,7 @@ func (o *DeleteRouteOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeleteRoute(ctx context.Context, factory DPDKClientFactory, opts DeleteRouteOptions) error { +func RunDeleteRoute(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteRouteOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error deleting dpdk client: %w", err) @@ -86,7 +93,28 @@ func RunDeleteRoute(ctx context.Context, factory DPDKClientFactory, opts DeleteR if err := client.DeleteRoute(ctx, opts.VNI, opts.Prefix, opts.NextHopVNI, opts.NextHopIP); err != nil { return fmt.Errorf("error deleting route %d-%v:%d-%v: %v", opts.VNI, opts.Prefix, opts.NextHopVNI, opts.NextHopIP, err) } - fmt.Printf("Deleted route %d-%v:%d-%v\n", opts.VNI, opts.Prefix, opts.NextHopVNI, opts.NextHopIP) + + renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + route := api.Route{ + TypeMeta: api.TypeMeta{Kind: api.RouteKind}, + RouteMeta: api.RouteMeta{ + VNI: opts.VNI, + Prefix: opts.Prefix, + NextHop: api.RouteNextHop{ + VNI: opts.NextHopVNI, + IP: opts.NextHopIP, + }, + }, + Status: api.Status{ + Message: "Deleted", + }, + } + if err := renderer.Render(&route); err != nil { + return fmt.Errorf("error rendering route: %w", err) + } return nil } diff --git a/cmd/delete_virtualip.go b/cmd/delete_virtualip.go index 549d25d..5e91f86 100644 --- a/cmd/delete_virtualip.go +++ b/cmd/delete_virtualip.go @@ -17,13 +17,15 @@ package cmd import ( "context" "fmt" + "os" + "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteVirtualIP(factory DPDKClientFactory) *cobra.Command { +func DeleteVirtualIP(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteVirtualIPOptions ) @@ -36,7 +38,12 @@ func DeleteVirtualIP(factory DPDKClientFactory) *cobra.Command { Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - return RunDeleteVirtualIP(cmd.Context(), factory, opts) + return RunDeleteVirtualIP( + cmd.Context(), + factory, + rendererFactory, + opts, + ) }, } @@ -64,7 +71,7 @@ func (o *DeleteVirtualIPOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeleteVirtualIP(ctx context.Context, factory DPDKClientFactory, opts DeleteVirtualIPOptions) error { +func RunDeleteVirtualIP(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteVirtualIPOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -79,7 +86,22 @@ func RunDeleteVirtualIP(ctx context.Context, factory DPDKClientFactory, opts Del return fmt.Errorf("error deleting virtual ip of interface %s: %v", opts.InterfaceID, err) } - fmt.Printf("Deleted virtual ip of interface %s\n", opts.InterfaceID) + renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + virtualIP := api.VirtualIP{ + TypeMeta: api.TypeMeta{Kind: api.VirtualIPKind}, + VirtualIPMeta: api.VirtualIPMeta{ + InterfaceID: opts.InterfaceID, + }, + Status: api.Status{ + Message: "Deleted", + }, + } + if err := renderer.Render(&virtualIP); err != nil { + return fmt.Errorf("error rendering prefix: %w", err) + } return nil } diff --git a/cmd/init.go b/cmd/init.go index 38e0014..bc6c24a 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -86,5 +86,7 @@ func RunInit( return fmt.Errorf("error: %w", err) } + fmt.Println("{\"status\": \"initialized\"}") + return nil } diff --git a/cmd/initialized.go b/cmd/initialized.go index beab681..7ea4d34 100644 --- a/cmd/initialized.go +++ b/cmd/initialized.go @@ -57,6 +57,6 @@ func RunInitialized( if err != nil { return fmt.Errorf("error: %w", err) } - fmt.Println("UUID of dp-service:", uuid) + fmt.Printf("{\"status\": \"initialized\", \"uuid\": \"%s\"}\n", uuid) return nil } diff --git a/docs/development/README.md b/docs/development/README.md index 94893db..3dfac08 100644 --- a/docs/development/README.md +++ b/docs/development/README.md @@ -10,7 +10,7 @@ Before using dpservice-cli client, you need to have dpservice instance running. Please refer to this guide [net-dpservice](https://github.com/onmetal/net-dpservice/blob/osc/grpc_docs/docs/development/building.md) on how to build dpservice from source. -You can then run python script /test/dp_service.py that will start the dpservice with preloaded config. +You can then run python script **/test/dp_service.py** that will start the dpservice with preloaded config. ```bash sudo ./test/dp_service.py ``` @@ -23,19 +23,23 @@ echo 2048 > /sys/devices/system/node/node0/hugepages/hugepages-2048kB/nr_hugepag ## Running dpservice-cli -Go version 1.18 or newer is needed. \"make\" tool is also needed to utilize the Makefile. - +Go version 1.18 or newer is needed. \"make\" tool is also needed to utilize the Makefile. To run the dpservice-cli client build the binary first and then use it with commands and flags: ```bash make build ./bin/dpservice-cli -h ``` -When you are running dpservice on the same VM you don't need to specify the address and defaults are used (localhost:1337). - -If dpservice is running on different machine or you changed the default settings, use --address flag: +When you are running dpservice on the same VM you don't need to specify the address and defaults are used (localhost:1337). +If dpservice is running on different machine or you changed the default settings, use **--address \** flag: ```bash ./bin/dpservice-cli --address [command] [flags] ``` +To change the output format of commands you can use **-o, --output** flag with one of **json | yaml | table | name** + + - **json** - shows output in json (you can use --pretty flag to show formatted json) + - **yaml** - shows output in yaml + - **table** - shows output in predefined table format + - **name** - shows only short output with type/name
diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index 27974af..9ea3a5a 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -58,11 +58,11 @@ func ProtoLoadBalancerToLoadBalancer(dpdkLB *proto.GetLoadBalancerResponse, lbID }, Spec: LoadBalancerSpec{ VNI: dpdkLB.Vni, - LbVipIP: lbip, + LbVipIP: &lbip, Lbports: lbports, - UnderlayRoute: underlayRoute, + UnderlayRoute: &underlayRoute, }, - Status: LoadBalancerStatus{ + Status: Status{ Error: dpdkLB.Status.Error, Message: dpdkLB.Status.Message, }, @@ -125,10 +125,10 @@ func ProtoInterfaceToInterface(dpdkIface *proto.Interface) (*Interface, error) { ips = append(ips, ip) } - var underlayIP netip.Addr - if underlayIPString := string(dpdkIface.GetUnderlayRoute()); underlayIPString != "" { + var underlayRoute netip.Addr + if underlayRouteString := string(dpdkIface.GetUnderlayRoute()); underlayRouteString != "" { var err error - underlayIP, err = netip.ParseAddr(string(dpdkIface.GetUnderlayRoute())) + underlayRoute, err = netip.ParseAddr(string(dpdkIface.GetUnderlayRoute())) if err != nil { return nil, fmt.Errorf("error parsing underlay ip: %w", err) } @@ -142,12 +142,10 @@ func ProtoInterfaceToInterface(dpdkIface *proto.Interface) (*Interface, error) { ID: string(dpdkIface.InterfaceID), }, Spec: InterfaceSpec{ - VNI: dpdkIface.GetVni(), - Device: dpdkIface.GetPciDpName(), - IPs: ips, - }, - Status: InterfaceStatus{ - UnderlayIP: &underlayIP, + VNI: dpdkIface.GetVni(), + Device: dpdkIface.GetPciDpName(), + IPs: ips, + UnderlayRoute: &underlayRoute, }, }, nil } @@ -216,7 +214,7 @@ func ProtoPrefixToPrefix(interfaceID string, dpdkPrefix *proto.Prefix) (*Prefix, InterfaceID: interfaceID, Prefix: prefix, }, - Spec: PrefixSpec{UnderlayRoute: underlayRoute}, + Spec: PrefixSpec{UnderlayRoute: &underlayRoute}, }, nil } @@ -284,12 +282,12 @@ func ProtoNatToNat(dpdkNat *proto.GetNATResponse, interfaceID string) (*Nat, err InterfaceID: interfaceID, }, Spec: NatSpec{ - NatVIPIP: natvipip, + NatVIPIP: &natvipip, MinPort: dpdkNat.MinPort, MaxPort: dpdkNat.MaxPort, - UnderlayRoute: underlayRoute, + UnderlayRoute: &underlayRoute, }, - Status: NatStatus{ + Status: Status{ Error: dpdkNat.Status.Error, Message: dpdkNat.Status.Message, }, @@ -319,9 +317,9 @@ func ProtoFwRuleToFwRule(dpdkFwRule *proto.FirewallRule, interfaceID string) (*F FirewallAction: uint8(dpdkFwRule.Action), Priority: dpdkFwRule.Priority, IpVersion: uint8(dpdkFwRule.IpVersion), - SourcePrefix: srcPrefix.Prefix, - DestinationPrefix: dstPrefix.Prefix, - ProtocolFilter: *dpdkFwRule.ProtocolFilter, + SourcePrefix: &srcPrefix.Prefix, + DestinationPrefix: &dstPrefix.Prefix, + ProtocolFilter: dpdkFwRule.ProtocolFilter, }, }, nil } diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 02ab49a..08842d3 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -39,6 +39,11 @@ func (m *TypeMeta) GetKind() string { return m.Kind } +type Status struct { + Error int32 `json:"error,omitempty"` + Message string `json:"message,omitempty"` +} + type RouteList struct { TypeMeta `json:",inline"` Items []Route `json:"items"` @@ -56,6 +61,7 @@ type Route struct { TypeMeta `json:",inline"` RouteMeta `json:"metadata"` Spec RouteSpec `json:"spec"` + Status Status `json:"status"` } type RouteMeta struct { @@ -93,6 +99,7 @@ type Prefix struct { TypeMeta `json:",inline"` PrefixMeta `json:"metadata"` Spec PrefixSpec `json:"spec"` + Status Status `json:"status"` } type PrefixMeta struct { @@ -105,13 +112,14 @@ func (m *PrefixMeta) GetName() string { } type PrefixSpec struct { - UnderlayRoute netip.Addr `json:"underplayRoute"` + UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` } type VirtualIP struct { TypeMeta `json:",inline"` VirtualIPMeta `json:"metadata"` Spec VirtualIPSpec `json:"spec"` + Status Status `json:"status"` } type VirtualIPMeta struct { @@ -124,15 +132,15 @@ func (m *VirtualIPMeta) GetName() string { } type VirtualIPSpec struct { - UnderlayRoute netip.Addr `json:"underlayRoute"` + UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` } // LoadBalancer section type LoadBalancer struct { TypeMeta `json:",inline"` LoadBalancerMeta `json:"metadata"` - Spec LoadBalancerSpec `json:"spec"` - Status LoadBalancerStatus `json:"status"` + Spec LoadBalancerSpec `json:"spec"` + Status Status `json:"status"` } type LoadBalancerMeta struct { @@ -144,25 +152,15 @@ func (m *LoadBalancerMeta) GetName() string { } type LoadBalancerSpec struct { - VNI uint32 `json:"vni"` - LbVipIP netip.Addr `json:"lbVipIP"` - Lbports []LBPort `json:"lbports"` - UnderlayRoute netip.Addr `json:"underlayRoute"` -} - -type LoadBalancerStatus struct { - Error int32 `json:"error,omitempty"` - Message string `json:"message,omitempty"` -} - -type LBIP struct { - IpVersion string `json:"ipVersion"` - Address netip.Addr `json:"address"` + VNI uint32 `json:"vni,omitempty"` + LbVipIP *netip.Addr `json:"lbVipIP,omitempty"` + Lbports []LBPort `json:"lbports,omitempty"` + UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` } type LBPort struct { - Protocol uint32 `json:"protocol"` - Port uint32 `json:"port"` + Protocol uint32 `json:"protocol,omitempty"` + Port uint32 `json:"port,omitempty"` } type LoadBalancerList struct { @@ -182,6 +180,7 @@ type LoadBalancerTarget struct { TypeMeta `json:",inline"` LoadBalancerTargetMeta `json:"metadata"` Spec LoadBalancerTargetSpec `json:"spec"` + Status Status `json:"status"` } type LoadBalancerTargetMeta struct { @@ -193,7 +192,7 @@ func (m *LoadBalancerTargetMeta) GetName() string { } type LoadBalancerTargetSpec struct { - TargetIP netip.Addr `json:"targetIP"` + TargetIP *netip.Addr `json:"targetIP,omitempty"` } type LoadBalancerTargetList struct { @@ -205,8 +204,8 @@ type LoadBalancerTargetList struct { type Interface struct { TypeMeta `json:",inline"` InterfaceMeta `json:"metadata"` - Spec InterfaceSpec `json:"spec"` - Status InterfaceStatus `json:"status"` + Spec InterfaceSpec `json:"spec"` + Status Status `json:"status"` } type InterfaceMeta struct { @@ -218,13 +217,10 @@ func (m *InterfaceMeta) GetName() string { } type InterfaceSpec struct { - VNI uint32 `json:"vni,omitempty"` - Device string `json:"device,omitempty"` - IPs []netip.Addr `json:"ips,omitempty"` -} - -type InterfaceStatus struct { - UnderlayIP *netip.Addr `json:"underlayIP,omitempty"` + VNI uint32 `json:"vni,omitempty"` + Device string `json:"device,omitempty"` + IPs []netip.Addr `json:"ips,omitempty"` + UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` VirtualFunction *VirtualFunction `json:"virtualFunction,omitempty"` } @@ -257,8 +253,8 @@ func (l *InterfaceList) GetItems() []Object { type Nat struct { TypeMeta `json:",inline"` NatMeta `json:"metadata"` - Spec NatSpec `json:"spec"` - Status NatStatus `json:"status"` + Spec NatSpec `json:"spec"` + Status Status `json:"status"` } type NatMeta struct { @@ -270,15 +266,10 @@ func (m *NatMeta) GetName() string { } type NatSpec struct { - NatVIPIP netip.Addr `json:"natVIPIP"` - MinPort uint32 `json:"minPort"` - MaxPort uint32 `json:"maxPort"` - UnderlayRoute netip.Addr `json:"underlayRoute"` -} - -type NatStatus struct { - Error int32 `json:"error,omitempty"` - Message string `json:"message,omitempty"` + NatVIPIP *netip.Addr `json:"natVIPIP,omitempty"` + MinPort uint32 `json:"minPort,omitempty"` + MaxPort uint32 `json:"maxPort,omitempty"` + UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` } type NatList struct { @@ -297,12 +288,12 @@ func (l *NatList) GetItems() []Object { type NeighborNat struct { TypeMeta `json:",inline"` NeighborNatMeta `json:"metadata"` - Spec NeighborNatSpec `json:"spec"` - Status NeighborNatStatus `json:"status"` + Spec NeighborNatSpec `json:"spec"` + Status Status `json:"status"` } type NeighborNatMeta struct { - NatVIPIP netip.Addr `json:"natVIPIP"` + NatVIPIP *netip.Addr `json:"natVIPIP"` } func (m *NeighborNatMeta) GetName() string { @@ -310,23 +301,18 @@ func (m *NeighborNatMeta) GetName() string { } type NeighborNatSpec struct { - Vni uint32 `json:"vni"` - MinPort uint32 `json:"minPort"` - MaxPort uint32 `json:"maxPort"` - UnderlayRoute netip.Addr `json:"underlayRoute"` -} - -type NeighborNatStatus struct { - Error int32 `json:"error,omitempty"` - Message string `json:"message,omitempty"` + Vni uint32 `json:"vni,omitempty"` + MinPort uint32 `json:"minPort,omitempty"` + MaxPort uint32 `json:"maxPort,omitempty"` + UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` } // FirewallRule section type FirewallRule struct { TypeMeta `json:",inline"` FirewallRuleMeta `json:"metadata"` - Spec FirewallRuleSpec `json:"spec"` - Status FirewallRuleStatus `json:"status"` + Spec FirewallRuleSpec `json:"spec"` + Status Status `json:"status"` } type FirewallRuleMeta struct { @@ -339,18 +325,13 @@ func (m *FirewallRuleMeta) GetName() string { } type FirewallRuleSpec struct { - TrafficDirection uint8 `json:"trafficeDirection"` - FirewallAction uint8 `json:"firewallAction"` - Priority uint32 `json:"priority"` - IpVersion uint8 `json:"ipVersion"` - SourcePrefix netip.Prefix `json:"sourcePrefix"` - DestinationPrefix netip.Prefix `json:"destinationPrefix"` - ProtocolFilter proto.ProtocolFilter `json:"protocolFilter"` -} - -type FirewallRuleStatus struct { - Error int32 `json:"error,omitempty"` - Message string `json:"message,omitempty"` + TrafficDirection uint8 `json:"trafficeDirection,omitempty"` + FirewallAction uint8 `json:"firewallAction,omitempty"` + Priority uint32 `json:"priority,omitempty"` + IpVersion uint8 `json:"ipVersion,omitempty"` + SourcePrefix *netip.Prefix `json:"sourcePrefix,omitempty"` + DestinationPrefix *netip.Prefix `json:"destinationPrefix,omitempty"` + ProtocolFilter *proto.ProtocolFilter `json:"protocolFilter,omitempty"` } type FirewallRuleList struct { diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 785d6df..bc1378d 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -101,7 +101,7 @@ func (c *client) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) ( res, err := c.DPDKonmetalClient.CreateLoadBalancer(ctx, &dpdkproto.CreateLoadBalancerRequest{ LoadBalancerID: []byte(lb.LoadBalancerMeta.ID), Vni: lb.Spec.VNI, - LbVipIP: api.LbipToProtoLbip(lb.Spec.LbVipIP), + LbVipIP: api.LbipToProtoLbip(*lb.Spec.LbVipIP), Lbports: lbPorts, }) if err != nil { @@ -115,13 +115,13 @@ func (c *client) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) ( if err != nil { return nil, fmt.Errorf("error parsing underlay route: %w", err) } - lb.Spec.UnderlayRoute = underlayRoute + lb.Spec.UnderlayRoute = &underlayRoute return &api.LoadBalancer{ TypeMeta: api.TypeMeta{Kind: api.LoadBalancerKind}, LoadBalancerMeta: lb.LoadBalancerMeta, Spec: lb.Spec, - Status: api.LoadBalancerStatus{ + Status: api.Status{ Error: res.Status.Error, Message: res.Status.Message, }, @@ -158,7 +158,7 @@ func (c *client) ListLoadBalancerPrefixes(ctx context.Context, interfaceID strin } return &api.PrefixList{ - TypeMeta: api.TypeMeta{Kind: api.PrefixListKind}, + TypeMeta: api.TypeMeta{Kind: "LoadBalancerPrefixList"}, Items: prefixes, }, nil } @@ -185,9 +185,9 @@ func (c *client) CreateLoadBalancerPrefix(ctx context.Context, prefix *api.Prefi return nil, fmt.Errorf("error parsing underlay route: %w", err) } return &api.Prefix{ - TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, + TypeMeta: api.TypeMeta{Kind: "LoadBalancerPrefix"}, PrefixMeta: prefix.PrefixMeta, - Spec: api.PrefixSpec{UnderlayRoute: underlayRoute}, + Spec: api.PrefixSpec{UnderlayRoute: &underlayRoute}, }, nil } @@ -226,7 +226,7 @@ func (c *client) GetLoadBalancerTargets(ctx context.Context, loadBalancerID stri for i, dpdkLBtarget := range res.GetTargetIPs() { var lbtarget api.LoadBalancerTarget lbtarget.TypeMeta.Kind = api.LoadBalancerTargetKind - lbtarget.Spec.TargetIP = *api.ProtoLbipToLbip(*dpdkLBtarget) + lbtarget.Spec.TargetIP = api.ProtoLbipToLbip(*dpdkLBtarget) lbtarget.LoadBalancerTargetMeta.ID = loadBalancerID lbtargets[i] = lbtarget @@ -241,7 +241,7 @@ func (c *client) GetLoadBalancerTargets(ctx context.Context, loadBalancerID stri func (c *client) AddLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBalancerTarget) (*api.LoadBalancerTarget, error) { res, err := c.DPDKonmetalClient.AddLoadBalancerTarget(ctx, &dpdkproto.AddLoadBalancerTargetRequest{ LoadBalancerID: []byte(lbtarget.LoadBalancerTargetMeta.ID), - TargetIP: api.LbipToProtoLbip(lbtarget.Spec.TargetIP), + TargetIP: api.LbipToProtoLbip(*lbtarget.Spec.TargetIP), }) if err != nil { return nil, err @@ -320,7 +320,7 @@ func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*ap return nil, apierrors.NewStatusError(errorCode, res.GetResponse().GetStatus().GetMessage()) } - underlayIP, err := netip.ParseAddr(string(res.GetResponse().GetUnderlayRoute())) + underlayRoute, err := netip.ParseAddr(string(res.GetResponse().GetUnderlayRoute())) if err != nil { return nil, fmt.Errorf("error parsing underlay route: %w", err) } @@ -328,9 +328,11 @@ func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*ap return &api.Interface{ TypeMeta: api.TypeMeta{Kind: api.InterfaceKind}, InterfaceMeta: iface.InterfaceMeta, - Spec: iface.Spec, // TODO: Enable dynamic device allocation - Status: api.InterfaceStatus{ - UnderlayIP: &underlayIP, + Spec: api.InterfaceSpec{ + VNI: iface.Spec.VNI, + Device: iface.Spec.Device, + IPs: iface.Spec.IPs, + UnderlayRoute: &underlayRoute, VirtualFunction: &api.VirtualFunction{ Name: res.Vf.Name, Domain: res.Vf.Domain, @@ -339,6 +341,10 @@ func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*ap Function: res.Vf.Function, }, }, + Status: api.Status{ + Error: res.Response.Status.Error, + Message: res.Response.Status.Message, + }, }, nil } @@ -389,7 +395,7 @@ func (c *client) AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*a return &api.VirtualIP{ TypeMeta: api.TypeMeta{Kind: api.VirtualIPKind}, VirtualIPMeta: virtualIP.VirtualIPMeta, - Spec: api.VirtualIPSpec{UnderlayRoute: underlayRoute}, + Spec: api.VirtualIPSpec{UnderlayRoute: &underlayRoute}, }, nil } @@ -454,7 +460,7 @@ func (c *client) AddPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix return &api.Prefix{ TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, PrefixMeta: prefix.PrefixMeta, - Spec: api.PrefixSpec{UnderlayRoute: underlayRoute}, + Spec: api.PrefixSpec{UnderlayRoute: &underlayRoute}, }, nil } @@ -570,7 +576,7 @@ func (c *client) AddNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) { res, err := c.DPDKonmetalClient.AddNAT(ctx, &dpdkproto.AddNATRequest{ InterfaceID: []byte(nat.NatMeta.InterfaceID), NatVIPIP: &dpdkproto.NATIP{ - IpVersion: api.NetIPAddrToProtoIPVersion(nat.Spec.NatVIPIP), + IpVersion: api.NetIPAddrToProtoIPVersion(*nat.Spec.NatVIPIP), Address: []byte(nat.Spec.NatVIPIP.String()), }, MinPort: nat.Spec.MinPort, @@ -587,13 +593,13 @@ func (c *client) AddNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) { if err != nil { return nil, fmt.Errorf("error parsing underlay route: %w", err) } - nat.Spec.UnderlayRoute = underlayRoute + nat.Spec.UnderlayRoute = &underlayRoute return &api.Nat{ TypeMeta: api.TypeMeta{Kind: api.NatKind}, NatMeta: nat.NatMeta, Spec: nat.Spec, - Status: api.NatStatus{ + Status: api.Status{ Error: res.Status.Error, Message: res.Status.Message, }, @@ -617,7 +623,7 @@ func (c *client) AddNeighborNat(ctx context.Context, nNat *api.NeighborNat) erro res, err := c.DPDKonmetalClient.AddNeighborNAT(ctx, &dpdkproto.AddNeighborNATRequest{ NatVIPIP: &dpdkproto.NATIP{ - IpVersion: api.NetIPAddrToProtoIPVersion(nNat.NeighborNatMeta.NatVIPIP), + IpVersion: api.NetIPAddrToProtoIPVersion(*nNat.NeighborNatMeta.NatVIPIP), Address: []byte(nNat.NeighborNatMeta.NatVIPIP.String()), }, Vni: nNat.Spec.Vni, @@ -656,18 +662,18 @@ func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType in if err != nil { return nil, fmt.Errorf("error parsing underlay route: %w", err) } - nat.Spec.UnderlayRoute = underlayRoute + nat.Spec.UnderlayRoute = &underlayRoute vipIP, err = netip.ParseAddr(string(res.NatVIPIP.Address)) if err != nil { return nil, fmt.Errorf("error parsing vip ip: %w", err) } - nat.Spec.NatVIPIP = vipIP + nat.Spec.NatVIPIP = &vipIP } else if res.NatInfoType == 1 { vipIP, err = netip.ParseAddr(string(natInfoEntry.GetAddress())) if err != nil { return nil, fmt.Errorf("error parsing vip ip: %w", err) } - nat.Spec.NatVIPIP = vipIP + nat.Spec.NatVIPIP = &vipIP } nat.Kind = api.NatKind nat.Spec.MinPort = natInfoEntry.MinPort @@ -684,7 +690,7 @@ func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType in func (c *client) DeleteNeighborNat(ctx context.Context, neigbhorNat api.NeighborNat) error { res, err := c.DPDKonmetalClient.DeleteNeighborNAT(ctx, &dpdkproto.DeleteNeighborNATRequest{ NatVIPIP: &dpdkproto.NATIP{ - IpVersion: api.NetIPAddrToProtoIPVersion(neigbhorNat.NatVIPIP), + IpVersion: api.NetIPAddrToProtoIPVersion(*neigbhorNat.NatVIPIP), Address: []byte(neigbhorNat.NatVIPIP.String()), }, Vni: neigbhorNat.Spec.Vni, @@ -742,7 +748,7 @@ func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) Address: []byte(fwRule.Spec.DestinationPrefix.Addr().String()), PrefixLength: uint32(fwRule.Spec.DestinationPrefix.Bits()), }, - ProtocolFilter: &fwRule.Spec.ProtocolFilter, + ProtocolFilter: fwRule.Spec.ProtocolFilter, }, }) if err != nil { @@ -755,9 +761,10 @@ func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) return &api.FirewallRule{ TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, FirewallRuleMeta: api.FirewallRuleMeta{ - RuleID: fwRule.RuleID, + RuleID: string(res.RuleID), InterfaceID: fwRule.InterfaceID, }, + Spec: fwRule.Spec, }, nil } diff --git a/renderer/renderer.go b/renderer/renderer.go index f0c7722..4adf185 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -207,7 +207,7 @@ func (t defaultTableConverter) loadBalancerTargetTable(lbtargets []api.LoadBalan for i, lbtarget := range lbtargets { columns[i] = []any{ lbtarget.LoadBalancerTargetMeta.ID, - api.NetIPAddrToProtoIPVersion(lbtarget.Spec.TargetIP), + api.NetIPAddrToProtoIPVersion(*lbtarget.Spec.TargetIP), lbtarget.Spec.TargetIP, } } @@ -220,18 +220,18 @@ func (t defaultTableConverter) loadBalancerTargetTable(lbtargets []api.LoadBalan func (t defaultTableConverter) interfaceTable(ifaces []api.Interface) (*TableData, error) { var headers []any - if ifaces[0].Status.VirtualFunction == nil { - headers = []any{"ID", "VNI", "Device", "IPs", "UnderlayIP"} + if ifaces[0].Spec.VirtualFunction == nil { + headers = []any{"ID", "VNI", "Device", "IPs", "UnderlayRoute"} } else { - headers = []any{"ID", "VNI", "Device", "IPs", "UnderlayIP", "VirtualFunction"} + headers = []any{"ID", "VNI", "Device", "IPs", "UnderlayRoute", "VirtualFunction"} } columns := make([][]any, len(ifaces)) for i, iface := range ifaces { - if ifaces[0].Status.VirtualFunction == nil { - columns[i] = []any{iface.ID, iface.Spec.VNI, iface.Spec.Device, iface.Spec.IPs, iface.Status.UnderlayIP} + if ifaces[0].Spec.VirtualFunction == nil { + columns[i] = []any{iface.ID, iface.Spec.VNI, iface.Spec.Device, iface.Spec.IPs, iface.Spec.UnderlayRoute} } else { - columns[i] = []any{iface.ID, iface.Spec.VNI, iface.Spec.Device, iface.Spec.IPs, iface.Status.UnderlayIP, iface.Status.VirtualFunction.String()} + columns[i] = []any{iface.ID, iface.Spec.VNI, iface.Spec.Device, iface.Spec.IPs, iface.Spec.UnderlayRoute, iface.Spec.VirtualFunction.String()} } } From ef074326cf95dc0b6cf5382d4a2be2abe823a739 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Mon, 22 May 2023 16:25:07 +0200 Subject: [PATCH 38/65] change AddFirewallRule flag names, error handling --- .vscode/launch.json | 5 +- cmd/add_firewall_rule.go | 43 +++++++------- cmd/add_neighbor_nat.go | 4 +- cmd/delete.go | 8 ++- cmd/delete_firewall_rule.go | 24 +++++--- cmd/delete_nat.go | 2 +- cmd/get_nat_info.go | 10 ++-- dpdk/api/conversion.go | 31 ++++++++-- dpdk/api/errors/errors.go | 4 ++ dpdk/api/types.go | 22 +++++-- dpdk/client/client.go | 113 ++++++++++++++++++++++++++---------- main.go | 10 +++- renderer/renderer.go | 40 +++++++++++-- 13 files changed, 230 insertions(+), 86 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 3329cbf..4c25ae1 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -16,13 +16,14 @@ //"args": ["delete", "route", "10.200.2.0/24", "0", "ff80::1", "--vni", "100"] //"args": ["add", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] //"args": ["add", "lbtarget", "ff80::1", "--lb-id", "1"] - //"args": ["get", "natinfo", "10.20.30.40", "--nat-type=2"] + //"args": ["get", "natinfo", "--nat-ip=10.20.30.40", "--nat-type=Local"] //"args": ["get", "fwrule", "vm1", "--rule-id=4"] //"args": ["add", "fwrule", "vm1", "--action=1", "--direction=1", "--dst=5.5.5.0/24", "--ipv=0", // "--priority=100", "--rule-id=9", "--src=1.1.1.1/32", "--protocol=tcp"] //"args": ["add", "nnat", "10.20.30.40", "--vni", "100", "--minport", "30000", "--maxport", "30100", "--underlayroute", "ff80::1"] //"args": ["add", "interface", "--id=vm4", "--ip=10.200.1.4", "--ip=2000:200:1::4", "--vni=200", "--device=net_tap5"] - "args": ["add", "-f", "/tmp/addint.json"] + //"args": ["add", "-f", "/tmp/addint.json"] + "args": ["delete", "firewallrule", "--interface-id=vm1", "--rule-id=12"] } ] } \ No newline at end of file diff --git a/cmd/add_firewall_rule.go b/cmd/add_firewall_rule.go index 50b843e..e5dfa0d 100644 --- a/cmd/add_firewall_rule.go +++ b/cmd/add_firewall_rule.go @@ -36,7 +36,7 @@ func AddFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory Render cmd := &cobra.Command{ Use: "firewallrule <--interface-id> [flags]", Short: "Add a FirewallRule to interface", - Example: "dpservice-cli add fwrule --interface-id=vm1 --action=1 --direction=1 --dst=5.5.5.0/24 --ipv=0 --priority=100 --rule-id=12 --src=1.1.1.1/32 --protocol=tcp --srcPortLower=1 --srcPortUpper=1000 --dstPortLower=500 --dstPortUpper=600", + Example: "dpservice-cli add fwrule --interface-id=vm1 --action=1 --direction=1 --dst=5.5.5.0/24 --ipv=0 --priority=100 --rule-id=12 --src=1.1.1.1/32 --protocol=tcp --src-port-min=1 --src-port-max=1000 --dst-port-min=500 --dst-port-max=600", Aliases: FirewallRuleAliases, Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { @@ -60,10 +60,10 @@ func AddFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory Render type AddFirewallRuleOptions struct { InterfaceID string RuleID string - TrafficDirection uint8 - FirewallAction uint8 + TrafficDirection string + FirewallAction string Priority uint32 - IpVersion uint8 + IpVersion string SourcePrefix netip.Prefix DestinationPrefix netip.Prefix ProtocolFilter string @@ -78,19 +78,19 @@ type AddFirewallRuleOptions struct { func (o *AddFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "InterfaceID of FW Rule.") fs.StringVar(&o.RuleID, "rule-id", o.RuleID, "RuleID of FW Rule.") - fs.Uint8Var(&o.TrafficDirection, "direction", o.TrafficDirection, "Traffic direction of FW Rule: Ingress = 0/Egress = 1") - fs.Uint8Var(&o.FirewallAction, "action", o.FirewallAction, "Firewall action: Drop = 0/Accept = 1 // Can be only \"accept\" at the moment.") - fs.Uint32Var(&o.Priority, "priority", o.Priority, "Priority of FW Rule. // For future use. No effect at the moment.") - fs.Uint8Var(&o.IpVersion, "ipv", o.IpVersion, "IpVersion of FW Rule IPv4 = 0/IPv6 = 1.") - flag.PrefixVar(fs, &o.SourcePrefix, "src", o.SourcePrefix, "Source prefix // 0.0.0.0 with prefix length 0 matches all source IPs.") - flag.PrefixVar(fs, &o.DestinationPrefix, "dst", o.DestinationPrefix, "Destination prefix // 0.0.0.0 with prefix length 0 matches all destination IPs.") - fs.StringVar(&o.ProtocolFilter, "protocol", o.ProtocolFilter, "Protocol used icmp/tcp/udp // Not defining a protocol filter matches all protocols.") - fs.Int32Var(&o.SrcPortLower, "srcPortLower", o.SrcPortLower, "Source Ports start // -1 matches all source ports.") - fs.Int32Var(&o.SrcPortUpper, "srcPortUpper", o.SrcPortUpper, "Source Ports end.") - fs.Int32Var(&o.DstPortLower, "dstPortLower", o.DstPortLower, "Destination Ports start // -1 matches all destination ports.") - fs.Int32Var(&o.DstPortUpper, "dstPortUpper", o.DstPortUpper, "Destination Ports end.") - fs.Int32Var(&o.IcmpType, "icmpType", o.IcmpType, "ICMP type // -1 matches all ICMP Types.") - fs.Int32Var(&o.IcmpCode, "icmpCode", o.IcmpCode, "ICMP code // -1 matches all ICMP Codes.") + fs.StringVar(&o.TrafficDirection, "direction", o.TrafficDirection, "Traffic direction of FW Rule: Ingress = 0/Egress = 1") + fs.StringVar(&o.FirewallAction, "action", o.FirewallAction, "Firewall action: Drop = 0/Accept = 1 (Can be only \"accept\" at the moment).") + fs.Uint32Var(&o.Priority, "priority", o.Priority, "Priority of FW Rule. (For future use. No effect at the moment).") + fs.StringVar(&o.IpVersion, "ipv", o.IpVersion, "IpVersion of FW Rule IPv4 = 0/IPv6 = 1.") + flag.PrefixVar(fs, &o.SourcePrefix, "src", o.SourcePrefix, "Source prefix (0.0.0.0 with prefix length 0 matches all source IPs).") + flag.PrefixVar(fs, &o.DestinationPrefix, "dst", o.DestinationPrefix, "Destination prefix (0.0.0.0 with prefix length 0 matches all destination IPs).") + fs.StringVar(&o.ProtocolFilter, "protocol", o.ProtocolFilter, "Protocol used icmp/tcp/udp (Not defining a protocol filter matches all protocols).") + fs.Int32Var(&o.SrcPortLower, "src-port-min", o.SrcPortLower, "Source Ports start (-1 matches all source ports).") + fs.Int32Var(&o.SrcPortUpper, "src-port-max", o.SrcPortUpper, "Source Ports end.") + fs.Int32Var(&o.DstPortLower, "dst-port-min", o.DstPortLower, "Destination Ports start (-1 matches all destination ports).") + fs.Int32Var(&o.DstPortUpper, "dst-port-max", o.DstPortUpper, "Destination Ports end.") + fs.Int32Var(&o.IcmpType, "icmp-type", o.IcmpType, "ICMP type (-1 matches all ICMP Types).") + fs.Int32Var(&o.IcmpCode, "icmp-code", o.IcmpCode, "ICMP code (-1 matches all ICMP Codes).") } @@ -132,11 +132,11 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory // TODO add cases if icmp type or code is -1 var protocolFilter dpdkproto.ProtocolFilter switch opts.ProtocolFilter { - case "icmp": + case "icmp", "1": protocolFilter.Filter = &dpdkproto.ProtocolFilter_Icmp{Icmp: &dpdkproto.ICMPFilter{ IcmpType: opts.IcmpType, IcmpCode: opts.IcmpCode}} - case "tcp": + case "tcp", "6": if opts.SrcPortLower == -1 { opts.SrcPortLower = 1 opts.SrcPortUpper = 65535 @@ -151,7 +151,7 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory DstPortLower: opts.DstPortLower, DstPortUpper: opts.DstPortUpper, }} - case "udp": + case "udp", "17": if opts.SrcPortLower == -1 { opts.SrcPortLower = 1 opts.SrcPortUpper = 65535 @@ -166,6 +166,8 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory DstPortLower: opts.DstPortLower, DstPortUpper: opts.DstPortUpper, }} + default: + return fmt.Errorf("protocol can be only: icmp = 1/tcp = 6/udp = 17") } fwrule, err := client.AddFirewallRule(ctx, &api.FirewallRule{ @@ -178,6 +180,7 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory TrafficDirection: opts.TrafficDirection, FirewallAction: opts.FirewallAction, Priority: opts.Priority, + IpVersion: opts.IpVersion, SourcePrefix: &srcPfx, DestinationPrefix: &dstPfx, ProtocolFilter: &dpdkproto.ProtocolFilter{ diff --git a/cmd/add_neighbor_nat.go b/cmd/add_neighbor_nat.go index 9ffb15a..192145d 100644 --- a/cmd/add_neighbor_nat.go +++ b/cmd/add_neighbor_nat.go @@ -104,8 +104,8 @@ func RunAddNeighborNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, UnderlayRoute: &opts.UnderlayRoute, }, } - err = client.AddNeighborNat(ctx, nNat) + neighNat, err := client.AddNeighborNat(ctx, nNat) if err != nil { return fmt.Errorf("error adding neighbor nat: %w", err) } @@ -114,7 +114,7 @@ func RunAddNeighborNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, if err != nil { return fmt.Errorf("error creating renderer: %w", err) } - if err := renderer.Render(&nNat); err != nil { + if err := renderer.Render(&neighNat); err != nil { return fmt.Errorf("error rendering neighbor nat: %w", err) } diff --git a/cmd/delete.go b/cmd/delete.go index 4741b47..0c3b885 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -43,6 +43,12 @@ func Delete(factory DPDKClientFactory) *cobra.Command { sourcesOptions.AddFlags(cmd.Flags()) + errorOptions := &RendererOptions{Output: "json"} + errorRenderer, err := errorOptions.NewRenderer("", os.Stdout) + if err != nil { + return nil + } + subcommands := []*cobra.Command{ DeleteInterface(factory, rendererOptions), DeletePrefix(factory, rendererOptions), @@ -53,7 +59,7 @@ func Delete(factory DPDKClientFactory) *cobra.Command { DeleteLoadBalancerTarget(factory, rendererOptions), DeleteNat(factory, rendererOptions), DeleteNeighborNat(factory, rendererOptions), - DeleteFirewallRule(factory, rendererOptions), + DeleteFirewallRule(factory, rendererOptions, errorRenderer), } cmd.Short = fmt.Sprintf("Deletes one of %v", CommandNames(subcommands)) diff --git a/cmd/delete_firewall_rule.go b/cmd/delete_firewall_rule.go index a404b9c..26ea5c3 100644 --- a/cmd/delete_firewall_rule.go +++ b/cmd/delete_firewall_rule.go @@ -18,14 +18,17 @@ import ( "context" "fmt" "os" + "strconv" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" + "github.com/onmetal/dpservice-cli/renderer" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteFirewallRule(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func DeleteFirewallRule(factory DPDKClientFactory, rendererFactory RendererFactory, errorRenderer renderer.Renderer) *cobra.Command { var ( opts DeleteFirewallRuleOptions ) @@ -43,6 +46,7 @@ func DeleteFirewallRule(factory DPDKClientFactory, rendererFactory RendererFacto factory, rendererFactory, opts, + errorRenderer, ) }, } @@ -73,7 +77,7 @@ func (o *DeleteFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error return nil } -func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteFirewallRuleOptions) error { +func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteFirewallRuleOptions, errorRenderer renderer.Renderer) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) @@ -84,14 +88,13 @@ func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, rende } }() - if err := client.DeleteFirewallRule(ctx, opts.InterfaceID, opts.RuleID); err != nil { - return fmt.Errorf("error deleting firewall rule %s/%s: %v", opts.RuleID, opts.InterfaceID, err) + if status, err := client.DeleteFirewallRule(ctx, opts.InterfaceID, opts.RuleID); err != nil { + if rendErr := errorRenderer.Render(&status); rendErr != nil { + return fmt.Errorf("error rendering status: %w", rendErr) + } + return fmt.Errorf(strconv.Itoa(errors.SERVER_ERROR)) } - renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } fwrule := api.FirewallRule{ TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, FirewallRuleMeta: api.FirewallRuleMeta{ @@ -102,6 +105,11 @@ func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, rende Message: "Deleted", }, } + + renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } if err := renderer.Render(&fwrule); err != nil { return fmt.Errorf("error rendering prefix: %w", err) } diff --git a/cmd/delete_nat.go b/cmd/delete_nat.go index 87d1c37..acbcadf 100644 --- a/cmd/delete_nat.go +++ b/cmd/delete_nat.go @@ -95,7 +95,7 @@ func RunDeleteNat(ctx context.Context, factory DPDKClientFactory, rendererFactor NatMeta: api.NatMeta{ InterfaceID: opts.InterfaceID, }, - Status: api.Status{ + Status: &api.Status{ Message: "Deleted", }, } diff --git a/cmd/get_nat_info.go b/cmd/get_nat_info.go index 46ae3cc..bb5dcc6 100644 --- a/cmd/get_nat_info.go +++ b/cmd/get_nat_info.go @@ -55,17 +55,17 @@ func GetNatInfo(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFac } type GetNatInfoOptions struct { - NatIP netip.Addr - NatType int32 + NatIP netip.Addr + NatInfoType string } func (o *GetNatInfoOptions) AddFlags(fs *pflag.FlagSet) { flag.AddrVar(fs, &o.NatIP, "nat-ip", o.NatIP, "NAT IP to get info for") - fs.Int32Var(&o.NatType, "nat-type", o.NatType, "NAT Info type: NATInfoTypeZero = 0/NATInfoLocal = 1/NATInfoNeigh = 2") + fs.StringVar(&o.NatInfoType, "info-type", o.NatInfoType, "NAT Info type: Local = 1/Neigh(bor) = 2") } func (o *GetNatInfoOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"nat-ip", "nat-type"} { + for _, name := range []string{"nat-ip", "info-type"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -94,7 +94,7 @@ func RunGetNatInfo( return fmt.Errorf("error creating renderer: %w", err) } - natinfo, err := client.GetNATInfo(ctx, opts.NatIP, opts.NatType) + natinfo, err := client.GetNATInfo(ctx, opts.NatIP, opts.NatInfoType) if err != nil { return fmt.Errorf("error getting nat info for ip %s: %v", opts.NatIP, err) } diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index 9ea3a5a..131f7ad 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -287,7 +287,7 @@ func ProtoNatToNat(dpdkNat *proto.GetNATResponse, interfaceID string) (*Nat, err MaxPort: dpdkNat.MaxPort, UnderlayRoute: &underlayRoute, }, - Status: Status{ + Status: &Status{ Error: dpdkNat.Status.Error, Message: dpdkNat.Status.Message, }, @@ -305,6 +305,22 @@ func ProtoFwRuleToFwRule(dpdkFwRule *proto.FirewallRule, interfaceID string) (*F if err != nil { return nil, fmt.Errorf("error converting prefix: %w", err) } + var direction, action, ipv string + if dpdkFwRule.Direction == 0 { + direction = "Ingress" + } else { + direction = "Egress" + } + if dpdkFwRule.Action == 0 { + action = "Drop" + } else { + action = "Accept" + } + if dpdkFwRule.IpVersion == 0 { + ipv = "IPv4" + } else { + ipv = "IPv6" + } return &FirewallRule{ TypeMeta: TypeMeta{Kind: FirewallRuleKind}, @@ -313,13 +329,20 @@ func ProtoFwRuleToFwRule(dpdkFwRule *proto.FirewallRule, interfaceID string) (*F RuleID: string(dpdkFwRule.RuleID), }, Spec: FirewallRuleSpec{ - TrafficDirection: uint8(dpdkFwRule.Direction), - FirewallAction: uint8(dpdkFwRule.Action), + TrafficDirection: direction, + FirewallAction: action, Priority: dpdkFwRule.Priority, - IpVersion: uint8(dpdkFwRule.IpVersion), + IpVersion: ipv, SourcePrefix: &srcPrefix.Prefix, DestinationPrefix: &dstPrefix.Prefix, ProtocolFilter: dpdkFwRule.ProtocolFilter, }, }, nil } + +func ProtoStatusToStatus(dpdkStatus *proto.Status) Status { + return Status{ + Error: dpdkStatus.Error, + Message: dpdkStatus.Message, + } +} diff --git a/dpdk/api/errors/errors.go b/dpdk/api/errors/errors.go index 3ff4e9b..918cf18 100644 --- a/dpdk/api/errors/errors.go +++ b/dpdk/api/errors/errors.go @@ -104,6 +104,10 @@ const ( GET_NO_FWALL_RULE_ERR = 811 DEL_FWALL_ERR = 820 DEL_NO_FWALL_RULE_ERR = 821 + + // os.Exit value + CLIENT_ERROR = 1 + SERVER_ERROR = 2 ) type StatusError struct { diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 08842d3..d447aab 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -39,11 +39,23 @@ func (m *TypeMeta) GetKind() string { return m.Kind } +type ServerError struct { + ServerError Status `json:"serverError"` +} + type Status struct { Error int32 `json:"error,omitempty"` Message string `json:"message,omitempty"` } +func (m *Status) GetKind() string { + return "Status" +} + +func (m *Status) GetName() string { + return m.Message +} + type RouteList struct { TypeMeta `json:",inline"` Items []Route `json:"items"` @@ -254,11 +266,11 @@ type Nat struct { TypeMeta `json:",inline"` NatMeta `json:"metadata"` Spec NatSpec `json:"spec"` - Status Status `json:"status"` + Status *Status `json:"status,omitempty"` } type NatMeta struct { - InterfaceID string `json:"interfaceID"` + InterfaceID string `json:"interfaceID,omitempty"` } func (m *NatMeta) GetName() string { @@ -325,10 +337,10 @@ func (m *FirewallRuleMeta) GetName() string { } type FirewallRuleSpec struct { - TrafficDirection uint8 `json:"trafficeDirection,omitempty"` - FirewallAction uint8 `json:"firewallAction,omitempty"` + TrafficDirection string `json:"trafficDirection,omitempty"` + FirewallAction string `json:"firewallAction,omitempty"` Priority uint32 `json:"priority,omitempty"` - IpVersion uint8 `json:"ipVersion,omitempty"` + IpVersion string `json:"ipVersion,omitempty"` SourcePrefix *netip.Prefix `json:"sourcePrefix,omitempty"` DestinationPrefix *netip.Prefix `json:"destinationPrefix,omitempty"` ProtocolFilter *proto.ProtocolFilter `json:"protocolFilter,omitempty"` diff --git a/dpdk/client/client.go b/dpdk/client/client.go index bc1378d..a94e508 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "net/netip" + "strings" "github.com/onmetal/dpservice-cli/dpdk/api" apierrors "github.com/onmetal/dpservice-cli/dpdk/api/errors" @@ -59,14 +60,14 @@ type Client interface { AddNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) DeleteNat(ctx context.Context, interfaceID string) error - AddNeighborNat(ctx context.Context, nat *api.NeighborNat) error - GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType int32) (*api.NatList, error) + AddNeighborNat(ctx context.Context, nat *api.NeighborNat) (*api.NeighborNat, error) + GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType string) (*api.NatList, error) DeleteNeighborNat(ctx context.Context, neigbhorNat api.NeighborNat) error ListFirewallRules(ctx context.Context, interfaceID string) (*api.FirewallRuleList, error) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) GetFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) - DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) error + DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) (api.ServerError, error) Initialized(ctx context.Context) (string, error) Init(ctx context.Context, initConfig dpdkproto.InitConfig) error @@ -121,10 +122,7 @@ func (c *client) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) ( TypeMeta: api.TypeMeta{Kind: api.LoadBalancerKind}, LoadBalancerMeta: lb.LoadBalancerMeta, Spec: lb.Spec, - Status: api.Status{ - Error: res.Status.Error, - Message: res.Status.Message, - }, + Status: api.ProtoStatusToStatus(res.Status), }, nil } @@ -188,6 +186,7 @@ func (c *client) CreateLoadBalancerPrefix(ctx context.Context, prefix *api.Prefi TypeMeta: api.TypeMeta{Kind: "LoadBalancerPrefix"}, PrefixMeta: prefix.PrefixMeta, Spec: api.PrefixSpec{UnderlayRoute: &underlayRoute}, + Status: api.ProtoStatusToStatus(res.Status), }, nil } @@ -254,6 +253,7 @@ func (c *client) AddLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBa TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, LoadBalancerTargetMeta: lbtarget.LoadBalancerTargetMeta, Spec: lbtarget.Spec, + Status: api.ProtoStatusToStatus(res), }, nil } @@ -341,10 +341,7 @@ func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*ap Function: res.Vf.Function, }, }, - Status: api.Status{ - Error: res.Response.Status.Error, - Message: res.Response.Status.Message, - }, + Status: api.ProtoStatusToStatus(res.Response.Status), }, nil } @@ -396,6 +393,7 @@ func (c *client) AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*a TypeMeta: api.TypeMeta{Kind: api.VirtualIPKind}, VirtualIPMeta: virtualIP.VirtualIPMeta, Spec: api.VirtualIPSpec{UnderlayRoute: &underlayRoute}, + Status: api.ProtoStatusToStatus(res.Status), }, nil } @@ -461,6 +459,7 @@ func (c *client) AddPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, PrefixMeta: prefix.PrefixMeta, Spec: api.PrefixSpec{UnderlayRoute: &underlayRoute}, + Status: api.ProtoStatusToStatus(res.Status), }, nil } @@ -509,6 +508,7 @@ func (c *client) AddRoute(ctx context.Context, route *api.Route) (*api.Route, er TypeMeta: api.TypeMeta{Kind: api.RouteKind}, RouteMeta: route.RouteMeta, Spec: route.Spec, + Status: api.ProtoStatusToStatus(res), }, nil } @@ -594,15 +594,13 @@ func (c *client) AddNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) { return nil, fmt.Errorf("error parsing underlay route: %w", err) } nat.Spec.UnderlayRoute = &underlayRoute + status := api.ProtoStatusToStatus(res.Status) return &api.Nat{ TypeMeta: api.TypeMeta{Kind: api.NatKind}, NatMeta: nat.NatMeta, Spec: nat.Spec, - Status: api.Status{ - Error: res.Status.Error, - Message: res.Status.Message, - }, + Status: &status, }, nil } @@ -619,7 +617,7 @@ func (c *client) DeleteNat(ctx context.Context, interfaceID string) error { return nil } -func (c *client) AddNeighborNat(ctx context.Context, nNat *api.NeighborNat) error { +func (c *client) AddNeighborNat(ctx context.Context, nNat *api.NeighborNat) (*api.NeighborNat, error) { res, err := c.DPDKonmetalClient.AddNeighborNAT(ctx, &dpdkproto.AddNeighborNATRequest{ NatVIPIP: &dpdkproto.NATIP{ @@ -632,21 +630,36 @@ func (c *client) AddNeighborNat(ctx context.Context, nNat *api.NeighborNat) erro UnderlayRoute: []byte(nNat.Spec.UnderlayRoute.String()), }) if err != nil { - return err + return nil, err } if res.Error == 0 { - return nil + return &api.NeighborNat{ + TypeMeta: api.TypeMeta{Kind: api.NeighborNatKind}, + NeighborNatMeta: nNat.NeighborNatMeta, + Spec: nNat.Spec, + Status: api.ProtoStatusToStatus(res), + }, nil } - return fmt.Errorf("%d", res.Error) + return nil, fmt.Errorf("%d", res.Error) } -func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType int32) (*api.NatList, error) { +func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType string) (*api.NatList, error) { + var nType int32 + switch strings.ToLower(natType) { + case "local", "1": + nType = 1 + case "neigh", "2", "neighbor": + nType = 2 + default: + return nil, fmt.Errorf("nat info type can be only: Local = 1/Neigh(bor) = 2") + } + res, err := c.DPDKonmetalClient.GetNATInfo(ctx, &dpdkproto.GetNATInfoRequest{ NatVIPIP: &dpdkproto.NATIP{IpVersion: api.NetIPAddrToProtoIPVersion(natVIPIP), Address: []byte(natVIPIP.String()), }, - NatInfoType: dpdkproto.NATInfoType(natType), + NatInfoType: dpdkproto.NATInfoType(nType), }) if err != nil { return nil, err @@ -678,7 +691,6 @@ func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType in nat.Kind = api.NatKind nat.Spec.MinPort = natInfoEntry.MinPort nat.Spec.MaxPort = natInfoEntry.MaxPort - nats[i] = nat } return &api.NatList{ @@ -730,21 +742,56 @@ func (c *client) ListFirewallRules(ctx context.Context, interfaceID string) (*ap } func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) { + var action, direction, ipv uint8 + + switch strings.ToLower(fwRule.Spec.FirewallAction) { + case "accept", "1": + action = 1 + fwRule.Spec.FirewallAction = "Accept" + case "drop", "0": + action = 0 + fwRule.Spec.FirewallAction = "Drop" + default: + return nil, fmt.Errorf("firewall action can be only: Drop = 0/Accept = 1") + } + + switch strings.ToLower(fwRule.Spec.TrafficDirection) { + case "ingress", "0": + direction = 0 + fwRule.Spec.TrafficDirection = "Ingress" + case "egress", "1": + direction = 1 + fwRule.Spec.TrafficDirection = "Egress" + default: + return nil, fmt.Errorf("traffic direction can be only: Ingress = 0/Egress = 1") + } + + switch strings.ToLower(fwRule.Spec.IpVersion) { + case "ipv4", "0": + ipv = 0 + fwRule.Spec.IpVersion = "IPv4" + case "ipv6", "1": + ipv = 1 + fwRule.Spec.IpVersion = "IPv6" + default: + return nil, fmt.Errorf("ip version can be only: IPv4 = 0/IPv6 = 1") + } + res, err := c.DPDKonmetalClient.AddFirewallRule(ctx, &dpdkproto.AddFirewallRuleRequest{ InterfaceID: []byte(fwRule.FirewallRuleMeta.InterfaceID), Rule: &dpdkproto.FirewallRule{ RuleID: []byte(fwRule.FirewallRuleMeta.RuleID), - Direction: dpdkproto.TrafficDirection(fwRule.Spec.TrafficDirection), - Action: dpdkproto.FirewallAction(fwRule.Spec.FirewallAction), + Direction: dpdkproto.TrafficDirection(direction), + Action: dpdkproto.FirewallAction(action), Priority: fwRule.Spec.Priority, - IpVersion: dpdkproto.IPVersion(fwRule.Spec.IpVersion), + IpVersion: dpdkproto.IPVersion(ipv), SourcePrefix: &dpdkproto.Prefix{ - IpVersion: dpdkproto.IPVersion(fwRule.Spec.IpVersion), + IpVersion: dpdkproto.IPVersion(ipv), Address: []byte(fwRule.Spec.SourcePrefix.Addr().String()), PrefixLength: uint32(fwRule.Spec.SourcePrefix.Bits()), }, DestinationPrefix: &dpdkproto.Prefix{ - IpVersion: dpdkproto.IPVersion(fwRule.Spec.IpVersion), + IpVersion: dpdkproto.IPVersion(ipv), Address: []byte(fwRule.Spec.DestinationPrefix.Addr().String()), PrefixLength: uint32(fwRule.Spec.DestinationPrefix.Bits()), }, @@ -764,7 +811,8 @@ func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) RuleID: string(res.RuleID), InterfaceID: fwRule.InterfaceID, }, - Spec: fwRule.Spec, + Spec: fwRule.Spec, + Status: api.ProtoStatusToStatus(res.Status), }, nil } @@ -788,18 +836,19 @@ func (c *client) GetFirewallRule(ctx context.Context, ruleID string, interfaceID return fwrule, err } -func (c *client) DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) error { +func (c *client) DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) (api.ServerError, error) { res, err := c.DPDKonmetalClient.DeleteFirewallRule(ctx, &dpdkproto.DeleteFirewallRuleRequest{ InterfaceID: []byte(interfaceID), RuleID: []byte(ruleID), }) if err != nil { - return err + return api.ServerError{}, err } if errorCode := res.GetError(); errorCode != 0 { - return apierrors.NewStatusError(errorCode, res.GetMessage()) + srvErr := api.ServerError{ServerError: api.ProtoStatusToStatus(res)} + return srvErr, apierrors.NewStatusError(errorCode, res.GetMessage()) } - return nil + return api.ServerError{}, nil } func (c *client) Initialized(ctx context.Context) (string, error) { diff --git a/main.go b/main.go index f017dc8..4133aca 100644 --- a/main.go +++ b/main.go @@ -17,13 +17,19 @@ package main import ( "fmt" "os" + "strconv" "github.com/onmetal/dpservice-cli/cmd" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" ) func main() { if err := cmd.Command().Execute(); err != nil { - fmt.Printf("Error running command: %v\n", err) - os.Exit(1) + if err.Error() == strconv.Itoa(errors.SERVER_ERROR) { + os.Exit(errors.SERVER_ERROR) + } + + fmt.Fprintf(os.Stderr, "Error running command: %v\n", err) + os.Exit(errors.CLIENT_ERROR) } } diff --git a/renderer/renderer.go b/renderer/renderer.go index 4adf185..1cc4986 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -177,6 +177,8 @@ func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) { return t.fwruleTable([]api.FirewallRule{*obj}) case *api.FirewallRuleList: return t.fwruleTable(obj.Items) + case *api.Status: + return t.statusTable([]api.Status{*obj}) default: return nil, fmt.Errorf("unsupported type %T", v) } @@ -284,11 +286,24 @@ func (t defaultTableConverter) virtualIPTable(virtualIPs []api.VirtualIP) (*Tabl } func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { - headers := []any{"interfaceID", "NatIP", "minPort", "maxPort", "underlayRoute"} + var headers []any + if nats[0].Spec.UnderlayRoute == nil { + headers = []any{"NatIP", "minPort", "maxPort"} + } else if nats[0].NatMeta.InterfaceID == "" { + headers = []any{"NatIP", "minPort", "maxPort", "underlayRoute"} + } else { + headers = []any{"interfaceID", "NatIP", "minPort", "maxPort", "underlayRoute"} + } columns := make([][]any, len(nats)) for i, nat := range nats { - columns[i] = []any{nat.NatMeta.InterfaceID, nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute} + if nats[0].Spec.UnderlayRoute == nil { + columns[i] = []any{nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort} + } else if nats[0].NatMeta.InterfaceID == "" { + columns[i] = []any{nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute} + } else { + columns[i] = []any{nat.NatMeta.InterfaceID, nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute} + } } return &TableData{ @@ -305,10 +320,10 @@ func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableDa columns[i] = []any{ fwrule.FirewallRuleMeta.InterfaceID, fwrule.FirewallRuleMeta.RuleID, - dpdkproto.TrafficDirection(fwrule.Spec.TrafficDirection).String(), + fwrule.Spec.TrafficDirection, fwrule.Spec.SourcePrefix, fwrule.Spec.DestinationPrefix, - dpdkproto.FirewallAction(fwrule.Spec.FirewallAction).String(), + fwrule.Spec.FirewallAction, fwrule.Spec.ProtocolFilter.String(), fwrule.Spec.Priority, } @@ -320,6 +335,23 @@ func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableDa }, nil } +func (t defaultTableConverter) statusTable(statuses []api.Status) (*TableData, error) { + headers := []any{"error", "message"} + + columns := make([][]any, len(statuses)) + for i, status := range statuses { + columns[i] = []any{ + status.Error, + status.Message, + } + } + + return &TableData{ + Headers: headers, + Columns: columns, + }, nil +} + var ( lightBoxStyle = table.BoxStyle{ BottomLeft: "", From c729d4974d25e14b32f495897095932dd60cdb51 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 23 May 2023 14:08:23 +0200 Subject: [PATCH 39/65] add functions to clean up code --- cmd/add_firewall_rule.go | 4 ++-- cmd/common.go | 31 +++++++++++++++++++++++++++++++ cmd/delete.go | 8 +------- cmd/delete_firewall_rule.go | 29 +++++------------------------ dpdk/api/types.go | 18 +++++++++--------- main.go | 3 ++- renderer/renderer.go | 14 +++++++------- 7 files changed, 57 insertions(+), 50 deletions(-) diff --git a/cmd/add_firewall_rule.go b/cmd/add_firewall_rule.go index e5dfa0d..b2249cd 100644 --- a/cmd/add_firewall_rule.go +++ b/cmd/add_firewall_rule.go @@ -81,7 +81,7 @@ func (o *AddFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&o.TrafficDirection, "direction", o.TrafficDirection, "Traffic direction of FW Rule: Ingress = 0/Egress = 1") fs.StringVar(&o.FirewallAction, "action", o.FirewallAction, "Firewall action: Drop = 0/Accept = 1 (Can be only \"accept\" at the moment).") fs.Uint32Var(&o.Priority, "priority", o.Priority, "Priority of FW Rule. (For future use. No effect at the moment).") - fs.StringVar(&o.IpVersion, "ipv", o.IpVersion, "IpVersion of FW Rule IPv4 = 0/IPv6 = 1.") + fs.StringVar(&o.IpVersion, "ipver", o.IpVersion, "IpVersion of FW Rule: IPv4 = 0/IPv6 = 1.") flag.PrefixVar(fs, &o.SourcePrefix, "src", o.SourcePrefix, "Source prefix (0.0.0.0 with prefix length 0 matches all source IPs).") flag.PrefixVar(fs, &o.DestinationPrefix, "dst", o.DestinationPrefix, "Destination prefix (0.0.0.0 with prefix length 0 matches all destination IPs).") fs.StringVar(&o.ProtocolFilter, "protocol", o.ProtocolFilter, "Protocol used icmp/tcp/udp (Not defining a protocol filter matches all protocols).") @@ -96,7 +96,7 @@ func (o *AddFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { func (o *AddFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error { // TODO if protocol is not specified it should match all protocols - for _, name := range []string{"interface-id", "rule-id", "direction", "action", "ipv", "src", "dst", "protocol"} { + for _, name := range []string{"interface-id", "rule-id", "direction", "action", "ipver", "src", "dst", "protocol"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } diff --git a/cmd/common.go b/cmd/common.go index e9c24e3..9c75e21 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -23,6 +23,8 @@ import ( "strconv" "time" + "github.com/onmetal/dpservice-cli/dpdk/api" + apierrors "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/dpdk/client" "github.com/onmetal/dpservice-cli/renderer" "github.com/onmetal/dpservice-cli/sources" @@ -62,6 +64,11 @@ func (o *DPDKClientOptions) NewClient(ctx context.Context) (client.Client, func( cleanup := conn.Close return c, cleanup, nil } +func DpdkClose(cleanup func() error) { + if err := cleanup(); err != nil { + fmt.Printf("error cleaning up client: %s", err) + } +} func SubcommandRequired(cmd *cobra.Command, args []string) error { if err := cmd.Help(); err != nil { @@ -133,8 +140,32 @@ func (o *RendererOptions) NewRenderer(operation string, w io.Writer) (renderer.R return registry.New(output, w) } +func (o *RendererOptions) RenderObject(operation string, w io.Writer, obj api.Object) error { + renderer, err := o.NewRenderer(operation, w) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + if err := renderer.Render(obj); err != nil { + return fmt.Errorf("error rendering %s: %w", obj.GetKind(), err) + } + return nil +} + +func (o *RendererOptions) RenderError(w io.Writer, serverError *api.ServerError) error { + renderer, err := o.NewRenderer("ServerError", w) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } + if err := renderer.Render(serverError); err != nil { + return fmt.Errorf("error rendering %s: %w", serverError.GetKind(), err) + } + return fmt.Errorf(strconv.Itoa(apierrors.SERVER_ERROR)) +} + type RendererFactory interface { NewRenderer(operation string, w io.Writer) (renderer.Renderer, error) + RenderObject(operation string, w io.Writer, obj api.Object) error + RenderError(w io.Writer, serverError *api.ServerError) error } type SourcesOptions struct { diff --git a/cmd/delete.go b/cmd/delete.go index 0c3b885..4741b47 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -43,12 +43,6 @@ func Delete(factory DPDKClientFactory) *cobra.Command { sourcesOptions.AddFlags(cmd.Flags()) - errorOptions := &RendererOptions{Output: "json"} - errorRenderer, err := errorOptions.NewRenderer("", os.Stdout) - if err != nil { - return nil - } - subcommands := []*cobra.Command{ DeleteInterface(factory, rendererOptions), DeletePrefix(factory, rendererOptions), @@ -59,7 +53,7 @@ func Delete(factory DPDKClientFactory) *cobra.Command { DeleteLoadBalancerTarget(factory, rendererOptions), DeleteNat(factory, rendererOptions), DeleteNeighborNat(factory, rendererOptions), - DeleteFirewallRule(factory, rendererOptions, errorRenderer), + DeleteFirewallRule(factory, rendererOptions), } cmd.Short = fmt.Sprintf("Deletes one of %v", CommandNames(subcommands)) diff --git a/cmd/delete_firewall_rule.go b/cmd/delete_firewall_rule.go index 26ea5c3..a697eb0 100644 --- a/cmd/delete_firewall_rule.go +++ b/cmd/delete_firewall_rule.go @@ -18,17 +18,14 @@ import ( "context" "fmt" "os" - "strconv" "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" - "github.com/onmetal/dpservice-cli/renderer" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteFirewallRule(factory DPDKClientFactory, rendererFactory RendererFactory, errorRenderer renderer.Renderer) *cobra.Command { +func DeleteFirewallRule(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteFirewallRuleOptions ) @@ -46,7 +43,6 @@ func DeleteFirewallRule(factory DPDKClientFactory, rendererFactory RendererFacto factory, rendererFactory, opts, - errorRenderer, ) }, } @@ -77,22 +73,15 @@ func (o *DeleteFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error return nil } -func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteFirewallRuleOptions, errorRenderer renderer.Renderer) error { +func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteFirewallRuleOptions) error { client, cleanup, err := factory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() + defer DpdkClose(cleanup) if status, err := client.DeleteFirewallRule(ctx, opts.InterfaceID, opts.RuleID); err != nil { - if rendErr := errorRenderer.Render(&status); rendErr != nil { - return fmt.Errorf("error rendering status: %w", rendErr) - } - return fmt.Errorf(strconv.Itoa(errors.SERVER_ERROR)) + return rendererFactory.RenderError(os.Stdout, &status) } fwrule := api.FirewallRule{ @@ -106,13 +95,5 @@ func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, rende }, } - renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } - if err := renderer.Render(&fwrule); err != nil { - return fmt.Errorf("error rendering prefix: %w", err) - } - - return nil + return rendererFactory.RenderObject("deleted", os.Stdout, &fwrule) } diff --git a/dpdk/api/types.go b/dpdk/api/types.go index d447aab..0b1da82 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -39,21 +39,21 @@ func (m *TypeMeta) GetKind() string { return m.Kind } -type ServerError struct { - ServerError Status `json:"serverError"` +type Status struct { + Error int32 `json:"error"` + Message string `json:"message"` } -type Status struct { - Error int32 `json:"error,omitempty"` - Message string `json:"message,omitempty"` +type ServerError struct { + ServerError Status `json:"serverError"` } -func (m *Status) GetKind() string { - return "Status" +func (m *ServerError) GetKind() string { + return "ServerError" } -func (m *Status) GetName() string { - return m.Message +func (m *ServerError) GetName() string { + return fmt.Sprintf("%d", m.ServerError.Error) } type RouteList struct { diff --git a/main.go b/main.go index 4133aca..ab66cb0 100644 --- a/main.go +++ b/main.go @@ -25,10 +25,11 @@ import ( func main() { if err := cmd.Command().Execute(); err != nil { + // check if it is Server side error if err.Error() == strconv.Itoa(errors.SERVER_ERROR) { os.Exit(errors.SERVER_ERROR) } - + // else it is Client side error fmt.Fprintf(os.Stderr, "Error running command: %v\n", err) os.Exit(errors.CLIENT_ERROR) } diff --git a/renderer/renderer.go b/renderer/renderer.go index 1cc4986..4581c07 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -177,8 +177,8 @@ func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) { return t.fwruleTable([]api.FirewallRule{*obj}) case *api.FirewallRuleList: return t.fwruleTable(obj.Items) - case *api.Status: - return t.statusTable([]api.Status{*obj}) + case *api.ServerError: + return t.serverErrorTable([]api.ServerError{*obj}) default: return nil, fmt.Errorf("unsupported type %T", v) } @@ -335,14 +335,14 @@ func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableDa }, nil } -func (t defaultTableConverter) statusTable(statuses []api.Status) (*TableData, error) { +func (t defaultTableConverter) serverErrorTable(serverErrors []api.ServerError) (*TableData, error) { headers := []any{"error", "message"} - columns := make([][]any, len(statuses)) - for i, status := range statuses { + columns := make([][]any, len(serverErrors)) + for i, serverError := range serverErrors { columns[i] = []any{ - status.Error, - status.Message, + serverError.ServerError.Error, + serverError.ServerError.Message, } } From 08ab28bdccba09a3ded2c0db75b844d57a04054a Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 23 May 2023 15:14:34 +0200 Subject: [PATCH 40/65] change error rendering of DeleteFirewallRule --- cmd/common.go | 3 +++ cmd/delete_firewall_rule.go | 11 ++++++----- dpdk/api/types.go | 4 ++++ renderer/renderer.go | 3 ++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/cmd/common.go b/cmd/common.go index 9c75e21..8cab411 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -148,6 +148,9 @@ func (o *RendererOptions) RenderObject(operation string, w io.Writer, obj api.Ob if err := renderer.Render(obj); err != nil { return fmt.Errorf("error rendering %s: %w", obj.GetKind(), err) } + if operation == "server error" { + return fmt.Errorf(strconv.Itoa(apierrors.SERVER_ERROR)) + } return nil } diff --git a/cmd/delete_firewall_rule.go b/cmd/delete_firewall_rule.go index a697eb0..002fcd9 100644 --- a/cmd/delete_firewall_rule.go +++ b/cmd/delete_firewall_rule.go @@ -80,9 +80,7 @@ func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, rende } defer DpdkClose(cleanup) - if status, err := client.DeleteFirewallRule(ctx, opts.InterfaceID, opts.RuleID); err != nil { - return rendererFactory.RenderError(os.Stdout, &status) - } + status, err := client.DeleteFirewallRule(ctx, opts.InterfaceID, opts.RuleID) fwrule := api.FirewallRule{ TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, @@ -91,9 +89,12 @@ func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, rende RuleID: opts.RuleID, }, Status: api.Status{ - Message: "Deleted", + Error: status.ServerError.Error, + Message: status.ServerError.Message, }, } - + if err != nil { + return rendererFactory.RenderObject("server error", os.Stdout, &fwrule) + } return rendererFactory.RenderObject("deleted", os.Stdout, &fwrule) } diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 0b1da82..9d487f5 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -44,6 +44,10 @@ type Status struct { Message string `json:"message"` } +func (status *Status) String() string { + return fmt.Sprintf("Error: %d, Message: %s", status.Error, status.Message) +} + type ServerError struct { ServerError Status `json:"serverError"` } diff --git a/renderer/renderer.go b/renderer/renderer.go index 4581c07..716a867 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -313,7 +313,7 @@ func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { } func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableData, error) { - headers := []any{"interfaceID", "ruleID", "direction", "src", "dst", "action", "protocol", "priority"} + headers := []any{"interfaceID", "ruleID", "direction", "src", "dst", "action", "protocol", "priority", "status"} columns := make([][]any, len(fwrules)) for i, fwrule := range fwrules { @@ -326,6 +326,7 @@ func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableDa fwrule.Spec.FirewallAction, fwrule.Spec.ProtocolFilter.String(), fwrule.Spec.Priority, + fwrule.Status.String(), } } From f7da39186c7637d2a64304c2508fdf2be45d5018 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 23 May 2023 16:58:26 +0200 Subject: [PATCH 41/65] refactor renderer and dpdkclient connection --- .vscode/launch.json | 2 +- cmd/add_firewall_rule.go | 26 ++++++++++--------------- cmd/delete_firewall_rule.go | 33 ++++++++++++++------------------ cmd/get_firewall_rule.go | 29 ++++++++++++---------------- cmd/list_firewall_rules.go | 17 ++++++----------- dpdk/api/errors/errors.go | 2 ++ dpdk/client/client.go | 38 ++++++++++++++++--------------------- 7 files changed, 61 insertions(+), 86 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 4c25ae1..c5e9f80 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -23,7 +23,7 @@ //"args": ["add", "nnat", "10.20.30.40", "--vni", "100", "--minport", "30000", "--maxport", "30100", "--underlayroute", "ff80::1"] //"args": ["add", "interface", "--id=vm4", "--ip=10.200.1.4", "--ip=2000:200:1::4", "--vni=200", "--device=net_tap5"] //"args": ["add", "-f", "/tmp/addint.json"] - "args": ["delete", "firewallrule", "--interface-id=vm1", "--rule-id=12"] + "args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=12"] } ] } \ No newline at end of file diff --git a/cmd/add_firewall_rule.go b/cmd/add_firewall_rule.go index b2249cd..2b00dda 100644 --- a/cmd/add_firewall_rule.go +++ b/cmd/add_firewall_rule.go @@ -21,6 +21,7 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" dpdkproto "github.com/onmetal/net-dpservice-go/proto" @@ -109,16 +110,7 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("added", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } + defer DpdkClose(cleanup) srcPfx, err := netip.ParsePrefix(opts.SourcePrefix.String()) if err != nil { @@ -188,12 +180,14 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory }, }, ) - if err != nil { - return fmt.Errorf("error adding firewall rule: %w", err) - } - if err := renderer.Render(fwrule); err != nil { - return fmt.Errorf("error rendering firewall rule: %w", err) + fwrule.TypeMeta.Kind = api.FirewallRuleKind + fwrule.FirewallRuleMeta.InterfaceID = opts.InterfaceID + fwrule.FirewallRuleMeta.RuleID = opts.RuleID + if err == errors.ErrServerError { + return rendererFactory.RenderObject("server error", os.Stdout, fwrule) + } else if err != nil { + return fmt.Errorf("error adding firewall rule: %w", err) } - return nil + return rendererFactory.RenderObject("added", os.Stdout, fwrule) } diff --git a/cmd/delete_firewall_rule.go b/cmd/delete_firewall_rule.go index 002fcd9..6ae3513 100644 --- a/cmd/delete_firewall_rule.go +++ b/cmd/delete_firewall_rule.go @@ -20,12 +20,13 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteFirewallRule(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func DeleteFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteFirewallRuleOptions ) @@ -40,7 +41,7 @@ func DeleteFirewallRule(factory DPDKClientFactory, rendererFactory RendererFacto return RunDeleteFirewallRule( cmd.Context(), - factory, + dpdkClientFactory, rendererFactory, opts, ) @@ -73,28 +74,22 @@ func (o *DeleteFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error return nil } -func RunDeleteFirewallRule(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteFirewallRuleOptions) error { - client, cleanup, err := factory.NewClient(ctx) +func RunDeleteFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteFirewallRuleOptions) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } defer DpdkClose(cleanup) - status, err := client.DeleteFirewallRule(ctx, opts.InterfaceID, opts.RuleID) + fwrule, err := client.DeleteFirewallRule(ctx, opts.InterfaceID, opts.RuleID) - fwrule := api.FirewallRule{ - TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, - FirewallRuleMeta: api.FirewallRuleMeta{ - InterfaceID: opts.InterfaceID, - RuleID: opts.RuleID, - }, - Status: api.Status{ - Error: status.ServerError.Error, - Message: status.ServerError.Message, - }, - } - if err != nil { - return rendererFactory.RenderObject("server error", os.Stdout, &fwrule) + fwrule.TypeMeta.Kind = api.FirewallRuleKind + fwrule.FirewallRuleMeta.InterfaceID = opts.InterfaceID + fwrule.FirewallRuleMeta.RuleID = opts.RuleID + if err == errors.ErrServerError { + return rendererFactory.RenderObject("server error", os.Stdout, fwrule) + } else if err != nil { + return fmt.Errorf("error deleting firewall rule: %w", err) } - return rendererFactory.RenderObject("deleted", os.Stdout, &fwrule) + return rendererFactory.RenderObject("added", os.Stdout, fwrule) } diff --git a/cmd/get_firewall_rule.go b/cmd/get_firewall_rule.go index 05cfd44..5887774 100644 --- a/cmd/get_firewall_rule.go +++ b/cmd/get_firewall_rule.go @@ -19,6 +19,8 @@ import ( "fmt" "os" + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -80,26 +82,19 @@ func RunGetFirewallRule( ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { - return fmt.Errorf("error getting dpdk client: %w", err) - } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) + return fmt.Errorf("error creating dpdk client: %w", err) } + defer DpdkClose(cleanup) fwrule, err := client.GetFirewallRule(ctx, opts.RuleID, opts.InterfaceID) - if err != nil { - return fmt.Errorf("error getting firewall rule: %w", err) - } - if err := renderer.Render(fwrule); err != nil { - return fmt.Errorf("error rendering firewall rule %s/%s: %w", opts.RuleID, opts.InterfaceID, err) + fwrule.TypeMeta.Kind = api.FirewallRuleKind + fwrule.FirewallRuleMeta.InterfaceID = opts.InterfaceID + fwrule.FirewallRuleMeta.RuleID = opts.RuleID + if err == errors.ErrServerError { + return rendererFactory.RenderObject("server error", os.Stdout, fwrule) + } else if err != nil { + return fmt.Errorf("error getting firewall rule: %w", err) } - return nil + return rendererFactory.RenderObject("", os.Stdout, fwrule) } diff --git a/cmd/list_firewall_rules.go b/cmd/list_firewall_rules.go index 4348c91..afe6164 100644 --- a/cmd/list_firewall_rules.go +++ b/cmd/list_firewall_rules.go @@ -77,24 +77,19 @@ func RunListFirewallRules( ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { - return fmt.Errorf("error getting dpdk client: %w", err) - } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) + return fmt.Errorf("error creating dpdk client: %w", err) } + defer DpdkClose(cleanup) fwrules, err := client.ListFirewallRules(ctx, opts.InterfaceID) if err != nil { return fmt.Errorf("error listing firewall rules: %w", err) } + renderer, err := rendererFactory.NewRenderer("", os.Stdout) + if err != nil { + return fmt.Errorf("error creating renderer: %w", err) + } if err := renderer.Render(fwrules); err != nil { return fmt.Errorf("error rendering firewall rules on interface %s: %w", opts.InterfaceID, err) } diff --git a/dpdk/api/errors/errors.go b/dpdk/api/errors/errors.go index 918cf18..e07459c 100644 --- a/dpdk/api/errors/errors.go +++ b/dpdk/api/errors/errors.go @@ -110,6 +110,8 @@ const ( SERVER_ERROR = 2 ) +var ErrServerError = fmt.Errorf("server error") + type StatusError struct { errorCode int32 message string diff --git a/dpdk/client/client.go b/dpdk/client/client.go index a94e508..99756e7 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -67,7 +67,7 @@ type Client interface { ListFirewallRules(ctx context.Context, interfaceID string) (*api.FirewallRuleList, error) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) GetFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) - DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) (api.ServerError, error) + DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) Initialized(ctx context.Context) (string, error) Init(ctx context.Context, initConfig dpdkproto.InitConfig) error @@ -723,14 +723,14 @@ func (c *client) ListFirewallRules(ctx context.Context, interfaceID string) (*ap InterfaceID: []byte(interfaceID), }) if err != nil { - return nil, err + return &api.FirewallRuleList{}, err } fwRules := make([]api.FirewallRule, len(res.GetRules())) for i, dpdkFwRule := range res.GetRules() { fwRule, err := api.ProtoFwRuleToFwRule(dpdkFwRule, interfaceID) if err != nil { - return nil, err + return &api.FirewallRuleList{}, err } fwRules[i] = *fwRule } @@ -752,7 +752,7 @@ func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) action = 0 fwRule.Spec.FirewallAction = "Drop" default: - return nil, fmt.Errorf("firewall action can be only: Drop = 0/Accept = 1") + return &api.FirewallRule{}, fmt.Errorf("firewall action can be only: Drop = 0/Accept = 1") } switch strings.ToLower(fwRule.Spec.TrafficDirection) { @@ -763,7 +763,7 @@ func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) direction = 1 fwRule.Spec.TrafficDirection = "Egress" default: - return nil, fmt.Errorf("traffic direction can be only: Ingress = 0/Egress = 1") + return &api.FirewallRule{}, fmt.Errorf("traffic direction can be only: Ingress = 0/Egress = 1") } switch strings.ToLower(fwRule.Spec.IpVersion) { @@ -774,7 +774,7 @@ func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) ipv = 1 fwRule.Spec.IpVersion = "IPv6" default: - return nil, fmt.Errorf("ip version can be only: IPv4 = 0/IPv6 = 1") + return &api.FirewallRule{}, fmt.Errorf("ip version can be only: IPv4 = 0/IPv6 = 1") } res, err := c.DPDKonmetalClient.AddFirewallRule(ctx, &dpdkproto.AddFirewallRuleRequest{ @@ -799,12 +799,12 @@ func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) }, }) if err != nil { - return nil, err + return &api.FirewallRule{Status: api.ProtoStatusToStatus(res.Status)}, err } - if res.Status.Error != 0 { - return nil, err + return &api.FirewallRule{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } + return &api.FirewallRule{ TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, FirewallRuleMeta: api.FirewallRuleMeta{ @@ -822,33 +822,27 @@ func (c *client) GetFirewallRule(ctx context.Context, ruleID string, interfaceID RuleID: []byte(ruleID), }) if err != nil { - return nil, err + return &api.FirewallRule{Status: api.ProtoStatusToStatus(res.Status)}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) - } - - fwrule, err := api.ProtoFwRuleToFwRule(res.Rule, interfaceID) - if err != nil { - return nil, err + return &api.FirewallRule{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } - return fwrule, err + return api.ProtoFwRuleToFwRule(res.Rule, interfaceID) } -func (c *client) DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) (api.ServerError, error) { +func (c *client) DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) { res, err := c.DPDKonmetalClient.DeleteFirewallRule(ctx, &dpdkproto.DeleteFirewallRuleRequest{ InterfaceID: []byte(interfaceID), RuleID: []byte(ruleID), }) if err != nil { - return api.ServerError{}, err + return &api.FirewallRule{Status: api.ProtoStatusToStatus(res)}, err } if errorCode := res.GetError(); errorCode != 0 { - srvErr := api.ServerError{ServerError: api.ProtoStatusToStatus(res)} - return srvErr, apierrors.NewStatusError(errorCode, res.GetMessage()) + return &api.FirewallRule{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } - return api.ServerError{}, nil + return &api.FirewallRule{Status: api.ProtoStatusToStatus(res)}, nil } func (c *client) Initialized(ctx context.Context) (string, error) { From eac322a4854e89b8fdf1ef9f18191d7787156687 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Wed, 24 May 2023 16:11:42 +0200 Subject: [PATCH 42/65] refactor renderer and dpdkclient connection --- .vscode/launch.json | 8 +- cmd/add_firewall_rule.go | 15 +-- cmd/add_loadbalancer_target.go | 23 ++-- cmd/add_nat.go | 21 +--- cmd/add_neighbor_nat.go | 31 ++--- cmd/add_prefix.go | 24 ++-- cmd/add_route.go | 24 ++-- cmd/add_virtualip.go | 22 ++-- cmd/common.go | 20 ++-- cmd/create_interface.go | 23 ++-- cmd/create_loadbalancer.go | 25 ++-- cmd/create_loadbalancer_prefix.go | 25 ++-- cmd/delete_firewall_rule.go | 10 +- cmd/delete_interface.go | 39 ++---- cmd/delete_loadbalancer.go | 41 ++----- cmd/delete_loadbalancer_prefix.go | 45 +++---- cmd/delete_loadbalancer_target.go | 45 ++----- cmd/delete_nat.go | 41 ++----- cmd/delete_neighbor_nat.go | 41 ++----- cmd/delete_prefix.go | 45 +++---- cmd/delete_route.go | 59 +++------ cmd/delete_virtualip.go | 41 ++----- cmd/get_firewall_rule.go | 8 +- cmd/get_interface.go | 24 ++-- cmd/get_loadbalancer.go | 24 ++-- cmd/get_loadbalancer_target.go | 28 ++--- cmd/get_nat.go | 27 ++--- cmd/get_nat_info.go | 21 +--- cmd/get_virtualip.go | 26 ++-- cmd/list_firewall_rules.go | 9 +- cmd/list_interfaces.go | 18 +-- cmd/list_loadbalancer_prefixes.go | 22 +--- cmd/list_prefixes.go | 16 +-- cmd/list_routes.go | 16 +-- dpdk/api/conversion.go | 13 +- dpdk/api/types.go | 64 +++++++--- dpdk/client/client.go | 192 +++++++++++++++--------------- dpdk/client/dynamic/dynamic.go | 14 ++- renderer/renderer.go | 100 +++++++++------- 39 files changed, 500 insertions(+), 790 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index c5e9f80..108bdc8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,11 +11,13 @@ "mode": "auto", "program": "main.go", //"program": "${fileDirname}", - //"args": ["add", "loadbalancer", "7", "--lbports", "389", "--vip", "10.3.4.5", "--vni", "100"] + //"args": ["add", "loadbalancer", "--id=4", "--lbports", "tcp/389", "--vip", "10.3.4.5", "--vni", "100"] + "args": ["delete", "loadbalancer", "--id=4"] //"args": ["get", "route", "0.0.0.0/24", "0", "fc00:2::64:0:1", "--vni", "100"] //"args": ["delete", "route", "10.200.2.0/24", "0", "ff80::1", "--vni", "100"] //"args": ["add", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] - //"args": ["add", "lbtarget", "ff80::1", "--lb-id", "1"] + //"args": ["delete", "lbtarget", "--target-ip=ff80::1", "--lb-id=1"] + //"args": ["get", "lbtarget", "--lb-id=4"] //"args": ["get", "natinfo", "--nat-ip=10.20.30.40", "--nat-type=Local"] //"args": ["get", "fwrule", "vm1", "--rule-id=4"] //"args": ["add", "fwrule", "vm1", "--action=1", "--direction=1", "--dst=5.5.5.0/24", "--ipv=0", @@ -23,7 +25,7 @@ //"args": ["add", "nnat", "10.20.30.40", "--vni", "100", "--minport", "30000", "--maxport", "30100", "--underlayroute", "ff80::1"] //"args": ["add", "interface", "--id=vm4", "--ip=10.200.1.4", "--ip=2000:200:1::4", "--vni=200", "--device=net_tap5"] //"args": ["add", "-f", "/tmp/addint.json"] - "args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=12"] + //"args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=12"] } ] } \ No newline at end of file diff --git a/cmd/add_firewall_rule.go b/cmd/add_firewall_rule.go index 2b00dda..53084ce 100644 --- a/cmd/add_firewall_rule.go +++ b/cmd/add_firewall_rule.go @@ -37,7 +37,7 @@ func AddFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory Render cmd := &cobra.Command{ Use: "firewallrule <--interface-id> [flags]", Short: "Add a FirewallRule to interface", - Example: "dpservice-cli add fwrule --interface-id=vm1 --action=1 --direction=1 --dst=5.5.5.0/24 --ipv=0 --priority=100 --rule-id=12 --src=1.1.1.1/32 --protocol=tcp --src-port-min=1 --src-port-max=1000 --dst-port-min=500 --dst-port-max=600", + Example: "dpservice-cli add fwrule --interface-id=vm1 --action=1 --direction=1 --dst=5.5.5.0/24 --ipver=0 --priority=100 --rule-id=12 --src=1.1.1.1/32 --protocol=tcp --src-port-min=1 --src-port-max=1000 --dst-port-min=500 --dst-port-max=600", Aliases: FirewallRuleAliases, Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { @@ -80,7 +80,7 @@ func (o *AddFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "InterfaceID of FW Rule.") fs.StringVar(&o.RuleID, "rule-id", o.RuleID, "RuleID of FW Rule.") fs.StringVar(&o.TrafficDirection, "direction", o.TrafficDirection, "Traffic direction of FW Rule: Ingress = 0/Egress = 1") - fs.StringVar(&o.FirewallAction, "action", o.FirewallAction, "Firewall action: Drop = 0/Accept = 1 (Can be only \"accept\" at the moment).") + fs.StringVar(&o.FirewallAction, "action", o.FirewallAction, "Firewall action: drop/deny/0|accept/allow/1 (Can be only \"accept/allow/1\" at the moment).") fs.Uint32Var(&o.Priority, "priority", o.Priority, "Priority of FW Rule. (For future use. No effect at the moment).") fs.StringVar(&o.IpVersion, "ipver", o.IpVersion, "IpVersion of FW Rule: IPv4 = 0/IPv6 = 1.") flag.PrefixVar(fs, &o.SourcePrefix, "src", o.SourcePrefix, "Source prefix (0.0.0.0 with prefix length 0 matches all source IPs).") @@ -178,16 +178,13 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory ProtocolFilter: &dpdkproto.ProtocolFilter{ Filter: protocolFilter.Filter}, }, - }, - ) + }) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error adding firewall rule: %w", err) + } fwrule.TypeMeta.Kind = api.FirewallRuleKind fwrule.FirewallRuleMeta.InterfaceID = opts.InterfaceID fwrule.FirewallRuleMeta.RuleID = opts.RuleID - if err == errors.ErrServerError { - return rendererFactory.RenderObject("server error", os.Stdout, fwrule) - } else if err != nil { - return fmt.Errorf("error adding firewall rule: %w", err) - } return rendererFactory.RenderObject("added", os.Stdout, fwrule) } diff --git a/cmd/add_loadbalancer_target.go b/cmd/add_loadbalancer_target.go index ce43658..74797ff 100644 --- a/cmd/add_loadbalancer_target.go +++ b/cmd/add_loadbalancer_target.go @@ -21,6 +21,7 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -88,28 +89,18 @@ func RunAddLoadBalancerTarget( if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("added", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } + defer DpdkClose(cleanup) - res, err := client.AddLoadBalancerTarget(ctx, &api.LoadBalancerTarget{ + lbtarget, err := client.AddLoadBalancerTarget(ctx, &api.LoadBalancerTarget{ TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, LoadBalancerTargetMeta: api.LoadBalancerTargetMeta{ID: opts.LoadBalancerID}, Spec: api.LoadBalancerTargetSpec{TargetIP: &opts.TargetIP}, }) - if err != nil { + if err != nil && err != errors.ErrServerError { return fmt.Errorf("error adding loadbalancer target: %w", err) } - if err := renderer.Render(res); err != nil { - return fmt.Errorf("error rendering loadbalancer target: %w", err) - } - return nil + lbtarget.TypeMeta.Kind = api.LoadBalancerKind + lbtarget.LoadBalancerTargetMeta.ID = opts.LoadBalancerID + return rendererFactory.RenderObject("added", os.Stdout, lbtarget) } diff --git a/cmd/add_nat.go b/cmd/add_nat.go index 215cabd..ef721f7 100644 --- a/cmd/add_nat.go +++ b/cmd/add_nat.go @@ -21,6 +21,7 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -84,16 +85,7 @@ func RunAddNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendere if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("added", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } + defer DpdkClose(cleanup) nat, err := client.AddNat(ctx, &api.Nat{ NatMeta: api.NatMeta{ @@ -105,12 +97,11 @@ func RunAddNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendere MaxPort: opts.MaxPort, }, }) - if err != nil { + if err != nil && err != errors.ErrServerError { return fmt.Errorf("error adding nat: %w", err) } - if err := renderer.Render(nat); err != nil { - return fmt.Errorf("error rendering nat: %w", err) - } - return nil + nat.TypeMeta.Kind = api.NatKind + nat.NatMeta.InterfaceID = opts.InterfaceID + return rendererFactory.RenderObject("added", os.Stdout, nat) } diff --git a/cmd/add_neighbor_nat.go b/cmd/add_neighbor_nat.go index 192145d..e64a23c 100644 --- a/cmd/add_neighbor_nat.go +++ b/cmd/add_neighbor_nat.go @@ -21,6 +21,7 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -86,17 +87,11 @@ func RunAddNeighborNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() + defer DpdkClose(cleanup) - nNat := &api.NeighborNat{ - TypeMeta: api.TypeMeta{Kind: api.NeighborNatKind}, - NeighborNatMeta: api.NeighborNatMeta{ - NatVIPIP: &opts.NatIP, - }, + neigbhorNat := &api.NeighborNat{ + TypeMeta: api.TypeMeta{Kind: api.NeighborNatKind}, + NeighborNatMeta: api.NeighborNatMeta{NatVIPIP: &opts.NatIP}, Spec: api.NeighborNatSpec{ Vni: opts.Vni, MinPort: opts.MinPort, @@ -105,18 +100,12 @@ func RunAddNeighborNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, }, } - neighNat, err := client.AddNeighborNat(ctx, nNat) - if err != nil { + nnat, err := client.AddNeighborNat(ctx, neigbhorNat) + if err != nil && err != errors.ErrServerError { return fmt.Errorf("error adding neighbor nat: %w", err) } - renderer, err := rendererFactory.NewRenderer("created", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } - if err := renderer.Render(&neighNat); err != nil { - return fmt.Errorf("error rendering neighbor nat: %w", err) - } - - return nil + nnat.TypeMeta.Kind = api.NeighborNatKind + nnat.NeighborNatMeta.NatVIPIP = &opts.NatIP + return rendererFactory.RenderObject("added", os.Stdout, nnat) } diff --git a/cmd/add_prefix.go b/cmd/add_prefix.go index dadec79..9e45a59 100644 --- a/cmd/add_prefix.go +++ b/cmd/add_prefix.go @@ -21,6 +21,7 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -88,29 +89,20 @@ func RunAddPrefix( if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("added", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } + defer DpdkClose(cleanup) - res, err := client.AddPrefix(ctx, &api.Prefix{ + prefix, err := client.AddPrefix(ctx, &api.Prefix{ PrefixMeta: api.PrefixMeta{ InterfaceID: opts.InterfaceID, Prefix: opts.Prefix, }, }) - if err != nil { + if err != nil && err != errors.ErrServerError { return fmt.Errorf("error adding prefix: %w", err) } - if err := renderer.Render(res); err != nil { - return fmt.Errorf("error rendering prefix: %w", err) - } - return nil + prefix.TypeMeta.Kind = api.PrefixKind + prefix.PrefixMeta.InterfaceID = opts.InterfaceID + prefix.PrefixMeta.Prefix = opts.Prefix + return rendererFactory.RenderObject("added", os.Stdout, prefix) } diff --git a/cmd/add_route.go b/cmd/add_route.go index 0f3f688..f2a51b6 100644 --- a/cmd/add_route.go +++ b/cmd/add_route.go @@ -21,6 +21,7 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -89,16 +90,7 @@ func RunAddRoute( if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("added", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } + defer DpdkClose(cleanup) route, err := client.AddRoute(ctx, &api.Route{ RouteMeta: api.RouteMeta{ @@ -106,17 +98,17 @@ func RunAddRoute( Prefix: opts.Prefix, NextHop: api.RouteNextHop{ VNI: opts.NextHopVNI, - IP: opts.NextHopIP, + IP: &opts.NextHopIP, }, }, Spec: api.RouteSpec{}, }) - if err != nil { + if err != nil && err != errors.ErrServerError { return fmt.Errorf("error adding route: %w", err) } - if err := renderer.Render(route); err != nil { - return fmt.Errorf("error rendering route: %w", err) - } - return nil + route.TypeMeta.Kind = api.RouteKind + route.RouteMeta.VNI = opts.VNI + route.RouteMeta.Prefix = opts.Prefix + return rendererFactory.RenderObject("added", os.Stdout, route) } diff --git a/cmd/add_virtualip.go b/cmd/add_virtualip.go index a4f6324..3ce246f 100644 --- a/cmd/add_virtualip.go +++ b/cmd/add_virtualip.go @@ -21,6 +21,7 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -85,16 +86,7 @@ func RunAddVirtualIP( if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("added", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } + defer DpdkClose(cleanup) virtualIP, err := client.AddVirtualIP(ctx, &api.VirtualIP{ VirtualIPMeta: api.VirtualIPMeta{ @@ -102,12 +94,12 @@ func RunAddVirtualIP( IP: opts.Vip, }, }) - if err != nil { + if err != nil && err != errors.ErrServerError { return fmt.Errorf("error adding virtual ip: %w", err) } - if err := renderer.Render(virtualIP); err != nil { - return fmt.Errorf("error rendering virtual ip: %w", err) - } - return nil + virtualIP.TypeMeta.Kind = api.RouteKind + virtualIP.VirtualIPMeta.IP = opts.Vip + virtualIP.VirtualIPMeta.InterfaceID = opts.InterfaceID + return rendererFactory.RenderObject("added", os.Stdout, virtualIP) } diff --git a/cmd/common.go b/cmd/common.go index 8cab411..73a8b2d 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -141,6 +141,9 @@ func (o *RendererOptions) NewRenderer(operation string, w io.Writer) (renderer.R } func (o *RendererOptions) RenderObject(operation string, w io.Writer, obj api.Object) error { + if obj.GetStatus() != 0 { + operation = "server error" + } renderer, err := o.NewRenderer(operation, w) if err != nil { return fmt.Errorf("error creating renderer: %w", err) @@ -148,27 +151,30 @@ func (o *RendererOptions) RenderObject(operation string, w io.Writer, obj api.Ob if err := renderer.Render(obj); err != nil { return fmt.Errorf("error rendering %s: %w", obj.GetKind(), err) } - if operation == "server error" { + if obj.GetStatus() != 0 { return fmt.Errorf(strconv.Itoa(apierrors.SERVER_ERROR)) } return nil } -func (o *RendererOptions) RenderError(w io.Writer, serverError *api.ServerError) error { - renderer, err := o.NewRenderer("ServerError", w) +func (o *RendererOptions) RenderList(operation string, w io.Writer, list api.List) error { + renderer, err := o.NewRenderer("", w) if err != nil { return fmt.Errorf("error creating renderer: %w", err) } - if err := renderer.Render(serverError); err != nil { - return fmt.Errorf("error rendering %s: %w", serverError.GetKind(), err) + if err := renderer.Render(list); err != nil { + return fmt.Errorf("error rendering %s: %w", list.GetItems()[0].GetKind(), err) + } + if operation == "server error" { + return fmt.Errorf(strconv.Itoa(apierrors.SERVER_ERROR)) } - return fmt.Errorf(strconv.Itoa(apierrors.SERVER_ERROR)) + return nil } type RendererFactory interface { NewRenderer(operation string, w io.Writer) (renderer.Renderer, error) RenderObject(operation string, w io.Writer, obj api.Object) error - RenderError(w io.Writer, serverError *api.ServerError) error + RenderList(operation string, w io.Writer, list api.List) error } type SourcesOptions struct { diff --git a/cmd/create_interface.go b/cmd/create_interface.go index 35ea1f3..a6367f5 100644 --- a/cmd/create_interface.go +++ b/cmd/create_interface.go @@ -21,6 +21,7 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -83,16 +84,7 @@ func RunCreateInterface(ctx context.Context, dpdkClientFactory DPDKClientFactory if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("created", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } + defer DpdkClose(cleanup) iface, err := client.CreateInterface(ctx, &api.Interface{ InterfaceMeta: api.InterfaceMeta{ @@ -104,12 +96,11 @@ func RunCreateInterface(ctx context.Context, dpdkClientFactory DPDKClientFactory IPs: opts.IP, }, }) - if err != nil { - return fmt.Errorf("error creating interface: %w", err) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error adding interface: %w", err) } - if err := renderer.Render(iface); err != nil { - return fmt.Errorf("error rendering interface: %w", err) - } - return nil + iface.TypeMeta.Kind = api.InterfaceKind + iface.InterfaceMeta.ID = opts.ID + return rendererFactory.RenderObject("added", os.Stdout, iface) } diff --git a/cmd/create_loadbalancer.go b/cmd/create_loadbalancer.go index 36bb581..7ee948f 100644 --- a/cmd/create_loadbalancer.go +++ b/cmd/create_loadbalancer.go @@ -21,6 +21,7 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -35,7 +36,7 @@ func CreateLoadBalancer(dpdkClientFactory DPDKClientFactory, rendererFactory Ren cmd := &cobra.Command{ Use: "loadbalancer <--id> <--vni> <--vip> <--lbports>", Short: "Create a loadbalancer", - Example: "dpservice-cli create lb --id=4 --vni=100 --vip=10.20.30.40 --lbports=TCP/443,UDP/53", + Example: "dpservice-cli add loadbalancer --id=4 --vni=100 --vip=10.20.30.40 --lbports=TCP/443,UDP/53", Aliases: LoadBalancerAliases, Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { @@ -83,16 +84,7 @@ func RunCreateLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFact if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("created", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } + defer DpdkClose(cleanup) var ports = make([]api.LBPort, 0, len(opts.Lbports)) for _, p := range opts.Lbports { @@ -113,12 +105,11 @@ func RunCreateLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFact Lbports: ports, }, }) - if err != nil { - return fmt.Errorf("error creating loadbalancer: %w", err) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error adding loadbalancer: %w", err) } - if err := renderer.Render(lb); err != nil { - return fmt.Errorf("error rendering loadbalancer: %w", err) - } - return nil + lb.TypeMeta.Kind = api.LoadBalancerKind + lb.LoadBalancerMeta.ID = opts.Id + return rendererFactory.RenderObject("added", os.Stdout, lb) } diff --git a/cmd/create_loadbalancer_prefix.go b/cmd/create_loadbalancer_prefix.go index 18089f5..cd8cd9b 100644 --- a/cmd/create_loadbalancer_prefix.go +++ b/cmd/create_loadbalancer_prefix.go @@ -21,6 +21,7 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -88,16 +89,7 @@ func RunCreateLoadBalancerPrefix( if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("created", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } + defer DpdkClose(cleanup) lbprefix, err := client.CreateLoadBalancerPrefix(ctx, &api.Prefix{ PrefixMeta: api.PrefixMeta{ @@ -105,12 +97,11 @@ func RunCreateLoadBalancerPrefix( Prefix: opts.Prefix, }, }) - if err != nil { - return fmt.Errorf("error creating prefix: %w", err) - } - - if err := renderer.Render(lbprefix); err != nil { - return fmt.Errorf("error rendering prefix: %w", err) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error adding loadbalancer prefix: %w", err) } - return nil + lbprefix.TypeMeta.Kind = "LoadBalancerPrefix" + lbprefix.PrefixMeta.InterfaceID = opts.InterfaceID + lbprefix.PrefixMeta.Prefix = opts.Prefix + return rendererFactory.RenderObject("added", os.Stdout, lbprefix) } diff --git a/cmd/delete_firewall_rule.go b/cmd/delete_firewall_rule.go index 6ae3513..df161e6 100644 --- a/cmd/delete_firewall_rule.go +++ b/cmd/delete_firewall_rule.go @@ -82,14 +82,12 @@ func RunDeleteFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFact defer DpdkClose(cleanup) fwrule, err := client.DeleteFirewallRule(ctx, opts.InterfaceID, opts.RuleID) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error deleting firewall rule: %w", err) + } fwrule.TypeMeta.Kind = api.FirewallRuleKind fwrule.FirewallRuleMeta.InterfaceID = opts.InterfaceID fwrule.FirewallRuleMeta.RuleID = opts.RuleID - if err == errors.ErrServerError { - return rendererFactory.RenderObject("server error", os.Stdout, fwrule) - } else if err != nil { - return fmt.Errorf("error deleting firewall rule: %w", err) - } - return rendererFactory.RenderObject("added", os.Stdout, fwrule) + return rendererFactory.RenderObject("deleted", os.Stdout, fwrule) } diff --git a/cmd/delete_interface.go b/cmd/delete_interface.go index 69146c2..f39cc76 100644 --- a/cmd/delete_interface.go +++ b/cmd/delete_interface.go @@ -20,12 +20,13 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteInterface(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func DeleteInterface(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteInterfaceOptions ) @@ -40,7 +41,7 @@ func DeleteInterface(factory DPDKClientFactory, rendererFactory RendererFactory) return RunDeleteInterface( cmd.Context(), - factory, + dpdkClientFactory, rendererFactory, opts, ) @@ -71,35 +72,19 @@ func (o *DeleteInterfaceOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeleteInterface(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteInterfaceOptions) error { - client, cleanup, err := factory.NewClient(ctx) +func RunDeleteInterface(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteInterfaceOptions) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - if err := client.DeleteInterface(ctx, opts.ID); err != nil { - return fmt.Errorf("error deleting interface %s: %v", opts.ID, err) - } + defer DpdkClose(cleanup) - renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } - iface := api.Interface{ - TypeMeta: api.TypeMeta{Kind: api.InterfaceKind}, - InterfaceMeta: api.InterfaceMeta{ID: opts.ID}, - Status: api.Status{ - Message: "Deleted", - }, - } - if err := renderer.Render(&iface); err != nil { - return fmt.Errorf("error rendering interface: %w", err) + iface, err := client.DeleteInterface(ctx, opts.ID) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error deleting interface: %w", err) } - return nil + iface.TypeMeta.Kind = api.InterfaceKind + iface.InterfaceMeta.ID = opts.ID + return rendererFactory.RenderObject("deleted", os.Stdout, iface) } diff --git a/cmd/delete_loadbalancer.go b/cmd/delete_loadbalancer.go index 498b478..6d9b8c2 100644 --- a/cmd/delete_loadbalancer.go +++ b/cmd/delete_loadbalancer.go @@ -20,12 +20,13 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteLoadBalancer(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func DeleteLoadBalancer(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteLoadBalancerOptions ) @@ -40,7 +41,7 @@ func DeleteLoadBalancer(factory DPDKClientFactory, rendererFactory RendererFacto return RunDeleteLoadBalancer( cmd.Context(), - factory, + dpdkClientFactory, rendererFactory, opts, ) @@ -71,37 +72,19 @@ func (o *DeleteLoadBalancerOptions) MarkRequiredFlags(cmd *cobra.Command) error return nil } -func RunDeleteLoadBalancer(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteLoadBalancerOptions) error { - client, cleanup, err := factory.NewClient(ctx) +func RunDeleteLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteLoadBalancerOptions) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() + defer DpdkClose(cleanup) - if err := client.DeleteLoadBalancer(ctx, opts.ID); err != nil { - return fmt.Errorf("error deleting loadbalancer %s: %v", opts.ID, err) + lb, err := client.DeleteLoadBalancer(ctx, opts.ID) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error deleting loadbalancer: %w", err) } - renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } - lb := api.LoadBalancer{ - TypeMeta: api.TypeMeta{Kind: api.LoadBalancerKind}, - LoadBalancerMeta: api.LoadBalancerMeta{ - ID: opts.ID, - }, - Status: api.Status{ - Message: "Deleted", - }, - } - if err := renderer.Render(&lb); err != nil { - return fmt.Errorf("error rendering loadbalancer: %w", err) - } - - return nil + lb.TypeMeta.Kind = api.LoadBalancerKind + lb.LoadBalancerMeta.ID = opts.ID + return rendererFactory.RenderObject("deleted", os.Stdout, lb) } diff --git a/cmd/delete_loadbalancer_prefix.go b/cmd/delete_loadbalancer_prefix.go index 11c99ae..d1c0fad 100644 --- a/cmd/delete_loadbalancer_prefix.go +++ b/cmd/delete_loadbalancer_prefix.go @@ -20,14 +20,14 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteLoadBalancerPrefix(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func DeleteLoadBalancerPrefix(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteLoadBalancerPrefixOptions ) @@ -42,7 +42,7 @@ func DeleteLoadBalancerPrefix(factory DPDKClientFactory, rendererFactory Rendere return RunDeleteLoadBalancerPrefix( cmd.Context(), - factory, + dpdkClientFactory, rendererFactory, opts, ) @@ -75,37 +75,20 @@ func (o *DeleteLoadBalancerPrefixOptions) MarkRequiredFlags(cmd *cobra.Command) return nil } -func RunDeleteLoadBalancerPrefix(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteLoadBalancerPrefixOptions) error { - client, cleanup, err := factory.NewClient(ctx) +func RunDeleteLoadBalancerPrefix(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteLoadBalancerPrefixOptions) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { - return fmt.Errorf("error deleting dpdk client: %w", err) + return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() + defer DpdkClose(cleanup) - if err := client.DeleteLoadBalancerPrefix(ctx, opts.InterfaceID, opts.Prefix); err != nil { - return fmt.Errorf("error deleting loadbalancer prefix %s/%v: %v", opts.InterfaceID, opts.Prefix, err) + lbprefix, err := client.DeleteLoadBalancerPrefix(ctx, opts.InterfaceID, opts.Prefix) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error deleting loadbalancer prefix: %w", err) } - renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } - lbprefix := api.Prefix{ - TypeMeta: api.TypeMeta{Kind: "LoadBalancerPrefix"}, - PrefixMeta: api.PrefixMeta{ - InterfaceID: opts.InterfaceID, - Prefix: opts.Prefix, - }, - Status: api.Status{ - Message: "Deleted", - }, - } - if err := renderer.Render(&lbprefix); err != nil { - return fmt.Errorf("error rendering loadbalancer prefix: %w", err) - } - return nil + lbprefix.TypeMeta.Kind = "LoadBalancerPrefix" + lbprefix.PrefixMeta.InterfaceID = opts.InterfaceID + lbprefix.PrefixMeta.Prefix = opts.Prefix + return rendererFactory.RenderObject("deleted", os.Stdout, lbprefix) } diff --git a/cmd/delete_loadbalancer_target.go b/cmd/delete_loadbalancer_target.go index a4eb628..92efd81 100644 --- a/cmd/delete_loadbalancer_target.go +++ b/cmd/delete_loadbalancer_target.go @@ -21,13 +21,14 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteLoadBalancerTarget(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func DeleteLoadBalancerTarget(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteLoadBalancerTargetOptions ) @@ -42,7 +43,7 @@ func DeleteLoadBalancerTarget(factory DPDKClientFactory, rendererFactory Rendere return RunDeleteLoadBalancerTarget( cmd.Context(), - factory, + dpdkClientFactory, rendererFactory, opts, ) @@ -75,39 +76,19 @@ func (o *DeleteLoadBalancerTargetOptions) MarkRequiredFlags(cmd *cobra.Command) return nil } -func RunDeleteLoadBalancerTarget(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteLoadBalancerTargetOptions) error { - client, cleanup, err := factory.NewClient(ctx) +func RunDeleteLoadBalancerTarget(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteLoadBalancerTargetOptions) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { - return fmt.Errorf("error deleting dpdk client: %w", err) + return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() + defer DpdkClose(cleanup) - if err := client.DeleteLoadBalancerTarget(ctx, opts.LoadBalancerID, opts.TargetIP); err != nil { - return fmt.Errorf("error deleting loadbalancer target %s/%v: %v", opts.LoadBalancerID, opts.TargetIP, err) - } - renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } - lbtarget := api.LoadBalancerTarget{ - TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, - LoadBalancerTargetMeta: api.LoadBalancerTargetMeta{ - ID: opts.LoadBalancerID, - }, - Spec: api.LoadBalancerTargetSpec{ - TargetIP: &opts.TargetIP, - }, - Status: api.Status{ - Message: "Deleted", - }, - } - if err := renderer.Render(&lbtarget); err != nil { - return fmt.Errorf("error rendering loadbalancer target: %w", err) + lbtarget, err := client.DeleteLoadBalancerTarget(ctx, opts.LoadBalancerID, opts.TargetIP) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error deleting neighbor nat: %w", err) } - return nil + lbtarget.TypeMeta.Kind = api.LoadBalancerTargetKind + lbtarget.LoadBalancerTargetMeta.ID = opts.LoadBalancerID + return rendererFactory.RenderObject("deleted", os.Stdout, lbtarget) } diff --git a/cmd/delete_nat.go b/cmd/delete_nat.go index acbcadf..68acdc0 100644 --- a/cmd/delete_nat.go +++ b/cmd/delete_nat.go @@ -20,12 +20,13 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteNat(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func DeleteNat(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteNatOptions ) @@ -40,7 +41,7 @@ func DeleteNat(factory DPDKClientFactory, rendererFactory RendererFactory) *cobr return RunDeleteNat( cmd.Context(), - factory, + dpdkClientFactory, rendererFactory, opts, ) @@ -71,37 +72,19 @@ func (o *DeleteNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeleteNat(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteNatOptions) error { - client, cleanup, err := factory.NewClient(ctx) +func RunDeleteNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteNatOptions) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() + defer DpdkClose(cleanup) - if err := client.DeleteNat(ctx, opts.InterfaceID); err != nil { - return fmt.Errorf("error deleting nat of interface %s: %v", opts.InterfaceID, err) + nat, err := client.DeleteNat(ctx, opts.InterfaceID) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error deleting nat: %w", err) } - renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } - nat := api.Nat{ - TypeMeta: api.TypeMeta{Kind: api.NatKind}, - NatMeta: api.NatMeta{ - InterfaceID: opts.InterfaceID, - }, - Status: &api.Status{ - Message: "Deleted", - }, - } - if err := renderer.Render(&nat); err != nil { - return fmt.Errorf("error rendering prefix: %w", err) - } - - return nil + nat.TypeMeta.Kind = api.NatKind + nat.NatMeta.InterfaceID = opts.InterfaceID + return rendererFactory.RenderObject("deleted", os.Stdout, nat) } diff --git a/cmd/delete_neighbor_nat.go b/cmd/delete_neighbor_nat.go index 67c3b2f..02b9b08 100644 --- a/cmd/delete_neighbor_nat.go +++ b/cmd/delete_neighbor_nat.go @@ -21,13 +21,14 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteNeighborNat(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func DeleteNeighborNat(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteNeighborNatOptions ) @@ -42,7 +43,7 @@ func DeleteNeighborNat(factory DPDKClientFactory, rendererFactory RendererFactor return RunDeleteNeighborNat( cmd.Context(), - factory, + dpdkClientFactory, rendererFactory, opts, ) @@ -79,16 +80,12 @@ func (o *DeleteNeighborNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeleteNeighborNat(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteNeighborNatOptions) error { - client, cleanup, err := factory.NewClient(ctx) +func RunDeleteNeighborNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteNeighborNatOptions) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() + defer DpdkClose(cleanup) neigbhorNat := api.NeighborNat{ TypeMeta: api.TypeMeta{Kind: api.NatKind}, @@ -99,26 +96,12 @@ func RunDeleteNeighborNat(ctx context.Context, factory DPDKClientFactory, render MaxPort: opts.MaxPort, }, } - if err := client.DeleteNeighborNat(ctx, neigbhorNat); err != nil { - return fmt.Errorf("error deleting neighbor nat with ip %s: %v", opts.NatIP, err) + nnat, err := client.DeleteNeighborNat(ctx, neigbhorNat) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error deleting neighbor nat: %w", err) } - renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } - nNat := api.NeighborNat{ - TypeMeta: api.TypeMeta{Kind: api.NeighborNatKind}, - NeighborNatMeta: api.NeighborNatMeta{ - NatVIPIP: &opts.NatIP, - }, - Status: api.Status{ - Message: "Deleted", - }, - } - if err := renderer.Render(&nNat); err != nil { - return fmt.Errorf("error rendering prefix: %w", err) - } - - return nil + nnat.TypeMeta.Kind = api.NeighborNatKind + nnat.NeighborNatMeta.NatVIPIP = &opts.NatIP + return rendererFactory.RenderObject("deleted", os.Stdout, nnat) } diff --git a/cmd/delete_prefix.go b/cmd/delete_prefix.go index 15beb23..389b8bd 100644 --- a/cmd/delete_prefix.go +++ b/cmd/delete_prefix.go @@ -21,13 +21,14 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeletePrefix(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func DeletePrefix(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeletePrefixOptions ) @@ -42,7 +43,7 @@ func DeletePrefix(factory DPDKClientFactory, rendererFactory RendererFactory) *c return RunDeletePrefix( cmd.Context(), - factory, + dpdkClientFactory, rendererFactory, opts, ) @@ -75,38 +76,20 @@ func (o *DeletePrefixOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeletePrefix(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeletePrefixOptions) error { - client, cleanup, err := factory.NewClient(ctx) +func RunDeletePrefix(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts DeletePrefixOptions) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { - return fmt.Errorf("error deleting dpdk client: %w", err) + return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() + defer DpdkClose(cleanup) - if err := client.DeletePrefix(ctx, opts.InterfaceID, opts.Prefix); err != nil { - return fmt.Errorf("error deleting prefix %s/%v: %v", opts.InterfaceID, opts.Prefix, err) + prefix, err := client.DeletePrefix(ctx, opts.InterfaceID, opts.Prefix) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error deleting prefix: %w", err) } - renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } - prefix := api.Prefix{ - TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, - PrefixMeta: api.PrefixMeta{ - InterfaceID: opts.InterfaceID, - Prefix: opts.Prefix, - }, - Status: api.Status{ - Message: "Deleted", - }, - } - if err := renderer.Render(&prefix); err != nil { - return fmt.Errorf("error rendering prefix: %w", err) - } - - return nil + prefix.TypeMeta.Kind = api.PrefixKind + prefix.PrefixMeta.InterfaceID = opts.InterfaceID + prefix.PrefixMeta.Prefix = opts.Prefix + return rendererFactory.RenderObject("deleted", os.Stdout, prefix) } diff --git a/cmd/delete_route.go b/cmd/delete_route.go index 5bf84bc..e06b791 100644 --- a/cmd/delete_route.go +++ b/cmd/delete_route.go @@ -21,13 +21,14 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteRoute(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func DeleteRoute(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteRouteOptions ) @@ -42,7 +43,7 @@ func DeleteRoute(factory DPDKClientFactory, rendererFactory RendererFactory) *co return RunDeleteRoute( cmd.Context(), - factory, + dpdkClientFactory, rendererFactory, opts, ) @@ -57,21 +58,17 @@ func DeleteRoute(factory DPDKClientFactory, rendererFactory RendererFactory) *co } type DeleteRouteOptions struct { - Prefix netip.Prefix - NextHopVNI uint32 - NextHopIP netip.Addr - VNI uint32 + Prefix netip.Prefix + VNI uint32 } func (o *DeleteRouteOptions) AddFlags(fs *pflag.FlagSet) { flag.PrefixVar(fs, &o.Prefix, "prefix", o.Prefix, "Prefix of the route.") - fs.Uint32Var(&o.NextHopVNI, "next-hop-vni", o.NextHopVNI, "Next hop VNI of the route.") - flag.AddrVar(fs, &o.NextHopIP, "next-hop-ip", o.NextHopIP, "Next hop IP of the route.") fs.Uint32Var(&o.VNI, "vni", o.VNI, "VNI of the route.") } func (o *DeleteRouteOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"prefix", "next-hop-vni", "next-hop-ip", "vni"} { + for _, name := range []string{"prefix", "vni"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -79,42 +76,20 @@ func (o *DeleteRouteOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeleteRoute(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteRouteOptions) error { - client, cleanup, err := factory.NewClient(ctx) +func RunDeleteRoute(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteRouteOptions) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { - return fmt.Errorf("error deleting dpdk client: %w", err) + return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() + defer DpdkClose(cleanup) - if err := client.DeleteRoute(ctx, opts.VNI, opts.Prefix, opts.NextHopVNI, opts.NextHopIP); err != nil { - return fmt.Errorf("error deleting route %d-%v:%d-%v: %v", opts.VNI, opts.Prefix, opts.NextHopVNI, opts.NextHopIP, err) + route, err := client.DeleteRoute(ctx, opts.VNI, opts.Prefix) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error deleting route: %w", err) } - renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } - route := api.Route{ - TypeMeta: api.TypeMeta{Kind: api.RouteKind}, - RouteMeta: api.RouteMeta{ - VNI: opts.VNI, - Prefix: opts.Prefix, - NextHop: api.RouteNextHop{ - VNI: opts.NextHopVNI, - IP: opts.NextHopIP, - }, - }, - Status: api.Status{ - Message: "Deleted", - }, - } - if err := renderer.Render(&route); err != nil { - return fmt.Errorf("error rendering route: %w", err) - } - - return nil + route.TypeMeta.Kind = api.RouteKind + route.RouteMeta.VNI = opts.VNI + route.RouteMeta.Prefix = opts.Prefix + return rendererFactory.RenderObject("deleted", os.Stdout, route) } diff --git a/cmd/delete_virtualip.go b/cmd/delete_virtualip.go index 5e91f86..52e1d38 100644 --- a/cmd/delete_virtualip.go +++ b/cmd/delete_virtualip.go @@ -20,12 +20,13 @@ import ( "os" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" ) -func DeleteVirtualIP(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func DeleteVirtualIP(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts DeleteVirtualIPOptions ) @@ -40,7 +41,7 @@ func DeleteVirtualIP(factory DPDKClientFactory, rendererFactory RendererFactory) return RunDeleteVirtualIP( cmd.Context(), - factory, + dpdkClientFactory, rendererFactory, opts, ) @@ -71,37 +72,19 @@ func (o *DeleteVirtualIPOptions) MarkRequiredFlags(cmd *cobra.Command) error { return nil } -func RunDeleteVirtualIP(ctx context.Context, factory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteVirtualIPOptions) error { - client, cleanup, err := factory.NewClient(ctx) +func RunDeleteVirtualIP(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts DeleteVirtualIPOptions) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() + defer DpdkClose(cleanup) - if err := client.DeleteVirtualIP(ctx, opts.InterfaceID); err != nil { - return fmt.Errorf("error deleting virtual ip of interface %s: %v", opts.InterfaceID, err) + vip, err := client.DeleteVirtualIP(ctx, opts.InterfaceID) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error deleting virtual ip: %w", err) } - renderer, err := rendererFactory.NewRenderer("deleted", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } - virtualIP := api.VirtualIP{ - TypeMeta: api.TypeMeta{Kind: api.VirtualIPKind}, - VirtualIPMeta: api.VirtualIPMeta{ - InterfaceID: opts.InterfaceID, - }, - Status: api.Status{ - Message: "Deleted", - }, - } - if err := renderer.Render(&virtualIP); err != nil { - return fmt.Errorf("error rendering prefix: %w", err) - } - - return nil + vip.TypeMeta.Kind = api.VirtualIPKind + vip.VirtualIPMeta.InterfaceID = opts.InterfaceID + return rendererFactory.RenderObject("deleted", os.Stdout, vip) } diff --git a/cmd/get_firewall_rule.go b/cmd/get_firewall_rule.go index 5887774..4078a20 100644 --- a/cmd/get_firewall_rule.go +++ b/cmd/get_firewall_rule.go @@ -87,14 +87,12 @@ func RunGetFirewallRule( defer DpdkClose(cleanup) fwrule, err := client.GetFirewallRule(ctx, opts.RuleID, opts.InterfaceID) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error getting firewall rule: %w", err) + } fwrule.TypeMeta.Kind = api.FirewallRuleKind fwrule.FirewallRuleMeta.InterfaceID = opts.InterfaceID fwrule.FirewallRuleMeta.RuleID = opts.RuleID - if err == errors.ErrServerError { - return rendererFactory.RenderObject("server error", os.Stdout, fwrule) - } else if err != nil { - return fmt.Errorf("error getting firewall rule: %w", err) - } return rendererFactory.RenderObject("", os.Stdout, fwrule) } diff --git a/cmd/get_interface.go b/cmd/get_interface.go index 911f1ec..de11f3a 100644 --- a/cmd/get_interface.go +++ b/cmd/get_interface.go @@ -19,6 +19,8 @@ import ( "fmt" "os" + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -77,26 +79,16 @@ func RunGetInterface( ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { - return fmt.Errorf("error getting dpdk client: %w", err) - } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) + return fmt.Errorf("error creating dpdk client: %w", err) } + defer DpdkClose(cleanup) iface, err := client.GetInterface(ctx, opts.ID) - if err != nil { + if err != nil && err != errors.ErrServerError { return fmt.Errorf("error getting interface: %w", err) } - if err := renderer.Render(iface); err != nil { - return fmt.Errorf("error rendering interface %s: %w", opts.ID, err) - } - return nil + iface.TypeMeta.Kind = api.InterfaceKind + iface.InterfaceMeta.ID = opts.ID + return rendererFactory.RenderObject("", os.Stdout, iface) } diff --git a/cmd/get_loadbalancer.go b/cmd/get_loadbalancer.go index 66c5b53..a6753d9 100644 --- a/cmd/get_loadbalancer.go +++ b/cmd/get_loadbalancer.go @@ -19,6 +19,8 @@ import ( "fmt" "os" + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -78,26 +80,16 @@ func RunGetLoadBalancer( ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { - return fmt.Errorf("error getting dpdk client: %w", err) - } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) + return fmt.Errorf("error creating dpdk client: %w", err) } + defer DpdkClose(cleanup) lb, err := client.GetLoadBalancer(ctx, opts.ID) - if err != nil { + if err != nil && err != errors.ErrServerError { return fmt.Errorf("error getting loadbalancer: %w", err) } - if err := renderer.Render(lb); err != nil { - return fmt.Errorf("error rendering loadbalancer %s: %w", opts.ID, err) - } - return nil + lb.TypeMeta.Kind = api.LoadBalancerKind + lb.LoadBalancerMeta.ID = opts.ID + return rendererFactory.RenderObject("", os.Stdout, lb) } diff --git a/cmd/get_loadbalancer_target.go b/cmd/get_loadbalancer_target.go index ec942a7..aa32ddd 100644 --- a/cmd/get_loadbalancer_target.go +++ b/cmd/get_loadbalancer_target.go @@ -19,6 +19,8 @@ import ( "fmt" "os" + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -78,27 +80,15 @@ func RunGetLoadBalancerTargets( ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { - return fmt.Errorf("error getting dpdk client: %w", err) - } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) + return fmt.Errorf("error creating dpdk client: %w", err) } + defer DpdkClose(cleanup) - lbtarget, err := client.GetLoadBalancerTargets(ctx, opts.LoadBalancerID) - if err != nil { - return fmt.Errorf("error getting loadbalancer target for interface %s: %v", opts.LoadBalancerID, err) - } - - if err := renderer.Render(lbtarget); err != nil { - return fmt.Errorf("error rendering loadbalancer target: %w", err) + lbtargets, err := client.GetLoadBalancerTargets(ctx, opts.LoadBalancerID) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error listing loadbalancer targets: %w", err) } - return nil + lbtargets.TypeMeta.Kind = api.LoadBalancerTargetListKind + return rendererFactory.RenderList("", os.Stdout, lbtargets) } diff --git a/cmd/get_nat.go b/cmd/get_nat.go index 41697d5..9b8c6d5 100644 --- a/cmd/get_nat.go +++ b/cmd/get_nat.go @@ -19,6 +19,8 @@ import ( "fmt" "os" + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -78,27 +80,16 @@ func RunGetNat( ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { - return fmt.Errorf("error getting dpdk client: %w", err) - } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) + return fmt.Errorf("error creating dpdk client: %w", err) } + defer DpdkClose(cleanup) nat, err := client.GetNat(ctx, opts.InterfaceID) - if err != nil { - return fmt.Errorf("error getting nat for interface %s: %v", opts.InterfaceID, err) - } - - if err := renderer.Render(nat); err != nil { - return fmt.Errorf("error rendering nat: %w", err) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error getting nat: %w", err) } - return nil + nat.TypeMeta.Kind = api.NatKind + nat.NatMeta.InterfaceID = opts.InterfaceID + return rendererFactory.RenderObject("", os.Stdout, nat) } diff --git a/cmd/get_nat_info.go b/cmd/get_nat_info.go index bb5dcc6..c5c85da 100644 --- a/cmd/get_nat_info.go +++ b/cmd/get_nat_info.go @@ -81,27 +81,14 @@ func RunGetNatInfo( ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { - return fmt.Errorf("error getting dpdk client: %w", err) - } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) + return fmt.Errorf("error creating dpdk client: %w", err) } + defer DpdkClose(cleanup) natinfo, err := client.GetNATInfo(ctx, opts.NatIP, opts.NatInfoType) if err != nil { - return fmt.Errorf("error getting nat info for ip %s: %v", opts.NatIP, err) + return fmt.Errorf("error listing nats: %w", err) } - if err := renderer.Render(natinfo); err != nil { - return fmt.Errorf("error rendering nat info: %w", err) - } - - return nil + return rendererFactory.RenderList("", os.Stdout, natinfo) } diff --git a/cmd/get_virtualip.go b/cmd/get_virtualip.go index b9d1b9e..563d627 100644 --- a/cmd/get_virtualip.go +++ b/cmd/get_virtualip.go @@ -19,6 +19,8 @@ import ( "fmt" "os" + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -78,26 +80,16 @@ func RunGetVirtualIP( ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { - return fmt.Errorf("error getting dpdk client: %w", err) - } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) + return fmt.Errorf("error creating dpdk client: %w", err) } + defer DpdkClose(cleanup) virtualIP, err := client.GetVirtualIP(ctx, opts.InterfaceID) - if err != nil { - return fmt.Errorf("error getting virtual ip for interface %s: %v", opts.InterfaceID, err) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error getting virtual ip: %w", err) } - if err := renderer.Render(virtualIP); err != nil { - return fmt.Errorf("error rendering virtual ip: %w", err) - } - return nil + virtualIP.TypeMeta.Kind = api.VirtualIPKind + virtualIP.VirtualIPMeta.InterfaceID = opts.InterfaceID + return rendererFactory.RenderObject("", os.Stdout, virtualIP) } diff --git a/cmd/list_firewall_rules.go b/cmd/list_firewall_rules.go index afe6164..c7d2d48 100644 --- a/cmd/list_firewall_rules.go +++ b/cmd/list_firewall_rules.go @@ -86,12 +86,5 @@ func RunListFirewallRules( return fmt.Errorf("error listing firewall rules: %w", err) } - renderer, err := rendererFactory.NewRenderer("", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } - if err := renderer.Render(fwrules); err != nil { - return fmt.Errorf("error rendering firewall rules on interface %s: %w", opts.InterfaceID, err) - } - return nil + return rendererFactory.RenderList("", os.Stdout, fwrules) } diff --git a/cmd/list_interfaces.go b/cmd/list_interfaces.go index 1a540b4..480037d 100644 --- a/cmd/list_interfaces.go +++ b/cmd/list_interfaces.go @@ -61,24 +61,12 @@ func RunListInterfaces( if err != nil { return fmt.Errorf("error getting dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } + defer DpdkClose(cleanup) interfaceList, err := client.ListInterfaces(ctx) if err != nil { - return fmt.Errorf("error listing firewall rules: %w", err) + return fmt.Errorf("error listing interfaces: %w", err) } - if err := renderer.Render(interfaceList); err != nil { - return fmt.Errorf("error rendering interfaces: %w", err) - } - return nil + return rendererFactory.RenderList("", os.Stdout, interfaceList) } diff --git a/cmd/list_loadbalancer_prefixes.go b/cmd/list_loadbalancer_prefixes.go index acf7c9f..4d23074 100644 --- a/cmd/list_loadbalancer_prefixes.go +++ b/cmd/list_loadbalancer_prefixes.go @@ -72,32 +72,20 @@ func (o *ListLoadBalancerPrefixesOptions) MarkRequiredFlags(cmd *cobra.Command) func RunListLoadBalancerPrefixes( ctx context.Context, - factory DPDKClientFactory, + dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, opts ListLoadBalancerPrefixesOptions, ) error { - client, cleanup, err := factory.NewClient(ctx) + client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { - return fmt.Errorf("error creating client: %w", err) - } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) + return fmt.Errorf("error creating dpdk client: %w", err) } + defer DpdkClose(cleanup) prefixList, err := client.ListLoadBalancerPrefixes(ctx, opts.InterfaceID) if err != nil { return fmt.Errorf("error listing loadbalancer prefixes: %w", err) } - if err := renderer.Render(prefixList); err != nil { - return fmt.Errorf("error rendering list: %w", err) - } - return nil + return rendererFactory.RenderList("", os.Stdout, prefixList) } diff --git a/cmd/list_prefixes.go b/cmd/list_prefixes.go index 289805c..e592434 100644 --- a/cmd/list_prefixes.go +++ b/cmd/list_prefixes.go @@ -79,24 +79,12 @@ func RunListPrefixes( if err != nil { return fmt.Errorf("error creating client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } + defer DpdkClose(cleanup) prefixList, err := client.ListPrefixes(ctx, opts.InterfaceID) if err != nil { return fmt.Errorf("error listing prefixes: %w", err) } - if err := renderer.Render(prefixList); err != nil { - return fmt.Errorf("error rendering prefix list: %w", err) - } - return nil + return rendererFactory.RenderList("", os.Stdout, prefixList) } diff --git a/cmd/list_routes.go b/cmd/list_routes.go index 563794c..ccf6df5 100644 --- a/cmd/list_routes.go +++ b/cmd/list_routes.go @@ -80,24 +80,12 @@ func RunGetRoute( if err != nil { return fmt.Errorf("error creating dpdk client: %w", err) } - defer func() { - if err := cleanup(); err != nil { - fmt.Printf("Error cleaning up client: %v\n", err) - } - }() - - renderer, err := rendererFactory.NewRenderer("", os.Stdout) - if err != nil { - return fmt.Errorf("error creating renderer: %w", err) - } + defer DpdkClose(cleanup) routeList, err := client.ListRoutes(ctx, opts.VNI) if err != nil { return fmt.Errorf("error listing routes: %w", err) } - if err := renderer.Render(routeList); err != nil { - return fmt.Errorf("error rendering list: %w", err) - } - return nil + return rendererFactory.RenderList("", os.Stdout, routeList) } diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index 131f7ad..a62d5bb 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -178,6 +178,11 @@ func ProtoVirtualIPToVirtualIP(interfaceID string, dpdkVIP *proto.InterfaceVIPIP return nil, fmt.Errorf("error parsing virtual ip address: %w", err) } + underlayRoute, err := netip.ParseAddr(string(dpdkVIP.UnderlayRoute)) + if err != nil { + return nil, fmt.Errorf("error parsing underlay route: %w", err) + } + return &VirtualIP{ TypeMeta: TypeMeta{ Kind: VirtualIPKind, @@ -186,7 +191,9 @@ func ProtoVirtualIPToVirtualIP(interfaceID string, dpdkVIP *proto.InterfaceVIPIP InterfaceID: interfaceID, IP: ip, }, - Spec: VirtualIPSpec{}, + Spec: VirtualIPSpec{ + UnderlayRoute: &underlayRoute, + }, }, nil } @@ -240,7 +247,7 @@ func ProtoRouteToRoute(vni uint32, dpdkRoute *proto.Route) (*Route, error) { Prefix: prefix, NextHop: RouteNextHop{ VNI: dpdkRoute.GetNexthopVNI(), - IP: nextHopIP, + IP: &nextHopIP, }, }, Spec: RouteSpec{}, @@ -287,7 +294,7 @@ func ProtoNatToNat(dpdkNat *proto.GetNATResponse, interfaceID string) (*Nat, err MaxPort: dpdkNat.MaxPort, UnderlayRoute: &underlayRoute, }, - Status: &Status{ + Status: Status{ Error: dpdkNat.Status.Error, Message: dpdkNat.Status.Message, }, diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 9d487f5..6c8a6ce 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -25,6 +25,7 @@ import ( type Object interface { GetKind() string GetName() string + GetStatus() int32 } type List interface { @@ -48,18 +49,6 @@ func (status *Status) String() string { return fmt.Sprintf("Error: %d, Message: %s", status.Error, status.Message) } -type ServerError struct { - ServerError Status `json:"serverError"` -} - -func (m *ServerError) GetKind() string { - return "ServerError" -} - -func (m *ServerError) GetName() string { - return fmt.Sprintf("%d", m.ServerError.Error) -} - type RouteList struct { TypeMeta `json:",inline"` Items []Route `json:"items"` @@ -90,12 +79,16 @@ func (m *RouteMeta) GetName() string { return fmt.Sprintf("%s-%d:%s", m.Prefix, m.NextHop.VNI, m.NextHop.IP) } +func (m *Route) GetStatus() int32 { + return m.Status.Error +} + type RouteSpec struct { } type RouteNextHop struct { - VNI uint32 `json:"vni"` - IP netip.Addr `json:"ip"` + VNI uint32 `json:"vni"` + IP *netip.Addr `json:"ip,omitempty"` } type PrefixList struct { @@ -127,6 +120,10 @@ func (m *PrefixMeta) GetName() string { return m.Prefix.String() } +func (m *Prefix) GetStatus() int32 { + return m.Status.Error +} + type PrefixSpec struct { UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` } @@ -147,6 +144,10 @@ func (m *VirtualIPMeta) GetName() string { return m.IP.String() } +func (m *VirtualIP) GetStatus() int32 { + return m.Status.Error +} + type VirtualIPSpec struct { UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` } @@ -167,6 +168,10 @@ func (m *LoadBalancerMeta) GetName() string { return m.ID } +func (m *LoadBalancer) GetStatus() int32 { + return m.Status.Error +} + type LoadBalancerSpec struct { VNI uint32 `json:"vni,omitempty"` LbVipIP *netip.Addr `json:"lbVipIP,omitempty"` @@ -207,6 +212,10 @@ func (m *LoadBalancerTargetMeta) GetName() string { return m.ID } +func (m *LoadBalancerTarget) GetStatus() int32 { + return m.Status.Error +} + type LoadBalancerTargetSpec struct { TargetIP *netip.Addr `json:"targetIP,omitempty"` } @@ -214,6 +223,15 @@ type LoadBalancerTargetSpec struct { type LoadBalancerTargetList struct { TypeMeta `json:",inline"` Items []LoadBalancerTarget `json:"items"` + Status Status `json:"status"` +} + +func (l *LoadBalancerTargetList) GetItems() []Object { + res := make([]Object, len(l.Items)) + for i := range l.Items { + res[i] = &l.Items[i] + } + return res } // Interface section @@ -232,6 +250,10 @@ func (m *InterfaceMeta) GetName() string { return m.ID } +func (m *Interface) GetStatus() int32 { + return m.Status.Error +} + type InterfaceSpec struct { VNI uint32 `json:"vni,omitempty"` Device string `json:"device,omitempty"` @@ -270,7 +292,7 @@ type Nat struct { TypeMeta `json:",inline"` NatMeta `json:"metadata"` Spec NatSpec `json:"spec"` - Status *Status `json:"status,omitempty"` + Status Status `json:"status"` } type NatMeta struct { @@ -281,6 +303,10 @@ func (m *NatMeta) GetName() string { return m.InterfaceID } +func (m *Nat) GetStatus() int32 { + return m.Status.Error +} + type NatSpec struct { NatVIPIP *netip.Addr `json:"natVIPIP,omitempty"` MinPort uint32 `json:"minPort,omitempty"` @@ -316,6 +342,10 @@ func (m *NeighborNatMeta) GetName() string { return m.NatVIPIP.String() } +func (m *NeighborNat) GetStatus() int32 { + return m.Status.Error +} + type NeighborNatSpec struct { Vni uint32 `json:"vni,omitempty"` MinPort uint32 `json:"minPort,omitempty"` @@ -340,6 +370,10 @@ func (m *FirewallRuleMeta) GetName() string { return m.InterfaceID + "/" + m.RuleID } +func (m *FirewallRule) GetStatus() int32 { + return m.Status.Error +} + type FirewallRuleSpec struct { TrafficDirection string `json:"trafficDirection,omitempty"` FirewallAction string `json:"firewallAction,omitempty"` diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 99756e7..e43cbfd 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -29,40 +29,40 @@ import ( type Client interface { GetLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) (*api.LoadBalancer, error) - DeleteLoadBalancer(ctx context.Context, id string) error + DeleteLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) ListLoadBalancerPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) CreateLoadBalancerPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix, error) - DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) error + DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) (*api.Prefix, error) GetLoadBalancerTargets(ctx context.Context, interfaceID string) (*api.LoadBalancerTargetList, error) AddLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBalancerTarget) (*api.LoadBalancerTarget, error) - DeleteLoadBalancerTarget(ctx context.Context, id string, targetIP netip.Addr) error + DeleteLoadBalancerTarget(ctx context.Context, id string, targetIP netip.Addr) (*api.LoadBalancerTarget, error) GetInterface(ctx context.Context, id string) (*api.Interface, error) ListInterfaces(ctx context.Context) (*api.InterfaceList, error) CreateInterface(ctx context.Context, iface *api.Interface) (*api.Interface, error) - DeleteInterface(ctx context.Context, id string) error + DeleteInterface(ctx context.Context, id string) (*api.Interface, error) GetVirtualIP(ctx context.Context, interfaceID string) (*api.VirtualIP, error) AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*api.VirtualIP, error) - DeleteVirtualIP(ctx context.Context, interfaceID string) error + DeleteVirtualIP(ctx context.Context, interfaceID string) (*api.VirtualIP, error) ListPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) AddPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix, error) - DeletePrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) error + DeletePrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) (*api.Prefix, error) ListRoutes(ctx context.Context, vni uint32) (*api.RouteList, error) AddRoute(ctx context.Context, route *api.Route) (*api.Route, error) - DeleteRoute(ctx context.Context, vni uint32, prefix netip.Prefix, nextHopVNI uint32, nextHopIP netip.Addr) error + DeleteRoute(ctx context.Context, vni uint32, prefix netip.Prefix) (*api.Route, error) GetNat(ctx context.Context, interfaceID string) (*api.Nat, error) AddNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) - DeleteNat(ctx context.Context, interfaceID string) error + DeleteNat(ctx context.Context, interfaceID string) (*api.Nat, error) AddNeighborNat(ctx context.Context, nat *api.NeighborNat) (*api.NeighborNat, error) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType string) (*api.NatList, error) - DeleteNeighborNat(ctx context.Context, neigbhorNat api.NeighborNat) error + DeleteNeighborNat(ctx context.Context, neigbhorNat api.NeighborNat) (*api.NeighborNat, error) ListFirewallRules(ctx context.Context, interfaceID string) (*api.FirewallRuleList, error) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) @@ -84,13 +84,12 @@ func NewClient(protoClient dpdkproto.DPDKonmetalClient) Client { func (c *client) GetLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) { res, err := c.DPDKonmetalClient.GetLoadBalancer(ctx, &dpdkproto.GetLoadBalancerRequest{LoadBalancerID: []byte(id)}) if err != nil { - return nil, err + return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res.Status)}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } - lb, err := api.ProtoLoadBalancerToLoadBalancer(res, id) - return lb, err + return api.ProtoLoadBalancerToLoadBalancer(res, id) } func (c *client) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) (*api.LoadBalancer, error) { @@ -106,15 +105,15 @@ func (c *client) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) ( Lbports: lbPorts, }) if err != nil { - return nil, err + return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res.Status)}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) if err != nil { - return nil, fmt.Errorf("error parsing underlay route: %w", err) + return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res.Status)}, fmt.Errorf("error parsing underlay route: %w", err) } lb.Spec.UnderlayRoute = &underlayRoute @@ -126,15 +125,15 @@ func (c *client) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) ( }, nil } -func (c *client) DeleteLoadBalancer(ctx context.Context, id string) error { +func (c *client) DeleteLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) { res, err := c.DPDKonmetalClient.DeleteLoadBalancer(ctx, &dpdkproto.DeleteLoadBalancerRequest{LoadBalancerID: []byte(id)}) if err != nil { - return err + return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res)}, err } if errorCode := res.GetError(); errorCode != 0 { - return apierrors.NewStatusError(errorCode, res.GetMessage()) + return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } - return nil + return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res)}, nil } func (c *client) ListLoadBalancerPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) { @@ -173,14 +172,14 @@ func (c *client) CreateLoadBalancerPrefix(ctx context.Context, prefix *api.Prefi }, }) if err != nil { - return nil, err + return &api.Prefix{Status: api.ProtoStatusToStatus(res.Status)}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + return &api.Prefix{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) if err != nil { - return nil, fmt.Errorf("error parsing underlay route: %w", err) + return &api.Prefix{Status: api.ProtoStatusToStatus(res.Status)}, fmt.Errorf("error parsing underlay route: %w", err) } return &api.Prefix{ TypeMeta: api.TypeMeta{Kind: "LoadBalancerPrefix"}, @@ -190,7 +189,7 @@ func (c *client) CreateLoadBalancerPrefix(ctx context.Context, prefix *api.Prefi }, nil } -func (c *client) DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) error { +func (c *client) DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) (*api.Prefix, error) { res, err := c.DPDKonmetalClient.DeleteInterfaceLoadBalancerPrefix(ctx, &dpdkproto.DeleteInterfaceLoadBalancerPrefixRequest{ InterfaceID: &dpdkproto.InterfaceIDMsg{ InterfaceID: []byte(interfaceID), @@ -202,12 +201,12 @@ func (c *client) DeleteLoadBalancerPrefix(ctx context.Context, interfaceID strin }, }) if err != nil { - return err + return &api.Prefix{Status: api.ProtoStatusToStatus(res)}, err } if errorCode := res.GetError(); errorCode != 0 { - return apierrors.NewStatusError(errorCode, res.GetMessage()) + return &api.Prefix{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } - return nil + return &api.Prefix{Status: api.ProtoStatusToStatus(res)}, nil } func (c *client) GetLoadBalancerTargets(ctx context.Context, loadBalancerID string) (*api.LoadBalancerTargetList, error) { @@ -215,10 +214,10 @@ func (c *client) GetLoadBalancerTargets(ctx context.Context, loadBalancerID stri LoadBalancerID: []byte(loadBalancerID), }) if err != nil { - return nil, err + return &api.LoadBalancerTargetList{Status: api.ProtoStatusToStatus(res.Status)}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + return &api.LoadBalancerTargetList{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } lbtargets := make([]api.LoadBalancerTarget, len(res.GetTargetIPs())) @@ -234,6 +233,8 @@ func (c *client) GetLoadBalancerTargets(ctx context.Context, loadBalancerID stri return &api.LoadBalancerTargetList{ TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetListKind}, Items: lbtargets, + // TODO server is not returning correct status + //Status: api.ProtoStatusToStatus(res.Status), }, nil } @@ -257,27 +258,27 @@ func (c *client) AddLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBa }, nil } -func (c *client) DeleteLoadBalancerTarget(ctx context.Context, id string, targetIP netip.Addr) error { +func (c *client) DeleteLoadBalancerTarget(ctx context.Context, id string, targetIP netip.Addr) (*api.LoadBalancerTarget, error) { res, err := c.DPDKonmetalClient.DeleteLoadBalancerTarget(ctx, &dpdkproto.DeleteLoadBalancerTargetRequest{ LoadBalancerID: []byte(id), TargetIP: api.LbipToProtoLbip(targetIP), }) if err != nil { - return err + return &api.LoadBalancerTarget{Status: api.ProtoStatusToStatus(res)}, err } if errorCode := res.GetError(); errorCode != 0 { - return apierrors.NewStatusError(errorCode, res.GetMessage()) + return &api.LoadBalancerTarget{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } - return nil + return &api.LoadBalancerTarget{Status: api.ProtoStatusToStatus(res)}, nil } func (c *client) GetInterface(ctx context.Context, name string) (*api.Interface, error) { res, err := c.DPDKonmetalClient.GetInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(name)}) if err != nil { - return nil, err + return &api.Interface{Status: api.ProtoStatusToStatus(res.Status)}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + return &api.Interface{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } return api.ProtoInterfaceToInterface(res.GetInterface()) } @@ -314,15 +315,15 @@ func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*ap DeviceName: iface.Spec.Device, }) if err != nil { - return nil, err + return &api.Interface{Status: api.ProtoStatusToStatus(res.Response.Status)}, err } if errorCode := res.GetResponse().GetStatus().GetError(); errorCode != 0 { - return nil, apierrors.NewStatusError(errorCode, res.GetResponse().GetStatus().GetMessage()) + return &api.Interface{Status: api.ProtoStatusToStatus(res.Response.Status)}, apierrors.ErrServerError } underlayRoute, err := netip.ParseAddr(string(res.GetResponse().GetUnderlayRoute())) if err != nil { - return nil, fmt.Errorf("error parsing underlay route: %w", err) + return &api.Interface{Status: api.ProtoStatusToStatus(res.Response.Status)}, fmt.Errorf("error parsing underlay route: %w", err) } return &api.Interface{ @@ -345,15 +346,15 @@ func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*ap }, nil } -func (c *client) DeleteInterface(ctx context.Context, name string) error { +func (c *client) DeleteInterface(ctx context.Context, name string) (*api.Interface, error) { res, err := c.DPDKonmetalClient.DeleteInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(name)}) if err != nil { - return err + return &api.Interface{Status: api.ProtoStatusToStatus(res)}, err } if errorCode := res.GetError(); errorCode != 0 { - return apierrors.NewStatusError(errorCode, res.GetMessage()) + return &api.Interface{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } - return nil + return &api.Interface{Status: api.ProtoStatusToStatus(res)}, nil } func (c *client) GetVirtualIP(ctx context.Context, interfaceName string) (*api.VirtualIP, error) { @@ -361,12 +362,11 @@ func (c *client) GetVirtualIP(ctx context.Context, interfaceName string) (*api.V InterfaceID: []byte(interfaceName), }) if err != nil { - return nil, err + return &api.VirtualIP{Status: api.ProtoStatusToStatus(res.Status)}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + return &api.VirtualIP{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } - return api.ProtoVirtualIPToVirtualIP(interfaceName, res) } @@ -379,14 +379,14 @@ func (c *client) AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*a }, }) if err != nil { - return nil, err + return &api.VirtualIP{Status: api.ProtoStatusToStatus(res.Status)}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + return &api.VirtualIP{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) if err != nil { - return nil, fmt.Errorf("error parsing underlay route: %w", err) + return &api.VirtualIP{Status: api.ProtoStatusToStatus(res.Status)}, fmt.Errorf("error parsing underlay route: %w", err) } return &api.VirtualIP{ @@ -397,17 +397,17 @@ func (c *client) AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*a }, nil } -func (c *client) DeleteVirtualIP(ctx context.Context, interfaceID string) error { +func (c *client) DeleteVirtualIP(ctx context.Context, interfaceID string) (*api.VirtualIP, error) { res, err := c.DPDKonmetalClient.DeleteInterfaceVIP(ctx, &dpdkproto.InterfaceIDMsg{ InterfaceID: []byte(interfaceID), }) if err != nil { - return err + return &api.VirtualIP{Status: api.ProtoStatusToStatus(res)}, err } if errorCode := res.GetError(); errorCode != 0 { - return apierrors.NewStatusError(errorCode, res.GetMessage()) + return &api.VirtualIP{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } - return nil + return &api.VirtualIP{Status: api.ProtoStatusToStatus(res)}, nil } func (c *client) ListPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) { @@ -446,14 +446,14 @@ func (c *client) AddPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix }, }) if err != nil { - return nil, err + return &api.Prefix{Status: api.ProtoStatusToStatus(res.Status)}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + return &api.Prefix{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) if err != nil { - return nil, fmt.Errorf("error parsing underlay route: %w", err) + return &api.Prefix{Status: api.ProtoStatusToStatus(res.Status)}, fmt.Errorf("error parsing underlay route: %w", err) } return &api.Prefix{ TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, @@ -463,7 +463,7 @@ func (c *client) AddPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix }, nil } -func (c *client) DeletePrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) error { +func (c *client) DeletePrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) (*api.Prefix, error) { res, err := c.DPDKonmetalClient.DeleteInterfacePrefix(ctx, &dpdkproto.InterfacePrefixMsg{ InterfaceID: &dpdkproto.InterfaceIDMsg{ InterfaceID: []byte(interfaceID), @@ -475,19 +475,19 @@ func (c *client) DeletePrefix(ctx context.Context, interfaceID string, prefix ne }, }) if err != nil { - return err + return &api.Prefix{Status: api.ProtoStatusToStatus(res)}, err } if errorCode := res.GetError(); errorCode != 0 { - return apierrors.NewStatusError(errorCode, res.GetMessage()) + return &api.Prefix{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } - return nil + return &api.Prefix{Status: api.ProtoStatusToStatus(res)}, nil } func (c *client) AddRoute(ctx context.Context, route *api.Route) (*api.Route, error) { res, err := c.DPDKonmetalClient.AddRoute(ctx, &dpdkproto.VNIRouteMsg{ Vni: &dpdkproto.VNIMsg{Vni: route.VNI}, Route: &dpdkproto.Route{ - IpVersion: api.NetIPAddrToProtoIPVersion(route.NextHop.IP), + IpVersion: api.NetIPAddrToProtoIPVersion(*route.NextHop.IP), Weight: 100, Prefix: &dpdkproto.Prefix{ IpVersion: api.NetIPAddrToProtoIPVersion(route.Prefix.Addr()), @@ -499,10 +499,10 @@ func (c *client) AddRoute(ctx context.Context, route *api.Route) (*api.Route, er }, }) if err != nil { - return nil, err + return &api.Route{Status: api.ProtoStatusToStatus(res)}, err } if errorCode := res.GetError(); errorCode != 0 { - return nil, apierrors.NewStatusError(errorCode, res.GetMessage()) + return &api.Route{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } return &api.Route{ TypeMeta: api.TypeMeta{Kind: api.RouteKind}, @@ -512,28 +512,26 @@ func (c *client) AddRoute(ctx context.Context, route *api.Route) (*api.Route, er }, nil } -func (c *client) DeleteRoute(ctx context.Context, vni uint32, prefix netip.Prefix, nextHopVNI uint32, nextHopIP netip.Addr) error { +func (c *client) DeleteRoute(ctx context.Context, vni uint32, prefix netip.Prefix) (*api.Route, error) { res, err := c.DPDKonmetalClient.DeleteRoute(ctx, &dpdkproto.VNIRouteMsg{ Vni: &dpdkproto.VNIMsg{Vni: vni}, Route: &dpdkproto.Route{ - IpVersion: api.NetIPAddrToProtoIPVersion(nextHopIP), + IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Addr()), Weight: 100, Prefix: &dpdkproto.Prefix{ IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Addr()), Address: []byte(prefix.Addr().String()), PrefixLength: uint32(prefix.Bits()), }, - NexthopVNI: nextHopVNI, - NexthopAddress: []byte(nextHopIP.String()), }, }) if err != nil { - return err + return &api.Route{Status: api.ProtoStatusToStatus(res)}, err } if errorCode := res.GetError(); errorCode != 0 { - return apierrors.NewStatusError(errorCode, res.GetMessage()) + return &api.Route{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } - return nil + return &api.Route{Status: api.ProtoStatusToStatus(res)}, nil } func (c *client) ListRoutes(ctx context.Context, vni uint32) (*api.RouteList, error) { @@ -563,13 +561,12 @@ func (c *client) ListRoutes(ctx context.Context, vni uint32) (*api.RouteList, er func (c *client) GetNat(ctx context.Context, interfaceID string) (*api.Nat, error) { res, err := c.DPDKonmetalClient.GetNAT(ctx, &dpdkproto.GetNATRequest{InterfaceID: []byte(interfaceID)}) if err != nil { - return nil, err + return &api.Nat{Status: api.ProtoStatusToStatus(res.Status)}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + return &api.Nat{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } - nat, err := api.ProtoNatToNat(res, interfaceID) - return nat, err + return api.ProtoNatToNat(res, interfaceID) } func (c *client) AddNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) { @@ -583,15 +580,15 @@ func (c *client) AddNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) { MaxPort: nat.Spec.MaxPort, }) if err != nil { - return nil, err + return &api.Nat{Status: api.ProtoStatusToStatus(res.Status)}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return nil, apierrors.NewStatusError(errorCode, res.GetStatus().GetMessage()) + return &api.Nat{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) if err != nil { - return nil, fmt.Errorf("error parsing underlay route: %w", err) + return &api.Nat{Status: api.ProtoStatusToStatus(res.Status)}, fmt.Errorf("error parsing underlay route: %w", err) } nat.Spec.UnderlayRoute = &underlayRoute status := api.ProtoStatusToStatus(res.Status) @@ -600,21 +597,21 @@ func (c *client) AddNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) { TypeMeta: api.TypeMeta{Kind: api.NatKind}, NatMeta: nat.NatMeta, Spec: nat.Spec, - Status: &status, + Status: status, }, nil } -func (c *client) DeleteNat(ctx context.Context, interfaceID string) error { +func (c *client) DeleteNat(ctx context.Context, interfaceID string) (*api.Nat, error) { res, err := c.DPDKonmetalClient.DeleteNAT(ctx, &dpdkproto.DeleteNATRequest{ InterfaceID: []byte(interfaceID), }) if err != nil { - return err + return &api.Nat{Status: api.ProtoStatusToStatus(res)}, err } if errorCode := res.GetError(); errorCode != 0 { - return apierrors.NewStatusError(errorCode, res.GetMessage()) + return &api.Nat{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } - return nil + return &api.Nat{Status: api.ProtoStatusToStatus(res)}, nil } func (c *client) AddNeighborNat(ctx context.Context, nNat *api.NeighborNat) (*api.NeighborNat, error) { @@ -630,18 +627,17 @@ func (c *client) AddNeighborNat(ctx context.Context, nNat *api.NeighborNat) (*ap UnderlayRoute: []byte(nNat.Spec.UnderlayRoute.String()), }) if err != nil { - return nil, err + return &api.NeighborNat{Status: api.ProtoStatusToStatus(res)}, err } - - if res.Error == 0 { - return &api.NeighborNat{ - TypeMeta: api.TypeMeta{Kind: api.NeighborNatKind}, - NeighborNatMeta: nNat.NeighborNatMeta, - Spec: nNat.Spec, - Status: api.ProtoStatusToStatus(res), - }, nil + if errorCode := res.GetError(); errorCode != 0 { + return &api.NeighborNat{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } - return nil, fmt.Errorf("%d", res.Error) + return &api.NeighborNat{ + TypeMeta: api.TypeMeta{Kind: api.NeighborNatKind}, + NeighborNatMeta: nNat.NeighborNatMeta, + Spec: nNat.Spec, + Status: api.ProtoStatusToStatus(res), + }, nil } func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType string) (*api.NatList, error) { @@ -699,7 +695,7 @@ func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType st }, nil } -func (c *client) DeleteNeighborNat(ctx context.Context, neigbhorNat api.NeighborNat) error { +func (c *client) DeleteNeighborNat(ctx context.Context, neigbhorNat api.NeighborNat) (*api.NeighborNat, error) { res, err := c.DPDKonmetalClient.DeleteNeighborNAT(ctx, &dpdkproto.DeleteNeighborNATRequest{ NatVIPIP: &dpdkproto.NATIP{ IpVersion: api.NetIPAddrToProtoIPVersion(*neigbhorNat.NatVIPIP), @@ -710,12 +706,12 @@ func (c *client) DeleteNeighborNat(ctx context.Context, neigbhorNat api.Neighbor MaxPort: neigbhorNat.Spec.MaxPort, }) if err != nil { - return err + return &api.NeighborNat{Status: api.ProtoStatusToStatus(res)}, err } if errorCode := res.GetError(); errorCode != 0 { - return apierrors.NewStatusError(errorCode, res.GetMessage()) + return &api.NeighborNat{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } - return nil + return &api.NeighborNat{Status: api.ProtoStatusToStatus(res)}, nil } func (c *client) ListFirewallRules(ctx context.Context, interfaceID string) (*api.FirewallRuleList, error) { @@ -745,14 +741,14 @@ func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) var action, direction, ipv uint8 switch strings.ToLower(fwRule.Spec.FirewallAction) { - case "accept", "1": + case "accept", "allow", "1": action = 1 fwRule.Spec.FirewallAction = "Accept" - case "drop", "0": + case "drop", "deny", "0": action = 0 fwRule.Spec.FirewallAction = "Drop" default: - return &api.FirewallRule{}, fmt.Errorf("firewall action can be only: Drop = 0/Accept = 1") + return &api.FirewallRule{}, fmt.Errorf("firewall action can be only: drop/deny/0|accept/allow/1") } switch strings.ToLower(fwRule.Spec.TrafficDirection) { diff --git a/dpdk/client/dynamic/dynamic.go b/dpdk/client/dynamic/dynamic.go index 4462562..c68e87c 100644 --- a/dpdk/client/dynamic/dynamic.go +++ b/dpdk/client/dynamic/dynamic.go @@ -106,7 +106,7 @@ func ObjectKeyFromObject(obj any) ObjectKey { VNI: obj.VNI, Prefix: obj.Prefix, NextHopVNI: obj.NextHop.VNI, - NextHopIP: obj.NextHop.IP, + NextHopIP: *obj.NextHop.IP, } case *api.VirtualIP: return VirtualIPKey{ @@ -168,13 +168,17 @@ func (c *client) Create(ctx context.Context, obj any) error { func (c *client) Delete(ctx context.Context, obj any) error { switch obj := obj.(type) { case *api.Interface: - return c.structured.DeleteInterface(ctx, obj.ID) + _, err := c.structured.DeleteInterface(ctx, obj.ID) + return err case *api.Prefix: - return c.structured.DeletePrefix(ctx, obj.InterfaceID, obj.Prefix) + _, err := c.structured.DeletePrefix(ctx, obj.InterfaceID, obj.Prefix) + return err case *api.Route: - return c.structured.DeleteRoute(ctx, obj.VNI, obj.Prefix, obj.NextHop.VNI, obj.NextHop.IP) + _, err := c.structured.DeleteRoute(ctx, obj.VNI, obj.Prefix) + return err case *api.VirtualIP: - return c.structured.DeleteVirtualIP(ctx, obj.InterfaceID) + _, err := c.structured.DeleteVirtualIP(ctx, obj.InterfaceID) + return err default: return fmt.Errorf("unsupported object %T", obj) } diff --git a/renderer/renderer.go b/renderer/renderer.go index 716a867..ba8f0c0 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -171,30 +171,30 @@ func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) { return t.virtualIPTable([]api.VirtualIP{*obj}) case *api.Nat: return t.natTable([]api.Nat{*obj}) + case *api.NeighborNat: + return t.neighborNatTable([]api.NeighborNat{*obj}) case *api.NatList: return t.natTable(obj.Items) case *api.FirewallRule: return t.fwruleTable([]api.FirewallRule{*obj}) case *api.FirewallRuleList: return t.fwruleTable(obj.Items) - case *api.ServerError: - return t.serverErrorTable([]api.ServerError{*obj}) default: return nil, fmt.Errorf("unsupported type %T", v) } } -func (t defaultTableConverter) loadBalancerTable(lbs api.LoadBalancer) (*TableData, error) { - headers := []any{"ID", "VNI", "LbVipIP", "Lbports", "UnderlayRoute"} +func (t defaultTableConverter) loadBalancerTable(lb api.LoadBalancer) (*TableData, error) { + headers := []any{"ID", "VNI", "LbVipIP", "Lbports", "UnderlayRoute", "Status"} columns := make([][]any, 1) - var ports = make([]string, 0, len(lbs.Spec.Lbports)) - for _, port := range lbs.Spec.Lbports { + 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[0] = []any{lbs.ID, lbs.Spec.VNI, lbs.Spec.LbVipIP, ports, lbs.Spec.UnderlayRoute} + columns[0] = []any{lb.ID, lb.Spec.VNI, lb.Spec.LbVipIP, ports, lb.Spec.UnderlayRoute, lb.Status.String()} return &TableData{ Headers: headers, @@ -203,7 +203,7 @@ func (t defaultTableConverter) loadBalancerTable(lbs api.LoadBalancer) (*TableDa } func (t defaultTableConverter) loadBalancerTargetTable(lbtargets []api.LoadBalancerTarget) (*TableData, error) { - headers := []any{"LoadBalancerID", "IpVersion", "Address"} + headers := []any{"LoadBalancerID", "IpVersion", "Address", "Status"} columns := make([][]any, len(lbtargets)) for i, lbtarget := range lbtargets { @@ -211,6 +211,7 @@ func (t defaultTableConverter) loadBalancerTargetTable(lbtargets []api.LoadBalan lbtarget.LoadBalancerTargetMeta.ID, api.NetIPAddrToProtoIPVersion(*lbtarget.Spec.TargetIP), lbtarget.Spec.TargetIP, + lbtarget.Status.String(), } } @@ -227,7 +228,9 @@ func (t defaultTableConverter) interfaceTable(ifaces []api.Interface) (*TableDat } else { headers = []any{"ID", "VNI", "Device", "IPs", "UnderlayRoute", "VirtualFunction"} } - + if len(ifaces) == 1 { + headers = append(headers, "Status") + } columns := make([][]any, len(ifaces)) for i, iface := range ifaces { if ifaces[0].Spec.VirtualFunction == nil { @@ -235,6 +238,9 @@ func (t defaultTableConverter) interfaceTable(ifaces []api.Interface) (*TableDat } else { columns[i] = []any{iface.ID, iface.Spec.VNI, iface.Spec.Device, iface.Spec.IPs, iface.Spec.UnderlayRoute, iface.Spec.VirtualFunction.String()} } + if len(ifaces) == 1 { + columns[i] = append(columns[i], iface.Status.String()) + } } return &TableData{ @@ -245,10 +251,15 @@ func (t defaultTableConverter) interfaceTable(ifaces []api.Interface) (*TableDat func (t defaultTableConverter) prefixTable(prefixes []api.Prefix) (*TableData, error) { headers := []any{"Prefix", "UnderlayRoute"} - + if len(prefixes) == 1 { + headers = append(headers, "Status") + } columns := make([][]any, len(prefixes)) for i, prefix := range prefixes { columns[i] = []any{prefix.Prefix, prefix.Spec.UnderlayRoute} + if len(prefixes) == 1 { + columns[i] = append(columns[i], prefix.Status.String()) + } } return &TableData{ @@ -258,11 +269,16 @@ func (t defaultTableConverter) prefixTable(prefixes []api.Prefix) (*TableData, e } func (t defaultTableConverter) routeTable(routes []api.Route) (*TableData, error) { - headers := []any{"Prefix", "NextHopVNI", "NextHopIP"} - + headers := []any{"Prefix", "VNI", "NextHopVNI", "NextHopIP"} + if len(routes) == 1 { + headers = append(headers, "Status") + } columns := make([][]any, len(routes)) for i, route := range routes { - columns[i] = []any{route.Prefix, route.NextHop.VNI, route.NextHop.IP} + columns[i] = []any{route.Prefix, route.VNI, route.NextHop.VNI, route.NextHop.IP} + if len(routes) == 1 { + columns[i] = append(columns[i], route.Status.String()) + } } return &TableData{ @@ -272,11 +288,11 @@ func (t defaultTableConverter) routeTable(routes []api.Route) (*TableData, error } func (t defaultTableConverter) virtualIPTable(virtualIPs []api.VirtualIP) (*TableData, error) { - headers := []any{"interfaceID", "virtualIP"} + headers := []any{"InterfaceID", "VirtualIP", "UnderlayRoute", "Status"} columns := make([][]any, len(virtualIPs)) for i, virtualIP := range virtualIPs { - columns[i] = []any{virtualIP.InterfaceID, virtualIP.IP} + columns[i] = []any{virtualIP.InterfaceID, virtualIP.IP, virtualIP.Spec.UnderlayRoute, virtualIP.Status.String()} } return &TableData{ @@ -288,21 +304,21 @@ func (t defaultTableConverter) virtualIPTable(virtualIPs []api.VirtualIP) (*Tabl func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { var headers []any if nats[0].Spec.UnderlayRoute == nil { - headers = []any{"NatIP", "minPort", "maxPort"} + headers = []any{"NatIP", "MinPort", "MaxPort", "Status"} } else if nats[0].NatMeta.InterfaceID == "" { - headers = []any{"NatIP", "minPort", "maxPort", "underlayRoute"} + headers = []any{"NatIP", "MinPort", "MaxPort", "UnderlayRoute", "Status"} } else { - headers = []any{"interfaceID", "NatIP", "minPort", "maxPort", "underlayRoute"} + headers = []any{"InterfaceID", "NatIP", "MinPort", "MaxPort", "UnderlayRoute", "Status"} } columns := make([][]any, len(nats)) for i, nat := range nats { if nats[0].Spec.UnderlayRoute == nil { - columns[i] = []any{nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort} + columns[i] = []any{nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Status.String()} } else if nats[0].NatMeta.InterfaceID == "" { - columns[i] = []any{nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute} + columns[i] = []any{nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute, nat.Status.String()} } else { - columns[i] = []any{nat.NatMeta.InterfaceID, nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute} + columns[i] = []any{nat.NatMeta.InterfaceID, nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute, nat.Status.String()} } } @@ -312,9 +328,28 @@ func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { }, nil } -func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableData, error) { - headers := []any{"interfaceID", "ruleID", "direction", "src", "dst", "action", "protocol", "priority", "status"} +func (t defaultTableConverter) neighborNatTable(nats []api.NeighborNat) (*TableData, error) { + + headers := []any{"VNI", "NatIP", "MinPort", "MaxPort", "UnderlayRoute", "Status"} + + columns := make([][]any, len(nats)) + for i, nat := range nats { + columns[i] = []any{nat.Spec.Vni, nat.NeighborNatMeta.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute, nat.Status.String()} + + } + + return &TableData{ + Headers: headers, + Columns: columns, + }, nil +} + +func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableData, error) { + headers := []any{"InterfaceID", "RuleID", "Direction", "Src", "Dst", "Action", "Protocol", "Priority"} + if len(fwrules) == 1 { + headers = append(headers, "Status") + } columns := make([][]any, len(fwrules)) for i, fwrule := range fwrules { columns[i] = []any{ @@ -326,24 +361,9 @@ func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableDa fwrule.Spec.FirewallAction, fwrule.Spec.ProtocolFilter.String(), fwrule.Spec.Priority, - fwrule.Status.String(), } - } - - return &TableData{ - Headers: headers, - Columns: columns, - }, nil -} - -func (t defaultTableConverter) serverErrorTable(serverErrors []api.ServerError) (*TableData, error) { - headers := []any{"error", "message"} - - columns := make([][]any, len(serverErrors)) - for i, serverError := range serverErrors { - columns[i] = []any{ - serverError.ServerError.Error, - serverError.ServerError.Message, + if len(fwrules) == 1 { + columns[i] = append(columns[i], fwrule.Status.String()) } } From bc524f9bb459167e832fd18e9a659f96fff76c5b Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Fri, 26 May 2023 14:44:03 +0200 Subject: [PATCH 43/65] add status and metadata to List structs --- .vscode/launch.json | 5 +- cmd/add_firewall_rule.go | 44 ++++++++++++++++- cmd/add_loadbalancer_target.go | 4 +- cmd/delete_loadbalancer_target.go | 2 +- docs/development/README.md | 1 + dpdk/api/types.go | 78 ++++++++++++++++++++----------- dpdk/client/client.go | 34 ++++++++------ renderer/renderer.go | 2 +- 8 files changed, 120 insertions(+), 50 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 108bdc8..263e243 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,7 +12,7 @@ "program": "main.go", //"program": "${fileDirname}", //"args": ["add", "loadbalancer", "--id=4", "--lbports", "tcp/389", "--vip", "10.3.4.5", "--vni", "100"] - "args": ["delete", "loadbalancer", "--id=4"] + //"args": ["delete", "loadbalancer", "--id=4"] //"args": ["get", "route", "0.0.0.0/24", "0", "fc00:2::64:0:1", "--vni", "100"] //"args": ["delete", "route", "10.200.2.0/24", "0", "ff80::1", "--vni", "100"] //"args": ["add", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] @@ -25,7 +25,8 @@ //"args": ["add", "nnat", "10.20.30.40", "--vni", "100", "--minport", "30000", "--maxport", "30100", "--underlayroute", "ff80::1"] //"args": ["add", "interface", "--id=vm4", "--ip=10.200.1.4", "--ip=2000:200:1::4", "--vni=200", "--device=net_tap5"] //"args": ["add", "-f", "/tmp/addint.json"] - //"args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=12"] + "args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=1"] + //"args": ["list", "interfaces"] } ] } \ No newline at end of file diff --git a/cmd/add_firewall_rule.go b/cmd/add_firewall_rule.go index 53084ce..c39086b 100644 --- a/cmd/add_firewall_rule.go +++ b/cmd/add_firewall_rule.go @@ -40,6 +40,24 @@ func AddFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory Render Example: "dpservice-cli add fwrule --interface-id=vm1 --action=1 --direction=1 --dst=5.5.5.0/24 --ipver=0 --priority=100 --rule-id=12 --src=1.1.1.1/32 --protocol=tcp --src-port-min=1 --src-port-max=1000 --dst-port-min=500 --dst-port-max=600", Aliases: FirewallRuleAliases, Args: cobra.ExactArgs(0), + // if protocol flag is set, require also additional flags + PreRun: func(cmd *cobra.Command, args []string) { + filter, _ := cmd.Flags().GetString("protocol") + switch filter { + case "icmp", "1": + cmd.MarkFlagRequired("icmp-type") + cmd.MarkFlagRequired("icmp-code") + case "tcp", "6", "udp", "17": + cmd.MarkFlagRequired("src-port-min") + if src, _ := cmd.Flags().GetInt32("src-port-min"); src != -1 { + cmd.MarkFlagRequired("src-port-max") + } + cmd.MarkFlagRequired("dst-port-min") + if dst, _ := cmd.Flags().GetInt32("dst-port-min"); dst != -1 { + cmd.MarkFlagRequired("dst-port-max") + } + } + }, RunE: func(cmd *cobra.Command, args []string) error { return RunAddFirewallRule( @@ -81,7 +99,7 @@ func (o *AddFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&o.RuleID, "rule-id", o.RuleID, "RuleID of FW Rule.") fs.StringVar(&o.TrafficDirection, "direction", o.TrafficDirection, "Traffic direction of FW Rule: Ingress = 0/Egress = 1") fs.StringVar(&o.FirewallAction, "action", o.FirewallAction, "Firewall action: drop/deny/0|accept/allow/1 (Can be only \"accept/allow/1\" at the moment).") - fs.Uint32Var(&o.Priority, "priority", o.Priority, "Priority of FW Rule. (For future use. No effect at the moment).") + fs.Uint32Var(&o.Priority, "priority", 1000, "Priority of FW Rule. (For future use. No effect at the moment).") fs.StringVar(&o.IpVersion, "ipver", o.IpVersion, "IpVersion of FW Rule: IPv4 = 0/IPv6 = 1.") flag.PrefixVar(fs, &o.SourcePrefix, "src", o.SourcePrefix, "Source prefix (0.0.0.0 with prefix length 0 matches all source IPs).") flag.PrefixVar(fs, &o.DestinationPrefix, "dst", o.DestinationPrefix, "Destination prefix (0.0.0.0 with prefix length 0 matches all destination IPs).") @@ -97,7 +115,7 @@ func (o *AddFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { func (o *AddFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error { // TODO if protocol is not specified it should match all protocols - for _, name := range []string{"interface-id", "rule-id", "direction", "action", "ipver", "src", "dst", "protocol"} { + for _, name := range []string{"interface-id", "rule-id", "direction", "action", "ipver", "src", "dst"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -137,6 +155,15 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory opts.DstPortLower = 1 opts.DstPortUpper = 65535 } + if opts.SrcPortLower < -1 || opts.SrcPortLower == 0 || opts.SrcPortLower > 65535 || + opts.SrcPortUpper < 1 || opts.SrcPortUpper > 65535 || + opts.DstPortLower < -1 || opts.DstPortLower == 0 || opts.DstPortLower > 65535 || + opts.DstPortUpper < 1 || opts.DstPortUpper > 65535 { + return fmt.Errorf("ports can only be -1 or <1,65535>") + } + if opts.SrcPortLower > opts.SrcPortUpper || opts.DstPortLower > opts.DstPortUpper { + return fmt.Errorf("min port must be lower or equal to max port") + } protocolFilter.Filter = &dpdkproto.ProtocolFilter_Tcp{Tcp: &dpdkproto.TCPFilter{ SrcPortLower: opts.SrcPortLower, SrcPortUpper: opts.SrcPortUpper, @@ -152,15 +179,28 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory opts.DstPortLower = 1 opts.DstPortUpper = 65535 } + if opts.SrcPortLower < -1 || opts.SrcPortLower == 0 || opts.SrcPortLower > 65535 || + opts.SrcPortUpper < 1 || opts.SrcPortUpper > 65535 || + opts.DstPortLower < -1 || opts.DstPortLower == 0 || opts.DstPortLower > 65535 || + opts.DstPortUpper < 1 || opts.DstPortUpper > 65535 { + return fmt.Errorf("ports can only be -1 or <1,65535>") + } + if opts.SrcPortLower > opts.SrcPortUpper || opts.DstPortLower > opts.DstPortUpper { + return fmt.Errorf("min port must be lower or equal to max port") + } protocolFilter.Filter = &dpdkproto.ProtocolFilter_Udp{Udp: &dpdkproto.UDPFilter{ SrcPortLower: opts.SrcPortLower, SrcPortUpper: opts.SrcPortUpper, DstPortLower: opts.DstPortLower, DstPortUpper: opts.DstPortUpper, }} + case "": default: return fmt.Errorf("protocol can be only: icmp = 1/tcp = 6/udp = 17") } + if opts.Priority > 65536 { + return fmt.Errorf("priority can be only: <0,65536") + } fwrule, err := client.AddFirewallRule(ctx, &api.FirewallRule{ TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, diff --git a/cmd/add_loadbalancer_target.go b/cmd/add_loadbalancer_target.go index 74797ff..8ff8435 100644 --- a/cmd/add_loadbalancer_target.go +++ b/cmd/add_loadbalancer_target.go @@ -93,7 +93,7 @@ func RunAddLoadBalancerTarget( lbtarget, err := client.AddLoadBalancerTarget(ctx, &api.LoadBalancerTarget{ TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, - LoadBalancerTargetMeta: api.LoadBalancerTargetMeta{ID: opts.LoadBalancerID}, + LoadBalancerTargetMeta: api.LoadBalancerTargetMeta{LoadbalancerID: opts.LoadBalancerID}, Spec: api.LoadBalancerTargetSpec{TargetIP: &opts.TargetIP}, }) if err != nil && err != errors.ErrServerError { @@ -101,6 +101,6 @@ func RunAddLoadBalancerTarget( } lbtarget.TypeMeta.Kind = api.LoadBalancerKind - lbtarget.LoadBalancerTargetMeta.ID = opts.LoadBalancerID + lbtarget.LoadBalancerTargetMeta.LoadbalancerID = opts.LoadBalancerID return rendererFactory.RenderObject("added", os.Stdout, lbtarget) } diff --git a/cmd/delete_loadbalancer_target.go b/cmd/delete_loadbalancer_target.go index 92efd81..aaf40d8 100644 --- a/cmd/delete_loadbalancer_target.go +++ b/cmd/delete_loadbalancer_target.go @@ -89,6 +89,6 @@ func RunDeleteLoadBalancerTarget(ctx context.Context, dpdkClientFactory DPDKClie } lbtarget.TypeMeta.Kind = api.LoadBalancerTargetKind - lbtarget.LoadBalancerTargetMeta.ID = opts.LoadBalancerID + lbtarget.LoadBalancerTargetMeta.LoadbalancerID = opts.LoadBalancerID return rendererFactory.RenderObject("deleted", os.Stdout, lbtarget) } diff --git a/docs/development/README.md b/docs/development/README.md index 3dfac08..9cfe975 100644 --- a/docs/development/README.md +++ b/docs/development/README.md @@ -1,6 +1,7 @@ # dpservice-cli development guide This page is intended as a general overview for all development-oriented topics. +[Cobra](https://github.com/spf13/cobra) framework is used for generating and handling commands in this project.
diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 6c8a6ce..c72f669 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -50,8 +50,14 @@ func (status *Status) String() string { } type RouteList struct { - TypeMeta `json:",inline"` - Items []Route `json:"items"` + TypeMeta `json:",inline"` + RouteListMeta `json:"metadata"` + Status Status `json:"status"` + Items []Route `json:"items"` +} + +type RouteListMeta struct { + VNI uint32 `json:"vni"` } func (l *RouteList) GetItems() []Object { @@ -92,8 +98,14 @@ type RouteNextHop struct { } type PrefixList struct { - TypeMeta `json:",inline"` - Items []Prefix `json:"items"` + TypeMeta `json:",inline"` + PrefixListMeta `json:"metadata"` + Status Status `json:"status"` + Items []Prefix `json:"items"` +} + +type PrefixListMeta struct { + InterfaceID string `json:"interfaceID"` } func (l *PrefixList) GetItems() []Object { @@ -184,19 +196,6 @@ type LBPort struct { Port uint32 `json:"port,omitempty"` } -type LoadBalancerList struct { - TypeMeta `json:",inline"` - Items []LoadBalancer `json:"items"` -} - -func (l *LoadBalancerList) GetItems() []Object { - res := make([]Object, len(l.Items)) - for i := range l.Items { - res[i] = &l.Items[i] - } - return res -} - type LoadBalancerTarget struct { TypeMeta `json:",inline"` LoadBalancerTargetMeta `json:"metadata"` @@ -205,11 +204,11 @@ type LoadBalancerTarget struct { } type LoadBalancerTargetMeta struct { - ID string `json:"id"` + LoadbalancerID string `json:"loadbalancerId"` } func (m *LoadBalancerTargetMeta) GetName() string { - return m.ID + return m.LoadbalancerID } func (m *LoadBalancerTarget) GetStatus() int32 { @@ -221,9 +220,14 @@ type LoadBalancerTargetSpec struct { } type LoadBalancerTargetList struct { - TypeMeta `json:",inline"` - Items []LoadBalancerTarget `json:"items"` - Status Status `json:"status"` + TypeMeta `json:",inline"` + LoadBalancerTargetListMeta `json:"metadata"` + Status Status `json:"status"` + Items []LoadBalancerTarget `json:"items"` +} + +type LoadBalancerTargetListMeta struct { + LoadBalancerID string `json:"loadbalancerID"` } func (l *LoadBalancerTargetList) GetItems() []Object { @@ -275,8 +279,13 @@ func (vf *VirtualFunction) String() string { } type InterfaceList struct { - TypeMeta `json:",inline"` - Items []Interface `json:"items"` + TypeMeta `json:",inline"` + InterfaceListMeta `json:"metadata"` + Status Status `json:"status"` + Items []Interface `json:"items"` +} + +type InterfaceListMeta struct { } func (l *InterfaceList) GetItems() []Object { @@ -315,8 +324,15 @@ type NatSpec struct { } type NatList struct { - TypeMeta `json:",inline"` - Items []Nat `json:"items"` + TypeMeta `json:",inline"` + NatListMeta `json:"metadata"` + Status Status `json:"status"` + Items []Nat `json:"items"` +} + +type NatListMeta struct { + NatIP netip.Addr `json:"natIp"` + NatInfoType string `json:"infoType"` } func (l *NatList) GetItems() []Object { @@ -385,8 +401,14 @@ type FirewallRuleSpec struct { } type FirewallRuleList struct { - TypeMeta `json:",inline"` - Items []FirewallRule `json:"items"` + TypeMeta `json:",inline"` + FirewallRuleListMeta `json:"metadata"` + Status Status `json:"status"` + Items []FirewallRule `json:"items"` +} + +type FirewallRuleListMeta struct { + InterfaceID string `json:"interfaceID"` } func (l *FirewallRuleList) GetItems() []Object { diff --git a/dpdk/client/client.go b/dpdk/client/client.go index e43cbfd..2026be0 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -155,8 +155,9 @@ func (c *client) ListLoadBalancerPrefixes(ctx context.Context, interfaceID strin } return &api.PrefixList{ - TypeMeta: api.TypeMeta{Kind: "LoadBalancerPrefixList"}, - Items: prefixes, + TypeMeta: api.TypeMeta{Kind: "LoadBalancerPrefixList"}, + PrefixListMeta: api.PrefixListMeta{InterfaceID: interfaceID}, + Items: prefixes, }, nil } @@ -225,14 +226,15 @@ func (c *client) GetLoadBalancerTargets(ctx context.Context, loadBalancerID stri var lbtarget api.LoadBalancerTarget lbtarget.TypeMeta.Kind = api.LoadBalancerTargetKind lbtarget.Spec.TargetIP = api.ProtoLbipToLbip(*dpdkLBtarget) - lbtarget.LoadBalancerTargetMeta.ID = loadBalancerID + lbtarget.LoadBalancerTargetMeta.LoadbalancerID = loadBalancerID lbtargets[i] = lbtarget } return &api.LoadBalancerTargetList{ - TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetListKind}, - Items: lbtargets, + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetListKind}, + LoadBalancerTargetListMeta: api.LoadBalancerTargetListMeta{LoadBalancerID: loadBalancerID}, + Items: lbtargets, // TODO server is not returning correct status //Status: api.ProtoStatusToStatus(res.Status), }, nil @@ -240,7 +242,7 @@ func (c *client) GetLoadBalancerTargets(ctx context.Context, loadBalancerID stri func (c *client) AddLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBalancerTarget) (*api.LoadBalancerTarget, error) { res, err := c.DPDKonmetalClient.AddLoadBalancerTarget(ctx, &dpdkproto.AddLoadBalancerTargetRequest{ - LoadBalancerID: []byte(lbtarget.LoadBalancerTargetMeta.ID), + LoadBalancerID: []byte(lbtarget.LoadBalancerTargetMeta.LoadbalancerID), TargetIP: api.LbipToProtoLbip(*lbtarget.Spec.TargetIP), }) if err != nil { @@ -429,8 +431,9 @@ func (c *client) ListPrefixes(ctx context.Context, interfaceID string) (*api.Pre } return &api.PrefixList{ - TypeMeta: api.TypeMeta{Kind: api.PrefixListKind}, - Items: prefixes, + TypeMeta: api.TypeMeta{Kind: api.PrefixListKind}, + PrefixListMeta: api.PrefixListMeta{InterfaceID: interfaceID}, + Items: prefixes, }, nil } @@ -553,8 +556,9 @@ func (c *client) ListRoutes(ctx context.Context, vni uint32) (*api.RouteList, er } return &api.RouteList{ - TypeMeta: api.TypeMeta{Kind: api.RouteListKind}, - Items: routes, + TypeMeta: api.TypeMeta{Kind: api.RouteListKind}, + RouteListMeta: api.RouteListMeta{VNI: vni}, + Items: routes, }, nil } @@ -690,8 +694,9 @@ func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType st nats[i] = nat } return &api.NatList{ - TypeMeta: api.TypeMeta{Kind: api.NatListKind}, - Items: nats, + TypeMeta: api.TypeMeta{Kind: api.NatListKind}, + NatListMeta: api.NatListMeta{NatIP: natVIPIP, NatInfoType: natType}, + Items: nats, }, nil } @@ -732,8 +737,9 @@ func (c *client) ListFirewallRules(ctx context.Context, interfaceID string) (*ap } return &api.FirewallRuleList{ - TypeMeta: api.TypeMeta{Kind: api.FirewallRuleListKind}, - Items: fwRules, + TypeMeta: api.TypeMeta{Kind: api.FirewallRuleListKind}, + FirewallRuleListMeta: api.FirewallRuleListMeta{InterfaceID: interfaceID}, + Items: fwRules, }, nil } diff --git a/renderer/renderer.go b/renderer/renderer.go index ba8f0c0..bb9fc00 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -208,7 +208,7 @@ func (t defaultTableConverter) loadBalancerTargetTable(lbtargets []api.LoadBalan columns := make([][]any, len(lbtargets)) for i, lbtarget := range lbtargets { columns[i] = []any{ - lbtarget.LoadBalancerTargetMeta.ID, + lbtarget.LoadBalancerTargetMeta.LoadbalancerID, api.NetIPAddrToProtoIPVersion(*lbtarget.Spec.TargetIP), lbtarget.Spec.TargetIP, lbtarget.Status.String(), From fc3688c08414214b583300abddf31d7c71f365d3 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Fri, 26 May 2023 16:25:10 +0200 Subject: [PATCH 44/65] add output options for init and initialized --- .vscode/launch.json | 4 ++-- cmd/command.go | 4 ++-- cmd/init.go | 18 ++++++++++-------- cmd/initialized.go | 16 ++++++++++++---- dpdk/api/types.go | 43 +++++++++++++++++++++++++++++++++++++++++++ dpdk/client/client.go | 14 +++++++------- renderer/renderer.go | 26 ++++++++++++++++++++++++++ 7 files changed, 102 insertions(+), 23 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 263e243..f108ba3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -16,7 +16,7 @@ //"args": ["get", "route", "0.0.0.0/24", "0", "fc00:2::64:0:1", "--vni", "100"] //"args": ["delete", "route", "10.200.2.0/24", "0", "ff80::1", "--vni", "100"] //"args": ["add", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] - //"args": ["delete", "lbtarget", "--target-ip=ff80::1", "--lb-id=1"] + "args": ["add", "lbtarget", "--target-ip=ff80::5", "--lb-id=1"] //"args": ["get", "lbtarget", "--lb-id=4"] //"args": ["get", "natinfo", "--nat-ip=10.20.30.40", "--nat-type=Local"] //"args": ["get", "fwrule", "vm1", "--rule-id=4"] @@ -25,7 +25,7 @@ //"args": ["add", "nnat", "10.20.30.40", "--vni", "100", "--minport", "30000", "--maxport", "30100", "--underlayroute", "ff80::1"] //"args": ["add", "interface", "--id=vm4", "--ip=10.200.1.4", "--ip=2000:200:1::4", "--vni=200", "--device=net_tap5"] //"args": ["add", "-f", "/tmp/addint.json"] - "args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=1"] + //"args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=1"] //"args": ["list", "interfaces"] } ] diff --git a/cmd/command.go b/cmd/command.go index 4c852c5..c1b763a 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -38,8 +38,8 @@ func Command() *cobra.Command { Get(dpdkClientOptions), List(dpdkClientOptions), Delete(dpdkClientOptions), - Initialized(dpdkClientOptions), - Init(dpdkClientOptions), + Initialized(dpdkClientOptions, rendererOptions), + Init(dpdkClientOptions, rendererOptions), completionCmd, ) diff --git a/cmd/init.go b/cmd/init.go index bc6c24a..2ccd573 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -17,7 +17,9 @@ package cmd import ( "context" "fmt" + "os" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" dpdkproto "github.com/onmetal/net-dpservice-go/proto" "github.com/spf13/cobra" @@ -25,7 +27,7 @@ import ( ) // func Init is not up to dpdk.proto spec, but is implemented to comply with current dpservice implementation -func Init(factory DPDKClientFactory) *cobra.Command { +func Init(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( opts InitOptions ) @@ -39,7 +41,8 @@ func Init(factory DPDKClientFactory) *cobra.Command { return RunInit( cmd.Context(), - factory, + dpdkClientFactory, + rendererFactory, opts, ) }, @@ -64,6 +67,7 @@ func (o *InitOptions) MarkRequiredFlags(cmd *cobra.Command) error { func RunInit( ctx context.Context, dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, opts InitOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) @@ -81,12 +85,10 @@ func RunInit( return fmt.Errorf("error dp-service already initialized, uuid: %s", uuid) } - err = client.Init(ctx, dpdkproto.InitConfig{}) - if err != nil { - return fmt.Errorf("error: %w", err) + init, err := client.Init(ctx, dpdkproto.InitConfig{}) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error init: %w", err) } - fmt.Println("{\"status\": \"initialized\"}") - - return nil + return rendererFactory.RenderObject("", os.Stdout, init) } diff --git a/cmd/initialized.go b/cmd/initialized.go index 7ea4d34..5b81bf6 100644 --- a/cmd/initialized.go +++ b/cmd/initialized.go @@ -17,11 +17,13 @@ package cmd import ( "context" "fmt" + "os" + "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/spf13/cobra" ) -func Initialized(factory DPDKClientFactory) *cobra.Command { +func Initialized(factory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { cmd := &cobra.Command{ Use: "initialized", Short: "Indicates if the DPDK app has been initialized already", @@ -32,6 +34,7 @@ func Initialized(factory DPDKClientFactory) *cobra.Command { return RunInitialized( cmd.Context(), factory, + rendererFactory, ) }, } @@ -42,6 +45,7 @@ func Initialized(factory DPDKClientFactory) *cobra.Command { func RunInitialized( ctx context.Context, dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { @@ -55,8 +59,12 @@ func RunInitialized( uuid, err := client.Initialized(ctx) if err != nil { - return fmt.Errorf("error: %w", err) + return fmt.Errorf("error initialized: %w", err) } - fmt.Printf("{\"status\": \"initialized\", \"uuid\": \"%s\"}\n", uuid) - return nil + + initialized := api.Initialized{ + TypeMeta: api.TypeMeta{Kind: "Initialized"}, + Spec: api.InitializedSpec{UUID: uuid}, + } + return rendererFactory.RenderObject("", os.Stdout, &initialized) } diff --git a/dpdk/api/types.go b/dpdk/api/types.go index c72f669..991ce50 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -419,6 +419,49 @@ func (l *FirewallRuleList) GetItems() []Object { return res } +type Init struct { + TypeMeta `json:",inline"` + InitMeta `json:"metadata"` + Spec InitSpec `json:"spec"` + Status Status `json:"status"` +} + +type InitMeta struct { +} + +type InitSpec struct { +} + +func (m *InitMeta) GetName() string { + return "init" +} + +func (m *Init) GetStatus() int32 { + return m.Status.Error +} + +type Initialized struct { + TypeMeta `json:",inline"` + InitializedMeta `json:"metadata"` + Spec InitializedSpec `json:"spec"` + Status Status `json:"status"` +} + +type InitializedMeta struct { +} + +type InitializedSpec struct { + UUID string `json:"uuid"` +} + +func (m *InitializedMeta) GetName() string { + return "initialized" +} + +func (m *Initialized) GetStatus() int32 { + return 0 +} + var ( InterfaceKind = reflect.TypeOf(Interface{}).Name() InterfaceListKind = reflect.TypeOf(InterfaceList{}).Name() diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 2026be0..1999fe9 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -70,7 +70,7 @@ type Client interface { DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) Initialized(ctx context.Context) (string, error) - Init(ctx context.Context, initConfig dpdkproto.InitConfig) error + Init(ctx context.Context, initConfig dpdkproto.InitConfig) (*api.Init, error) } type client struct { @@ -246,10 +246,10 @@ func (c *client) AddLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBa TargetIP: api.LbipToProtoLbip(*lbtarget.Spec.TargetIP), }) if err != nil { - return nil, err + return &api.LoadBalancerTarget{Status: api.ProtoStatusToStatus(res)}, err } if errorCode := res.GetError(); errorCode != 0 { - return nil, apierrors.NewStatusError(errorCode, res.GetMessage()) + return &api.LoadBalancerTarget{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } return &api.LoadBalancerTarget{ @@ -855,13 +855,13 @@ func (c *client) Initialized(ctx context.Context) (string, error) { return res.Uuid, nil } -func (c *client) Init(ctx context.Context, initConfig dpdkproto.InitConfig) error { +func (c *client) Init(ctx context.Context, initConfig dpdkproto.InitConfig) (*api.Init, error) { res, err := c.DPDKonmetalClient.Init(ctx, &initConfig) if err != nil { - return err + return &api.Init{Status: api.ProtoStatusToStatus(res)}, err } if errorCode := res.GetError(); errorCode != 0 { - return apierrors.NewStatusError(errorCode, res.GetMessage()) + return &api.Init{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } - return nil + return &api.Init{TypeMeta: api.TypeMeta{Kind: "Init"}, Status: api.ProtoStatusToStatus(res)}, nil } diff --git a/renderer/renderer.go b/renderer/renderer.go index bb9fc00..ff61629 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -179,6 +179,10 @@ func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) { return t.fwruleTable([]api.FirewallRule{*obj}) case *api.FirewallRuleList: return t.fwruleTable(obj.Items) + case *api.Init: + return t.initTable(*obj) + case *api.Initialized: + return t.initializedTable(*obj) default: return nil, fmt.Errorf("unsupported type %T", v) } @@ -373,6 +377,28 @@ func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableDa }, nil } +func (t defaultTableConverter) initTable(init api.Init) (*TableData, error) { + headers := []any{"Error", "Message"} + columns := make([][]any, 1) + columns[0] = []any{init.Status.Error, init.Status.Message} + + return &TableData{ + Headers: headers, + Columns: columns, + }, nil +} + +func (t defaultTableConverter) initializedTable(initialized api.Initialized) (*TableData, error) { + headers := []any{"UUID", "Error", "Message"} + columns := make([][]any, 1) + columns[0] = []any{initialized.Spec.UUID, initialized.Status.Error, initialized.Status.Message} + + return &TableData{ + Headers: headers, + Columns: columns, + }, nil +} + var ( lightBoxStyle = table.BoxStyle{ BottomLeft: "", From 6a14c025eb59d1d801886bed85fd152d463e9f01 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Mon, 29 May 2023 16:18:36 +0200 Subject: [PATCH 45/65] add isVniInUse func and fixes --- .vscode/launch.json | 7 +-- cmd/add_firewall_rule.go | 16 ------ cmd/create_interface.go | 15 ++++-- cmd/get.go | 2 +- cmd/get_loadbalancer_target.go | 20 +++---- cmd/get_vni.go | 98 ++++++++++++++++++++++++++++++++++ cmd/list.go | 2 + cmd/list_nats.go | 91 +++++++++++++++++++++++++++++++ dpdk/api/conversion.go | 10 ++-- dpdk/api/register.go | 1 + dpdk/api/types.go | 37 +++++++++++-- dpdk/client/client.go | 84 ++++++++++++++++++----------- go.mod | 2 +- go.sum | 4 +- 14 files changed, 313 insertions(+), 76 deletions(-) create mode 100644 cmd/get_vni.go create mode 100644 cmd/list_nats.go diff --git a/.vscode/launch.json b/.vscode/launch.json index f108ba3..0dc7561 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -16,7 +16,7 @@ //"args": ["get", "route", "0.0.0.0/24", "0", "fc00:2::64:0:1", "--vni", "100"] //"args": ["delete", "route", "10.200.2.0/24", "0", "ff80::1", "--vni", "100"] //"args": ["add", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] - "args": ["add", "lbtarget", "--target-ip=ff80::5", "--lb-id=1"] + //"args": ["add", "lbtarget", "--target-ip=ff80::5", "--lb-id=1"] //"args": ["get", "lbtarget", "--lb-id=4"] //"args": ["get", "natinfo", "--nat-ip=10.20.30.40", "--nat-type=Local"] //"args": ["get", "fwrule", "vm1", "--rule-id=4"] @@ -25,8 +25,9 @@ //"args": ["add", "nnat", "10.20.30.40", "--vni", "100", "--minport", "30000", "--maxport", "30100", "--underlayroute", "ff80::1"] //"args": ["add", "interface", "--id=vm4", "--ip=10.200.1.4", "--ip=2000:200:1::4", "--vni=200", "--device=net_tap5"] //"args": ["add", "-f", "/tmp/addint.json"] - //"args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=1"] - //"args": ["list", "interfaces"] + //"args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=11"] + //"args": ["list", "nats"] + "args": ["get", "vni", "--vni=100", "--vni-type=0"] } ] } \ No newline at end of file diff --git a/cmd/add_firewall_rule.go b/cmd/add_firewall_rule.go index c39086b..e1ff149 100644 --- a/cmd/add_firewall_rule.go +++ b/cmd/add_firewall_rule.go @@ -147,14 +147,6 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory IcmpType: opts.IcmpType, IcmpCode: opts.IcmpCode}} case "tcp", "6": - if opts.SrcPortLower == -1 { - opts.SrcPortLower = 1 - opts.SrcPortUpper = 65535 - } - if opts.DstPortLower == -1 { - opts.DstPortLower = 1 - opts.DstPortUpper = 65535 - } if opts.SrcPortLower < -1 || opts.SrcPortLower == 0 || opts.SrcPortLower > 65535 || opts.SrcPortUpper < 1 || opts.SrcPortUpper > 65535 || opts.DstPortLower < -1 || opts.DstPortLower == 0 || opts.DstPortLower > 65535 || @@ -171,14 +163,6 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory DstPortUpper: opts.DstPortUpper, }} case "udp", "17": - if opts.SrcPortLower == -1 { - opts.SrcPortLower = 1 - opts.SrcPortUpper = 65535 - } - if opts.DstPortLower == -1 { - opts.DstPortLower = 1 - opts.DstPortUpper = 65535 - } if opts.SrcPortLower < -1 || opts.SrcPortLower == 0 || opts.SrcPortLower > 65535 || opts.SrcPortUpper < 1 || opts.SrcPortUpper > 65535 || opts.DstPortLower < -1 || opts.DstPortLower == 0 || opts.DstPortLower > 65535 || diff --git a/cmd/create_interface.go b/cmd/create_interface.go index a6367f5..5e08478 100644 --- a/cmd/create_interface.go +++ b/cmd/create_interface.go @@ -57,10 +57,12 @@ func CreateInterface(dpdkClientFactory DPDKClientFactory, rendererFactory Render } type CreateInterfaceOptions struct { - ID string - VNI uint32 - IP []netip.Addr - Device string + ID string + VNI uint32 + IP []netip.Addr + Device string + PxeServer string + PxeFileName string } func (o *CreateInterfaceOptions) AddFlags(fs *pflag.FlagSet) { @@ -68,6 +70,8 @@ func (o *CreateInterfaceOptions) AddFlags(fs *pflag.FlagSet) { fs.Uint32Var(&o.VNI, "vni", o.VNI, "VNI to add the interface to.") flag.AddrSliceVar(fs, &o.IP, "ip", o.IP, "IP to assign to the interface.") fs.StringVar(&o.Device, "device", o.Device, "Device to allocate.") + fs.StringVar(&o.PxeServer, "pxe-server", o.PxeServer, "PXE next server.") + fs.StringVar(&o.PxeFileName, "pxe-file-name", o.PxeFileName, "PXE boot file name.") } func (o *CreateInterfaceOptions) MarkRequiredFlags(cmd *cobra.Command) error { @@ -88,7 +92,8 @@ func RunCreateInterface(ctx context.Context, dpdkClientFactory DPDKClientFactory iface, err := client.CreateInterface(ctx, &api.Interface{ InterfaceMeta: api.InterfaceMeta{ - ID: opts.ID, + ID: opts.ID, + PXE: api.PXE{Server: opts.PxeServer, FileName: opts.PxeFileName}, }, Spec: api.InterfaceSpec{ VNI: opts.VNI, diff --git a/cmd/get.go b/cmd/get.go index 9c42387..5c881e7 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -35,10 +35,10 @@ func Get(factory DPDKClientFactory) *cobra.Command { GetInterface(factory, rendererOptions), GetVirtualIP(factory, rendererOptions), GetLoadBalancer(factory, rendererOptions), - GetLoadBalancerTargets(factory, rendererOptions), GetNat(factory, rendererOptions), GetNatInfo(factory, rendererOptions), GetFirewallRule(factory, rendererOptions), + GetVni(factory, rendererOptions), } cmd.Short = fmt.Sprintf("Gets one of %v", CommandNames(subcommands)) diff --git a/cmd/get_loadbalancer_target.go b/cmd/get_loadbalancer_target.go index aa32ddd..3343c56 100644 --- a/cmd/get_loadbalancer_target.go +++ b/cmd/get_loadbalancer_target.go @@ -26,20 +26,20 @@ import ( "github.com/spf13/pflag" ) -func GetLoadBalancerTargets(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { +func ListLoadBalancerTargets(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { var ( - opts GetLoadBalancerTargetOptions + opts ListLoadBalancerTargetOptions ) cmd := &cobra.Command{ Use: "lbtarget <--lb-id>", - Short: "Get LoadBalancer Targets", - Example: "dpservice-cli get lbtarget --lb-id=1", + Short: "List LoadBalancer Targets", + Example: "dpservice-cli list lbtarget --lb-id=1", Aliases: LoadBalancerTargetAliases, Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - return RunGetLoadBalancerTargets( + return RunListLoadBalancerTargets( cmd.Context(), dpdkClientFactory, rendererFactory, @@ -55,15 +55,15 @@ func GetLoadBalancerTargets(dpdkClientFactory DPDKClientFactory, rendererFactory return cmd } -type GetLoadBalancerTargetOptions struct { +type ListLoadBalancerTargetOptions struct { LoadBalancerID string } -func (o *GetLoadBalancerTargetOptions) AddFlags(fs *pflag.FlagSet) { +func (o *ListLoadBalancerTargetOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&o.LoadBalancerID, "lb-id", o.LoadBalancerID, "ID of the loadbalancer to get the targets for.") } -func (o *GetLoadBalancerTargetOptions) MarkRequiredFlags(cmd *cobra.Command) error { +func (o *ListLoadBalancerTargetOptions) MarkRequiredFlags(cmd *cobra.Command) error { for _, name := range []string{"lb-id"} { if err := cmd.MarkFlagRequired(name); err != nil { return err @@ -72,11 +72,11 @@ func (o *GetLoadBalancerTargetOptions) MarkRequiredFlags(cmd *cobra.Command) err return nil } -func RunGetLoadBalancerTargets( +func RunListLoadBalancerTargets( ctx context.Context, dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory, - opts GetLoadBalancerTargetOptions, + opts ListLoadBalancerTargetOptions, ) error { client, cleanup, err := dpdkClientFactory.NewClient(ctx) if err != nil { diff --git a/cmd/get_vni.go b/cmd/get_vni.go new file mode 100644 index 0000000..5ccb1b0 --- /dev/null +++ b/cmd/get_vni.go @@ -0,0 +1,98 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" + "github.com/onmetal/dpservice-cli/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func GetVni(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + var ( + opts GetVniOptions + ) + + cmd := &cobra.Command{ + Use: "vni <--vni> <--vni-type>", + Short: "Get vni usage information", + Example: "dpservice-cli get vni --vni=vm1 --vni-type=0", + Aliases: NatAliases, + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + + return RunGetVni( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type GetVniOptions struct { + VNI uint32 + VniType uint8 +} + +func (o *GetVniOptions) AddFlags(fs *pflag.FlagSet) { + fs.Uint32Var(&o.VNI, "vni", o.VNI, "VNI to check.") + fs.Uint8Var(&o.VniType, "vni-type", o.VniType, "VNI Type.") +} + +func (o *GetVniOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"vni", "vni-type"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunGetVni( + ctx context.Context, + dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, + opts GetVniOptions, +) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating dpdk client: %w", err) + } + defer DpdkClose(cleanup) + + vni, err := client.GetVni(ctx, opts.VNI, opts.VniType) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error getting vni: %w", err) + } + + vni.TypeMeta.Kind = api.VniKind + vni.VniMeta.VNI = opts.VNI + vni.VniMeta.VniType = opts.VniType + return rendererFactory.RenderObject("", os.Stdout, vni) +} diff --git a/cmd/list.go b/cmd/list.go index dc6f189..6ce49eb 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -37,6 +37,8 @@ func List(factory DPDKClientFactory) *cobra.Command { ListPrefixes(factory, rendererOptions), ListLoadBalancerPrefixes(factory, rendererOptions), ListRoutes(factory, rendererOptions), + ListLoadBalancerTargets(factory, rendererOptions), + ListNats(factory, rendererOptions), } cmd.Short = fmt.Sprintf("Lists one of %v", CommandNames(subcommands)) diff --git a/cmd/list_nats.go b/cmd/list_nats.go new file mode 100644 index 0000000..1ea6f83 --- /dev/null +++ b/cmd/list_nats.go @@ -0,0 +1,91 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func ListNats(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + cmd := &cobra.Command{ + Use: "nats", + Short: "List all nats", + Example: "dpservice-cli list nats", + Aliases: InterfaceAliases, + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + return RunListNats( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + ) + }, + } + + return cmd +} + +type ListNatsOptions struct { +} + +func (o *ListNatsOptions) AddFlags(fs *pflag.FlagSet) { +} + +func (o *ListNatsOptions) MarkRequiredFlags(cmd *cobra.Command) error { + return nil +} + +func RunListNats( + ctx context.Context, + dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, +) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error getting dpdk client: %w", err) + } + defer DpdkClose(cleanup) + + interfaceList, err := client.ListInterfaces(ctx) + if err != nil { + return fmt.Errorf("error listing interfaces: %w", err) + } + interfaces := interfaceList.Items + nats := make([]api.Nat, len(interfaceList.Items)) + for i, iface := range interfaces { + nat, err := client.GetNat(ctx, iface.InterfaceMeta.ID) + if err == errors.ErrServerError { + nat.InterfaceID = iface.InterfaceMeta.ID + } + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error getting nat: %w", err) + } + nats[i] = *nat + } + + natList := api.NatList{ + TypeMeta: api.TypeMeta{Kind: api.NatListKind}, + Items: nats, + } + + return rendererFactory.RenderList("", os.Stdout, &natList) +} diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index a62d5bb..49041a7 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -208,7 +208,7 @@ func ProtoPrefixToPrefix(interfaceID string, dpdkPrefix *proto.Prefix) (*Prefix, return nil, fmt.Errorf("invalid dpdk prefix length %d for address %s", dpdkPrefix.PrefixLength, addr) } - underlayRoute, err := netip.ParseAddr(string(dpdkPrefix.Address)) + underlayRoute, err := netip.ParseAddr(string(dpdkPrefix.UnderlayRoute)) if err != nil { return nil, fmt.Errorf("error parsing underlay route: %w", err) } @@ -303,12 +303,12 @@ func ProtoNatToNat(dpdkNat *proto.GetNATResponse, interfaceID string) (*Nat, err func ProtoFwRuleToFwRule(dpdkFwRule *proto.FirewallRule, interfaceID string) (*FirewallRule, error) { - srcPrefix, err := ProtoPrefixToPrefix(interfaceID, dpdkFwRule.SourcePrefix) + srcPrefix, err := netip.ParsePrefix(string(dpdkFwRule.SourcePrefix.Address) + "/" + strconv.Itoa(int(dpdkFwRule.SourcePrefix.PrefixLength))) if err != nil { return nil, fmt.Errorf("error converting prefix: %w", err) } - dstPrefix, err := ProtoPrefixToPrefix(interfaceID, dpdkFwRule.DestinationPrefix) + dstPrefix, err := netip.ParsePrefix(string(dpdkFwRule.DestinationPrefix.Address) + "/" + strconv.Itoa(int(dpdkFwRule.DestinationPrefix.PrefixLength))) if err != nil { return nil, fmt.Errorf("error converting prefix: %w", err) } @@ -340,8 +340,8 @@ func ProtoFwRuleToFwRule(dpdkFwRule *proto.FirewallRule, interfaceID string) (*F FirewallAction: action, Priority: dpdkFwRule.Priority, IpVersion: ipv, - SourcePrefix: &srcPrefix.Prefix, - DestinationPrefix: &dstPrefix.Prefix, + SourcePrefix: &srcPrefix, + DestinationPrefix: &dstPrefix, ProtocolFilter: dpdkFwRule.ProtocolFilter, }, }, nil diff --git a/dpdk/api/register.go b/dpdk/api/register.go index 926f1eb..4a04ca4 100644 --- a/dpdk/api/register.go +++ b/dpdk/api/register.go @@ -36,6 +36,7 @@ func init() { &NatList{}, &NeighborNat{}, &FirewallRule{}, + &Vni{}, ); err != nil { panic(err) } diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 991ce50..75b0911 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -247,7 +247,13 @@ type Interface struct { } type InterfaceMeta struct { - ID string `json:"id"` + ID string `json:"id"` + PXE PXE `json:"pxe"` +} + +type PXE struct { + Server string `json:"server"` + FileName string `json:"fileName"` } func (m *InterfaceMeta) GetName() string { @@ -331,8 +337,8 @@ type NatList struct { } type NatListMeta struct { - NatIP netip.Addr `json:"natIp"` - NatInfoType string `json:"infoType"` + NatIP *netip.Addr `json:"natIp,omitempty"` + NatInfoType string `json:"infoType,omitempty"` } func (l *NatList) GetItems() []Object { @@ -462,6 +468,30 @@ func (m *Initialized) GetStatus() int32 { return 0 } +type Vni struct { + TypeMeta `json:",inline"` + VniMeta `json:"metadata"` + Spec VniSpec `json:"spec"` + Status Status `json:"status"` +} + +type VniMeta struct { + VNI uint32 `json:"vni"` + VniType uint8 `json:"vniType"` +} + +type VniSpec struct { + InUse bool `json:"inUse"` +} + +func (m *VniMeta) GetName() string { + return "vni" +} + +func (m *Vni) GetStatus() int32 { + return 0 +} + var ( InterfaceKind = reflect.TypeOf(Interface{}).Name() InterfaceListKind = reflect.TypeOf(InterfaceList{}).Name() @@ -478,4 +508,5 @@ var ( NeighborNatKind = reflect.TypeOf(NeighborNat{}).Name() FirewallRuleKind = reflect.TypeOf(FirewallRule{}).Name() FirewallRuleListKind = reflect.TypeOf(FirewallRuleList{}).Name() + VniKind = reflect.TypeOf(Vni{}).Name() ) diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 1999fe9..34b9594 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -71,6 +71,7 @@ type Client interface { Initialized(ctx context.Context) (string, error) Init(ctx context.Context, initConfig dpdkproto.InitConfig) (*api.Init, error) + GetVni(ctx context.Context, vni uint32, vniType uint8) (*api.Vni, error) } type client struct { @@ -84,7 +85,7 @@ func NewClient(protoClient dpdkproto.DPDKonmetalClient) Client { func (c *client) GetLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) { res, err := c.DPDKonmetalClient.GetLoadBalancer(ctx, &dpdkproto.GetLoadBalancerRequest{LoadBalancerID: []byte(id)}) if err != nil { - return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res.Status)}, err + return &api.LoadBalancer{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError @@ -105,7 +106,7 @@ func (c *client) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) ( Lbports: lbPorts, }) if err != nil { - return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res.Status)}, err + return &api.LoadBalancer{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError @@ -128,7 +129,7 @@ func (c *client) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) ( func (c *client) DeleteLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) { res, err := c.DPDKonmetalClient.DeleteLoadBalancer(ctx, &dpdkproto.DeleteLoadBalancerRequest{LoadBalancerID: []byte(id)}) if err != nil { - return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res)}, err + return &api.LoadBalancer{}, err } if errorCode := res.GetError(); errorCode != 0 { return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError @@ -173,7 +174,7 @@ func (c *client) CreateLoadBalancerPrefix(ctx context.Context, prefix *api.Prefi }, }) if err != nil { - return &api.Prefix{Status: api.ProtoStatusToStatus(res.Status)}, err + return &api.Prefix{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { return &api.Prefix{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError @@ -202,7 +203,7 @@ func (c *client) DeleteLoadBalancerPrefix(ctx context.Context, interfaceID strin }, }) if err != nil { - return &api.Prefix{Status: api.ProtoStatusToStatus(res)}, err + return &api.Prefix{}, err } if errorCode := res.GetError(); errorCode != 0 { return &api.Prefix{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError @@ -215,7 +216,7 @@ func (c *client) GetLoadBalancerTargets(ctx context.Context, loadBalancerID stri LoadBalancerID: []byte(loadBalancerID), }) if err != nil { - return &api.LoadBalancerTargetList{Status: api.ProtoStatusToStatus(res.Status)}, err + return &api.LoadBalancerTargetList{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { return &api.LoadBalancerTargetList{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError @@ -246,7 +247,7 @@ func (c *client) AddLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBa TargetIP: api.LbipToProtoLbip(*lbtarget.Spec.TargetIP), }) if err != nil { - return &api.LoadBalancerTarget{Status: api.ProtoStatusToStatus(res)}, err + return &api.LoadBalancerTarget{}, err } if errorCode := res.GetError(); errorCode != 0 { return &api.LoadBalancerTarget{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError @@ -266,7 +267,7 @@ func (c *client) DeleteLoadBalancerTarget(ctx context.Context, id string, target TargetIP: api.LbipToProtoLbip(targetIP), }) if err != nil { - return &api.LoadBalancerTarget{Status: api.ProtoStatusToStatus(res)}, err + return &api.LoadBalancerTarget{}, err } if errorCode := res.GetError(); errorCode != 0 { return &api.LoadBalancerTarget{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError @@ -277,7 +278,7 @@ func (c *client) DeleteLoadBalancerTarget(ctx context.Context, id string, target func (c *client) GetInterface(ctx context.Context, name string) (*api.Interface, error) { res, err := c.DPDKonmetalClient.GetInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(name)}) if err != nil { - return &api.Interface{Status: api.ProtoStatusToStatus(res.Status)}, err + return &api.Interface{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { return &api.Interface{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError @@ -308,16 +309,20 @@ func (c *client) ListInterfaces(ctx context.Context) (*api.InterfaceList, error) } func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*api.Interface, error) { - res, err := c.DPDKonmetalClient.CreateInterface(ctx, &dpdkproto.CreateInterfaceRequest{ + req := dpdkproto.CreateInterfaceRequest{ InterfaceType: dpdkproto.InterfaceType_VirtualInterface, InterfaceID: []byte(iface.ID), Vni: iface.Spec.VNI, Ipv4Config: api.NetIPAddrToProtoIPConfig(netiputil.FindIPv4(iface.Spec.IPs)), Ipv6Config: api.NetIPAddrToProtoIPConfig(netiputil.FindIPv6(iface.Spec.IPs)), DeviceName: iface.Spec.Device, - }) + } + req.Ipv4Config.PxeConfig = &dpdkproto.PXEConfig{NextServer: iface.PXE.Server, BootFileName: iface.PXE.FileName} + req.Ipv6Config.PxeConfig = &dpdkproto.PXEConfig{NextServer: iface.PXE.Server, BootFileName: iface.PXE.FileName} + + res, err := c.DPDKonmetalClient.CreateInterface(ctx, &req) if err != nil { - return &api.Interface{Status: api.ProtoStatusToStatus(res.Response.Status)}, err + return &api.Interface{}, err } if errorCode := res.GetResponse().GetStatus().GetError(); errorCode != 0 { return &api.Interface{Status: api.ProtoStatusToStatus(res.Response.Status)}, apierrors.ErrServerError @@ -351,7 +356,7 @@ func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*ap func (c *client) DeleteInterface(ctx context.Context, name string) (*api.Interface, error) { res, err := c.DPDKonmetalClient.DeleteInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(name)}) if err != nil { - return &api.Interface{Status: api.ProtoStatusToStatus(res)}, err + return &api.Interface{}, err } if errorCode := res.GetError(); errorCode != 0 { return &api.Interface{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError @@ -364,7 +369,7 @@ func (c *client) GetVirtualIP(ctx context.Context, interfaceName string) (*api.V InterfaceID: []byte(interfaceName), }) if err != nil { - return &api.VirtualIP{Status: api.ProtoStatusToStatus(res.Status)}, err + return &api.VirtualIP{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { return &api.VirtualIP{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError @@ -381,7 +386,7 @@ func (c *client) AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*a }, }) if err != nil { - return &api.VirtualIP{Status: api.ProtoStatusToStatus(res.Status)}, err + return &api.VirtualIP{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { return &api.VirtualIP{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError @@ -404,7 +409,7 @@ func (c *client) DeleteVirtualIP(ctx context.Context, interfaceID string) (*api. InterfaceID: []byte(interfaceID), }) if err != nil { - return &api.VirtualIP{Status: api.ProtoStatusToStatus(res)}, err + return &api.VirtualIP{}, err } if errorCode := res.GetError(); errorCode != 0 { return &api.VirtualIP{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError @@ -449,7 +454,7 @@ func (c *client) AddPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix }, }) if err != nil { - return &api.Prefix{Status: api.ProtoStatusToStatus(res.Status)}, err + return &api.Prefix{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { return &api.Prefix{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError @@ -478,7 +483,7 @@ func (c *client) DeletePrefix(ctx context.Context, interfaceID string, prefix ne }, }) if err != nil { - return &api.Prefix{Status: api.ProtoStatusToStatus(res)}, err + return &api.Prefix{}, err } if errorCode := res.GetError(); errorCode != 0 { return &api.Prefix{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError @@ -502,7 +507,7 @@ func (c *client) AddRoute(ctx context.Context, route *api.Route) (*api.Route, er }, }) if err != nil { - return &api.Route{Status: api.ProtoStatusToStatus(res)}, err + return &api.Route{}, err } if errorCode := res.GetError(); errorCode != 0 { return &api.Route{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError @@ -529,7 +534,7 @@ func (c *client) DeleteRoute(ctx context.Context, vni uint32, prefix netip.Prefi }, }) if err != nil { - return &api.Route{Status: api.ProtoStatusToStatus(res)}, err + return &api.Route{}, err } if errorCode := res.GetError(); errorCode != 0 { return &api.Route{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError @@ -565,7 +570,7 @@ func (c *client) ListRoutes(ctx context.Context, vni uint32) (*api.RouteList, er func (c *client) GetNat(ctx context.Context, interfaceID string) (*api.Nat, error) { res, err := c.DPDKonmetalClient.GetNAT(ctx, &dpdkproto.GetNATRequest{InterfaceID: []byte(interfaceID)}) if err != nil { - return &api.Nat{Status: api.ProtoStatusToStatus(res.Status)}, err + return &api.Nat{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { return &api.Nat{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError @@ -584,7 +589,7 @@ func (c *client) AddNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) { MaxPort: nat.Spec.MaxPort, }) if err != nil { - return &api.Nat{Status: api.ProtoStatusToStatus(res.Status)}, err + return &api.Nat{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { return &api.Nat{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError @@ -610,7 +615,7 @@ func (c *client) DeleteNat(ctx context.Context, interfaceID string) (*api.Nat, e InterfaceID: []byte(interfaceID), }) if err != nil { - return &api.Nat{Status: api.ProtoStatusToStatus(res)}, err + return &api.Nat{}, err } if errorCode := res.GetError(); errorCode != 0 { return &api.Nat{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError @@ -631,7 +636,7 @@ func (c *client) AddNeighborNat(ctx context.Context, nNat *api.NeighborNat) (*ap UnderlayRoute: []byte(nNat.Spec.UnderlayRoute.String()), }) if err != nil { - return &api.NeighborNat{Status: api.ProtoStatusToStatus(res)}, err + return &api.NeighborNat{}, err } if errorCode := res.GetError(); errorCode != 0 { return &api.NeighborNat{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError @@ -695,7 +700,7 @@ func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType st } return &api.NatList{ TypeMeta: api.TypeMeta{Kind: api.NatListKind}, - NatListMeta: api.NatListMeta{NatIP: natVIPIP, NatInfoType: natType}, + NatListMeta: api.NatListMeta{NatIP: &natVIPIP, NatInfoType: natType}, Items: nats, }, nil } @@ -711,7 +716,7 @@ func (c *client) DeleteNeighborNat(ctx context.Context, neigbhorNat api.Neighbor MaxPort: neigbhorNat.Spec.MaxPort, }) if err != nil { - return &api.NeighborNat{Status: api.ProtoStatusToStatus(res)}, err + return &api.NeighborNat{}, err } if errorCode := res.GetError(); errorCode != 0 { return &api.NeighborNat{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError @@ -801,7 +806,7 @@ func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) }, }) if err != nil { - return &api.FirewallRule{Status: api.ProtoStatusToStatus(res.Status)}, err + return &api.FirewallRule{}, err } if res.Status.Error != 0 { return &api.FirewallRule{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError @@ -824,7 +829,7 @@ func (c *client) GetFirewallRule(ctx context.Context, ruleID string, interfaceID RuleID: []byte(ruleID), }) if err != nil { - return &api.FirewallRule{Status: api.ProtoStatusToStatus(res.Status)}, err + return &api.FirewallRule{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { return &api.FirewallRule{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError @@ -839,7 +844,7 @@ func (c *client) DeleteFirewallRule(ctx context.Context, interfaceID string, rul RuleID: []byte(ruleID), }) if err != nil { - return &api.FirewallRule{Status: api.ProtoStatusToStatus(res)}, err + return &api.FirewallRule{}, err } if errorCode := res.GetError(); errorCode != 0 { return &api.FirewallRule{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError @@ -858,10 +863,29 @@ func (c *client) Initialized(ctx context.Context) (string, error) { func (c *client) Init(ctx context.Context, initConfig dpdkproto.InitConfig) (*api.Init, error) { res, err := c.DPDKonmetalClient.Init(ctx, &initConfig) if err != nil { - return &api.Init{Status: api.ProtoStatusToStatus(res)}, err + return &api.Init{}, err } if errorCode := res.GetError(); errorCode != 0 { return &api.Init{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } return &api.Init{TypeMeta: api.TypeMeta{Kind: "Init"}, Status: api.ProtoStatusToStatus(res)}, nil } + +func (c *client) GetVni(ctx context.Context, vni uint32, vniType uint8) (*api.Vni, error) { + res, err := c.DPDKonmetalClient.IsVniInUse(ctx, &dpdkproto.IsVniInUseRequest{ + Vni: vni, + Type: dpdkproto.VniType(vniType), + }) + if err != nil { + return &api.Vni{}, err + } + if errorCode := res.GetStatus().GetError(); errorCode != 0 { + return &api.Vni{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError + } + + return &api.Vni{TypeMeta: api.TypeMeta{Kind: api.VniKind}, + VniMeta: api.VniMeta{VNI: vni, VniType: vniType}, + Spec: api.VniSpec{InUse: res.InUse}, + Status: api.ProtoStatusToStatus(res.Status), + }, nil +} diff --git a/go.mod b/go.mod index 0605b77..bbb8d3a 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/ghodss/yaml v1.0.0 github.com/google/addlicense v1.1.1 github.com/jedib0t/go-pretty/v6 v6.3.9 - github.com/onmetal/net-dpservice-go v0.1.10 + github.com/onmetal/net-dpservice-go v0.1.11 github.com/onsi/ginkgo/v2 v2.2.0 github.com/onsi/gomega v1.20.2 github.com/spf13/cobra v1.4.0 diff --git a/go.sum b/go.sum index 3bf249b..43d1349 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/onmetal/net-dpservice-go v0.1.10 h1:yzVMj+jylw7UGhJhLCP46gxNBn2GOfwunlxe0t/7/P0= -github.com/onmetal/net-dpservice-go v0.1.10/go.mod h1:dbItLY1P+nYl7/c04JdJTEaMCG6GLXgAZsMP/OuZBmg= +github.com/onmetal/net-dpservice-go v0.1.11 h1:1YGfARGVkwJ6WG+6KcYla6cggYqif4yIoHtJS8jOysQ= +github.com/onmetal/net-dpservice-go v0.1.11/go.mod h1:dbItLY1P+nYl7/c04JdJTEaMCG6GLXgAZsMP/OuZBmg= github.com/onsi/ginkgo/v2 v2.2.0 h1:3ZNA3L1c5FYDFTTxbFeVGGD8jYvjYauHD30YgLxVsNI= github.com/onsi/ginkgo/v2 v2.2.0/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk= github.com/onsi/gomega v1.20.2 h1:8uQq0zMgLEfa0vRrrBgaJF2gyW9Da9BmfGV+OyUzfkY= From f7b9bb2a9e273c39fce1f23db6b3584afe55fd96 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 30 May 2023 09:24:08 +0200 Subject: [PATCH 46/65] update command tree md files in docs --- cmd/create_interface.go | 2 +- docs/README.md | 16 +++++++-- docs/commands/dpservice-cli.md | 8 +++-- docs/commands/dpservice-cli_add.md | 6 ++-- .../dpservice-cli_add_firewallrule.md | 32 ++++++++--------- docs/commands/dpservice-cli_add_interface.md | 16 +++++---- docs/commands/dpservice-cli_add_lbprefix.md | 4 +-- docs/commands/dpservice-cli_add_lbtarget.md | 4 +-- .../dpservice-cli_add_loadbalancer.md | 6 ++-- docs/commands/dpservice-cli_add_nat.md | 4 +-- .../commands/dpservice-cli_add_neighbornat.md | 4 +-- docs/commands/dpservice-cli_add_prefix.md | 4 +-- docs/commands/dpservice-cli_add_route.md | 4 +-- docs/commands/dpservice-cli_add_virtualip.md | 4 +-- docs/commands/dpservice-cli_completion.md | 4 ++- docs/commands/dpservice-cli_delete.md | 4 ++- .../dpservice-cli_delete_firewallrule.md | 4 ++- .../dpservice-cli_delete_interface.md | 4 ++- .../commands/dpservice-cli_delete_lbprefix.md | 4 ++- .../commands/dpservice-cli_delete_lbtarget.md | 4 ++- .../dpservice-cli_delete_loadbalancer.md | 4 ++- docs/commands/dpservice-cli_delete_nat.md | 4 ++- .../dpservice-cli_delete_neighbornat.md | 4 ++- docs/commands/dpservice-cli_delete_prefix.md | 4 ++- docs/commands/dpservice-cli_delete_route.md | 12 +++---- .../dpservice-cli_delete_virtualip.md | 4 ++- docs/commands/dpservice-cli_get.md | 14 ++++---- .../dpservice-cli_get_firewallrule.md | 6 ++-- docs/commands/dpservice-cli_get_interface.md | 6 ++-- .../dpservice-cli_get_loadbalancer.md | 6 ++-- docs/commands/dpservice-cli_get_nat.md | 6 ++-- docs/commands/dpservice-cli_get_natinfo.md | 12 +++---- docs/commands/dpservice-cli_get_virtualip.md | 6 ++-- docs/commands/dpservice-cli_get_vni.md | 36 +++++++++++++++++++ docs/commands/dpservice-cli_init.md | 4 ++- docs/commands/dpservice-cli_initialized.md | 4 ++- docs/commands/dpservice-cli_list.md | 14 ++++---- .../dpservice-cli_list_firewallrules.md | 6 ++-- .../commands/dpservice-cli_list_interfaces.md | 6 ++-- .../commands/dpservice-cli_list_lbprefixes.md | 6 ++-- ...rget.md => dpservice-cli_list_lbtarget.md} | 14 ++++---- docs/commands/dpservice-cli_list_nats.md | 34 ++++++++++++++++++ docs/commands/dpservice-cli_list_prefixes.md | 6 ++-- docs/commands/dpservice-cli_list_routes.md | 6 ++-- docs/development/README.md | 9 +++++ dpdk/api/types.go | 6 ++-- 46 files changed, 250 insertions(+), 127 deletions(-) create mode 100644 docs/commands/dpservice-cli_get_vni.md rename docs/commands/{dpservice-cli_get_lbtarget.md => dpservice-cli_list_lbtarget.md} (54%) create mode 100644 docs/commands/dpservice-cli_list_nats.md diff --git a/cmd/create_interface.go b/cmd/create_interface.go index 5e08478..f327122 100644 --- a/cmd/create_interface.go +++ b/cmd/create_interface.go @@ -93,7 +93,7 @@ func RunCreateInterface(ctx context.Context, dpdkClientFactory DPDKClientFactory iface, err := client.CreateInterface(ctx, &api.Interface{ InterfaceMeta: api.InterfaceMeta{ ID: opts.ID, - PXE: api.PXE{Server: opts.PxeServer, FileName: opts.PxeFileName}, + PXE: &api.PXE{Server: opts.PxeServer, FileName: opts.PxeFileName}, }, Spec: api.InterfaceSpec{ VNI: opts.VNI, diff --git a/docs/README.md b/docs/README.md index c8c2170..da52313 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,5 +1,17 @@ # Documentation - dpservice-cli -All available commands can be found [here](/docs/commands/). +## All available commands can be found [here](/docs/commands/). -Development details can be found [here](/docs/development/) \ No newline at end of file +To generate current command tree add this at the start of main.go: +``` + err := doc.GenMarkdownTree(cmd.Command(), "/tmp/") + if err != nil { + log.Fatal(err) + } +``` +run the program once and then remove this code. +This will generate a whole series of files, one for each command in the tree, in the directory specified (in this case "/tmp/"). + +Cobra command Markdown [docs](https://github.com/spf13/cobra/blob/main/doc/md_docs.md) + +## Development details can be found [here](/docs/development/) \ No newline at end of file diff --git a/docs/commands/dpservice-cli.md b/docs/commands/dpservice-cli.md index b456396..fdb2e7e 100644 --- a/docs/commands/dpservice-cli.md +++ b/docs/commands/dpservice-cli.md @@ -12,6 +12,8 @@ dpservice-cli [flags] --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -h, --help help for dpservice-cli + -o, --output string Output format. [json|yaml|table|name] + --pretty Whether to render pretty output. ``` ### SEE ALSO @@ -19,9 +21,9 @@ dpservice-cli [flags] * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] * [dpservice-cli completion](dpservice-cli_completion.md) - Generate completion script * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] * [dpservice-cli init](dpservice-cli_init.md) - Initial set up of the DPDK app * [dpservice-cli initialized](dpservice-cli_initialized.md) - Indicates if the DPDK app has been initialized already -* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_add.md b/docs/commands/dpservice-cli_add.md index 4cc831c..bb15c95 100644 --- a/docs/commands/dpservice-cli_add.md +++ b/docs/commands/dpservice-cli_add.md @@ -15,8 +15,6 @@ dpservice-cli add [flags] ``` -f, --filename strings Filename, directory, or URL to file to use to create the resource -h, --help help for add - -o, --output string Output format. (default "name") - --pretty Whether to render pretty output. ``` ### Options inherited from parent commands @@ -24,6 +22,8 @@ dpservice-cli add [flags] ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] + --pretty Whether to render pretty output. ``` ### SEE ALSO @@ -40,4 +40,4 @@ dpservice-cli add [flags] * [dpservice-cli add route](dpservice-cli_add_route.md) - Add a route * [dpservice-cli add virtualip](dpservice-cli_add_virtualip.md) - Add a virtual IP to interface. -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_add_firewallrule.md b/docs/commands/dpservice-cli_add_firewallrule.md index 6a9c0cf..7d82f65 100644 --- a/docs/commands/dpservice-cli_add_firewallrule.md +++ b/docs/commands/dpservice-cli_add_firewallrule.md @@ -9,28 +9,28 @@ dpservice-cli add firewallrule <--interface-id> [flags] ### Examples ``` -dpservice-cli add fwrule --interface-id=vm1 --action=1 --direction=1 --dst=5.5.5.0/24 --ipv=0 --priority=100 --rule-id=12 --src=1.1.1.1/32 --protocol=tcp --srcPortLower=1 --srcPortUpper=1000 --dstPortLower=500 --dstPortUpper=600 +dpservice-cli add fwrule --interface-id=vm1 --action=1 --direction=1 --dst=5.5.5.0/24 --ipver=0 --priority=100 --rule-id=12 --src=1.1.1.1/32 --protocol=tcp --src-port-min=1 --src-port-max=1000 --dst-port-min=500 --dst-port-max=600 ``` ### Options ``` - --action uint8 Firewall action: Drop = 0/Accept = 1 // Can be only "accept" at the moment. - --direction uint8 Traffic direction of FW Rule: Ingress = 0/Egress = 1 - --dst ipprefix Destination prefix // 0.0.0.0 with prefix length 0 matches all destination IPs. (default invalid Prefix) - --dstPortLower int32 Destination Ports start // -1 matches all destination ports. - --dstPortUpper int32 Destination Ports end. + --action string Firewall action: drop/deny/0|accept/allow/1 (Can be only "accept/allow/1" at the moment). + --direction string Traffic direction of FW Rule: Ingress = 0/Egress = 1 + --dst ipprefix Destination prefix (0.0.0.0 with prefix length 0 matches all destination IPs). (default invalid Prefix) + --dst-port-max int32 Destination Ports end. + --dst-port-min int32 Destination Ports start (-1 matches all destination ports). -h, --help help for firewallrule - --icmpCode int32 ICMP code // -1 matches all ICMP Codes. - --icmpType int32 ICMP type // -1 matches all ICMP Types. + --icmp-code int32 ICMP code (-1 matches all ICMP Codes). + --icmp-type int32 ICMP type (-1 matches all ICMP Types). --interface-id string InterfaceID of FW Rule. - --ipv uint8 IpVersion of FW Rule IPv4 = 0/IPv6 = 1. - --priority uint32 Priority of FW Rule. // For future use. No effect at the moment. - --protocol string Protocol used icmp/tcp/udp // Not defining a protocol filter matches all protocols. + --ipver string IpVersion of FW Rule: IPv4 = 0/IPv6 = 1. + --priority uint32 Priority of FW Rule. (For future use. No effect at the moment). (default 1000) + --protocol string Protocol used icmp/tcp/udp (Not defining a protocol filter matches all protocols). --rule-id string RuleID of FW Rule. - --src ipprefix Source prefix // 0.0.0.0 with prefix length 0 matches all source IPs. (default invalid Prefix) - --srcPortLower int32 Source Ports start // -1 matches all source ports. - --srcPortUpper int32 Source Ports end. + --src ipprefix Source prefix (0.0.0.0 with prefix length 0 matches all source IPs). (default invalid Prefix) + --src-port-max int32 Source Ports end. + --src-port-min int32 Source Ports start (-1 matches all source ports). ``` ### Options inherited from parent commands @@ -38,7 +38,7 @@ dpservice-cli add fwrule --interface-id=vm1 --action=1 --direction=1 --dst=5.5.5 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. (default "name") + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. ``` @@ -46,4 +46,4 @@ dpservice-cli add fwrule --interface-id=vm1 --action=1 --direction=1 --dst=5.5.5 * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_add_interface.md b/docs/commands/dpservice-cli_add_interface.md index 1e6c5bd..78ef0fb 100644 --- a/docs/commands/dpservice-cli_add_interface.md +++ b/docs/commands/dpservice-cli_add_interface.md @@ -15,11 +15,13 @@ dpservice-cli add interface --id=vm4 --ip=10.200.1.4 --ip=2000:200:1::4 --vni=20 ### Options ``` - --device string Device to allocate. - -h, --help help for interface - --id string ID of the interface. - --ip addrSlice IP to assign to the interface. (default []) - --vni uint32 VNI to add the interface to. + --device string Device to allocate. + -h, --help help for interface + --id string ID of the interface. + --ip addrSlice IP to assign to the interface. (default []) + --pxe-file-name string PXE boot file name. + --pxe-server string PXE next server. + --vni uint32 VNI to add the interface to. ``` ### Options inherited from parent commands @@ -27,7 +29,7 @@ dpservice-cli add interface --id=vm4 --ip=10.200.1.4 --ip=2000:200:1::4 --vni=20 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. (default "name") + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. ``` @@ -35,4 +37,4 @@ dpservice-cli add interface --id=vm4 --ip=10.200.1.4 --ip=2000:200:1::4 --vni=20 * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_add_lbprefix.md b/docs/commands/dpservice-cli_add_lbprefix.md index e54d83f..591eeb7 100644 --- a/docs/commands/dpservice-cli_add_lbprefix.md +++ b/docs/commands/dpservice-cli_add_lbprefix.md @@ -25,7 +25,7 @@ dpservice-cli add lbprefix --prefix=10.10.10.0/24 --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. (default "name") + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. ``` @@ -33,4 +33,4 @@ dpservice-cli add lbprefix --prefix=10.10.10.0/24 --interface-id=vm1 * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_add_lbtarget.md b/docs/commands/dpservice-cli_add_lbtarget.md index e704773..a579f5a 100644 --- a/docs/commands/dpservice-cli_add_lbtarget.md +++ b/docs/commands/dpservice-cli_add_lbtarget.md @@ -25,7 +25,7 @@ dpservice-cli add lbtarget --target-ip=ff80::5 --lb-id=2 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. (default "name") + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. ``` @@ -33,4 +33,4 @@ dpservice-cli add lbtarget --target-ip=ff80::5 --lb-id=2 * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_add_loadbalancer.md b/docs/commands/dpservice-cli_add_loadbalancer.md index 92d7d15..7fe2aab 100644 --- a/docs/commands/dpservice-cli_add_loadbalancer.md +++ b/docs/commands/dpservice-cli_add_loadbalancer.md @@ -9,7 +9,7 @@ dpservice-cli add loadbalancer <--id> <--vni> <--vip> <--lbports> [flags] ### Examples ``` -dpservice-cli create lb --id=4 --vni=100 --vip=10.20.30.40 --lbports=TCP/443,UDP/53 +dpservice-cli add loadbalancer --id=4 --vni=100 --vip=10.20.30.40 --lbports=TCP/443,UDP/53 ``` ### Options @@ -27,7 +27,7 @@ dpservice-cli create lb --id=4 --vni=100 --vip=10.20.30.40 --lbports=TCP/443,UDP ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. (default "name") + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. ``` @@ -35,4 +35,4 @@ dpservice-cli create lb --id=4 --vni=100 --vip=10.20.30.40 --lbports=TCP/443,UDP * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_add_nat.md b/docs/commands/dpservice-cli_add_nat.md index 27439a2..4c55029 100644 --- a/docs/commands/dpservice-cli_add_nat.md +++ b/docs/commands/dpservice-cli_add_nat.md @@ -27,7 +27,7 @@ dpservice-cli add nat --interface-id=vm1 --natip=10.20.30.40 --minport=30000 --m ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. (default "name") + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. ``` @@ -35,4 +35,4 @@ dpservice-cli add nat --interface-id=vm1 --natip=10.20.30.40 --minport=30000 --m * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_add_neighbornat.md b/docs/commands/dpservice-cli_add_neighbornat.md index bbfcf78..b1bc756 100644 --- a/docs/commands/dpservice-cli_add_neighbornat.md +++ b/docs/commands/dpservice-cli_add_neighbornat.md @@ -28,7 +28,7 @@ dpservice-cli add neighbornat --natip=10.20.30.40 --vni=100 --minport=30000 --ma ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. (default "name") + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. ``` @@ -36,4 +36,4 @@ dpservice-cli add neighbornat --natip=10.20.30.40 --vni=100 --minport=30000 --ma * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_add_prefix.md b/docs/commands/dpservice-cli_add_prefix.md index cd05a61..dd8d0ab 100644 --- a/docs/commands/dpservice-cli_add_prefix.md +++ b/docs/commands/dpservice-cli_add_prefix.md @@ -25,7 +25,7 @@ dpservice-cli add prefix --prefix=10.20.30.0/24 --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. (default "name") + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. ``` @@ -33,4 +33,4 @@ dpservice-cli add prefix --prefix=10.20.30.0/24 --interface-id=vm1 * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_add_route.md b/docs/commands/dpservice-cli_add_route.md index 91cf4db..89bf142 100644 --- a/docs/commands/dpservice-cli_add_route.md +++ b/docs/commands/dpservice-cli_add_route.md @@ -27,7 +27,7 @@ dpservice-cli add route --prefix=10.100.3.0/24 --next-hop-vni=0 --next-hop-ip=fc ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. (default "name") + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. ``` @@ -35,4 +35,4 @@ dpservice-cli add route --prefix=10.100.3.0/24 --next-hop-vni=0 --next-hop-ip=fc * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_add_virtualip.md b/docs/commands/dpservice-cli_add_virtualip.md index 38ae9f0..d656d8f 100644 --- a/docs/commands/dpservice-cli_add_virtualip.md +++ b/docs/commands/dpservice-cli_add_virtualip.md @@ -25,7 +25,7 @@ dpservice-cli add virtualip --vip=20.20.20.20 --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. (default "name") + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. ``` @@ -33,4 +33,4 @@ dpservice-cli add virtualip --vip=20.20.20.20 --interface-id=vm1 * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_completion.md b/docs/commands/dpservice-cli_completion.md index 9a36414..0d0378e 100644 --- a/docs/commands/dpservice-cli_completion.md +++ b/docs/commands/dpservice-cli_completion.md @@ -59,10 +59,12 @@ dpservice-cli completion [bash|zsh|fish|powershell] ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] + --pretty Whether to render pretty output. ``` ### SEE ALSO * [dpservice-cli](dpservice-cli.md) - -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_delete.md b/docs/commands/dpservice-cli_delete.md index cfa1cf4..548001a 100644 --- a/docs/commands/dpservice-cli_delete.md +++ b/docs/commands/dpservice-cli_delete.md @@ -22,6 +22,8 @@ dpservice-cli delete [flags] ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] + --pretty Whether to render pretty output. ``` ### SEE ALSO @@ -38,4 +40,4 @@ dpservice-cli delete [flags] * [dpservice-cli delete route](dpservice-cli_delete_route.md) - Delete a route * [dpservice-cli delete virtualip](dpservice-cli_delete_virtualip.md) - Delete virtual IP from interface -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_delete_firewallrule.md b/docs/commands/dpservice-cli_delete_firewallrule.md index 76a5553..9c67bad 100644 --- a/docs/commands/dpservice-cli_delete_firewallrule.md +++ b/docs/commands/dpservice-cli_delete_firewallrule.md @@ -25,10 +25,12 @@ dpservice-cli delete firewallrule --rule-id=1 --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] (default "name") + --pretty Whether to render pretty output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_delete_interface.md b/docs/commands/dpservice-cli_delete_interface.md index 2f52f3a..471a49c 100644 --- a/docs/commands/dpservice-cli_delete_interface.md +++ b/docs/commands/dpservice-cli_delete_interface.md @@ -24,10 +24,12 @@ dpservice-cli delete interface --id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] (default "name") + --pretty Whether to render pretty output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_delete_lbprefix.md b/docs/commands/dpservice-cli_delete_lbprefix.md index 66d700d..9259bb2 100644 --- a/docs/commands/dpservice-cli_delete_lbprefix.md +++ b/docs/commands/dpservice-cli_delete_lbprefix.md @@ -25,10 +25,12 @@ dpservice-cli delete lbprefix --prefix=ff80::1/64 --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] (default "name") + --pretty Whether to render pretty output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_delete_lbtarget.md b/docs/commands/dpservice-cli_delete_lbtarget.md index 43ab24f..2ace014 100644 --- a/docs/commands/dpservice-cli_delete_lbtarget.md +++ b/docs/commands/dpservice-cli_delete_lbtarget.md @@ -25,10 +25,12 @@ dpservice-cli delete lbtarget --target-ip=ff80::1 --lb-id=1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] (default "name") + --pretty Whether to render pretty output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_delete_loadbalancer.md b/docs/commands/dpservice-cli_delete_loadbalancer.md index 28ad1f3..88a9d87 100644 --- a/docs/commands/dpservice-cli_delete_loadbalancer.md +++ b/docs/commands/dpservice-cli_delete_loadbalancer.md @@ -24,10 +24,12 @@ dpservice-cli delete loadbalancer --id=1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] (default "name") + --pretty Whether to render pretty output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_delete_nat.md b/docs/commands/dpservice-cli_delete_nat.md index 09585c6..a681f6d 100644 --- a/docs/commands/dpservice-cli_delete_nat.md +++ b/docs/commands/dpservice-cli_delete_nat.md @@ -24,10 +24,12 @@ dpservice-cli delete nat --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] (default "name") + --pretty Whether to render pretty output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_delete_neighbornat.md b/docs/commands/dpservice-cli_delete_neighbornat.md index 0cc4c21..6ca448b 100644 --- a/docs/commands/dpservice-cli_delete_neighbornat.md +++ b/docs/commands/dpservice-cli_delete_neighbornat.md @@ -27,10 +27,12 @@ dpservice-cli delete neighbornat --natip=10.20.30.40 --vni=100 --minport=30000 - ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] (default "name") + --pretty Whether to render pretty output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_delete_prefix.md b/docs/commands/dpservice-cli_delete_prefix.md index b90c1d5..4807047 100644 --- a/docs/commands/dpservice-cli_delete_prefix.md +++ b/docs/commands/dpservice-cli_delete_prefix.md @@ -25,10 +25,12 @@ dpservice-cli delete prefix --prefix=10.20.30.0/24 --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] (default "name") + --pretty Whether to render pretty output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_delete_route.md b/docs/commands/dpservice-cli_delete_route.md index 71d8889..c3f6b8b 100644 --- a/docs/commands/dpservice-cli_delete_route.md +++ b/docs/commands/dpservice-cli_delete_route.md @@ -15,11 +15,9 @@ dpservice-cli delete route --prefix=10.100.2.0/24 --next-hop-vni=0 --next-hop-ip ### Options ``` - -h, --help help for route - --next-hop-ip ip Next hop IP of the route. (default invalid IP) - --next-hop-vni uint32 Next hop VNI of the route. - --prefix ipprefix Prefix of the route. (default invalid Prefix) - --vni uint32 VNI of the route. + -h, --help help for route + --prefix ipprefix Prefix of the route. (default invalid Prefix) + --vni uint32 VNI of the route. ``` ### Options inherited from parent commands @@ -27,10 +25,12 @@ dpservice-cli delete route --prefix=10.100.2.0/24 --next-hop-vni=0 --next-hop-ip ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] (default "name") + --pretty Whether to render pretty output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_delete_virtualip.md b/docs/commands/dpservice-cli_delete_virtualip.md index 9920f63..341bb75 100644 --- a/docs/commands/dpservice-cli_delete_virtualip.md +++ b/docs/commands/dpservice-cli_delete_virtualip.md @@ -24,10 +24,12 @@ dpservice-cli delete virtualip --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] (default "name") + --pretty Whether to render pretty output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_get.md b/docs/commands/dpservice-cli_get.md index 8cacf5a..1ce0760 100644 --- a/docs/commands/dpservice-cli_get.md +++ b/docs/commands/dpservice-cli_get.md @@ -1,10 +1,10 @@ ## dpservice-cli get -Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] +Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] ### Synopsis -Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] +Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] ``` dpservice-cli get [flags] @@ -13,9 +13,7 @@ dpservice-cli get [flags] ### Options ``` - -h, --help help for get - -o, --output string Output format. - --pretty Whether to render pretty output. + -h, --help help for get ``` ### Options inherited from parent commands @@ -23,6 +21,8 @@ dpservice-cli get [flags] ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] + --pretty Whether to render pretty output. ``` ### SEE ALSO @@ -30,10 +30,10 @@ dpservice-cli get [flags] * [dpservice-cli](dpservice-cli.md) - * [dpservice-cli get firewallrule](dpservice-cli_get_firewallrule.md) - Get firewall rule * [dpservice-cli get interface](dpservice-cli_get_interface.md) - Get interface -* [dpservice-cli get lbtarget](dpservice-cli_get_lbtarget.md) - Get LoadBalancer Targets * [dpservice-cli get loadbalancer](dpservice-cli_get_loadbalancer.md) - Get loadbalancer * [dpservice-cli get nat](dpservice-cli_get_nat.md) - Get NAT on interface * [dpservice-cli get natinfo](dpservice-cli_get_natinfo.md) - List all machines that are behind this IP * [dpservice-cli get virtualip](dpservice-cli_get_virtualip.md) - Get Virtual IP on interface +* [dpservice-cli get vni](dpservice-cli_get_vni.md) - Get vni usage information -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_get_firewallrule.md b/docs/commands/dpservice-cli_get_firewallrule.md index 84e11ae..bae6d35 100644 --- a/docs/commands/dpservice-cli_get_firewallrule.md +++ b/docs/commands/dpservice-cli_get_firewallrule.md @@ -25,12 +25,12 @@ dpservice-cli get fwrule --rule-id=1 --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. + -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. ``` ### SEE ALSO -* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_get_interface.md b/docs/commands/dpservice-cli_get_interface.md index c6fc160..d913cc2 100644 --- a/docs/commands/dpservice-cli_get_interface.md +++ b/docs/commands/dpservice-cli_get_interface.md @@ -24,12 +24,12 @@ dpservice-cli get interface --id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. + -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. ``` ### SEE ALSO -* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_get_loadbalancer.md b/docs/commands/dpservice-cli_get_loadbalancer.md index 5216938..da060c9 100644 --- a/docs/commands/dpservice-cli_get_loadbalancer.md +++ b/docs/commands/dpservice-cli_get_loadbalancer.md @@ -24,12 +24,12 @@ dpservice-cli get loadbalancer --id=4 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. + -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. ``` ### SEE ALSO -* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_get_nat.md b/docs/commands/dpservice-cli_get_nat.md index 4fc8d10..41006a8 100644 --- a/docs/commands/dpservice-cli_get_nat.md +++ b/docs/commands/dpservice-cli_get_nat.md @@ -24,12 +24,12 @@ dpservice-cli get nat --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. + -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. ``` ### SEE ALSO -* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_get_natinfo.md b/docs/commands/dpservice-cli_get_natinfo.md index 459da64..42640ce 100644 --- a/docs/commands/dpservice-cli_get_natinfo.md +++ b/docs/commands/dpservice-cli_get_natinfo.md @@ -15,9 +15,9 @@ dpservice-cli get natinfo --nat-ip=10.20.30.40 --nat-type=1 ### Options ``` - -h, --help help for natinfo - --nat-ip ip NAT IP to get info for (default invalid IP) - --nat-type int32 NAT Info type: NATInfoTypeZero = 0/NATInfoLocal = 1/NATInfoNeigh = 2 + -h, --help help for natinfo + --info-type string NAT Info type: Local = 1/Neigh(bor) = 2 + --nat-ip ip NAT IP to get info for (default invalid IP) ``` ### Options inherited from parent commands @@ -25,12 +25,12 @@ dpservice-cli get natinfo --nat-ip=10.20.30.40 --nat-type=1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. + -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. ``` ### SEE ALSO -* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_get_virtualip.md b/docs/commands/dpservice-cli_get_virtualip.md index 17ec2d7..b37a2c2 100644 --- a/docs/commands/dpservice-cli_get_virtualip.md +++ b/docs/commands/dpservice-cli_get_virtualip.md @@ -24,12 +24,12 @@ dpservice-cli get virtualip --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. + -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. ``` ### SEE ALSO -* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_get_vni.md b/docs/commands/dpservice-cli_get_vni.md new file mode 100644 index 0000000..68b9c51 --- /dev/null +++ b/docs/commands/dpservice-cli_get_vni.md @@ -0,0 +1,36 @@ +## dpservice-cli get vni + +Get vni usage information + +``` +dpservice-cli get vni <--vni> <--vni-type> [flags] +``` + +### Examples + +``` +dpservice-cli get vni --vni=vm1 --vni-type=0 +``` + +### Options + +``` + -h, --help help for vni + --vni uint32 VNI to check. + --vni-type uint8 VNI Type. +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] + +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_init.md b/docs/commands/dpservice-cli_init.md index efc1a0f..3c55296 100644 --- a/docs/commands/dpservice-cli_init.md +++ b/docs/commands/dpservice-cli_init.md @@ -23,10 +23,12 @@ dpservice-cli init ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] + --pretty Whether to render pretty output. ``` ### SEE ALSO * [dpservice-cli](dpservice-cli.md) - -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_initialized.md b/docs/commands/dpservice-cli_initialized.md index 77abd2c..c1d0acf 100644 --- a/docs/commands/dpservice-cli_initialized.md +++ b/docs/commands/dpservice-cli_initialized.md @@ -23,10 +23,12 @@ dpservice-cli initialized ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] + --pretty Whether to render pretty output. ``` ### SEE ALSO * [dpservice-cli](dpservice-cli.md) - -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_list.md b/docs/commands/dpservice-cli_list.md index f310830..f9375d8 100644 --- a/docs/commands/dpservice-cli_list.md +++ b/docs/commands/dpservice-cli_list.md @@ -1,10 +1,10 @@ ## dpservice-cli list -Lists one of [firewallrules interfaces prefixes lbprefixes routes] +Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] ### Synopsis -Lists one of [firewallrules interfaces prefixes lbprefixes routes] +Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] ``` dpservice-cli list [flags] @@ -13,9 +13,7 @@ dpservice-cli list [flags] ### Options ``` - -h, --help help for list - -o, --output string Output format. - --pretty Whether to render pretty output. + -h, --help help for list ``` ### Options inherited from parent commands @@ -23,6 +21,8 @@ dpservice-cli list [flags] ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] + --pretty Whether to render pretty output. ``` ### SEE ALSO @@ -31,7 +31,9 @@ dpservice-cli list [flags] * [dpservice-cli list firewallrules](dpservice-cli_list_firewallrules.md) - List firewall rules on interface * [dpservice-cli list interfaces](dpservice-cli_list_interfaces.md) - List all interfaces * [dpservice-cli list lbprefixes](dpservice-cli_list_lbprefixes.md) - List loadbalancer prefixes on interface. +* [dpservice-cli list lbtarget](dpservice-cli_list_lbtarget.md) - List LoadBalancer Targets +* [dpservice-cli list nats](dpservice-cli_list_nats.md) - List all nats * [dpservice-cli list prefixes](dpservice-cli_list_prefixes.md) - List prefix(es) on interface. * [dpservice-cli list routes](dpservice-cli_list_routes.md) - List routes of specified VNI -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_list_firewallrules.md b/docs/commands/dpservice-cli_list_firewallrules.md index 5a56984..6c65ed1 100644 --- a/docs/commands/dpservice-cli_list_firewallrules.md +++ b/docs/commands/dpservice-cli_list_firewallrules.md @@ -24,12 +24,12 @@ dpservice-cli list firewallrules --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. + -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. ``` ### SEE ALSO -* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_list_interfaces.md b/docs/commands/dpservice-cli_list_interfaces.md index 407a976..c2b735f 100644 --- a/docs/commands/dpservice-cli_list_interfaces.md +++ b/docs/commands/dpservice-cli_list_interfaces.md @@ -23,12 +23,12 @@ dpservice-cli list interfaces ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. + -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. ``` ### SEE ALSO -* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_list_lbprefixes.md b/docs/commands/dpservice-cli_list_lbprefixes.md index 508e03e..b4f32de 100644 --- a/docs/commands/dpservice-cli_list_lbprefixes.md +++ b/docs/commands/dpservice-cli_list_lbprefixes.md @@ -24,12 +24,12 @@ dpservice-cli list lbprefixes --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. + -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. ``` ### SEE ALSO -* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_get_lbtarget.md b/docs/commands/dpservice-cli_list_lbtarget.md similarity index 54% rename from docs/commands/dpservice-cli_get_lbtarget.md rename to docs/commands/dpservice-cli_list_lbtarget.md index 29a3e64..69a3fbb 100644 --- a/docs/commands/dpservice-cli_get_lbtarget.md +++ b/docs/commands/dpservice-cli_list_lbtarget.md @@ -1,15 +1,15 @@ -## dpservice-cli get lbtarget +## dpservice-cli list lbtarget -Get LoadBalancer Targets +List LoadBalancer Targets ``` -dpservice-cli get lbtarget <--lb-id> [flags] +dpservice-cli list lbtarget <--lb-id> [flags] ``` ### Examples ``` -dpservice-cli get lbtarget --lb-id=1 +dpservice-cli list lbtarget --lb-id=1 ``` ### Options @@ -24,12 +24,12 @@ dpservice-cli get lbtarget --lb-id=1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. + -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. ``` ### SEE ALSO -* [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer lbtarget nat natinfo firewallrule] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_list_nats.md b/docs/commands/dpservice-cli_list_nats.md new file mode 100644 index 0000000..2e4be3a --- /dev/null +++ b/docs/commands/dpservice-cli_list_nats.md @@ -0,0 +1,34 @@ +## dpservice-cli list nats + +List all nats + +``` +dpservice-cli list nats [flags] +``` + +### Examples + +``` +dpservice-cli list nats +``` + +### Options + +``` + -h, --help help for nats +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] + --pretty Whether to render pretty output. +``` + +### SEE ALSO + +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] + +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_list_prefixes.md b/docs/commands/dpservice-cli_list_prefixes.md index dd9d4de..3849c68 100644 --- a/docs/commands/dpservice-cli_list_prefixes.md +++ b/docs/commands/dpservice-cli_list_prefixes.md @@ -24,12 +24,12 @@ dpservice-cli list prefixes --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. + -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. ``` ### SEE ALSO -* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/commands/dpservice-cli_list_routes.md b/docs/commands/dpservice-cli_list_routes.md index 0d3b315..c009ae9 100644 --- a/docs/commands/dpservice-cli_list_routes.md +++ b/docs/commands/dpservice-cli_list_routes.md @@ -24,12 +24,12 @@ dpservice-cli list routes --vni=100 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. + -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. ``` ### SEE ALSO -* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] -###### Auto generated by spf13/cobra on 17-May-2023 +###### Auto generated by spf13/cobra on 30-May-2023 diff --git a/docs/development/README.md b/docs/development/README.md index 9cfe975..bfbc0d7 100644 --- a/docs/development/README.md +++ b/docs/development/README.md @@ -70,4 +70,13 @@ This client uses golang bindings from repo [net-dpservice-go](https://github.com Definition go files in [proto](https://github.com/onmetal/net-dpservice-go/tree/main/proto) folder are auto-generated from [dpdk.proto](https://github.com/onmetal/net-dpservice/blob/osc/main/proto/dpdk.proto) file in [net-dpservice](https://github.com/onmetal/net-dpservice/) repo. +In case of upgrade of net-dpservice-go, it is needed to pull the latest version: +``` +go get github.com/onmetal/net-dpservice-go@[|latest] +``` +In order to pull from private repository set the **GOPRIVATE** variable: +``` +export GOPRIVATE=github.com/onmetal/* +``` + More info about gRPC can be found [here](https://grpc.io/docs/what-is-grpc/introduction/). \ No newline at end of file diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 75b0911..9990c52 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -248,12 +248,12 @@ type Interface struct { type InterfaceMeta struct { ID string `json:"id"` - PXE PXE `json:"pxe"` + PXE *PXE `json:"pxe,omitempty"` } type PXE struct { - Server string `json:"server"` - FileName string `json:"fileName"` + Server string `json:"server,omitempty"` + FileName string `json:"fileName,omitempty"` } func (m *InterfaceMeta) GetName() string { From f2bcadde4d4be70814dc2063468052dca766008e Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 30 May 2023 16:26:30 +0200 Subject: [PATCH 47/65] update getnatinfo to support no natinfotype --- .vscode/launch.json | 5 +++-- cmd/add_firewall_rule.go | 20 +++++++++---------- cmd/get_nat_info.go | 6 +++--- cmd/get_vni.go | 2 +- dpdk/api/conversion.go | 5 +---- dpdk/client/client.go | 42 +++++++++++++++++++++++++++++++--------- renderer/renderer.go | 36 +++++++++++++++++++++++----------- 7 files changed, 76 insertions(+), 40 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 0dc7561..627308a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -18,7 +18,7 @@ //"args": ["add", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] //"args": ["add", "lbtarget", "--target-ip=ff80::5", "--lb-id=1"] //"args": ["get", "lbtarget", "--lb-id=4"] - //"args": ["get", "natinfo", "--nat-ip=10.20.30.40", "--nat-type=Local"] + "args": ["get", "natinfo", "--nat-ip=10.20.30.40", "--info-type=0"] //"args": ["get", "fwrule", "vm1", "--rule-id=4"] //"args": ["add", "fwrule", "vm1", "--action=1", "--direction=1", "--dst=5.5.5.0/24", "--ipv=0", // "--priority=100", "--rule-id=9", "--src=1.1.1.1/32", "--protocol=tcp"] @@ -27,7 +27,8 @@ //"args": ["add", "-f", "/tmp/addint.json"] //"args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=11"] //"args": ["list", "nats"] - "args": ["get", "vni", "--vni=100", "--vni-type=0"] + //"args": ["get", "vni", "--vni=100", "--vni-type=0"] + //"args": ["list", "prefixes","--interface-id=vm1"] } ] } \ No newline at end of file diff --git a/cmd/add_firewall_rule.go b/cmd/add_firewall_rule.go index e1ff149..d16a288 100644 --- a/cmd/add_firewall_rule.go +++ b/cmd/add_firewall_rule.go @@ -104,12 +104,12 @@ func (o *AddFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { flag.PrefixVar(fs, &o.SourcePrefix, "src", o.SourcePrefix, "Source prefix (0.0.0.0 with prefix length 0 matches all source IPs).") flag.PrefixVar(fs, &o.DestinationPrefix, "dst", o.DestinationPrefix, "Destination prefix (0.0.0.0 with prefix length 0 matches all destination IPs).") fs.StringVar(&o.ProtocolFilter, "protocol", o.ProtocolFilter, "Protocol used icmp/tcp/udp (Not defining a protocol filter matches all protocols).") - fs.Int32Var(&o.SrcPortLower, "src-port-min", o.SrcPortLower, "Source Ports start (-1 matches all source ports).") - fs.Int32Var(&o.SrcPortUpper, "src-port-max", o.SrcPortUpper, "Source Ports end.") - fs.Int32Var(&o.DstPortLower, "dst-port-min", o.DstPortLower, "Destination Ports start (-1 matches all destination ports).") - fs.Int32Var(&o.DstPortUpper, "dst-port-max", o.DstPortUpper, "Destination Ports end.") - fs.Int32Var(&o.IcmpType, "icmp-type", o.IcmpType, "ICMP type (-1 matches all ICMP Types).") - fs.Int32Var(&o.IcmpCode, "icmp-code", o.IcmpCode, "ICMP code (-1 matches all ICMP Codes).") + fs.Int32Var(&o.SrcPortLower, "src-port-min", -1, "Source Ports start (-1 matches all source ports).") + fs.Int32Var(&o.SrcPortUpper, "src-port-max", -1, "Source Ports end.") + fs.Int32Var(&o.DstPortLower, "dst-port-min", -1, "Destination Ports start (-1 matches all destination ports).") + fs.Int32Var(&o.DstPortUpper, "dst-port-max", -1, "Destination Ports end.") + fs.Int32Var(&o.IcmpType, "icmp-type", -1, "ICMP type (-1 matches all ICMP Types).") + fs.Int32Var(&o.IcmpCode, "icmp-code", -1, "ICMP code (-1 matches all ICMP Codes).") } @@ -148,9 +148,9 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory IcmpCode: opts.IcmpCode}} case "tcp", "6": if opts.SrcPortLower < -1 || opts.SrcPortLower == 0 || opts.SrcPortLower > 65535 || - opts.SrcPortUpper < 1 || opts.SrcPortUpper > 65535 || + opts.SrcPortUpper < -1 || opts.SrcPortUpper == 0 || opts.SrcPortUpper > 65535 || opts.DstPortLower < -1 || opts.DstPortLower == 0 || opts.DstPortLower > 65535 || - opts.DstPortUpper < 1 || opts.DstPortUpper > 65535 { + opts.DstPortUpper < -1 || opts.DstPortUpper == 0 || opts.DstPortUpper > 65535 { return fmt.Errorf("ports can only be -1 or <1,65535>") } if opts.SrcPortLower > opts.SrcPortUpper || opts.DstPortLower > opts.DstPortUpper { @@ -164,9 +164,9 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory }} case "udp", "17": if opts.SrcPortLower < -1 || opts.SrcPortLower == 0 || opts.SrcPortLower > 65535 || - opts.SrcPortUpper < 1 || opts.SrcPortUpper > 65535 || + opts.SrcPortUpper < -1 || opts.SrcPortUpper == 0 || opts.SrcPortUpper > 65535 || opts.DstPortLower < -1 || opts.DstPortLower == 0 || opts.DstPortLower > 65535 || - opts.DstPortUpper < 1 || opts.DstPortUpper > 65535 { + opts.DstPortUpper < -1 || opts.DstPortUpper == 0 || opts.DstPortUpper > 65535 { return fmt.Errorf("ports can only be -1 or <1,65535>") } if opts.SrcPortLower > opts.SrcPortUpper || opts.DstPortLower > opts.DstPortUpper { diff --git a/cmd/get_nat_info.go b/cmd/get_nat_info.go index c5c85da..806ef57 100644 --- a/cmd/get_nat_info.go +++ b/cmd/get_nat_info.go @@ -34,7 +34,7 @@ func GetNatInfo(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFac cmd := &cobra.Command{ Use: "natinfo <--nat-ip> <--nat-type>", Short: "List all machines that are behind this IP", - Example: "dpservice-cli get natinfo --nat-ip=10.20.30.40 --nat-type=1", + Example: "dpservice-cli get natinfo --nat-ip=10.20.30.40 --info-type=1", Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { @@ -61,11 +61,11 @@ type GetNatInfoOptions struct { func (o *GetNatInfoOptions) AddFlags(fs *pflag.FlagSet) { flag.AddrVar(fs, &o.NatIP, "nat-ip", o.NatIP, "NAT IP to get info for") - fs.StringVar(&o.NatInfoType, "info-type", o.NatInfoType, "NAT Info type: Local = 1/Neigh(bor) = 2") + fs.StringVar(&o.NatInfoType, "info-type", o.NatInfoType, "NAT Info type: Any = 0/Local = 1/Neigh(bor) = 2") } func (o *GetNatInfoOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"nat-ip", "info-type"} { + for _, name := range []string{"nat-ip"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } diff --git a/cmd/get_vni.go b/cmd/get_vni.go index 5ccb1b0..088fe4b 100644 --- a/cmd/get_vni.go +++ b/cmd/get_vni.go @@ -62,7 +62,7 @@ type GetVniOptions struct { func (o *GetVniOptions) AddFlags(fs *pflag.FlagSet) { fs.Uint32Var(&o.VNI, "vni", o.VNI, "VNI to check.") - fs.Uint8Var(&o.VniType, "vni-type", o.VniType, "VNI Type.") + fs.Uint8Var(&o.VniType, "vni-type", o.VniType, "VNI Type: VniIpv4 = 0/VniIpv6 = 1.") } func (o *GetVniOptions) MarkRequiredFlags(cmd *cobra.Command) error { diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index 49041a7..5904b89 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -203,10 +203,7 @@ func ProtoPrefixToPrefix(interfaceID string, dpdkPrefix *proto.Prefix) (*Prefix, return nil, fmt.Errorf("error parsing dpdk prefix address: %w", err) } - prefix, err := addr.Prefix(int(dpdkPrefix.PrefixLength)) - if err != nil { - return nil, fmt.Errorf("invalid dpdk prefix length %d for address %s", dpdkPrefix.PrefixLength, addr) - } + prefix := netip.PrefixFrom(addr, int(dpdkPrefix.GetPrefixLength())) underlayRoute, err := netip.ParseAddr(string(dpdkPrefix.UnderlayRoute)) if err != nil { diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 34b9594..581df3b 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -656,18 +656,40 @@ func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType st nType = 1 case "neigh", "2", "neighbor": nType = 2 + case "any", "0", "": + nType = 0 default: - return nil, fmt.Errorf("nat info type can be only: Local = 1/Neigh(bor) = 2") + return nil, fmt.Errorf("nat info type can be only: Any = 0/Local = 1/Neigh(bor) = 2") } - res, err := c.DPDKonmetalClient.GetNATInfo(ctx, &dpdkproto.GetNATInfoRequest{ + req := dpdkproto.GetNATInfoRequest{ NatVIPIP: &dpdkproto.NATIP{IpVersion: api.NetIPAddrToProtoIPVersion(natVIPIP), Address: []byte(natVIPIP.String()), }, NatInfoType: dpdkproto.NATInfoType(nType), - }) - if err != nil { - return nil, err + } + // nat info type not defined, try both types + res := &dpdkproto.GetNATInfoResponse{NatVIPIP: &dpdkproto.NATIP{}} + var err error + if nType == 0 { + req.NatInfoType = 1 + res1, err1 := c.DPDKonmetalClient.GetNATInfo(ctx, &req) + if err1 != nil { + return nil, err1 + } + req.NatInfoType = 2 + res2, err2 := c.DPDKonmetalClient.GetNATInfo(ctx, &req) + if err2 != nil { + return nil, err2 + } + res.NatInfoEntries = append(res.NatInfoEntries, res1.NatInfoEntries...) + res.NatInfoEntries = append(res.NatInfoEntries, res2.NatInfoEntries...) + res.NatVIPIP.Address = []byte(natVIPIP.String()) + } else { + res, err = c.DPDKonmetalClient.GetNATInfo(ctx, &req) + if err != nil { + return nil, err + } } var nats = make([]api.Nat, len(res.NatInfoEntries)) @@ -675,7 +697,7 @@ func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType st for i, natInfoEntry := range res.GetNatInfoEntries() { var underlayRoute, vipIP netip.Addr - if res.NatInfoType == 2 { + if natInfoEntry.UnderlayRoute != nil { underlayRoute, err = netip.ParseAddr(string(natInfoEntry.GetUnderlayRoute())) if err != nil { return nil, fmt.Errorf("error parsing underlay route: %w", err) @@ -686,7 +708,7 @@ func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType st return nil, fmt.Errorf("error parsing vip ip: %w", err) } nat.Spec.NatVIPIP = &vipIP - } else if res.NatInfoType == 1 { + } else if natInfoEntry.Address != nil { vipIP, err = netip.ParseAddr(string(natInfoEntry.GetAddress())) if err != nil { return nil, fmt.Errorf("error parsing vip ip: %w", err) @@ -784,7 +806,7 @@ func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) return &api.FirewallRule{}, fmt.Errorf("ip version can be only: IPv4 = 0/IPv6 = 1") } - res, err := c.DPDKonmetalClient.AddFirewallRule(ctx, &dpdkproto.AddFirewallRuleRequest{ + req := dpdkproto.AddFirewallRuleRequest{ InterfaceID: []byte(fwRule.FirewallRuleMeta.InterfaceID), Rule: &dpdkproto.FirewallRule{ RuleID: []byte(fwRule.FirewallRuleMeta.RuleID), @@ -804,7 +826,9 @@ func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) }, ProtocolFilter: fwRule.Spec.ProtocolFilter, }, - }) + } + + res, err := c.DPDKonmetalClient.AddFirewallRule(ctx, &req) if err != nil { return &api.FirewallRule{}, err } diff --git a/renderer/renderer.go b/renderer/renderer.go index ff61629..8b4c4fb 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -183,6 +183,8 @@ func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) { return t.initTable(*obj) case *api.Initialized: return t.initializedTable(*obj) + case *api.Vni: + return t.vniTable(*obj) default: return nil, fmt.Errorf("unsupported type %T", v) } @@ -306,24 +308,25 @@ func (t defaultTableConverter) virtualIPTable(virtualIPs []api.VirtualIP) (*Tabl } func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { - var headers []any - if nats[0].Spec.UnderlayRoute == nil { + // TODO clean up table output + //var headers []any + /*if len(nats) != 0 && nats[0].Spec.UnderlayRoute == nil { headers = []any{"NatIP", "MinPort", "MaxPort", "Status"} - } else if nats[0].NatMeta.InterfaceID == "" { + } else if len(nats) != 0 && nats[0].NatMeta.InterfaceID == "" { headers = []any{"NatIP", "MinPort", "MaxPort", "UnderlayRoute", "Status"} - } else { - headers = []any{"InterfaceID", "NatIP", "MinPort", "MaxPort", "UnderlayRoute", "Status"} - } + } else {*/ + headers := []any{"InterfaceID", "NatIP", "MinPort", "MaxPort", "UnderlayRoute", "Status"} + //} columns := make([][]any, len(nats)) for i, nat := range nats { - if nats[0].Spec.UnderlayRoute == nil { + /*if len(nats) != 0 && nats[0].Spec.UnderlayRoute == nil { columns[i] = []any{nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Status.String()} - } else if nats[0].NatMeta.InterfaceID == "" { + } else if len(nats) != 0 && nats[0].NatMeta.InterfaceID == "" { columns[i] = []any{nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute, nat.Status.String()} - } else { - columns[i] = []any{nat.NatMeta.InterfaceID, nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute, nat.Status.String()} - } + } else {*/ + columns[i] = []any{nat.NatMeta.InterfaceID, nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute, nat.Status.String()} + //} } return &TableData{ @@ -388,6 +391,17 @@ func (t defaultTableConverter) initTable(init api.Init) (*TableData, error) { }, nil } +func (t defaultTableConverter) vniTable(vni api.Vni) (*TableData, error) { + headers := []any{"VNI", "VniType", "inUse", "Error", "Message"} + columns := make([][]any, 1) + columns[0] = []any{vni.VniMeta.VNI, vni.VniMeta.VniType, vni.Spec.InUse, vni.Status.Error, vni.Status.Message} + + return &TableData{ + Headers: headers, + Columns: columns, + }, nil +} + func (t defaultTableConverter) initializedTable(initialized api.Initialized) (*TableData, error) { headers := []any{"UUID", "Error", "Message"} columns := make([][]any, 1) From 3c9b30a123683aaea8a3b024c6dae8dcba0e11e0 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Wed, 31 May 2023 15:57:58 +0200 Subject: [PATCH 48/65] move some parameters from metadata to spec --- .vscode/launch.json | 4 +- cmd/add_firewall_rule.go | 4 +- cmd/add_prefix.go | 6 ++- cmd/add_route.go | 14 +++---- cmd/add_virtualip.go | 6 ++- cmd/common.go | 2 +- cmd/create_interface.go | 4 +- cmd/create_loadbalancer_prefix.go | 6 ++- cmd/delete_firewall_rule.go | 2 +- cmd/delete_loadbalancer_prefix.go | 2 +- cmd/delete_prefix.go | 2 +- cmd/delete_route.go | 3 +- cmd/get_firewall_rule.go | 2 +- ...target.go => list_loadbalancer_targets.go} | 4 +- dpdk/api/conversion.go | 21 +++++----- dpdk/api/types.go | 40 +++++++++---------- dpdk/client/client.go | 36 ++++++++--------- dpdk/client/dynamic/dynamic.go | 12 +++--- renderer/renderer.go | 31 ++++++-------- 19 files changed, 102 insertions(+), 99 deletions(-) rename cmd/{get_loadbalancer_target.go => list_loadbalancer_targets.go} (96%) diff --git a/.vscode/launch.json b/.vscode/launch.json index 627308a..345e5db 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,12 +13,12 @@ //"program": "${fileDirname}", //"args": ["add", "loadbalancer", "--id=4", "--lbports", "tcp/389", "--vip", "10.3.4.5", "--vni", "100"] //"args": ["delete", "loadbalancer", "--id=4"] - //"args": ["get", "route", "0.0.0.0/24", "0", "fc00:2::64:0:1", "--vni", "100"] + "args": ["add", "route", "--prefix=10.100.3.0/24", "--next-hop-vni=0", "--next-hop-ip=fc00:2::64:0:1", "--vni=100"] //"args": ["delete", "route", "10.200.2.0/24", "0", "ff80::1", "--vni", "100"] //"args": ["add", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] //"args": ["add", "lbtarget", "--target-ip=ff80::5", "--lb-id=1"] //"args": ["get", "lbtarget", "--lb-id=4"] - "args": ["get", "natinfo", "--nat-ip=10.20.30.40", "--info-type=0"] + //"args": ["get", "natinfo", "--nat-ip=10.20.30.40", "--info-type=0"] //"args": ["get", "fwrule", "vm1", "--rule-id=4"] //"args": ["add", "fwrule", "vm1", "--action=1", "--direction=1", "--dst=5.5.5.0/24", "--ipv=0", // "--priority=100", "--rule-id=9", "--src=1.1.1.1/32", "--protocol=tcp"] diff --git a/cmd/add_firewall_rule.go b/cmd/add_firewall_rule.go index d16a288..3a532da 100644 --- a/cmd/add_firewall_rule.go +++ b/cmd/add_firewall_rule.go @@ -189,10 +189,10 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory fwrule, err := client.AddFirewallRule(ctx, &api.FirewallRule{ TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, FirewallRuleMeta: api.FirewallRuleMeta{ - RuleID: opts.RuleID, InterfaceID: opts.InterfaceID, }, Spec: api.FirewallRuleSpec{ + RuleID: opts.RuleID, TrafficDirection: opts.TrafficDirection, FirewallAction: opts.FirewallAction, Priority: opts.Priority, @@ -209,6 +209,6 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory fwrule.TypeMeta.Kind = api.FirewallRuleKind fwrule.FirewallRuleMeta.InterfaceID = opts.InterfaceID - fwrule.FirewallRuleMeta.RuleID = opts.RuleID + fwrule.Spec.RuleID = opts.RuleID return rendererFactory.RenderObject("added", os.Stdout, fwrule) } diff --git a/cmd/add_prefix.go b/cmd/add_prefix.go index 9e45a59..68f3d46 100644 --- a/cmd/add_prefix.go +++ b/cmd/add_prefix.go @@ -94,7 +94,9 @@ func RunAddPrefix( prefix, err := client.AddPrefix(ctx, &api.Prefix{ PrefixMeta: api.PrefixMeta{ InterfaceID: opts.InterfaceID, - Prefix: opts.Prefix, + }, + Spec: api.PrefixSpec{ + Prefix: opts.Prefix, }, }) if err != nil && err != errors.ErrServerError { @@ -103,6 +105,6 @@ func RunAddPrefix( prefix.TypeMeta.Kind = api.PrefixKind prefix.PrefixMeta.InterfaceID = opts.InterfaceID - prefix.PrefixMeta.Prefix = opts.Prefix + prefix.Spec.Prefix = opts.Prefix return rendererFactory.RenderObject("added", os.Stdout, prefix) } diff --git a/cmd/add_route.go b/cmd/add_route.go index f2a51b6..c03c062 100644 --- a/cmd/add_route.go +++ b/cmd/add_route.go @@ -94,14 +94,13 @@ func RunAddRoute( route, err := client.AddRoute(ctx, &api.Route{ RouteMeta: api.RouteMeta{ - VNI: opts.VNI, - Prefix: opts.Prefix, - NextHop: api.RouteNextHop{ + VNI: opts.VNI, + }, + Spec: api.RouteSpec{Prefix: &opts.Prefix, + NextHop: &api.RouteNextHop{ VNI: opts.NextHopVNI, IP: &opts.NextHopIP, - }, - }, - Spec: api.RouteSpec{}, + }}, }) if err != nil && err != errors.ErrServerError { return fmt.Errorf("error adding route: %w", err) @@ -109,6 +108,7 @@ func RunAddRoute( route.TypeMeta.Kind = api.RouteKind route.RouteMeta.VNI = opts.VNI - route.RouteMeta.Prefix = opts.Prefix + route.Spec.Prefix = &opts.Prefix + route.Spec.NextHop = &api.RouteNextHop{} return rendererFactory.RenderObject("added", os.Stdout, route) } diff --git a/cmd/add_virtualip.go b/cmd/add_virtualip.go index 3ce246f..e33230c 100644 --- a/cmd/add_virtualip.go +++ b/cmd/add_virtualip.go @@ -91,7 +91,9 @@ func RunAddVirtualIP( virtualIP, err := client.AddVirtualIP(ctx, &api.VirtualIP{ VirtualIPMeta: api.VirtualIPMeta{ InterfaceID: opts.InterfaceID, - IP: opts.Vip, + }, + Spec: api.VirtualIPSpec{ + IP: opts.Vip, }, }) if err != nil && err != errors.ErrServerError { @@ -99,7 +101,7 @@ func RunAddVirtualIP( } virtualIP.TypeMeta.Kind = api.RouteKind - virtualIP.VirtualIPMeta.IP = opts.Vip + virtualIP.Spec.IP = opts.Vip virtualIP.VirtualIPMeta.InterfaceID = opts.InterfaceID return rendererFactory.RenderObject("added", os.Stdout, virtualIP) } diff --git a/cmd/common.go b/cmd/common.go index 73a8b2d..b9929cf 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -259,7 +259,7 @@ var ( VirtualIPAliases = []string{"virtualips", "vip", "vips"} LoadBalancerAliases = []string{"loadbalancers", "loadbalancer", "lbs", "lb"} LoadBalancerPrefixAliases = []string{"loadbalancer-prefixes", "lbprfx", "lbprfxs"} - LoadBalancerTargetAliases = []string{"loadbalancer-targets", "lbtrgt", "lbtrgts"} + LoadBalancerTargetAliases = []string{"loadbalancer-targets", "lbtrgt", "lbtrgts", "lbtarget"} NatAliases = []string{"translation"} NeighborNatAliases = []string{"nnat", "ngbnat", "neighnat"} FirewallRuleAliases = []string{"fwrule", "fw-rule", "firewallrules", "fwrules", "fw-rules"} diff --git a/cmd/create_interface.go b/cmd/create_interface.go index f327122..bb23842 100644 --- a/cmd/create_interface.go +++ b/cmd/create_interface.go @@ -92,13 +92,13 @@ func RunCreateInterface(ctx context.Context, dpdkClientFactory DPDKClientFactory iface, err := client.CreateInterface(ctx, &api.Interface{ InterfaceMeta: api.InterfaceMeta{ - ID: opts.ID, - PXE: &api.PXE{Server: opts.PxeServer, FileName: opts.PxeFileName}, + ID: opts.ID, }, Spec: api.InterfaceSpec{ VNI: opts.VNI, Device: opts.Device, IPs: opts.IP, + PXE: &api.PXE{Server: opts.PxeServer, FileName: opts.PxeFileName}, }, }) if err != nil && err != errors.ErrServerError { diff --git a/cmd/create_loadbalancer_prefix.go b/cmd/create_loadbalancer_prefix.go index cd8cd9b..5c4af0a 100644 --- a/cmd/create_loadbalancer_prefix.go +++ b/cmd/create_loadbalancer_prefix.go @@ -94,7 +94,9 @@ func RunCreateLoadBalancerPrefix( lbprefix, err := client.CreateLoadBalancerPrefix(ctx, &api.Prefix{ PrefixMeta: api.PrefixMeta{ InterfaceID: opts.InterfaceID, - Prefix: opts.Prefix, + }, + Spec: api.PrefixSpec{ + Prefix: opts.Prefix, }, }) if err != nil && err != errors.ErrServerError { @@ -102,6 +104,6 @@ func RunCreateLoadBalancerPrefix( } lbprefix.TypeMeta.Kind = "LoadBalancerPrefix" lbprefix.PrefixMeta.InterfaceID = opts.InterfaceID - lbprefix.PrefixMeta.Prefix = opts.Prefix + lbprefix.Spec.Prefix = opts.Prefix return rendererFactory.RenderObject("added", os.Stdout, lbprefix) } diff --git a/cmd/delete_firewall_rule.go b/cmd/delete_firewall_rule.go index df161e6..978f669 100644 --- a/cmd/delete_firewall_rule.go +++ b/cmd/delete_firewall_rule.go @@ -88,6 +88,6 @@ func RunDeleteFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFact fwrule.TypeMeta.Kind = api.FirewallRuleKind fwrule.FirewallRuleMeta.InterfaceID = opts.InterfaceID - fwrule.FirewallRuleMeta.RuleID = opts.RuleID + fwrule.Spec.RuleID = opts.RuleID return rendererFactory.RenderObject("deleted", os.Stdout, fwrule) } diff --git a/cmd/delete_loadbalancer_prefix.go b/cmd/delete_loadbalancer_prefix.go index d1c0fad..c744ad6 100644 --- a/cmd/delete_loadbalancer_prefix.go +++ b/cmd/delete_loadbalancer_prefix.go @@ -89,6 +89,6 @@ func RunDeleteLoadBalancerPrefix(ctx context.Context, dpdkClientFactory DPDKClie lbprefix.TypeMeta.Kind = "LoadBalancerPrefix" lbprefix.PrefixMeta.InterfaceID = opts.InterfaceID - lbprefix.PrefixMeta.Prefix = opts.Prefix + lbprefix.Spec.Prefix = opts.Prefix return rendererFactory.RenderObject("deleted", os.Stdout, lbprefix) } diff --git a/cmd/delete_prefix.go b/cmd/delete_prefix.go index 389b8bd..5eceb12 100644 --- a/cmd/delete_prefix.go +++ b/cmd/delete_prefix.go @@ -90,6 +90,6 @@ func RunDeletePrefix(ctx context.Context, dpdkClientFactory DPDKClientFactory, r prefix.TypeMeta.Kind = api.PrefixKind prefix.PrefixMeta.InterfaceID = opts.InterfaceID - prefix.PrefixMeta.Prefix = opts.Prefix + prefix.Spec.Prefix = opts.Prefix return rendererFactory.RenderObject("deleted", os.Stdout, prefix) } diff --git a/cmd/delete_route.go b/cmd/delete_route.go index e06b791..dba3c7c 100644 --- a/cmd/delete_route.go +++ b/cmd/delete_route.go @@ -90,6 +90,7 @@ func RunDeleteRoute(ctx context.Context, dpdkClientFactory DPDKClientFactory, re route.TypeMeta.Kind = api.RouteKind route.RouteMeta.VNI = opts.VNI - route.RouteMeta.Prefix = opts.Prefix + route.Spec.Prefix = &opts.Prefix + route.Spec.NextHop = &api.RouteNextHop{} return rendererFactory.RenderObject("deleted", os.Stdout, route) } diff --git a/cmd/get_firewall_rule.go b/cmd/get_firewall_rule.go index 4078a20..384738c 100644 --- a/cmd/get_firewall_rule.go +++ b/cmd/get_firewall_rule.go @@ -93,6 +93,6 @@ func RunGetFirewallRule( fwrule.TypeMeta.Kind = api.FirewallRuleKind fwrule.FirewallRuleMeta.InterfaceID = opts.InterfaceID - fwrule.FirewallRuleMeta.RuleID = opts.RuleID + fwrule.Spec.RuleID = opts.RuleID return rendererFactory.RenderObject("", os.Stdout, fwrule) } diff --git a/cmd/get_loadbalancer_target.go b/cmd/list_loadbalancer_targets.go similarity index 96% rename from cmd/get_loadbalancer_target.go rename to cmd/list_loadbalancer_targets.go index 3343c56..237d426 100644 --- a/cmd/get_loadbalancer_target.go +++ b/cmd/list_loadbalancer_targets.go @@ -32,9 +32,9 @@ func ListLoadBalancerTargets(dpdkClientFactory DPDKClientFactory, rendererFactor ) cmd := &cobra.Command{ - Use: "lbtarget <--lb-id>", + Use: "lbtargets <--lb-id>", Short: "List LoadBalancer Targets", - Example: "dpservice-cli list lbtarget --lb-id=1", + Example: "dpservice-cli list lbtargets --lb-id=1", Aliases: LoadBalancerTargetAliases, Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index 5904b89..44cd1a5 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -189,9 +189,9 @@ func ProtoVirtualIPToVirtualIP(interfaceID string, dpdkVIP *proto.InterfaceVIPIP }, VirtualIPMeta: VirtualIPMeta{ InterfaceID: interfaceID, - IP: ip, }, Spec: VirtualIPSpec{ + IP: ip, UnderlayRoute: &underlayRoute, }, }, nil @@ -216,9 +216,11 @@ func ProtoPrefixToPrefix(interfaceID string, dpdkPrefix *proto.Prefix) (*Prefix, }, PrefixMeta: PrefixMeta{ InterfaceID: interfaceID, - Prefix: prefix, }, - Spec: PrefixSpec{UnderlayRoute: &underlayRoute}, + Spec: PrefixSpec{ + Prefix: prefix, + UnderlayRoute: &underlayRoute, + }, }, nil } @@ -240,14 +242,13 @@ func ProtoRouteToRoute(vni uint32, dpdkRoute *proto.Route) (*Route, error) { RouteKind, }, RouteMeta: RouteMeta{ - VNI: vni, - Prefix: prefix, - NextHop: RouteNextHop{ + VNI: vni, + }, + Spec: RouteSpec{Prefix: &prefix, + NextHop: &RouteNextHop{ VNI: dpdkRoute.GetNexthopVNI(), IP: &nextHopIP, - }, - }, - Spec: RouteSpec{}, + }}, }, nil } @@ -330,9 +331,9 @@ func ProtoFwRuleToFwRule(dpdkFwRule *proto.FirewallRule, interfaceID string) (*F TypeMeta: TypeMeta{Kind: FirewallRuleKind}, FirewallRuleMeta: FirewallRuleMeta{ InterfaceID: interfaceID, - RuleID: string(dpdkFwRule.RuleID), }, Spec: FirewallRuleSpec{ + RuleID: string(dpdkFwRule.RuleID), TrafficDirection: direction, FirewallAction: action, Priority: dpdkFwRule.Priority, diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 9990c52..041808f 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -76,13 +76,11 @@ type Route struct { } type RouteMeta struct { - VNI uint32 `json:"vni"` - Prefix netip.Prefix `json:"prefix"` - NextHop RouteNextHop `json:"nextHop"` + VNI uint32 `json:"vni"` } -func (m *RouteMeta) GetName() string { - return fmt.Sprintf("%s-%d:%s", m.Prefix, m.NextHop.VNI, m.NextHop.IP) +func (m *Route) GetName() string { + return fmt.Sprintf("%s-%d:%s", m.Spec.Prefix, m.Spec.NextHop.VNI, m.Spec.NextHop.IP) } func (m *Route) GetStatus() int32 { @@ -90,10 +88,12 @@ func (m *Route) GetStatus() int32 { } type RouteSpec struct { + Prefix *netip.Prefix `json:"prefix,omitempty"` + NextHop *RouteNextHop `json:"nextHop,omitempty"` } type RouteNextHop struct { - VNI uint32 `json:"vni"` + VNI uint32 `json:"vni,omitempty"` IP *netip.Addr `json:"ip,omitempty"` } @@ -124,12 +124,11 @@ type Prefix struct { } type PrefixMeta struct { - InterfaceID string `json:"interfaceID"` - Prefix netip.Prefix `json:"prefix"` + InterfaceID string `json:"interfaceID"` } -func (m *PrefixMeta) GetName() string { - return m.Prefix.String() +func (m *Prefix) GetName() string { + return m.Spec.Prefix.String() } func (m *Prefix) GetStatus() int32 { @@ -137,7 +136,8 @@ func (m *Prefix) GetStatus() int32 { } type PrefixSpec struct { - UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` + Prefix netip.Prefix `json:"prefix"` + UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` } type VirtualIP struct { @@ -148,12 +148,11 @@ type VirtualIP struct { } type VirtualIPMeta struct { - InterfaceID string `json:"interfaceID"` - IP netip.Addr `json:"ip"` + InterfaceID string `json:"interfaceID"` } -func (m *VirtualIPMeta) GetName() string { - return m.IP.String() +func (m *VirtualIP) GetName() string { + return m.Spec.IP.String() } func (m *VirtualIP) GetStatus() int32 { @@ -161,6 +160,7 @@ func (m *VirtualIP) GetStatus() int32 { } type VirtualIPSpec struct { + IP netip.Addr `json:"ip"` UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` } @@ -247,8 +247,7 @@ type Interface struct { } type InterfaceMeta struct { - ID string `json:"id"` - PXE *PXE `json:"pxe,omitempty"` + ID string `json:"id"` } type PXE struct { @@ -270,6 +269,7 @@ type InterfaceSpec struct { IPs []netip.Addr `json:"ips,omitempty"` UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` VirtualFunction *VirtualFunction `json:"virtualFunction,omitempty"` + PXE *PXE `json:"pxe,omitempty"` } type VirtualFunction struct { @@ -385,11 +385,10 @@ type FirewallRule struct { type FirewallRuleMeta struct { InterfaceID string `json:"interfaceID"` - RuleID string `json:"ruleID"` } -func (m *FirewallRuleMeta) GetName() string { - return m.InterfaceID + "/" + m.RuleID +func (m *FirewallRule) GetName() string { + return m.FirewallRuleMeta.InterfaceID + "/" + m.Spec.RuleID } func (m *FirewallRule) GetStatus() int32 { @@ -397,6 +396,7 @@ func (m *FirewallRule) GetStatus() int32 { } type FirewallRuleSpec struct { + RuleID string `json:"ruleID"` TrafficDirection string `json:"trafficDirection,omitempty"` FirewallAction string `json:"firewallAction,omitempty"` Priority uint32 `json:"priority,omitempty"` diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 581df3b..58324e5 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -168,9 +168,9 @@ func (c *client) CreateLoadBalancerPrefix(ctx context.Context, prefix *api.Prefi InterfaceID: []byte(prefix.InterfaceID), }, Prefix: &dpdkproto.Prefix{ - IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Prefix.Addr()), - Address: []byte(prefix.Prefix.Addr().String()), - PrefixLength: uint32(prefix.Prefix.Bits()), + IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Spec.Prefix.Addr()), + Address: []byte(prefix.Spec.Prefix.Addr().String()), + PrefixLength: uint32(prefix.Spec.Prefix.Bits()), }, }) if err != nil { @@ -317,8 +317,8 @@ func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*ap Ipv6Config: api.NetIPAddrToProtoIPConfig(netiputil.FindIPv6(iface.Spec.IPs)), DeviceName: iface.Spec.Device, } - req.Ipv4Config.PxeConfig = &dpdkproto.PXEConfig{NextServer: iface.PXE.Server, BootFileName: iface.PXE.FileName} - req.Ipv6Config.PxeConfig = &dpdkproto.PXEConfig{NextServer: iface.PXE.Server, BootFileName: iface.PXE.FileName} + req.Ipv4Config.PxeConfig = &dpdkproto.PXEConfig{NextServer: iface.Spec.PXE.Server, BootFileName: iface.Spec.PXE.FileName} + req.Ipv6Config.PxeConfig = &dpdkproto.PXEConfig{NextServer: iface.Spec.PXE.Server, BootFileName: iface.Spec.PXE.FileName} res, err := c.DPDKonmetalClient.CreateInterface(ctx, &req) if err != nil { @@ -348,6 +348,7 @@ func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*ap Slot: res.Vf.Slot, Function: res.Vf.Function, }, + PXE: &api.PXE{Server: iface.Spec.PXE.Server, FileName: iface.Spec.PXE.FileName}, }, Status: api.ProtoStatusToStatus(res.Response.Status), }, nil @@ -381,8 +382,8 @@ func (c *client) AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*a res, err := c.DPDKonmetalClient.AddInterfaceVIP(ctx, &dpdkproto.InterfaceVIPMsg{ InterfaceID: []byte(virtualIP.InterfaceID), InterfaceVIPIP: &dpdkproto.InterfaceVIPIP{ - IpVersion: api.NetIPAddrToProtoIPVersion(virtualIP.IP), - Address: []byte(virtualIP.IP.String()), + IpVersion: api.NetIPAddrToProtoIPVersion(virtualIP.Spec.IP), + Address: []byte(virtualIP.Spec.IP.String()), }, }) if err != nil { @@ -448,9 +449,9 @@ func (c *client) AddPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix InterfaceID: []byte(prefix.InterfaceID), }, Prefix: &dpdkproto.Prefix{ - IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Prefix.Addr()), - Address: []byte(prefix.Prefix.Addr().String()), - PrefixLength: uint32(prefix.Prefix.Bits()), + IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Spec.Prefix.Addr()), + Address: []byte(prefix.Spec.Prefix.Addr().String()), + PrefixLength: uint32(prefix.Spec.Prefix.Bits()), }, }) if err != nil { @@ -495,15 +496,15 @@ func (c *client) AddRoute(ctx context.Context, route *api.Route) (*api.Route, er res, err := c.DPDKonmetalClient.AddRoute(ctx, &dpdkproto.VNIRouteMsg{ Vni: &dpdkproto.VNIMsg{Vni: route.VNI}, Route: &dpdkproto.Route{ - IpVersion: api.NetIPAddrToProtoIPVersion(*route.NextHop.IP), + IpVersion: api.NetIPAddrToProtoIPVersion(*route.Spec.NextHop.IP), Weight: 100, Prefix: &dpdkproto.Prefix{ - IpVersion: api.NetIPAddrToProtoIPVersion(route.Prefix.Addr()), - Address: []byte(route.Prefix.Addr().String()), - PrefixLength: uint32(route.Prefix.Bits()), + IpVersion: api.NetIPAddrToProtoIPVersion(route.Spec.Prefix.Addr()), + Address: []byte(route.Spec.Prefix.Addr().String()), + PrefixLength: uint32(route.Spec.Prefix.Bits()), }, - NexthopVNI: route.NextHop.VNI, - NexthopAddress: []byte(route.NextHop.IP.String()), + NexthopVNI: route.Spec.NextHop.VNI, + NexthopAddress: []byte(route.Spec.NextHop.IP.String()), }, }) if err != nil { @@ -809,7 +810,7 @@ func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) req := dpdkproto.AddFirewallRuleRequest{ InterfaceID: []byte(fwRule.FirewallRuleMeta.InterfaceID), Rule: &dpdkproto.FirewallRule{ - RuleID: []byte(fwRule.FirewallRuleMeta.RuleID), + RuleID: []byte(fwRule.Spec.RuleID), Direction: dpdkproto.TrafficDirection(direction), Action: dpdkproto.FirewallAction(action), Priority: fwRule.Spec.Priority, @@ -839,7 +840,6 @@ func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) return &api.FirewallRule{ TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, FirewallRuleMeta: api.FirewallRuleMeta{ - RuleID: string(res.RuleID), InterfaceID: fwRule.InterfaceID, }, Spec: fwRule.Spec, diff --git a/dpdk/client/dynamic/dynamic.go b/dpdk/client/dynamic/dynamic.go index c68e87c..4fd7ed6 100644 --- a/dpdk/client/dynamic/dynamic.go +++ b/dpdk/client/dynamic/dynamic.go @@ -99,14 +99,14 @@ func ObjectKeyFromObject(obj any) ObjectKey { case *api.Prefix: return PrefixKey{ InterfaceID: obj.InterfaceID, - Prefix: obj.Prefix, + Prefix: obj.Spec.Prefix, } case *api.Route: return RouteKey{ VNI: obj.VNI, - Prefix: obj.Prefix, - NextHopVNI: obj.NextHop.VNI, - NextHopIP: *obj.NextHop.IP, + Prefix: *obj.Spec.Prefix, + NextHopVNI: obj.Spec.NextHop.VNI, + NextHopIP: *obj.Spec.NextHop.IP, } case *api.VirtualIP: return VirtualIPKey{ @@ -171,10 +171,10 @@ func (c *client) Delete(ctx context.Context, obj any) error { _, err := c.structured.DeleteInterface(ctx, obj.ID) return err case *api.Prefix: - _, err := c.structured.DeletePrefix(ctx, obj.InterfaceID, obj.Prefix) + _, err := c.structured.DeletePrefix(ctx, obj.InterfaceID, obj.Spec.Prefix) return err case *api.Route: - _, err := c.structured.DeleteRoute(ctx, obj.VNI, obj.Prefix) + _, err := c.structured.DeleteRoute(ctx, obj.VNI, *obj.Spec.Prefix) return err case *api.VirtualIP: _, err := c.structured.DeleteVirtualIP(ctx, obj.InterfaceID) diff --git a/renderer/renderer.go b/renderer/renderer.go index 8b4c4fb..2adf962 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -262,7 +262,7 @@ func (t defaultTableConverter) prefixTable(prefixes []api.Prefix) (*TableData, e } columns := make([][]any, len(prefixes)) for i, prefix := range prefixes { - columns[i] = []any{prefix.Prefix, prefix.Spec.UnderlayRoute} + columns[i] = []any{prefix.Spec.Prefix, prefix.Spec.UnderlayRoute} if len(prefixes) == 1 { columns[i] = append(columns[i], prefix.Status.String()) } @@ -281,7 +281,7 @@ func (t defaultTableConverter) routeTable(routes []api.Route) (*TableData, error } columns := make([][]any, len(routes)) for i, route := range routes { - columns[i] = []any{route.Prefix, route.VNI, route.NextHop.VNI, route.NextHop.IP} + columns[i] = []any{route.Spec.Prefix, route.VNI, route.Spec.NextHop.VNI, route.Spec.NextHop.IP} if len(routes) == 1 { columns[i] = append(columns[i], route.Status.String()) } @@ -298,7 +298,7 @@ func (t defaultTableConverter) virtualIPTable(virtualIPs []api.VirtualIP) (*Tabl columns := make([][]any, len(virtualIPs)) for i, virtualIP := range virtualIPs { - columns[i] = []any{virtualIP.InterfaceID, virtualIP.IP, virtualIP.Spec.UnderlayRoute, virtualIP.Status.String()} + columns[i] = []any{virtualIP.InterfaceID, virtualIP.Spec.IP, virtualIP.Spec.UnderlayRoute, virtualIP.Status.String()} } return &TableData{ @@ -308,25 +308,20 @@ func (t defaultTableConverter) virtualIPTable(virtualIPs []api.VirtualIP) (*Tabl } func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { - // TODO clean up table output - //var headers []any - /*if len(nats) != 0 && nats[0].Spec.UnderlayRoute == nil { - headers = []any{"NatIP", "MinPort", "MaxPort", "Status"} - } else if len(nats) != 0 && nats[0].NatMeta.InterfaceID == "" { + var headers []any + if nats[0].InterfaceID != "" { + headers = []any{"InterfaceID", "NatIP", "MinPort", "MaxPort", "UnderlayRoute", "Status"} + } else { headers = []any{"NatIP", "MinPort", "MaxPort", "UnderlayRoute", "Status"} - } else {*/ - headers := []any{"InterfaceID", "NatIP", "MinPort", "MaxPort", "UnderlayRoute", "Status"} - //} + } columns := make([][]any, len(nats)) for i, nat := range nats { - /*if len(nats) != 0 && nats[0].Spec.UnderlayRoute == nil { - columns[i] = []any{nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Status.String()} - } else if len(nats) != 0 && nats[0].NatMeta.InterfaceID == "" { + if nats[0].InterfaceID != "" { + columns[i] = []any{nat.NatMeta.InterfaceID, nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute, nat.Status.String()} + } else { columns[i] = []any{nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute, nat.Status.String()} - } else {*/ - columns[i] = []any{nat.NatMeta.InterfaceID, nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute, nat.Status.String()} - //} + } } return &TableData{ @@ -361,7 +356,7 @@ func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableDa for i, fwrule := range fwrules { columns[i] = []any{ fwrule.FirewallRuleMeta.InterfaceID, - fwrule.FirewallRuleMeta.RuleID, + fwrule.Spec.RuleID, fwrule.Spec.TrafficDirection, fwrule.Spec.SourcePrefix, fwrule.Spec.DestinationPrefix, From 6c4b2776cd068cffa1691579a5e895a12973b300 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 1 Jun 2023 12:02:41 +0200 Subject: [PATCH 49/65] changes in output of commands --- cmd/add.go | 2 +- cmd/add_firewall_rule.go | 2 -- cmd/add_nat.go | 2 +- cmd/add_neighbor_nat.go | 2 +- cmd/add_prefix.go | 2 +- cmd/add_virtualip.go | 2 +- cmd/common.go | 6 ++-- cmd/create_interface.go | 2 +- cmd/create_loadbalancer.go | 2 +- cmd/get.go | 2 +- cmd/list.go | 2 +- dpdk/api/types.go | 57 ++++++++++++++++++++------------------ 12 files changed, 42 insertions(+), 41 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index d88b288..1b9eed1 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -26,7 +26,7 @@ import ( ) func Add(factory DPDKClientFactory) *cobra.Command { - rendererOptions := &RendererOptions{Output: "table"} + rendererOptions := &RendererOptions{Output: "name"} sourcesOptions := &SourcesOptions{} cmd := &cobra.Command{ diff --git a/cmd/add_firewall_rule.go b/cmd/add_firewall_rule.go index 3a532da..ea9317d 100644 --- a/cmd/add_firewall_rule.go +++ b/cmd/add_firewall_rule.go @@ -114,7 +114,6 @@ func (o *AddFirewallRuleOptions) AddFlags(fs *pflag.FlagSet) { } func (o *AddFirewallRuleOptions) MarkRequiredFlags(cmd *cobra.Command) error { - // TODO if protocol is not specified it should match all protocols for _, name := range []string{"interface-id", "rule-id", "direction", "action", "ipver", "src", "dst"} { if err := cmd.MarkFlagRequired(name); err != nil { return err @@ -139,7 +138,6 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory return fmt.Errorf("error parsing dst prefix: %w", err) } - // TODO add cases if icmp type or code is -1 var protocolFilter dpdkproto.ProtocolFilter switch opts.ProtocolFilter { case "icmp", "1": diff --git a/cmd/add_nat.go b/cmd/add_nat.go index ef721f7..a77cffc 100644 --- a/cmd/add_nat.go +++ b/cmd/add_nat.go @@ -103,5 +103,5 @@ func RunAddNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendere nat.TypeMeta.Kind = api.NatKind nat.NatMeta.InterfaceID = opts.InterfaceID - return rendererFactory.RenderObject("added", os.Stdout, nat) + return rendererFactory.RenderObject(fmt.Sprintf("added, underlay route: %s", nat.Spec.UnderlayRoute), os.Stdout, nat) } diff --git a/cmd/add_neighbor_nat.go b/cmd/add_neighbor_nat.go index e64a23c..a960810 100644 --- a/cmd/add_neighbor_nat.go +++ b/cmd/add_neighbor_nat.go @@ -107,5 +107,5 @@ func RunAddNeighborNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, nnat.TypeMeta.Kind = api.NeighborNatKind nnat.NeighborNatMeta.NatVIPIP = &opts.NatIP - return rendererFactory.RenderObject("added", os.Stdout, nnat) + return rendererFactory.RenderObject(fmt.Sprintf("added, underlay route: %s", nnat.Spec.UnderlayRoute), os.Stdout, nnat) } diff --git a/cmd/add_prefix.go b/cmd/add_prefix.go index 68f3d46..c1a29c7 100644 --- a/cmd/add_prefix.go +++ b/cmd/add_prefix.go @@ -106,5 +106,5 @@ func RunAddPrefix( prefix.TypeMeta.Kind = api.PrefixKind prefix.PrefixMeta.InterfaceID = opts.InterfaceID prefix.Spec.Prefix = opts.Prefix - return rendererFactory.RenderObject("added", os.Stdout, prefix) + return rendererFactory.RenderObject(fmt.Sprintf("added, underlay route: %s", prefix.Spec.UnderlayRoute), os.Stdout, prefix) } diff --git a/cmd/add_virtualip.go b/cmd/add_virtualip.go index e33230c..0582903 100644 --- a/cmd/add_virtualip.go +++ b/cmd/add_virtualip.go @@ -103,5 +103,5 @@ func RunAddVirtualIP( virtualIP.TypeMeta.Kind = api.RouteKind virtualIP.Spec.IP = opts.Vip virtualIP.VirtualIPMeta.InterfaceID = opts.InterfaceID - return rendererFactory.RenderObject("added", os.Stdout, virtualIP) + return rendererFactory.RenderObject(fmt.Sprintf("added, underlay route: %s", virtualIP.Spec.UnderlayRoute), os.Stdout, virtualIP) } diff --git a/cmd/common.go b/cmd/common.go index b9929cf..416bd4d 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -141,8 +141,8 @@ func (o *RendererOptions) NewRenderer(operation string, w io.Writer) (renderer.R } func (o *RendererOptions) RenderObject(operation string, w io.Writer, obj api.Object) error { - if obj.GetStatus() != 0 { - operation = "server error" + if obj.GetStatus().Error != 0 { + operation = fmt.Sprintf("server error: %d, %s", obj.GetStatus().Error, obj.GetStatus().Message) } renderer, err := o.NewRenderer(operation, w) if err != nil { @@ -151,7 +151,7 @@ func (o *RendererOptions) RenderObject(operation string, w io.Writer, obj api.Ob if err := renderer.Render(obj); err != nil { return fmt.Errorf("error rendering %s: %w", obj.GetKind(), err) } - if obj.GetStatus() != 0 { + if obj.GetStatus().Error != 0 { return fmt.Errorf(strconv.Itoa(apierrors.SERVER_ERROR)) } return nil diff --git a/cmd/create_interface.go b/cmd/create_interface.go index bb23842..e27558d 100644 --- a/cmd/create_interface.go +++ b/cmd/create_interface.go @@ -107,5 +107,5 @@ func RunCreateInterface(ctx context.Context, dpdkClientFactory DPDKClientFactory iface.TypeMeta.Kind = api.InterfaceKind iface.InterfaceMeta.ID = opts.ID - return rendererFactory.RenderObject("added", os.Stdout, iface) + return rendererFactory.RenderObject(fmt.Sprintf("added, underlay route: %s", iface.Spec.UnderlayRoute), os.Stdout, iface) } diff --git a/cmd/create_loadbalancer.go b/cmd/create_loadbalancer.go index 7ee948f..cd87e4b 100644 --- a/cmd/create_loadbalancer.go +++ b/cmd/create_loadbalancer.go @@ -111,5 +111,5 @@ func RunCreateLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFact lb.TypeMeta.Kind = api.LoadBalancerKind lb.LoadBalancerMeta.ID = opts.Id - return rendererFactory.RenderObject("added", os.Stdout, lb) + return rendererFactory.RenderObject(fmt.Sprintf("added, underlay route: %s", lb.Spec.UnderlayRoute), os.Stdout, lb) } diff --git a/cmd/get.go b/cmd/get.go index 5c881e7..db1cc1e 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -21,7 +21,7 @@ import ( ) func Get(factory DPDKClientFactory) *cobra.Command { - rendererOptions := &RendererOptions{} + rendererOptions := &RendererOptions{Output: "json"} cmd := &cobra.Command{ Use: "get", diff --git a/cmd/list.go b/cmd/list.go index 6ce49eb..1cbb43c 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -21,7 +21,7 @@ import ( ) func List(factory DPDKClientFactory) *cobra.Command { - rendererOptions := &RendererOptions{} + rendererOptions := &RendererOptions{Output: "table"} cmd := &cobra.Command{ Use: "list", diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 041808f..6133c30 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -25,7 +25,7 @@ import ( type Object interface { GetKind() string GetName() string - GetStatus() int32 + GetStatus() Status } type List interface { @@ -41,11 +41,14 @@ func (m *TypeMeta) GetKind() string { } type Status struct { - Error int32 `json:"error"` + Error int32 `json:"error,omitempty"` Message string `json:"message"` } func (status *Status) String() string { + if status.Error == 0 { + return status.Message + } return fmt.Sprintf("Error: %d, Message: %s", status.Error, status.Message) } @@ -83,8 +86,8 @@ func (m *Route) GetName() string { return fmt.Sprintf("%s-%d:%s", m.Spec.Prefix, m.Spec.NextHop.VNI, m.Spec.NextHop.IP) } -func (m *Route) GetStatus() int32 { - return m.Status.Error +func (m *Route) GetStatus() Status { + return m.Status } type RouteSpec struct { @@ -93,7 +96,7 @@ type RouteSpec struct { } type RouteNextHop struct { - VNI uint32 `json:"vni,omitempty"` + VNI uint32 `json:"vni"` IP *netip.Addr `json:"ip,omitempty"` } @@ -131,8 +134,8 @@ func (m *Prefix) GetName() string { return m.Spec.Prefix.String() } -func (m *Prefix) GetStatus() int32 { - return m.Status.Error +func (m *Prefix) GetStatus() Status { + return m.Status } type PrefixSpec struct { @@ -155,8 +158,8 @@ func (m *VirtualIP) GetName() string { return m.Spec.IP.String() } -func (m *VirtualIP) GetStatus() int32 { - return m.Status.Error +func (m *VirtualIP) GetStatus() Status { + return m.Status } type VirtualIPSpec struct { @@ -180,8 +183,8 @@ func (m *LoadBalancerMeta) GetName() string { return m.ID } -func (m *LoadBalancer) GetStatus() int32 { - return m.Status.Error +func (m *LoadBalancer) GetStatus() Status { + return m.Status } type LoadBalancerSpec struct { @@ -211,8 +214,8 @@ func (m *LoadBalancerTargetMeta) GetName() string { return m.LoadbalancerID } -func (m *LoadBalancerTarget) GetStatus() int32 { - return m.Status.Error +func (m *LoadBalancerTarget) GetStatus() Status { + return m.Status } type LoadBalancerTargetSpec struct { @@ -259,8 +262,8 @@ func (m *InterfaceMeta) GetName() string { return m.ID } -func (m *Interface) GetStatus() int32 { - return m.Status.Error +func (m *Interface) GetStatus() Status { + return m.Status } type InterfaceSpec struct { @@ -318,8 +321,8 @@ func (m *NatMeta) GetName() string { return m.InterfaceID } -func (m *Nat) GetStatus() int32 { - return m.Status.Error +func (m *Nat) GetStatus() Status { + return m.Status } type NatSpec struct { @@ -364,8 +367,8 @@ func (m *NeighborNatMeta) GetName() string { return m.NatVIPIP.String() } -func (m *NeighborNat) GetStatus() int32 { - return m.Status.Error +func (m *NeighborNat) GetStatus() Status { + return m.Status } type NeighborNatSpec struct { @@ -391,8 +394,8 @@ func (m *FirewallRule) GetName() string { return m.FirewallRuleMeta.InterfaceID + "/" + m.Spec.RuleID } -func (m *FirewallRule) GetStatus() int32 { - return m.Status.Error +func (m *FirewallRule) GetStatus() Status { + return m.Status } type FirewallRuleSpec struct { @@ -442,8 +445,8 @@ func (m *InitMeta) GetName() string { return "init" } -func (m *Init) GetStatus() int32 { - return m.Status.Error +func (m *Init) GetStatus() Status { + return m.Status } type Initialized struct { @@ -464,8 +467,8 @@ func (m *InitializedMeta) GetName() string { return "initialized" } -func (m *Initialized) GetStatus() int32 { - return 0 +func (m *Initialized) GetStatus() Status { + return Status{} } type Vni struct { @@ -488,8 +491,8 @@ func (m *VniMeta) GetName() string { return "vni" } -func (m *Vni) GetStatus() int32 { - return 0 +func (m *Vni) GetStatus() Status { + return Status{} } var ( From 504070bf01ce0800271198d25b8b345abf112c46 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 1 Jun 2023 12:17:19 +0200 Subject: [PATCH 50/65] change Status to always show error code --- dpdk/api/types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 6133c30..2fe54ee 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -41,7 +41,7 @@ func (m *TypeMeta) GetKind() string { } type Status struct { - Error int32 `json:"error,omitempty"` + Error int32 `json:"error"` Message string `json:"message"` } From 472fd40f7fc6fddced0cfa13abbe99d74678d22c Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 1 Jun 2023 14:19:43 +0200 Subject: [PATCH 51/65] change the error format of table output --- cmd/common.go | 3 +++ cmd/get.go | 2 +- dpdk/client/client.go | 6 ++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/cmd/common.go b/cmd/common.go index 416bd4d..4789fa8 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -143,6 +143,9 @@ func (o *RendererOptions) NewRenderer(operation string, w io.Writer) (renderer.R func (o *RendererOptions) RenderObject(operation string, w io.Writer, obj api.Object) error { if obj.GetStatus().Error != 0 { operation = fmt.Sprintf("server error: %d, %s", obj.GetStatus().Error, obj.GetStatus().Message) + if o.Output == "table" { + o.Output = "name" + } } renderer, err := o.NewRenderer(operation, w) if err != nil { diff --git a/cmd/get.go b/cmd/get.go index db1cc1e..f0c9a83 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -21,7 +21,7 @@ import ( ) func Get(factory DPDKClientFactory) *cobra.Command { - rendererOptions := &RendererOptions{Output: "json"} + rendererOptions := &RendererOptions{Output: "table"} cmd := &cobra.Command{ Use: "get", diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 58324e5..721a953 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -317,8 +317,10 @@ func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*ap Ipv6Config: api.NetIPAddrToProtoIPConfig(netiputil.FindIPv6(iface.Spec.IPs)), DeviceName: iface.Spec.Device, } - req.Ipv4Config.PxeConfig = &dpdkproto.PXEConfig{NextServer: iface.Spec.PXE.Server, BootFileName: iface.Spec.PXE.FileName} - req.Ipv6Config.PxeConfig = &dpdkproto.PXEConfig{NextServer: iface.Spec.PXE.Server, BootFileName: iface.Spec.PXE.FileName} + if iface.Spec.PXE.FileName != "" && iface.Spec.PXE.Server != "" { + req.Ipv4Config.PxeConfig = &dpdkproto.PXEConfig{NextServer: iface.Spec.PXE.Server, BootFileName: iface.Spec.PXE.FileName} + req.Ipv6Config.PxeConfig = &dpdkproto.PXEConfig{NextServer: iface.Spec.PXE.Server, BootFileName: iface.Spec.PXE.FileName} + } res, err := c.DPDKonmetalClient.CreateInterface(ctx, &req) if err != nil { From 41f76470e813e2c488c2b4d1f12990732c7f1c41 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Fri, 2 Jun 2023 16:50:46 +0200 Subject: [PATCH 52/65] change in table output, add --wide global flag --- .vscode/launch.json | 3 +- cmd/add_loadbalancer_target.go | 2 - cmd/add_route.go | 3 +- cmd/add_virtualip.go | 2 +- cmd/common.go | 4 ++ cmd/list_nats.go | 8 ++-- dpdk/api/types.go | 8 ++-- dpdk/client/client.go | 10 +++- renderer/renderer.go | 86 ++++++++++++++++------------------ 9 files changed, 65 insertions(+), 61 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 345e5db..cf68dc6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,7 +13,7 @@ //"program": "${fileDirname}", //"args": ["add", "loadbalancer", "--id=4", "--lbports", "tcp/389", "--vip", "10.3.4.5", "--vni", "100"] //"args": ["delete", "loadbalancer", "--id=4"] - "args": ["add", "route", "--prefix=10.100.3.0/24", "--next-hop-vni=0", "--next-hop-ip=fc00:2::64:0:1", "--vni=100"] + //"args": ["add", "route", "--prefix=10.100.3.0/24", "--next-hop-vni=0", "--next-hop-ip=fc00:2::64:0:1", "--vni=100"] //"args": ["delete", "route", "10.200.2.0/24", "0", "ff80::1", "--vni", "100"] //"args": ["add", "lbprefix", "1.1.1.1/24", "--interface-id", "vm1"] //"args": ["add", "lbtarget", "--target-ip=ff80::5", "--lb-id=1"] @@ -29,6 +29,7 @@ //"args": ["list", "nats"] //"args": ["get", "vni", "--vni=100", "--vni-type=0"] //"args": ["list", "prefixes","--interface-id=vm1"] + "args": ["list", "lbtargets","--lb-id=4"] } ] } \ No newline at end of file diff --git a/cmd/add_loadbalancer_target.go b/cmd/add_loadbalancer_target.go index 8ff8435..50ab308 100644 --- a/cmd/add_loadbalancer_target.go +++ b/cmd/add_loadbalancer_target.go @@ -100,7 +100,5 @@ func RunAddLoadBalancerTarget( return fmt.Errorf("error adding loadbalancer target: %w", err) } - lbtarget.TypeMeta.Kind = api.LoadBalancerKind - lbtarget.LoadBalancerTargetMeta.LoadbalancerID = opts.LoadBalancerID return rendererFactory.RenderObject("added", os.Stdout, lbtarget) } diff --git a/cmd/add_route.go b/cmd/add_route.go index c03c062..76fad49 100644 --- a/cmd/add_route.go +++ b/cmd/add_route.go @@ -109,6 +109,5 @@ func RunAddRoute( route.TypeMeta.Kind = api.RouteKind route.RouteMeta.VNI = opts.VNI route.Spec.Prefix = &opts.Prefix - route.Spec.NextHop = &api.RouteNextHop{} - return rendererFactory.RenderObject("added", os.Stdout, route) + return rendererFactory.RenderObject(fmt.Sprintf("added, Next Hop IP: %s", opts.NextHopIP), os.Stdout, route) } diff --git a/cmd/add_virtualip.go b/cmd/add_virtualip.go index 0582903..8f16770 100644 --- a/cmd/add_virtualip.go +++ b/cmd/add_virtualip.go @@ -100,7 +100,7 @@ func RunAddVirtualIP( return fmt.Errorf("error adding virtual ip: %w", err) } - virtualIP.TypeMeta.Kind = api.RouteKind + virtualIP.TypeMeta.Kind = api.VirtualIPKind virtualIP.Spec.IP = opts.Vip virtualIP.VirtualIPMeta.InterfaceID = opts.InterfaceID return rendererFactory.RenderObject(fmt.Sprintf("added, underlay route: %s", virtualIP.Spec.UnderlayRoute), os.Stdout, virtualIP) diff --git a/cmd/common.go b/cmd/common.go index 4789fa8..31e347f 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -64,6 +64,7 @@ func (o *DPDKClientOptions) NewClient(ctx context.Context) (client.Client, func( cleanup := conn.Close return c, cleanup, nil } + func DpdkClose(cleanup func() error) { if err := cleanup(); err != nil { fmt.Printf("error cleaning up client: %s", err) @@ -97,11 +98,13 @@ func CommandNames(cmds []*cobra.Command) []string { type RendererOptions struct { Output string Pretty bool + Wide bool } func (o *RendererOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVarP(&o.Output, "output", "o", o.Output, "Output format. [json|yaml|table|name]") fs.BoolVar(&o.Pretty, "pretty", o.Pretty, "Whether to render pretty output.") + fs.BoolVar(&o.Wide, "wide", o.Wide, "Whether to render more info.") } func (o *RendererOptions) NewRenderer(operation string, w io.Writer) (renderer.Renderer, error) { @@ -127,6 +130,7 @@ func (o *RendererOptions) NewRenderer(operation string, w io.Writer) (renderer.R } if err := registry.Register("table", func(w io.Writer) renderer.Renderer { + renderer.DefaultTableConverter.SetWide(o.Wide) return renderer.NewTable(w, renderer.DefaultTableConverter) }); err != nil { return nil, err diff --git a/cmd/list_nats.go b/cmd/list_nats.go index 1ea6f83..ffffd44 100644 --- a/cmd/list_nats.go +++ b/cmd/list_nats.go @@ -70,16 +70,16 @@ func RunListNats( return fmt.Errorf("error listing interfaces: %w", err) } interfaces := interfaceList.Items - nats := make([]api.Nat, len(interfaceList.Items)) - for i, iface := range interfaces { + var nats []api.Nat + for _, iface := range interfaces { nat, err := client.GetNat(ctx, iface.InterfaceMeta.ID) if err == errors.ErrServerError { - nat.InterfaceID = iface.InterfaceMeta.ID + continue } if err != nil && err != errors.ErrServerError { return fmt.Errorf("error getting nat: %w", err) } - nats[i] = *nat + nats = append(nats, *nat) } natList := api.NatList{ diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 2fe54ee..48d6362 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -83,7 +83,7 @@ type RouteMeta struct { } func (m *Route) GetName() string { - return fmt.Sprintf("%s-%d:%s", m.Spec.Prefix, m.Spec.NextHop.VNI, m.Spec.NextHop.IP) + return fmt.Sprintf("%s-%d", m.Spec.Prefix, m.Spec.NextHop.VNI) } func (m *Route) GetStatus() Status { @@ -155,7 +155,7 @@ type VirtualIPMeta struct { } func (m *VirtualIP) GetName() string { - return m.Spec.IP.String() + return "on interface: " + m.VirtualIPMeta.InterfaceID } func (m *VirtualIP) GetStatus() Status { @@ -210,8 +210,8 @@ type LoadBalancerTargetMeta struct { LoadbalancerID string `json:"loadbalancerId"` } -func (m *LoadBalancerTargetMeta) GetName() string { - return m.LoadbalancerID +func (m *LoadBalancerTarget) GetName() string { + return "on loadbalancer: " + m.LoadBalancerTargetMeta.LoadbalancerID } func (m *LoadBalancerTarget) GetStatus() Status { diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 721a953..2963761 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -250,7 +250,10 @@ func (c *client) AddLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBa return &api.LoadBalancerTarget{}, err } if errorCode := res.GetError(); errorCode != 0 { - return &api.LoadBalancerTarget{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError + return &api.LoadBalancerTarget{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, + LoadBalancerTargetMeta: lbtarget.LoadBalancerTargetMeta, + Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } return &api.LoadBalancerTarget{ @@ -513,7 +516,10 @@ func (c *client) AddRoute(ctx context.Context, route *api.Route) (*api.Route, er return &api.Route{}, err } if errorCode := res.GetError(); errorCode != 0 { - return &api.Route{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError + return &api.Route{ + Spec: api.RouteSpec{NextHop: &api.RouteNextHop{}}, + Status: api.ProtoStatusToStatus(res), + }, apierrors.ErrServerError } return &api.Route{ TypeMeta: api.TypeMeta{Kind: api.RouteKind}, diff --git a/renderer/renderer.go b/renderer/renderer.go index 2adf962..50b80f5 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -143,7 +143,13 @@ type TableConverter interface { ConvertToTable(v any) (*TableData, error) } -type defaultTableConverter struct{} +type defaultTableConverter struct { + Wide bool +} + +func (t *defaultTableConverter) SetWide(wide bool) { + t.Wide = wide +} var DefaultTableConverter = defaultTableConverter{} @@ -191,7 +197,7 @@ func (t defaultTableConverter) ConvertToTable(v any) (*TableData, error) { } func (t defaultTableConverter) loadBalancerTable(lb api.LoadBalancer) (*TableData, error) { - headers := []any{"ID", "VNI", "LbVipIP", "Lbports", "UnderlayRoute", "Status"} + headers := []any{"ID", "VNI", "LbVipIP", "Lbports", "UnderlayRoute"} columns := make([][]any, 1) @@ -200,7 +206,7 @@ func (t defaultTableConverter) loadBalancerTable(lb api.LoadBalancer) (*TableDat p := dpdkproto.Protocol_name[int32(port.Protocol)] + "/" + strconv.Itoa(int(port.Port)) ports = append(ports, p) } - columns[0] = []any{lb.ID, lb.Spec.VNI, lb.Spec.LbVipIP, ports, lb.Spec.UnderlayRoute, lb.Status.String()} + columns[0] = []any{lb.ID, lb.Spec.VNI, lb.Spec.LbVipIP, ports, lb.Spec.UnderlayRoute} return &TableData{ Headers: headers, @@ -209,15 +215,13 @@ func (t defaultTableConverter) loadBalancerTable(lb api.LoadBalancer) (*TableDat } func (t defaultTableConverter) loadBalancerTargetTable(lbtargets []api.LoadBalancerTarget) (*TableData, error) { - headers := []any{"LoadBalancerID", "IpVersion", "Address", "Status"} + headers := []any{"IpVersion", "Address"} columns := make([][]any, len(lbtargets)) for i, lbtarget := range lbtargets { columns[i] = []any{ - lbtarget.LoadBalancerTargetMeta.LoadbalancerID, api.NetIPAddrToProtoIPVersion(*lbtarget.Spec.TargetIP), lbtarget.Spec.TargetIP, - lbtarget.Status.String(), } } @@ -229,24 +233,22 @@ func (t defaultTableConverter) loadBalancerTargetTable(lbtargets []api.LoadBalan func (t defaultTableConverter) interfaceTable(ifaces []api.Interface) (*TableData, error) { var headers []any - if ifaces[0].Spec.VirtualFunction == nil { + if len(ifaces) > 0 && ifaces[0].Spec.VirtualFunction == nil { headers = []any{"ID", "VNI", "Device", "IPs", "UnderlayRoute"} } else { headers = []any{"ID", "VNI", "Device", "IPs", "UnderlayRoute", "VirtualFunction"} } - if len(ifaces) == 1 { - headers = append(headers, "Status") + if t.Wide { + headers = append(headers, "Wide") } + columns := make([][]any, len(ifaces)) for i, iface := range ifaces { - if ifaces[0].Spec.VirtualFunction == nil { + if len(ifaces) > 0 && ifaces[0].Spec.VirtualFunction == nil { columns[i] = []any{iface.ID, iface.Spec.VNI, iface.Spec.Device, iface.Spec.IPs, iface.Spec.UnderlayRoute} } else { columns[i] = []any{iface.ID, iface.Spec.VNI, iface.Spec.Device, iface.Spec.IPs, iface.Spec.UnderlayRoute, iface.Spec.VirtualFunction.String()} } - if len(ifaces) == 1 { - columns[i] = append(columns[i], iface.Status.String()) - } } return &TableData{ @@ -257,15 +259,10 @@ func (t defaultTableConverter) interfaceTable(ifaces []api.Interface) (*TableDat func (t defaultTableConverter) prefixTable(prefixes []api.Prefix) (*TableData, error) { headers := []any{"Prefix", "UnderlayRoute"} - if len(prefixes) == 1 { - headers = append(headers, "Status") - } + columns := make([][]any, len(prefixes)) for i, prefix := range prefixes { columns[i] = []any{prefix.Spec.Prefix, prefix.Spec.UnderlayRoute} - if len(prefixes) == 1 { - columns[i] = append(columns[i], prefix.Status.String()) - } } return &TableData{ @@ -276,15 +273,10 @@ func (t defaultTableConverter) prefixTable(prefixes []api.Prefix) (*TableData, e func (t defaultTableConverter) routeTable(routes []api.Route) (*TableData, error) { headers := []any{"Prefix", "VNI", "NextHopVNI", "NextHopIP"} - if len(routes) == 1 { - headers = append(headers, "Status") - } + columns := make([][]any, len(routes)) for i, route := range routes { columns[i] = []any{route.Spec.Prefix, route.VNI, route.Spec.NextHop.VNI, route.Spec.NextHop.IP} - if len(routes) == 1 { - columns[i] = append(columns[i], route.Status.String()) - } } return &TableData{ @@ -294,11 +286,11 @@ func (t defaultTableConverter) routeTable(routes []api.Route) (*TableData, error } func (t defaultTableConverter) virtualIPTable(virtualIPs []api.VirtualIP) (*TableData, error) { - headers := []any{"InterfaceID", "VirtualIP", "UnderlayRoute", "Status"} + headers := []any{"InterfaceID", "VirtualIP", "UnderlayRoute"} columns := make([][]any, len(virtualIPs)) for i, virtualIP := range virtualIPs { - columns[i] = []any{virtualIP.InterfaceID, virtualIP.Spec.IP, virtualIP.Spec.UnderlayRoute, virtualIP.Status.String()} + columns[i] = []any{virtualIP.InterfaceID, virtualIP.Spec.IP, virtualIP.Spec.UnderlayRoute} } return &TableData{ @@ -309,18 +301,27 @@ func (t defaultTableConverter) virtualIPTable(virtualIPs []api.VirtualIP) (*Tabl func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { var headers []any - if nats[0].InterfaceID != "" { - headers = []any{"InterfaceID", "NatIP", "MinPort", "MaxPort", "UnderlayRoute", "Status"} + // if command was nat + if len(nats) > 0 && nats[0].InterfaceID != "" { + headers = []any{"InterfaceID", "NatIP", "MinPort", "MaxPort", "UnderlayRoute"} + // if command was natinfo } else { - headers = []any{"NatIP", "MinPort", "MaxPort", "UnderlayRoute", "Status"} + headers = []any{"NatIP", "MinPort", "MaxPort", "UnderlayRoute", "NatInfoType"} } columns := make([][]any, len(nats)) for i, nat := range nats { - if nats[0].InterfaceID != "" { - columns[i] = []any{nat.NatMeta.InterfaceID, nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute, nat.Status.String()} + // if command was nat + if len(nats) > 0 && nats[0].InterfaceID != "" { + columns[i] = []any{nat.NatMeta.InterfaceID, nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute} + // if command was natinfo } else { - columns[i] = []any{nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute, nat.Status.String()} + columns[i] = []any{nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute} + if len(nats) > 0 && nats[0].Spec.UnderlayRoute == nil { + columns[i] = append(columns[i], "Local") + } else { + columns[i] = append(columns[i], "Neighbor") + } } } @@ -332,12 +333,12 @@ func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { func (t defaultTableConverter) neighborNatTable(nats []api.NeighborNat) (*TableData, error) { - headers := []any{"VNI", "NatIP", "MinPort", "MaxPort", "UnderlayRoute", "Status"} + headers := []any{"VNI", "NatIP", "MinPort", "MaxPort", "UnderlayRoute"} columns := make([][]any, len(nats)) for i, nat := range nats { - columns[i] = []any{nat.Spec.Vni, nat.NeighborNatMeta.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute, nat.Status.String()} + columns[i] = []any{nat.Spec.Vni, nat.NeighborNatMeta.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute} } @@ -349,9 +350,7 @@ func (t defaultTableConverter) neighborNatTable(nats []api.NeighborNat) (*TableD func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableData, error) { headers := []any{"InterfaceID", "RuleID", "Direction", "Src", "Dst", "Action", "Protocol", "Priority"} - if len(fwrules) == 1 { - headers = append(headers, "Status") - } + columns := make([][]any, len(fwrules)) for i, fwrule := range fwrules { columns[i] = []any{ @@ -364,9 +363,6 @@ func (t defaultTableConverter) fwruleTable(fwrules []api.FirewallRule) (*TableDa fwrule.Spec.ProtocolFilter.String(), fwrule.Spec.Priority, } - if len(fwrules) == 1 { - columns[i] = append(columns[i], fwrule.Status.String()) - } } return &TableData{ @@ -387,9 +383,9 @@ func (t defaultTableConverter) initTable(init api.Init) (*TableData, error) { } func (t defaultTableConverter) vniTable(vni api.Vni) (*TableData, error) { - headers := []any{"VNI", "VniType", "inUse", "Error", "Message"} + headers := []any{"VNI", "VniType", "inUse"} columns := make([][]any, 1) - columns[0] = []any{vni.VniMeta.VNI, vni.VniMeta.VniType, vni.Spec.InUse, vni.Status.Error, vni.Status.Message} + columns[0] = []any{vni.VniMeta.VNI, vni.VniMeta.VniType, vni.Spec.InUse} return &TableData{ Headers: headers, @@ -398,9 +394,9 @@ func (t defaultTableConverter) vniTable(vni api.Vni) (*TableData, error) { } func (t defaultTableConverter) initializedTable(initialized api.Initialized) (*TableData, error) { - headers := []any{"UUID", "Error", "Message"} + headers := []any{"UUID"} columns := make([][]any, 1) - columns[0] = []any{initialized.Spec.UUID, initialized.Status.Error, initialized.Status.Message} + columns[0] = []any{initialized.Spec.UUID} return &TableData{ Headers: headers, From eec6855d53b573d43e3f585b391af0c6c932128d Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Mon, 5 Jun 2023 14:00:59 +0200 Subject: [PATCH 53/65] add wide flag (more info) to interface table view --- .vscode/launch.json | 4 +- cmd/common.go | 7 ++- cmd/delete_route.go | 4 +- cmd/list_interfaces.go | 15 +++++++ docs/README.md | 2 +- docs/commands/README.md | 89 ++++++++++++++++++++++++++++++++++++++ docs/development/README.md | 4 +- dpdk/api/types.go | 6 +++ renderer/renderer.go | 59 +++++++++++++++++++------ 9 files changed, 169 insertions(+), 21 deletions(-) create mode 100644 docs/commands/README.md diff --git a/.vscode/launch.json b/.vscode/launch.json index cf68dc6..33943be 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -26,10 +26,10 @@ //"args": ["add", "interface", "--id=vm4", "--ip=10.200.1.4", "--ip=2000:200:1::4", "--vni=200", "--device=net_tap5"] //"args": ["add", "-f", "/tmp/addint.json"] //"args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=11"] - //"args": ["list", "nats"] + "args": ["list", "interfaces", "--wide"] //"args": ["get", "vni", "--vni=100", "--vni-type=0"] //"args": ["list", "prefixes","--interface-id=vm1"] - "args": ["list", "lbtargets","--lb-id=4"] + //"args": ["list", "lbtargets","--lb-id=4"] } ] } \ No newline at end of file diff --git a/cmd/common.go b/cmd/common.go index 31e347f..791dd55 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -104,7 +104,11 @@ type RendererOptions struct { func (o *RendererOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVarP(&o.Output, "output", "o", o.Output, "Output format. [json|yaml|table|name]") fs.BoolVar(&o.Pretty, "pretty", o.Pretty, "Whether to render pretty output.") - fs.BoolVar(&o.Wide, "wide", o.Wide, "Whether to render more info.") + fs.BoolVarP(&o.Wide, "wide", "w", o.Wide, "Whether to render more info in table output.") +} + +func (o *RendererOptions) GetWide() bool { + return o.Wide } func (o *RendererOptions) NewRenderer(operation string, w io.Writer) (renderer.Renderer, error) { @@ -182,6 +186,7 @@ type RendererFactory interface { NewRenderer(operation string, w io.Writer) (renderer.Renderer, error) RenderObject(operation string, w io.Writer, obj api.Object) error RenderList(operation string, w io.Writer, list api.List) error + GetWide() bool } type SourcesOptions struct { diff --git a/cmd/delete_route.go b/cmd/delete_route.go index dba3c7c..73789d4 100644 --- a/cmd/delete_route.go +++ b/cmd/delete_route.go @@ -34,9 +34,9 @@ func DeleteRoute(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFa ) cmd := &cobra.Command{ - Use: "route <--prefix> <--next-hop-vni> <--next-hop-ip> <--vni>", + Use: "route <--prefix> <--vni>", Short: "Delete a route", - Example: "dpservice-cli delete route --prefix=10.100.2.0/24 --next-hop-vni=0 --next-hop-ip=fc00:2::64:0:1 --vni=100", + Example: "dpservice-cli delete route --prefix=10.100.2.0/24 --vni=100", Aliases: RouteAliases, Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/list_interfaces.go b/cmd/list_interfaces.go index 480037d..d11247a 100644 --- a/cmd/list_interfaces.go +++ b/cmd/list_interfaces.go @@ -68,5 +68,20 @@ func RunListInterfaces( return fmt.Errorf("error listing interfaces: %w", err) } + if rendererFactory.GetWide() { + for i, iface := range interfaceList.Items { + nat, err := client.GetNat(ctx, iface.ID) + if err == nil { + interfaceList.Items[i].Spec.Nat = nat + } + } + for i, iface := range interfaceList.Items { + vip, err := client.GetVirtualIP(ctx, iface.ID) + if err == nil { + interfaceList.Items[i].Spec.VIP = vip + } + } + } + return rendererFactory.RenderList("", os.Stdout, interfaceList) } diff --git a/docs/README.md b/docs/README.md index da52313..a704692 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ # Documentation - dpservice-cli -## All available commands can be found [here](/docs/commands/). +## All available commands can be found [here](/docs/commands/README.md). To generate current command tree add this at the start of main.go: ``` diff --git a/docs/commands/README.md b/docs/commands/README.md new file mode 100644 index 0000000..99465a0 --- /dev/null +++ b/docs/commands/README.md @@ -0,0 +1,89 @@ +# dpservice-cli commands: + +You can browse help for all commands starting in main command [here](/docs/commands/dpservice-cli.md) + +# Available commands: + +Most of the validation is done on server side (dpservice). +All parameters are validated based on their type (see below). +In some cases there is validation also on client side (dpservice-cli) user is then notified with proper usage. + +## Initialization/check for initialized service and generating auto-completion: +``` +init +initialized +completion [bash|zsh|fish|powershell] +``` + +## Add/delete/list network interfaces: +``` +add interface --id= --ip= --ip= --vni= --device= +delete interface --id= +get interface --id= +list interfaces +``` + +## Add/delete/list routes (ip route equivalents): +``` +add route --prefix= --next-hop-vni= --next-hop-ip= --vni= +delete route --prefix= --vni= +list routes --vni= +``` + +## Add/delete/list prefixes (to route other IP ranges to a given interface): +``` +add prefix --prefix= --interface-id= +delete prefix --prefix= --interface-id= +list prefixes --interface-id= +``` + +## Create/delete/list loadbalancers: +``` +add loadbalancer --id= --vni= --vip= --lbports= +delete loadbalancer --id= +get loadbalancer --id= +``` + +## Add/delete/list loadbalancer backing IPs: +``` +add lbtarget --target-ip= --lb-id= +delete lbtarget --target-ip= --lb-id= +list lbtargets --lb-id= +``` + +## Add/delete/list loadbalancer prefixes (call on loadbalancer targets so the public IP packets can reach them): +``` +add lbprefix --prefix= --interface-id= +delete lbprefix --prefix= --interface-id= +list lbprefixes --interface-id= +``` + +## Add/delete/list a virtual IP for the interface (SNAT): +``` +add virtualip --vip= --interface-id= +delete virtualip --interface-id= +get virtualip --interface-id= +``` + +## Add/delete/list NAT IP (with port range) for the interface: +``` +add nat --interface-id= --natip= --minport= --maxport= +delete nat --interface-id= +get nat --interface-id= +list nats +``` + +## Add/delete/list neighbors (dp-services) with the same NAT IP: +``` +add neighbornat --natip= --vni= --minport= --maxport= --underlayroute= +delete neighbornat --natip= --vni= --minport= --maxport= +get natinfo --nat-ip= --info-type= +``` + +## Add/delete/list firewall rules: +``` +add fwrule --interface-id= --action= --direction= --dst= --ipver= --priority= --rule-id= --src= --protocol= --src-port-min= --src-port-max= --dst-port-min= --dst-port-max= --icmp-type= --icmp-code= +delete firewallrule --rule-id= --interface-id= +get fwrule --rule-id= --interface-id= +list firewallrules --interface-id= +``` \ No newline at end of file diff --git a/docs/development/README.md b/docs/development/README.md index bfbc0d7..2877029 100644 --- a/docs/development/README.md +++ b/docs/development/README.md @@ -37,9 +37,9 @@ If dpservice is running on different machine or you changed the default settings ``` To change the output format of commands you can use **-o, --output** flag with one of **json | yaml | table | name** - - **json** - shows output in json (you can use --pretty flag to show formatted json) + - **json** - shows output in json (you can use **--pretty** flag to show formatted json) - **yaml** - shows output in yaml - - **table** - shows output in predefined table format + - **table** - shows output in predefined table format (you can use **-w, --wide** for more information) - **name** - shows only short output with type/name
diff --git a/dpdk/api/types.go b/dpdk/api/types.go index 48d6362..ca00849 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -273,6 +273,8 @@ type InterfaceSpec struct { UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` VirtualFunction *VirtualFunction `json:"virtualFunction,omitempty"` PXE *PXE `json:"pxe,omitempty"` + Nat *Nat `json:"-"` + VIP *VirtualIP `json:"-"` } type VirtualFunction struct { @@ -325,6 +327,10 @@ func (m *Nat) GetStatus() Status { return m.Status } +func (m *Nat) String() string { + return fmt.Sprintf("%s <%d, %d>", m.Spec.NatVIPIP, m.Spec.MinPort, m.Spec.MaxPort) +} + type NatSpec struct { NatVIPIP *netip.Addr `json:"natVIPIP,omitempty"` MinPort uint32 `json:"minPort,omitempty"` diff --git a/renderer/renderer.go b/renderer/renderer.go index 50b80f5..00324b3 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -19,6 +19,7 @@ import ( "encoding/json" "fmt" "io" + "reflect" "strconv" "strings" @@ -232,22 +233,37 @@ func (t defaultTableConverter) loadBalancerTargetTable(lbtargets []api.LoadBalan } func (t defaultTableConverter) interfaceTable(ifaces []api.Interface) (*TableData, error) { - var headers []any - if len(ifaces) > 0 && ifaces[0].Spec.VirtualFunction == nil { - headers = []any{"ID", "VNI", "Device", "IPs", "UnderlayRoute"} - } else { - headers = []any{"ID", "VNI", "Device", "IPs", "UnderlayRoute", "VirtualFunction"} + headers := []any{"ID", "VNI", "Device", "IPs", "UnderlayRoute"} + vfNeeded := isColumnNeeded(ifaces, "Spec.VirtualFunction") + if vfNeeded { + headers = append(headers, "VirtualFunction") + } + natNeeded := isColumnNeeded(ifaces, "Spec.Nat") + if t.Wide && natNeeded { + headers = append(headers, "Nat") } - if t.Wide { - headers = append(headers, "Wide") + vipNeeded := isColumnNeeded(ifaces, "Spec.VIP") + if t.Wide && vipNeeded { + headers = append(headers, "VirtualIP") } columns := make([][]any, len(ifaces)) for i, iface := range ifaces { - if len(ifaces) > 0 && ifaces[0].Spec.VirtualFunction == nil { - columns[i] = []any{iface.ID, iface.Spec.VNI, iface.Spec.Device, iface.Spec.IPs, iface.Spec.UnderlayRoute} - } else { - columns[i] = []any{iface.ID, iface.Spec.VNI, iface.Spec.Device, iface.Spec.IPs, iface.Spec.UnderlayRoute, iface.Spec.VirtualFunction.String()} + columns[i] = []any{iface.ID, iface.Spec.VNI, iface.Spec.Device, iface.Spec.IPs, iface.Spec.UnderlayRoute} + if iface.Spec.VirtualFunction != nil { + columns[i] = append(columns[i], iface.Spec.VirtualFunction.String()) + } else if vfNeeded { + columns[i] = append(columns[i], "") + } + if t.Wide && iface.Spec.Nat != nil { + columns[i] = append(columns[i], iface.Spec.Nat.String()) + } else if natNeeded { + columns[i] = append(columns[i], "") + } + if t.Wide && iface.Spec.VIP != nil { + columns[i] = append(columns[i], iface.Spec.VIP.Spec.IP) + } else if vipNeeded { + columns[i] = append(columns[i], "") } } @@ -301,7 +317,7 @@ func (t defaultTableConverter) virtualIPTable(virtualIPs []api.VirtualIP) (*Tabl func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { var headers []any - // if command was nat + // if command was nat or there are no nats if len(nats) > 0 && nats[0].InterfaceID != "" { headers = []any{"InterfaceID", "NatIP", "MinPort", "MaxPort", "UnderlayRoute"} // if command was natinfo @@ -311,7 +327,7 @@ func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { columns := make([][]any, len(nats)) for i, nat := range nats { - // if command was nat + // if command was nat or there are no nats if len(nats) > 0 && nats[0].InterfaceID != "" { columns[i] = []any{nat.NatMeta.InterfaceID, nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute} // if command was natinfo @@ -476,3 +492,20 @@ func (r *Registry) New(name string, w io.Writer) (Renderer, error) { return newFunc(w), nil } + +// iterate over objects to check if it has any non nil value in given field +func isColumnNeeded(objs interface{}, field string) bool { + fields := strings.Split(field, ".") + v := reflect.ValueOf(objs) + if v.Kind() == reflect.Ptr { + v = v.Elem() + } + for i := 0; i < v.Len(); i++ { + r := reflect.ValueOf(v.Index(i).Interface()) + f := reflect.Indirect(r).FieldByName(fields[0]).FieldByName(fields[1]) + if !f.IsZero() { + return true + } + } + return false +} From 41dda39cc0b30bb4b3ab051808457710fb0faf23 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Mon, 5 Jun 2023 14:14:55 +0200 Subject: [PATCH 54/65] add wide flag to get interface table output --- cmd/get_interface.go | 12 ++++++++++++ renderer/renderer.go | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cmd/get_interface.go b/cmd/get_interface.go index de11f3a..7a7a990 100644 --- a/cmd/get_interface.go +++ b/cmd/get_interface.go @@ -88,6 +88,18 @@ func RunGetInterface( 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 + } + + vip, err := client.GetVirtualIP(ctx, iface.ID) + if err == nil { + iface.Spec.VIP = vip + } + } + iface.TypeMeta.Kind = api.InterfaceKind iface.InterfaceMeta.ID = opts.ID return rendererFactory.RenderObject("", os.Stdout, iface) diff --git a/renderer/renderer.go b/renderer/renderer.go index 00324b3..80249ac 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -238,11 +238,11 @@ func (t defaultTableConverter) interfaceTable(ifaces []api.Interface) (*TableDat if vfNeeded { headers = append(headers, "VirtualFunction") } - natNeeded := isColumnNeeded(ifaces, "Spec.Nat") + natNeeded := true if t.Wide && natNeeded { headers = append(headers, "Nat") } - vipNeeded := isColumnNeeded(ifaces, "Spec.VIP") + vipNeeded := true if t.Wide && vipNeeded { headers = append(headers, "VirtualIP") } From 232883291745172149ca1f07f7332fc23ef0729d Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Wed, 7 Jun 2023 16:14:54 +0200 Subject: [PATCH 55/65] fix support to add or delete objects from file --- .vscode/launch.json | 4 +- README.md | 8 ++ cmd/add.go | 17 ++- cmd/create_loadbalancer_prefix.go | 10 +- cmd/delete.go | 15 ++- cmd/delete_loadbalancer_prefix.go | 5 +- docs/development/README.md | 8 ++ dpdk/api/register.go | 1 + dpdk/api/types.go | 25 ++++ dpdk/client/client.go | 55 ++++---- dpdk/client/dynamic/dynamic.go | 213 +++++++++++++++++++++++++----- dpdk/runtime/decoder.go | 40 +++--- 12 files changed, 317 insertions(+), 84 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 33943be..808fd73 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -24,9 +24,9 @@ // "--priority=100", "--rule-id=9", "--src=1.1.1.1/32", "--protocol=tcp"] //"args": ["add", "nnat", "10.20.30.40", "--vni", "100", "--minport", "30000", "--maxport", "30100", "--underlayroute", "ff80::1"] //"args": ["add", "interface", "--id=vm4", "--ip=10.200.1.4", "--ip=2000:200:1::4", "--vni=200", "--device=net_tap5"] - //"args": ["add", "-f", "/tmp/addint.json"] + "args": ["delete", "-f", "/tmp/vip.yaml"] //"args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=11"] - "args": ["list", "interfaces", "--wide"] + //"args": ["list", "interfaces", "--wide"] //"args": ["get", "vni", "--vni=100", "--vni-type=0"] //"args": ["list", "prefixes","--interface-id=vm1"] //"args": ["list", "lbtargets","--lb-id=4"] diff --git a/README.md b/README.md index 86af161..210337e 100644 --- a/README.md +++ b/README.md @@ -67,9 +67,17 @@ Flags: -h, --help help for dpservice-cli -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. Use "dpservice-cli [command] --help" for more information about a command. ``` +Add and Delete commands also support file input with **-f, --filename** flag: +```bash +dpservice-cli [add|delete] -f //.[json|yaml] +``` +Filename, directory, or URL can be used. +One file can contain multiple objects of any kind. + All commands can be found in [docs](/docs/commands/dpservice-cli.md)
diff --git a/cmd/add.go b/cmd/add.go index 1b9eed1..609c72d 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -18,8 +18,10 @@ import ( "context" "fmt" "os" + "reflect" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/dpdk/client/dynamic" "github.com/onmetal/dpservice-cli/sources" "github.com/spf13/cobra" @@ -99,11 +101,20 @@ func RunAdd( } for _, obj := range objs { - if err := dc.Create(ctx, obj); err != nil { - fmt.Printf("Error creating %T: %v\n", obj, err) + res, err := dc.Create(ctx, obj) + if err == errors.ErrServerError { + r := reflect.ValueOf(res) + err := reflect.Indirect(r).FieldByName("Status").FieldByName("Error") + msg := reflect.Indirect(r).FieldByName("Status").FieldByName("Message") + fmt.Printf("Error adding %T: Server error: %v %v\n", res, err, msg) + continue + } + if err != nil { + fmt.Printf("Error adding %T: %v\n", obj, err) + continue } - if err := renderer.Render(obj); err != nil { + if err := renderer.Render(res); err != nil { return fmt.Errorf("error rendering %T: %w", obj, err) } } diff --git a/cmd/create_loadbalancer_prefix.go b/cmd/create_loadbalancer_prefix.go index 5c4af0a..86c624d 100644 --- a/cmd/create_loadbalancer_prefix.go +++ b/cmd/create_loadbalancer_prefix.go @@ -91,19 +91,19 @@ func RunCreateLoadBalancerPrefix( } defer DpdkClose(cleanup) - lbprefix, err := client.CreateLoadBalancerPrefix(ctx, &api.Prefix{ - PrefixMeta: api.PrefixMeta{ + lbprefix, err := client.CreateLoadBalancerPrefix(ctx, &api.LoadBalancerPrefix{ + LoadBalancerPrefixMeta: api.LoadBalancerPrefixMeta{ InterfaceID: opts.InterfaceID, }, - Spec: api.PrefixSpec{ + Spec: api.LoadBalancerPrefixSpec{ Prefix: opts.Prefix, }, }) if err != nil && err != errors.ErrServerError { return fmt.Errorf("error adding loadbalancer prefix: %w", err) } - lbprefix.TypeMeta.Kind = "LoadBalancerPrefix" - lbprefix.PrefixMeta.InterfaceID = opts.InterfaceID + lbprefix.TypeMeta.Kind = api.LoadBalancerPrefixKind + lbprefix.LoadBalancerPrefixMeta.InterfaceID = opts.InterfaceID lbprefix.Spec.Prefix = opts.Prefix return rendererFactory.RenderObject("added", os.Stdout, lbprefix) } diff --git a/cmd/delete.go b/cmd/delete.go index 4741b47..8fe0add 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -18,8 +18,10 @@ import ( "context" "fmt" "os" + "reflect" "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/dpdk/client/dynamic" "github.com/onmetal/dpservice-cli/sources" "github.com/spf13/cobra" @@ -100,8 +102,17 @@ func RunDelete( for _, obj := range objs { key := dynamic.ObjectKeyFromObject(obj) - if err := dc.Delete(ctx, obj); err != nil { - fmt.Printf("Error deleting %T %s: %v\n", obj, key, err) + res, err := dc.Delete(ctx, obj) + if err == errors.ErrServerError { + r := reflect.ValueOf(res) + err := reflect.Indirect(r).FieldByName("Status").FieldByName("Error") + msg := reflect.Indirect(r).FieldByName("Status").FieldByName("Message") + fmt.Printf("Error deleting %T %s: Server error: %v %v\n", res, key, err, msg) + continue + } + if err != nil { + fmt.Printf("Error deleting %T %s: %v\n", res, key, err) + continue } if err := renderer.Render(obj); err != nil { diff --git a/cmd/delete_loadbalancer_prefix.go b/cmd/delete_loadbalancer_prefix.go index c744ad6..76de06b 100644 --- a/cmd/delete_loadbalancer_prefix.go +++ b/cmd/delete_loadbalancer_prefix.go @@ -20,6 +20,7 @@ import ( "net/netip" "os" + "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" @@ -87,8 +88,8 @@ func RunDeleteLoadBalancerPrefix(ctx context.Context, dpdkClientFactory DPDKClie return fmt.Errorf("error deleting loadbalancer prefix: %w", err) } - lbprefix.TypeMeta.Kind = "LoadBalancerPrefix" - lbprefix.PrefixMeta.InterfaceID = opts.InterfaceID + lbprefix.TypeMeta.Kind = api.LoadBalancerPrefixKind + lbprefix.LoadBalancerPrefixMeta.InterfaceID = opts.InterfaceID lbprefix.Spec.Prefix = opts.Prefix return rendererFactory.RenderObject("deleted", os.Stdout, lbprefix) } diff --git a/docs/development/README.md b/docs/development/README.md index 2877029..ff62e2c 100644 --- a/docs/development/README.md +++ b/docs/development/README.md @@ -41,6 +41,13 @@ To change the output format of commands you can use **-o, --output** flag with o - **yaml** - shows output in yaml - **table** - shows output in predefined table format (you can use **-w, --wide** for more information) - **name** - shows only short output with type/name + +Add and Delete commands also support file input with **-f, --filename** flag: +```bash +./bin/dpservice-cli [add|delete] -f //.[json|yaml] +``` +Filename, directory, or URL can be used. +One file can contain multiple objects of any kind.
@@ -57,6 +64,7 @@ Basic steps when implementing new type (similar to Interface, Route, LoadBalance - add function to Client interface - implement the function - Add new \ to DefaultScheme in [/dpdk/api/register.go](/dpdk/api/register.go) +- Add new \Key structs and methods in [/dpdk/client/dynamic/dynamic.go](/dpdk/client/dynamic/dynamic.go) and add new \ to switch in Create and Delete methods - If needed create new conversion function(s) between dpdk struct and local struct in [/dpdk/api/conversion.go](/dpdk/api/conversion.go) - Add new function to show \ as table in [/renderer/renderer.go](/renderer/renderer.go) - add new \ to ConvertToTable method diff --git a/dpdk/api/register.go b/dpdk/api/register.go index 4a04ca4..5621bc9 100644 --- a/dpdk/api/register.go +++ b/dpdk/api/register.go @@ -31,6 +31,7 @@ func init() { &VirtualIP{}, &LoadBalancer{}, &LoadBalancerTarget{}, + &LoadBalancerPrefix{}, &LoadBalancerTargetList{}, &Nat{}, &NatList{}, diff --git a/dpdk/api/types.go b/dpdk/api/types.go index ca00849..7f04b67 100644 --- a/dpdk/api/types.go +++ b/dpdk/api/types.go @@ -241,6 +241,30 @@ func (l *LoadBalancerTargetList) GetItems() []Object { return res } +type LoadBalancerPrefix struct { + TypeMeta `json:",inline"` + LoadBalancerPrefixMeta `json:"metadata"` + Spec LoadBalancerPrefixSpec `json:"spec"` + Status Status `json:"status"` +} + +type LoadBalancerPrefixMeta struct { + InterfaceID string `json:"interfaceID"` +} + +func (m *LoadBalancerPrefix) GetName() string { + return m.Spec.Prefix.String() +} + +func (m *LoadBalancerPrefix) GetStatus() Status { + return m.Status +} + +type LoadBalancerPrefixSpec struct { + Prefix netip.Prefix `json:"prefix"` + UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` +} + // Interface section type Interface struct { TypeMeta `json:",inline"` @@ -507,6 +531,7 @@ var ( LoadBalancerKind = reflect.TypeOf(LoadBalancer{}).Name() LoadBalancerTargetKind = reflect.TypeOf(LoadBalancerTarget{}).Name() LoadBalancerTargetListKind = reflect.TypeOf(LoadBalancerTargetList{}).Name() + LoadBalancerPrefixKind = reflect.TypeOf(LoadBalancerPrefix{}).Name() PrefixKind = reflect.TypeOf(Prefix{}).Name() PrefixListKind = reflect.TypeOf(PrefixList{}).Name() VirtualIPKind = reflect.TypeOf(VirtualIP{}).Name() diff --git a/dpdk/client/client.go b/dpdk/client/client.go index 2963761..b907e4f 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -32,8 +32,8 @@ type Client interface { DeleteLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) ListLoadBalancerPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) - CreateLoadBalancerPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix, error) - DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) (*api.Prefix, error) + CreateLoadBalancerPrefix(ctx context.Context, prefix *api.LoadBalancerPrefix) (*api.LoadBalancerPrefix, error) + DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) (*api.LoadBalancerPrefix, error) GetLoadBalancerTargets(ctx context.Context, interfaceID string) (*api.LoadBalancerTargetList, error) AddLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBalancerTarget) (*api.LoadBalancerTarget, error) @@ -148,6 +148,7 @@ func (c *client) ListLoadBalancerPrefixes(ctx context.Context, interfaceID strin prefixes := make([]api.Prefix, len(res.GetPrefixes())) for i, dpdkPrefix := range res.GetPrefixes() { prefix, err := api.ProtoPrefixToPrefix(interfaceID, api.ProtoLBPrefixToProtoPrefix(*dpdkPrefix)) + prefix.Kind = api.LoadBalancerPrefixKind if err != nil { return nil, err } @@ -162,36 +163,39 @@ func (c *client) ListLoadBalancerPrefixes(ctx context.Context, interfaceID strin }, nil } -func (c *client) CreateLoadBalancerPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix, error) { +func (c *client) CreateLoadBalancerPrefix(ctx context.Context, lbprefix *api.LoadBalancerPrefix) (*api.LoadBalancerPrefix, error) { res, err := c.DPDKonmetalClient.CreateInterfaceLoadBalancerPrefix(ctx, &dpdkproto.CreateInterfaceLoadBalancerPrefixRequest{ InterfaceID: &dpdkproto.InterfaceIDMsg{ - InterfaceID: []byte(prefix.InterfaceID), + InterfaceID: []byte(lbprefix.InterfaceID), }, Prefix: &dpdkproto.Prefix{ - IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Spec.Prefix.Addr()), - Address: []byte(prefix.Spec.Prefix.Addr().String()), - PrefixLength: uint32(prefix.Spec.Prefix.Bits()), + IpVersion: api.NetIPAddrToProtoIPVersion(lbprefix.Spec.Prefix.Addr()), + Address: []byte(lbprefix.Spec.Prefix.Addr().String()), + PrefixLength: uint32(lbprefix.Spec.Prefix.Bits()), }, }) if err != nil { - return &api.Prefix{}, err + return &api.LoadBalancerPrefix{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.Prefix{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError + return &api.LoadBalancerPrefix{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) if err != nil { - return &api.Prefix{Status: api.ProtoStatusToStatus(res.Status)}, fmt.Errorf("error parsing underlay route: %w", err) + return &api.LoadBalancerPrefix{Status: api.ProtoStatusToStatus(res.Status)}, fmt.Errorf("error parsing underlay route: %w", err) } - return &api.Prefix{ - TypeMeta: api.TypeMeta{Kind: "LoadBalancerPrefix"}, - PrefixMeta: prefix.PrefixMeta, - Spec: api.PrefixSpec{UnderlayRoute: &underlayRoute}, - Status: api.ProtoStatusToStatus(res.Status), + return &api.LoadBalancerPrefix{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerPrefixKind}, + LoadBalancerPrefixMeta: lbprefix.LoadBalancerPrefixMeta, + Spec: api.LoadBalancerPrefixSpec{ + Prefix: lbprefix.Spec.Prefix, + UnderlayRoute: &underlayRoute, + }, + Status: api.ProtoStatusToStatus(res.Status), }, nil } -func (c *client) DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) (*api.Prefix, error) { +func (c *client) DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) (*api.LoadBalancerPrefix, error) { res, err := c.DPDKonmetalClient.DeleteInterfaceLoadBalancerPrefix(ctx, &dpdkproto.DeleteInterfaceLoadBalancerPrefixRequest{ InterfaceID: &dpdkproto.InterfaceIDMsg{ InterfaceID: []byte(interfaceID), @@ -203,12 +207,12 @@ func (c *client) DeleteLoadBalancerPrefix(ctx context.Context, interfaceID strin }, }) if err != nil { - return &api.Prefix{}, err + return &api.LoadBalancerPrefix{}, err } if errorCode := res.GetError(); errorCode != 0 { - return &api.Prefix{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError + return &api.LoadBalancerPrefix{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError } - return &api.Prefix{Status: api.ProtoStatusToStatus(res)}, nil + return &api.LoadBalancerPrefix{Status: api.ProtoStatusToStatus(res)}, nil } func (c *client) GetLoadBalancerTargets(ctx context.Context, loadBalancerID string) (*api.LoadBalancerTargetList, error) { @@ -405,8 +409,11 @@ func (c *client) AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*a return &api.VirtualIP{ TypeMeta: api.TypeMeta{Kind: api.VirtualIPKind}, VirtualIPMeta: virtualIP.VirtualIPMeta, - Spec: api.VirtualIPSpec{UnderlayRoute: &underlayRoute}, - Status: api.ProtoStatusToStatus(res.Status), + Spec: api.VirtualIPSpec{ + IP: virtualIP.Spec.IP, + UnderlayRoute: &underlayRoute, + }, + Status: api.ProtoStatusToStatus(res.Status), }, nil } @@ -472,8 +479,10 @@ func (c *client) AddPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix return &api.Prefix{ TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, PrefixMeta: prefix.PrefixMeta, - Spec: api.PrefixSpec{UnderlayRoute: &underlayRoute}, - Status: api.ProtoStatusToStatus(res.Status), + Spec: api.PrefixSpec{ + Prefix: prefix.Spec.Prefix, + UnderlayRoute: &underlayRoute}, + Status: api.ProtoStatusToStatus(res.Status), }, nil } diff --git a/dpdk/client/dynamic/dynamic.go b/dpdk/client/dynamic/dynamic.go index 4fd7ed6..562edf8 100644 --- a/dpdk/client/dynamic/dynamic.go +++ b/dpdk/client/dynamic/dynamic.go @@ -80,6 +80,84 @@ func (k RouteKey) Name() string { return fmt.Sprintf("%s-%d:%s", k.Prefix, k.NextHopVNI, k.NextHopIP) } +type LoadBalancerKey struct { + ID string +} + +func (k LoadBalancerKey) String() string { + return k.ID +} + +func (k LoadBalancerKey) Name() string { + return k.ID +} + +type LoadBalancerPrefixKey struct { + Prefix netip.Prefix + InterfaceID string +} + +func (k LoadBalancerPrefixKey) String() string { + return fmt.Sprintf("%s-%v", k.InterfaceID, k.Prefix) +} + +func (k LoadBalancerPrefixKey) Name() string { + return k.String() +} + +type LoadBalancerTargetKey struct { + TargetIP netip.Addr + LoadBalancerID string +} + +func (k LoadBalancerTargetKey) String() string { + return fmt.Sprintf("%s-%v", k.LoadBalancerID, k.TargetIP) +} + +func (k LoadBalancerTargetKey) Name() string { + return k.String() +} + +type NatKey struct { + InterfaceID string +} + +func (k NatKey) String() string { + return k.InterfaceID +} + +func (k NatKey) Name() string { + return k.String() +} + +type NeighborNatKey struct { + NatIP netip.Addr + Vni uint32 + MinPort uint32 + MaxPort uint32 +} + +func (k NeighborNatKey) String() string { + return fmt.Sprintf("%d-%v:<%d,%d>", k.Vni, k.NatIP, k.MinPort, k.MaxPort) +} + +func (k NeighborNatKey) Name() string { + return fmt.Sprintf("%d-%v", k.Vni, k.NatIP) +} + +type FirewallRuleKey struct { + RuleID string + InterfaceID string +} + +func (k FirewallRuleKey) String() string { + return fmt.Sprintf("%s-%s", k.InterfaceID, k.RuleID) +} + +func (k FirewallRuleKey) Name() string { + return k.String() +} + type emptyKey struct{} func (emptyKey) String() string { @@ -92,6 +170,7 @@ func (emptyKey) Name() string { var EmptyKey ObjectKey = emptyKey{} +// returns object key (parameters needed for deletion) func ObjectKeyFromObject(obj any) ObjectKey { switch obj := obj.(type) { case *api.Interface: @@ -103,84 +182,158 @@ func ObjectKeyFromObject(obj any) ObjectKey { } case *api.Route: return RouteKey{ - VNI: obj.VNI, - Prefix: *obj.Spec.Prefix, - NextHopVNI: obj.Spec.NextHop.VNI, - NextHopIP: *obj.Spec.NextHop.IP, + VNI: obj.VNI, + Prefix: *obj.Spec.Prefix, } case *api.VirtualIP: return VirtualIPKey{ InterfaceID: obj.InterfaceID, } + case *api.LoadBalancer: + return LoadBalancerKey{ + ID: obj.ID, + } + case *api.LoadBalancerPrefix: + return LoadBalancerPrefixKey{ + Prefix: obj.Spec.Prefix, + InterfaceID: obj.InterfaceID, + } + case *api.LoadBalancerTarget: + return LoadBalancerTargetKey{ + TargetIP: *obj.Spec.TargetIP, + LoadBalancerID: obj.LoadbalancerID, + } + case *api.Nat: + return NatKey{ + InterfaceID: obj.InterfaceID, + } + case *api.NeighborNat: + return NeighborNatKey{ + NatIP: *obj.NatVIPIP, + Vni: obj.Spec.Vni, + MinPort: obj.Spec.MinPort, + MaxPort: obj.Spec.MaxPort, + } + case *api.FirewallRule: + return FirewallRuleKey{ + RuleID: obj.Spec.RuleID, + InterfaceID: obj.InterfaceID, + } default: return EmptyKey } } type Client interface { - Create(ctx context.Context, obj any) error - Delete(ctx context.Context, obj any) error + Create(ctx context.Context, obj any) (any, error) + Delete(ctx context.Context, obj any) (any, error) } type client struct { structured structured.Client } -func (c *client) Create(ctx context.Context, obj any) error { +func (c *client) Create(ctx context.Context, obj any) (any, error) { switch obj := obj.(type) { case *api.Interface: res, err := c.structured.CreateInterface(ctx, obj) if err != nil { - return err + return res, err } - *obj = *res - return nil + return obj, nil case *api.Prefix: res, err := c.structured.AddPrefix(ctx, obj) if err != nil { - return err + return res, err } - *obj = *res - return nil + return obj, nil case *api.Route: res, err := c.structured.AddRoute(ctx, obj) if err != nil { - return err + return res, err } - *obj = *res - return nil + return obj, nil case *api.VirtualIP: res, err := c.structured.AddVirtualIP(ctx, obj) if err != nil { - return err + return res, err + } + *obj = *res + return obj, nil + case *api.LoadBalancer: + res, err := c.structured.CreateLoadBalancer(ctx, obj) + if err != nil { + return res, err + } + *obj = *res + return obj, nil + case *api.LoadBalancerPrefix: + res, err := c.structured.CreateLoadBalancerPrefix(ctx, obj) + if err != nil { + return res, err + } + *obj = *res + return obj, nil + case *api.LoadBalancerTarget: + res, err := c.structured.AddLoadBalancerTarget(ctx, obj) + if err != nil { + return res, err + } + *obj = *res + return obj, nil + case *api.Nat: + res, err := c.structured.AddNat(ctx, obj) + if err != nil { + return res, err + } + *obj = *res + return obj, nil + case *api.NeighborNat: + res, err := c.structured.AddNeighborNat(ctx, obj) + if err != nil { + return res, err + } + *obj = *res + return obj, nil + case *api.FirewallRule: + res, err := c.structured.AddFirewallRule(ctx, obj) + if err != nil { + return res, err } - *obj = *res - return nil + return obj, nil default: - return fmt.Errorf("unsupported object %T", obj) + return obj, fmt.Errorf("unsupported object %T", obj) } } -func (c *client) Delete(ctx context.Context, obj any) error { +func (c *client) Delete(ctx context.Context, obj any) (any, error) { switch obj := obj.(type) { case *api.Interface: - _, err := c.structured.DeleteInterface(ctx, obj.ID) - return err + return c.structured.DeleteInterface(ctx, obj.ID) case *api.Prefix: - _, err := c.structured.DeletePrefix(ctx, obj.InterfaceID, obj.Spec.Prefix) - return err + return c.structured.DeletePrefix(ctx, obj.InterfaceID, obj.Spec.Prefix) case *api.Route: - _, err := c.structured.DeleteRoute(ctx, obj.VNI, *obj.Spec.Prefix) - return err + return c.structured.DeleteRoute(ctx, obj.VNI, *obj.Spec.Prefix) case *api.VirtualIP: - _, err := c.structured.DeleteVirtualIP(ctx, obj.InterfaceID) - return err + return c.structured.DeleteVirtualIP(ctx, obj.InterfaceID) + case *api.LoadBalancer: + return c.structured.DeleteLoadBalancer(ctx, obj.ID) + case *api.LoadBalancerPrefix: + return c.structured.DeleteLoadBalancerPrefix(ctx, obj.InterfaceID, obj.Spec.Prefix) + case *api.LoadBalancerTarget: + return c.structured.DeleteLoadBalancerTarget(ctx, obj.LoadbalancerID, *obj.Spec.TargetIP) + case *api.Nat: + return c.structured.DeleteNat(ctx, obj.InterfaceID) + case *api.NeighborNat: + return c.structured.DeleteNeighborNat(ctx, *obj) + case *api.FirewallRule: + return c.structured.DeleteFirewallRule(ctx, obj.InterfaceID, obj.Spec.RuleID) default: - return fmt.Errorf("unsupported object %T", obj) + return obj, fmt.Errorf("unsupported object %T", obj) } } diff --git a/dpdk/runtime/decoder.go b/dpdk/runtime/decoder.go index c7b3de1..db35e69 100644 --- a/dpdk/runtime/decoder.go +++ b/dpdk/runtime/decoder.go @@ -77,25 +77,27 @@ func NewPeekDecoder(rd io.Reader, newDecoder func(rd io.Reader) Decoder) PeekDec } func (d *KindDecoder) Next() (any, error) { - typeMeta := &struct { - Kind string `json:"kind"` + obj := &struct { + Kind string `json:"kind" yaml:"kind"` + Metadata any `json:"metadata" yaml:"metadata"` + Spec any `json:"spec" yaml:"spec"` }{} - if err := d.decoder.Decode(typeMeta); err != nil { - return nil, fmt.Errorf("error decoding type meta: %w", err) + if err := d.decoder.Decode(&obj); err != nil { + return nil, err } - - if err := d.decoder.Undecode(); err != nil { - return nil, fmt.Errorf("error reversing decoder: %w", err) + res, err := d.scheme.New(obj.Kind) + if err != nil { + return nil, fmt.Errorf("error creating new %s: %w", obj.Kind, err) } - - res, err := d.scheme.New(typeMeta.Kind) + jsonObj, err := json.Marshal(obj) if err != nil { - return nil, fmt.Errorf("error creating new %s: %w", typeMeta.Kind, err) + return nil, fmt.Errorf("error marshaling %s: %w", obj.Kind, err) } - - if err = d.decoder.Decode(res); err != nil { - return nil, fmt.Errorf("error decoding %s: %w", typeMeta.Kind, err) + json.Unmarshal(jsonObj, res) + if err != nil { + return nil, fmt.Errorf("error unmarshaling %s: %w", obj.Kind, err) } + return res, nil } @@ -123,12 +125,16 @@ func NewYAMLToJSONDecoder(rd io.Reader) *YAMLToJSONDecoder { } func (d *YAMLToJSONDecoder) Decode(v any) error { - var yamlValue any - if err := d.decoder.Decode(&yamlValue); err != nil { + obj := &struct { + Kind string `yaml:"kind"` + Metadata any `yaml:"metadata"` + Spec any `yaml:"spec"` + }{} + if err := d.decoder.Decode(obj); err != nil { return err } - yamlData, err := yaml2.Marshal(yamlValue) + yamlData, err := yaml.Marshal(obj) if err != nil { return err } @@ -138,5 +144,5 @@ func (d *YAMLToJSONDecoder) Decode(v any) error { return err } - return json.Unmarshal(jsonData, v) + return json.Unmarshal(jsonData, &v) } From ac60aa2519493084cb14ba2523ca11eb7451e565 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 8 Jun 2023 15:31:11 +0200 Subject: [PATCH 56/65] change client.go to return needed info with error --- README.md | 10 +- cmd/add_firewall_rule.go | 3 - cmd/add_nat.go | 2 - cmd/add_neighbor_nat.go | 2 - cmd/add_prefix.go | 3 - cmd/add_route.go | 3 - cmd/add_virtualip.go | 3 - cmd/create_interface.go | 2 - cmd/create_loadbalancer.go | 2 - cmd/create_loadbalancer_prefix.go | 4 +- cmd/delete_firewall_rule.go | 4 - cmd/delete_interface.go | 3 - cmd/delete_loadbalancer.go | 3 - cmd/delete_loadbalancer_prefix.go | 4 - cmd/delete_loadbalancer_target.go | 3 - cmd/delete_nat.go | 3 - cmd/delete_neighbor_nat.go | 2 - cmd/delete_prefix.go | 4 - cmd/delete_route.go | 5 - cmd/delete_virtualip.go | 3 - cmd/get_firewall_rule.go | 4 - cmd/get_interface.go | 3 - cmd/get_loadbalancer.go | 3 - cmd/get_nat.go | 3 - cmd/get_virtualip.go | 3 - cmd/get_vni.go | 4 - cmd/list_loadbalancer_targets.go | 2 - dpdk/api/conversion.go | 1 + dpdk/client/client.go | 346 ++++++++++++++++++------------ 29 files changed, 225 insertions(+), 212 deletions(-) diff --git a/README.md b/README.md index 210337e..25a69e2 100644 --- a/README.md +++ b/README.md @@ -71,12 +71,20 @@ Flags: Use "dpservice-cli [command] --help" for more information about a command. ``` +--- Add and Delete commands also support file input with **-f, --filename** flag: ```bash dpservice-cli [add|delete] -f //.[json|yaml] ``` Filename, directory, or URL can be used. -One file can contain multiple objects of any kind. +One file can contain multiple objects of any kind, example file: +```bash +{"kind":"VirtualIP","metadata":{"interfaceID":"vm1"},"spec":{"ip":"20.20.20.20"}} +{"kind":"VirtualIP","metadata":{"interfaceID":"vm2"},"spec":{"ip":"20.20.20.21"}} +{"kind":"Prefix","metadata":{"interfaceID":"vm3"},"spec":{"prefix":"20.20.20.0/24"}} +{"kind":"LoadBalancer","metadata":{"id":"4"},"spec":{"vni":100,"lbVipIP":"10.20.30.40","lbports":[{"protocol":6,"port":443},{"protocol":17,"port":53}]}} +{"kind":"LoadBalancerPrefix","metadata":{"interfaceID":"vm1"},"spec":{"prefix":"10.10.10.0/24"}} +``` All commands can be found in [docs](/docs/commands/dpservice-cli.md) diff --git a/cmd/add_firewall_rule.go b/cmd/add_firewall_rule.go index ea9317d..17f0820 100644 --- a/cmd/add_firewall_rule.go +++ b/cmd/add_firewall_rule.go @@ -205,8 +205,5 @@ func RunAddFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFactory return fmt.Errorf("error adding firewall rule: %w", err) } - fwrule.TypeMeta.Kind = api.FirewallRuleKind - fwrule.FirewallRuleMeta.InterfaceID = opts.InterfaceID - fwrule.Spec.RuleID = opts.RuleID return rendererFactory.RenderObject("added", os.Stdout, fwrule) } diff --git a/cmd/add_nat.go b/cmd/add_nat.go index a77cffc..a89e352 100644 --- a/cmd/add_nat.go +++ b/cmd/add_nat.go @@ -101,7 +101,5 @@ func RunAddNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rendere return fmt.Errorf("error adding nat: %w", err) } - nat.TypeMeta.Kind = api.NatKind - nat.NatMeta.InterfaceID = opts.InterfaceID return rendererFactory.RenderObject(fmt.Sprintf("added, underlay route: %s", nat.Spec.UnderlayRoute), os.Stdout, nat) } diff --git a/cmd/add_neighbor_nat.go b/cmd/add_neighbor_nat.go index a960810..621dca8 100644 --- a/cmd/add_neighbor_nat.go +++ b/cmd/add_neighbor_nat.go @@ -105,7 +105,5 @@ func RunAddNeighborNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, return fmt.Errorf("error adding neighbor nat: %w", err) } - nnat.TypeMeta.Kind = api.NeighborNatKind - nnat.NeighborNatMeta.NatVIPIP = &opts.NatIP return rendererFactory.RenderObject(fmt.Sprintf("added, underlay route: %s", nnat.Spec.UnderlayRoute), os.Stdout, nnat) } diff --git a/cmd/add_prefix.go b/cmd/add_prefix.go index c1a29c7..e4959dc 100644 --- a/cmd/add_prefix.go +++ b/cmd/add_prefix.go @@ -103,8 +103,5 @@ func RunAddPrefix( return fmt.Errorf("error adding prefix: %w", err) } - prefix.TypeMeta.Kind = api.PrefixKind - prefix.PrefixMeta.InterfaceID = opts.InterfaceID - prefix.Spec.Prefix = opts.Prefix return rendererFactory.RenderObject(fmt.Sprintf("added, underlay route: %s", prefix.Spec.UnderlayRoute), os.Stdout, prefix) } diff --git a/cmd/add_route.go b/cmd/add_route.go index 76fad49..6758c6a 100644 --- a/cmd/add_route.go +++ b/cmd/add_route.go @@ -106,8 +106,5 @@ func RunAddRoute( return fmt.Errorf("error adding route: %w", err) } - route.TypeMeta.Kind = api.RouteKind - route.RouteMeta.VNI = opts.VNI - route.Spec.Prefix = &opts.Prefix return rendererFactory.RenderObject(fmt.Sprintf("added, Next Hop IP: %s", opts.NextHopIP), os.Stdout, route) } diff --git a/cmd/add_virtualip.go b/cmd/add_virtualip.go index 8f16770..f8d56f1 100644 --- a/cmd/add_virtualip.go +++ b/cmd/add_virtualip.go @@ -100,8 +100,5 @@ func RunAddVirtualIP( return fmt.Errorf("error adding virtual ip: %w", err) } - virtualIP.TypeMeta.Kind = api.VirtualIPKind - virtualIP.Spec.IP = opts.Vip - virtualIP.VirtualIPMeta.InterfaceID = opts.InterfaceID return rendererFactory.RenderObject(fmt.Sprintf("added, underlay route: %s", virtualIP.Spec.UnderlayRoute), os.Stdout, virtualIP) } diff --git a/cmd/create_interface.go b/cmd/create_interface.go index e27558d..3367c5d 100644 --- a/cmd/create_interface.go +++ b/cmd/create_interface.go @@ -105,7 +105,5 @@ func RunCreateInterface(ctx context.Context, dpdkClientFactory DPDKClientFactory return fmt.Errorf("error adding interface: %w", err) } - iface.TypeMeta.Kind = api.InterfaceKind - iface.InterfaceMeta.ID = opts.ID return rendererFactory.RenderObject(fmt.Sprintf("added, underlay route: %s", iface.Spec.UnderlayRoute), os.Stdout, iface) } diff --git a/cmd/create_loadbalancer.go b/cmd/create_loadbalancer.go index cd87e4b..cad6968 100644 --- a/cmd/create_loadbalancer.go +++ b/cmd/create_loadbalancer.go @@ -109,7 +109,5 @@ func RunCreateLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFact return fmt.Errorf("error adding loadbalancer: %w", err) } - lb.TypeMeta.Kind = api.LoadBalancerKind - lb.LoadBalancerMeta.ID = opts.Id return rendererFactory.RenderObject(fmt.Sprintf("added, underlay route: %s", lb.Spec.UnderlayRoute), os.Stdout, lb) } diff --git a/cmd/create_loadbalancer_prefix.go b/cmd/create_loadbalancer_prefix.go index 86c624d..9ea9760 100644 --- a/cmd/create_loadbalancer_prefix.go +++ b/cmd/create_loadbalancer_prefix.go @@ -102,8 +102,6 @@ func RunCreateLoadBalancerPrefix( if err != nil && err != errors.ErrServerError { return fmt.Errorf("error adding loadbalancer prefix: %w", err) } - lbprefix.TypeMeta.Kind = api.LoadBalancerPrefixKind - lbprefix.LoadBalancerPrefixMeta.InterfaceID = opts.InterfaceID - lbprefix.Spec.Prefix = opts.Prefix + return rendererFactory.RenderObject("added", os.Stdout, lbprefix) } diff --git a/cmd/delete_firewall_rule.go b/cmd/delete_firewall_rule.go index 978f669..19d443b 100644 --- a/cmd/delete_firewall_rule.go +++ b/cmd/delete_firewall_rule.go @@ -19,7 +19,6 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -86,8 +85,5 @@ func RunDeleteFirewallRule(ctx context.Context, dpdkClientFactory DPDKClientFact return fmt.Errorf("error deleting firewall rule: %w", err) } - fwrule.TypeMeta.Kind = api.FirewallRuleKind - fwrule.FirewallRuleMeta.InterfaceID = opts.InterfaceID - fwrule.Spec.RuleID = opts.RuleID return rendererFactory.RenderObject("deleted", os.Stdout, fwrule) } diff --git a/cmd/delete_interface.go b/cmd/delete_interface.go index f39cc76..206fb5e 100644 --- a/cmd/delete_interface.go +++ b/cmd/delete_interface.go @@ -19,7 +19,6 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -84,7 +83,5 @@ func RunDeleteInterface(ctx context.Context, dpdkClientFactory DPDKClientFactory return fmt.Errorf("error deleting interface: %w", err) } - iface.TypeMeta.Kind = api.InterfaceKind - iface.InterfaceMeta.ID = opts.ID return rendererFactory.RenderObject("deleted", os.Stdout, iface) } diff --git a/cmd/delete_loadbalancer.go b/cmd/delete_loadbalancer.go index 6d9b8c2..ed21d29 100644 --- a/cmd/delete_loadbalancer.go +++ b/cmd/delete_loadbalancer.go @@ -19,7 +19,6 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -84,7 +83,5 @@ func RunDeleteLoadBalancer(ctx context.Context, dpdkClientFactory DPDKClientFact return fmt.Errorf("error deleting loadbalancer: %w", err) } - lb.TypeMeta.Kind = api.LoadBalancerKind - lb.LoadBalancerMeta.ID = opts.ID return rendererFactory.RenderObject("deleted", os.Stdout, lb) } diff --git a/cmd/delete_loadbalancer_prefix.go b/cmd/delete_loadbalancer_prefix.go index 76de06b..2876f66 100644 --- a/cmd/delete_loadbalancer_prefix.go +++ b/cmd/delete_loadbalancer_prefix.go @@ -20,7 +20,6 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" @@ -88,8 +87,5 @@ func RunDeleteLoadBalancerPrefix(ctx context.Context, dpdkClientFactory DPDKClie return fmt.Errorf("error deleting loadbalancer prefix: %w", err) } - lbprefix.TypeMeta.Kind = api.LoadBalancerPrefixKind - lbprefix.LoadBalancerPrefixMeta.InterfaceID = opts.InterfaceID - lbprefix.Spec.Prefix = opts.Prefix return rendererFactory.RenderObject("deleted", os.Stdout, lbprefix) } diff --git a/cmd/delete_loadbalancer_target.go b/cmd/delete_loadbalancer_target.go index aaf40d8..9a211d2 100644 --- a/cmd/delete_loadbalancer_target.go +++ b/cmd/delete_loadbalancer_target.go @@ -20,7 +20,6 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" @@ -88,7 +87,5 @@ func RunDeleteLoadBalancerTarget(ctx context.Context, dpdkClientFactory DPDKClie return fmt.Errorf("error deleting neighbor nat: %w", err) } - lbtarget.TypeMeta.Kind = api.LoadBalancerTargetKind - lbtarget.LoadBalancerTargetMeta.LoadbalancerID = opts.LoadBalancerID return rendererFactory.RenderObject("deleted", os.Stdout, lbtarget) } diff --git a/cmd/delete_nat.go b/cmd/delete_nat.go index 68acdc0..4a05bb6 100644 --- a/cmd/delete_nat.go +++ b/cmd/delete_nat.go @@ -19,7 +19,6 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -84,7 +83,5 @@ func RunDeleteNat(ctx context.Context, dpdkClientFactory DPDKClientFactory, rend return fmt.Errorf("error deleting nat: %w", err) } - nat.TypeMeta.Kind = api.NatKind - nat.NatMeta.InterfaceID = opts.InterfaceID return rendererFactory.RenderObject("deleted", os.Stdout, nat) } diff --git a/cmd/delete_neighbor_nat.go b/cmd/delete_neighbor_nat.go index 02b9b08..5bda5b7 100644 --- a/cmd/delete_neighbor_nat.go +++ b/cmd/delete_neighbor_nat.go @@ -101,7 +101,5 @@ func RunDeleteNeighborNat(ctx context.Context, dpdkClientFactory DPDKClientFacto return fmt.Errorf("error deleting neighbor nat: %w", err) } - nnat.TypeMeta.Kind = api.NeighborNatKind - nnat.NeighborNatMeta.NatVIPIP = &opts.NatIP return rendererFactory.RenderObject("deleted", os.Stdout, nnat) } diff --git a/cmd/delete_prefix.go b/cmd/delete_prefix.go index 5eceb12..4df0d79 100644 --- a/cmd/delete_prefix.go +++ b/cmd/delete_prefix.go @@ -20,7 +20,6 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" @@ -88,8 +87,5 @@ func RunDeletePrefix(ctx context.Context, dpdkClientFactory DPDKClientFactory, r return fmt.Errorf("error deleting prefix: %w", err) } - prefix.TypeMeta.Kind = api.PrefixKind - prefix.PrefixMeta.InterfaceID = opts.InterfaceID - prefix.Spec.Prefix = opts.Prefix return rendererFactory.RenderObject("deleted", os.Stdout, prefix) } diff --git a/cmd/delete_route.go b/cmd/delete_route.go index 73789d4..8e02e35 100644 --- a/cmd/delete_route.go +++ b/cmd/delete_route.go @@ -20,7 +20,6 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" @@ -88,9 +87,5 @@ func RunDeleteRoute(ctx context.Context, dpdkClientFactory DPDKClientFactory, re return fmt.Errorf("error deleting route: %w", err) } - route.TypeMeta.Kind = api.RouteKind - route.RouteMeta.VNI = opts.VNI - route.Spec.Prefix = &opts.Prefix - route.Spec.NextHop = &api.RouteNextHop{} return rendererFactory.RenderObject("deleted", os.Stdout, route) } diff --git a/cmd/delete_virtualip.go b/cmd/delete_virtualip.go index 52e1d38..2cdc5e9 100644 --- a/cmd/delete_virtualip.go +++ b/cmd/delete_virtualip.go @@ -19,7 +19,6 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -84,7 +83,5 @@ func RunDeleteVirtualIP(ctx context.Context, dpdkClientFactory DPDKClientFactory return fmt.Errorf("error deleting virtual ip: %w", err) } - vip.TypeMeta.Kind = api.VirtualIPKind - vip.VirtualIPMeta.InterfaceID = opts.InterfaceID return rendererFactory.RenderObject("deleted", os.Stdout, vip) } diff --git a/cmd/get_firewall_rule.go b/cmd/get_firewall_rule.go index 384738c..ab1243e 100644 --- a/cmd/get_firewall_rule.go +++ b/cmd/get_firewall_rule.go @@ -19,7 +19,6 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -91,8 +90,5 @@ func RunGetFirewallRule( return fmt.Errorf("error getting firewall rule: %w", err) } - fwrule.TypeMeta.Kind = api.FirewallRuleKind - fwrule.FirewallRuleMeta.InterfaceID = opts.InterfaceID - fwrule.Spec.RuleID = opts.RuleID return rendererFactory.RenderObject("", os.Stdout, fwrule) } diff --git a/cmd/get_interface.go b/cmd/get_interface.go index 7a7a990..b97425d 100644 --- a/cmd/get_interface.go +++ b/cmd/get_interface.go @@ -19,7 +19,6 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -100,7 +99,5 @@ func RunGetInterface( } } - iface.TypeMeta.Kind = api.InterfaceKind - iface.InterfaceMeta.ID = opts.ID return rendererFactory.RenderObject("", os.Stdout, iface) } diff --git a/cmd/get_loadbalancer.go b/cmd/get_loadbalancer.go index a6753d9..84ef600 100644 --- a/cmd/get_loadbalancer.go +++ b/cmd/get_loadbalancer.go @@ -19,7 +19,6 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -89,7 +88,5 @@ func RunGetLoadBalancer( return fmt.Errorf("error getting loadbalancer: %w", err) } - lb.TypeMeta.Kind = api.LoadBalancerKind - lb.LoadBalancerMeta.ID = opts.ID return rendererFactory.RenderObject("", os.Stdout, lb) } diff --git a/cmd/get_nat.go b/cmd/get_nat.go index 9b8c6d5..171f695 100644 --- a/cmd/get_nat.go +++ b/cmd/get_nat.go @@ -19,7 +19,6 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -89,7 +88,5 @@ func RunGetNat( return fmt.Errorf("error getting nat: %w", err) } - nat.TypeMeta.Kind = api.NatKind - nat.NatMeta.InterfaceID = opts.InterfaceID return rendererFactory.RenderObject("", os.Stdout, nat) } diff --git a/cmd/get_virtualip.go b/cmd/get_virtualip.go index 563d627..62a588f 100644 --- a/cmd/get_virtualip.go +++ b/cmd/get_virtualip.go @@ -19,7 +19,6 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -89,7 +88,5 @@ func RunGetVirtualIP( return fmt.Errorf("error getting virtual ip: %w", err) } - virtualIP.TypeMeta.Kind = api.VirtualIPKind - virtualIP.VirtualIPMeta.InterfaceID = opts.InterfaceID return rendererFactory.RenderObject("", os.Stdout, virtualIP) } diff --git a/cmd/get_vni.go b/cmd/get_vni.go index 088fe4b..6fb1ab4 100644 --- a/cmd/get_vni.go +++ b/cmd/get_vni.go @@ -19,7 +19,6 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -91,8 +90,5 @@ func RunGetVni( return fmt.Errorf("error getting vni: %w", err) } - vni.TypeMeta.Kind = api.VniKind - vni.VniMeta.VNI = opts.VNI - vni.VniMeta.VniType = opts.VniType return rendererFactory.RenderObject("", os.Stdout, vni) } diff --git a/cmd/list_loadbalancer_targets.go b/cmd/list_loadbalancer_targets.go index 237d426..e089e96 100644 --- a/cmd/list_loadbalancer_targets.go +++ b/cmd/list_loadbalancer_targets.go @@ -19,7 +19,6 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" "github.com/spf13/cobra" @@ -89,6 +88,5 @@ func RunListLoadBalancerTargets( return fmt.Errorf("error listing loadbalancer targets: %w", err) } - lbtargets.TypeMeta.Kind = api.LoadBalancerTargetListKind return rendererFactory.RenderList("", os.Stdout, lbtargets) } diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go index 44cd1a5..0eb6b8b 100644 --- a/dpdk/api/conversion.go +++ b/dpdk/api/conversion.go @@ -194,6 +194,7 @@ func ProtoVirtualIPToVirtualIP(interfaceID string, dpdkVIP *proto.InterfaceVIPIP IP: ip, UnderlayRoute: &underlayRoute, }, + Status: ProtoStatusToStatus(dpdkVIP.Status), }, nil } diff --git a/dpdk/client/client.go b/dpdk/client/client.go index b907e4f..a32b153 100644 --- a/dpdk/client/client.go +++ b/dpdk/client/client.go @@ -87,8 +87,13 @@ func (c *client) GetLoadBalancer(ctx context.Context, id string) (*api.LoadBalan if err != nil { return &api.LoadBalancer{}, err } + retLoadBalancer := &api.LoadBalancer{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerKind}, + LoadBalancerMeta: api.LoadBalancerMeta{ID: id}, + Status: api.ProtoStatusToStatus(res.Status), + } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError + return retLoadBalancer, apierrors.ErrServerError } return api.ProtoLoadBalancerToLoadBalancer(res, id) } @@ -108,22 +113,23 @@ func (c *client) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) ( if err != nil { return &api.LoadBalancer{}, err } + retLoadBalancer := &api.LoadBalancer{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerKind}, + LoadBalancerMeta: lb.LoadBalancerMeta, + Status: api.ProtoStatusToStatus(res.Status), + } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError + return retLoadBalancer, apierrors.ErrServerError } underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) if err != nil { - return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res.Status)}, fmt.Errorf("error parsing underlay route: %w", err) + return retLoadBalancer, fmt.Errorf("error parsing underlay route: %w", err) } - lb.Spec.UnderlayRoute = &underlayRoute + retLoadBalancer.Spec = lb.Spec + retLoadBalancer.Spec.UnderlayRoute = &underlayRoute - return &api.LoadBalancer{ - TypeMeta: api.TypeMeta{Kind: api.LoadBalancerKind}, - LoadBalancerMeta: lb.LoadBalancerMeta, - Spec: lb.Spec, - Status: api.ProtoStatusToStatus(res.Status), - }, nil + return retLoadBalancer, nil } func (c *client) DeleteLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) { @@ -131,10 +137,15 @@ func (c *client) DeleteLoadBalancer(ctx context.Context, id string) (*api.LoadBa if err != nil { return &api.LoadBalancer{}, err } + retLoadBalancer := &api.LoadBalancer{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerKind}, + LoadBalancerMeta: api.LoadBalancerMeta{ID: id}, + Status: api.ProtoStatusToStatus(res), + } if errorCode := res.GetError(); errorCode != 0 { - return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError + return retLoadBalancer, apierrors.ErrServerError } - return &api.LoadBalancer{Status: api.ProtoStatusToStatus(res)}, nil + return retLoadBalancer, nil } func (c *client) ListLoadBalancerPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) { @@ -177,22 +188,23 @@ func (c *client) CreateLoadBalancerPrefix(ctx context.Context, lbprefix *api.Loa if err != nil { return &api.LoadBalancerPrefix{}, err } - if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.LoadBalancerPrefix{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError - } - underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) - if err != nil { - return &api.LoadBalancerPrefix{Status: api.ProtoStatusToStatus(res.Status)}, fmt.Errorf("error parsing underlay route: %w", err) - } - return &api.LoadBalancerPrefix{ + retLBPrefix := &api.LoadBalancerPrefix{ TypeMeta: api.TypeMeta{Kind: api.LoadBalancerPrefixKind}, LoadBalancerPrefixMeta: lbprefix.LoadBalancerPrefixMeta, Spec: api.LoadBalancerPrefixSpec{ - Prefix: lbprefix.Spec.Prefix, - UnderlayRoute: &underlayRoute, + Prefix: lbprefix.Spec.Prefix, }, Status: api.ProtoStatusToStatus(res.Status), - }, nil + } + if errorCode := res.GetStatus().GetError(); errorCode != 0 { + return retLBPrefix, apierrors.ErrServerError + } + underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) + if err != nil { + return retLBPrefix, fmt.Errorf("error parsing underlay route: %w", err) + } + retLBPrefix.Spec.UnderlayRoute = &underlayRoute + return retLBPrefix, nil } func (c *client) DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) (*api.LoadBalancerPrefix, error) { @@ -209,10 +221,16 @@ func (c *client) DeleteLoadBalancerPrefix(ctx context.Context, interfaceID strin if err != nil { return &api.LoadBalancerPrefix{}, err } + retLBPrefix := &api.LoadBalancerPrefix{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerPrefixKind}, + LoadBalancerPrefixMeta: api.LoadBalancerPrefixMeta{InterfaceID: interfaceID}, + Spec: api.LoadBalancerPrefixSpec{Prefix: prefix}, + Status: api.ProtoStatusToStatus(res), + } if errorCode := res.GetError(); errorCode != 0 { - return &api.LoadBalancerPrefix{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError + return retLBPrefix, apierrors.ErrServerError } - return &api.LoadBalancerPrefix{Status: api.ProtoStatusToStatus(res)}, nil + return retLBPrefix, nil } func (c *client) GetLoadBalancerTargets(ctx context.Context, loadBalancerID string) (*api.LoadBalancerTargetList, error) { @@ -223,7 +241,9 @@ func (c *client) GetLoadBalancerTargets(ctx context.Context, loadBalancerID stri return &api.LoadBalancerTargetList{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.LoadBalancerTargetList{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError + return &api.LoadBalancerTargetList{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetListKind}, + Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } lbtargets := make([]api.LoadBalancerTarget, len(res.GetTargetIPs())) @@ -253,42 +273,47 @@ func (c *client) AddLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBa if err != nil { return &api.LoadBalancerTarget{}, err } - if errorCode := res.GetError(); errorCode != 0 { - return &api.LoadBalancerTarget{ - TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, - LoadBalancerTargetMeta: lbtarget.LoadBalancerTargetMeta, - Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError - } - - return &api.LoadBalancerTarget{ + retLBTarget := &api.LoadBalancerTarget{ TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, LoadBalancerTargetMeta: lbtarget.LoadBalancerTargetMeta, - Spec: lbtarget.Spec, Status: api.ProtoStatusToStatus(res), - }, nil + } + if errorCode := res.GetError(); errorCode != 0 { + return retLBTarget, apierrors.ErrServerError + } + retLBTarget.Spec = lbtarget.Spec + return retLBTarget, nil } -func (c *client) DeleteLoadBalancerTarget(ctx context.Context, id string, targetIP netip.Addr) (*api.LoadBalancerTarget, error) { +func (c *client) DeleteLoadBalancerTarget(ctx context.Context, lbid string, targetIP netip.Addr) (*api.LoadBalancerTarget, error) { res, err := c.DPDKonmetalClient.DeleteLoadBalancerTarget(ctx, &dpdkproto.DeleteLoadBalancerTargetRequest{ - LoadBalancerID: []byte(id), + LoadBalancerID: []byte(lbid), TargetIP: api.LbipToProtoLbip(targetIP), }) if err != nil { return &api.LoadBalancerTarget{}, err } + retLBTarget := &api.LoadBalancerTarget{ + TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, + LoadBalancerTargetMeta: api.LoadBalancerTargetMeta{LoadbalancerID: lbid}, + Status: api.ProtoStatusToStatus(res), + } if errorCode := res.GetError(); errorCode != 0 { - return &api.LoadBalancerTarget{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError + return retLBTarget, apierrors.ErrServerError } - return &api.LoadBalancerTarget{Status: api.ProtoStatusToStatus(res)}, nil + return retLBTarget, nil } -func (c *client) GetInterface(ctx context.Context, name string) (*api.Interface, error) { - res, err := c.DPDKonmetalClient.GetInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(name)}) +func (c *client) GetInterface(ctx context.Context, id string) (*api.Interface, error) { + res, err := c.DPDKonmetalClient.GetInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(id)}) if err != nil { return &api.Interface{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.Interface{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError + return &api.Interface{ + TypeMeta: api.TypeMeta{Kind: api.InterfaceKind}, + InterfaceMeta: api.InterfaceMeta{ID: id}, + Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } return api.ProtoInterfaceToInterface(res.GetInterface()) } @@ -333,13 +358,18 @@ func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*ap if err != nil { return &api.Interface{}, err } + retInterface := &api.Interface{ + TypeMeta: iface.TypeMeta, + InterfaceMeta: iface.InterfaceMeta, + Status: api.ProtoStatusToStatus(res.Response.Status), + } if errorCode := res.GetResponse().GetStatus().GetError(); errorCode != 0 { - return &api.Interface{Status: api.ProtoStatusToStatus(res.Response.Status)}, apierrors.ErrServerError + return retInterface, apierrors.ErrServerError } underlayRoute, err := netip.ParseAddr(string(res.GetResponse().GetUnderlayRoute())) if err != nil { - return &api.Interface{Status: api.ProtoStatusToStatus(res.Response.Status)}, fmt.Errorf("error parsing underlay route: %w", err) + return retInterface, fmt.Errorf("error parsing underlay route: %w", err) } return &api.Interface{ @@ -363,28 +393,36 @@ func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*ap }, nil } -func (c *client) DeleteInterface(ctx context.Context, name string) (*api.Interface, error) { - res, err := c.DPDKonmetalClient.DeleteInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(name)}) +func (c *client) DeleteInterface(ctx context.Context, id string) (*api.Interface, error) { + res, err := c.DPDKonmetalClient.DeleteInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(id)}) if err != nil { return &api.Interface{}, err } + retInterface := &api.Interface{ + TypeMeta: api.TypeMeta{Kind: api.InterfaceKind}, + InterfaceMeta: api.InterfaceMeta{ID: id}, + Status: api.ProtoStatusToStatus(res), + } if errorCode := res.GetError(); errorCode != 0 { - return &api.Interface{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError + return retInterface, apierrors.ErrServerError } - return &api.Interface{Status: api.ProtoStatusToStatus(res)}, nil + return retInterface, nil } -func (c *client) GetVirtualIP(ctx context.Context, interfaceName string) (*api.VirtualIP, error) { +func (c *client) GetVirtualIP(ctx context.Context, interfaceID string) (*api.VirtualIP, error) { res, err := c.DPDKonmetalClient.GetInterfaceVIP(ctx, &dpdkproto.InterfaceIDMsg{ - InterfaceID: []byte(interfaceName), + InterfaceID: []byte(interfaceID), }) if err != nil { return &api.VirtualIP{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.VirtualIP{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError + return &api.VirtualIP{ + TypeMeta: api.TypeMeta{Kind: api.VirtualIPKind}, + VirtualIPMeta: api.VirtualIPMeta{InterfaceID: interfaceID}, + Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } - return api.ProtoVirtualIPToVirtualIP(interfaceName, res) + return api.ProtoVirtualIPToVirtualIP(interfaceID, res) } func (c *client) AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*api.VirtualIP, error) { @@ -398,23 +436,23 @@ func (c *client) AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*a if err != nil { return &api.VirtualIP{}, err } - if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.VirtualIP{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError - } - underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) - if err != nil { - return &api.VirtualIP{Status: api.ProtoStatusToStatus(res.Status)}, fmt.Errorf("error parsing underlay route: %w", err) - } - - return &api.VirtualIP{ + retVirtualIP := &api.VirtualIP{ TypeMeta: api.TypeMeta{Kind: api.VirtualIPKind}, VirtualIPMeta: virtualIP.VirtualIPMeta, Spec: api.VirtualIPSpec{ - IP: virtualIP.Spec.IP, - UnderlayRoute: &underlayRoute, + IP: virtualIP.Spec.IP, }, Status: api.ProtoStatusToStatus(res.Status), - }, nil + } + if errorCode := res.GetStatus().GetError(); errorCode != 0 { + return retVirtualIP, apierrors.ErrServerError + } + underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) + if err != nil { + return retVirtualIP, fmt.Errorf("error parsing underlay route: %w", err) + } + retVirtualIP.Spec.UnderlayRoute = &underlayRoute + return retVirtualIP, nil } func (c *client) DeleteVirtualIP(ctx context.Context, interfaceID string) (*api.VirtualIP, error) { @@ -424,10 +462,14 @@ func (c *client) DeleteVirtualIP(ctx context.Context, interfaceID string) (*api. if err != nil { return &api.VirtualIP{}, err } + retVirtualIP := &api.VirtualIP{ + TypeMeta: api.TypeMeta{Kind: api.VirtualIPKind}, + VirtualIPMeta: api.VirtualIPMeta{InterfaceID: interfaceID}, + } if errorCode := res.GetError(); errorCode != 0 { - return &api.VirtualIP{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError + return retVirtualIP, apierrors.ErrServerError } - return &api.VirtualIP{Status: api.ProtoStatusToStatus(res)}, nil + return retVirtualIP, nil } func (c *client) ListPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) { @@ -469,21 +511,22 @@ func (c *client) AddPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix if err != nil { return &api.Prefix{}, err } + retPrefix := &api.Prefix{ + TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, + PrefixMeta: prefix.PrefixMeta, + Spec: api.PrefixSpec{Prefix: prefix.Spec.Prefix}, + Status: api.ProtoStatusToStatus(res.Status), + } + if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.Prefix{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError + return retPrefix, apierrors.ErrServerError } underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) if err != nil { - return &api.Prefix{Status: api.ProtoStatusToStatus(res.Status)}, fmt.Errorf("error parsing underlay route: %w", err) + return retPrefix, fmt.Errorf("error parsing underlay route: %w", err) } - return &api.Prefix{ - TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, - PrefixMeta: prefix.PrefixMeta, - Spec: api.PrefixSpec{ - Prefix: prefix.Spec.Prefix, - UnderlayRoute: &underlayRoute}, - Status: api.ProtoStatusToStatus(res.Status), - }, nil + retPrefix.Spec.UnderlayRoute = &underlayRoute + return retPrefix, nil } func (c *client) DeletePrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) (*api.Prefix, error) { @@ -500,10 +543,16 @@ func (c *client) DeletePrefix(ctx context.Context, interfaceID string, prefix ne if err != nil { return &api.Prefix{}, err } + retPrefix := &api.Prefix{ + TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, + PrefixMeta: api.PrefixMeta{InterfaceID: interfaceID}, + Spec: api.PrefixSpec{Prefix: prefix}, + Status: api.ProtoStatusToStatus(res), + } if errorCode := res.GetError(); errorCode != 0 { - return &api.Prefix{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError + return retPrefix, apierrors.ErrServerError } - return &api.Prefix{Status: api.ProtoStatusToStatus(res)}, nil + return retPrefix, nil } func (c *client) AddRoute(ctx context.Context, route *api.Route) (*api.Route, error) { @@ -524,18 +573,19 @@ func (c *client) AddRoute(ctx context.Context, route *api.Route) (*api.Route, er if err != nil { return &api.Route{}, err } - if errorCode := res.GetError(); errorCode != 0 { - return &api.Route{ - Spec: api.RouteSpec{NextHop: &api.RouteNextHop{}}, - Status: api.ProtoStatusToStatus(res), - }, apierrors.ErrServerError - } - return &api.Route{ + retRoute := &api.Route{ TypeMeta: api.TypeMeta{Kind: api.RouteKind}, RouteMeta: route.RouteMeta, - Spec: route.Spec, - Status: api.ProtoStatusToStatus(res), - }, nil + Spec: api.RouteSpec{ + Prefix: route.Spec.Prefix, + NextHop: &api.RouteNextHop{}}, + Status: api.ProtoStatusToStatus(res), + } + if errorCode := res.GetError(); errorCode != 0 { + return retRoute, apierrors.ErrServerError + } + retRoute.Spec = route.Spec + return retRoute, nil } func (c *client) DeleteRoute(ctx context.Context, vni uint32, prefix netip.Prefix) (*api.Route, error) { @@ -554,10 +604,19 @@ func (c *client) DeleteRoute(ctx context.Context, vni uint32, prefix netip.Prefi if err != nil { return &api.Route{}, err } + retRoute := &api.Route{ + TypeMeta: api.TypeMeta{Kind: api.RouteKind}, + RouteMeta: api.RouteMeta{VNI: vni}, + Spec: api.RouteSpec{ + Prefix: &prefix, + NextHop: &api.RouteNextHop{}, + }, + Status: api.ProtoStatusToStatus(res), + } if errorCode := res.GetError(); errorCode != 0 { - return &api.Route{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError + return retRoute, apierrors.ErrServerError } - return &api.Route{Status: api.ProtoStatusToStatus(res)}, nil + return retRoute, nil } func (c *client) ListRoutes(ctx context.Context, vni uint32) (*api.RouteList, error) { @@ -591,7 +650,10 @@ func (c *client) GetNat(ctx context.Context, interfaceID string) (*api.Nat, erro return &api.Nat{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.Nat{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError + return &api.Nat{ + TypeMeta: api.TypeMeta{Kind: api.NatKind}, + NatMeta: api.NatMeta{InterfaceID: interfaceID}, + Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } return api.ProtoNatToNat(res, interfaceID) } @@ -609,23 +671,23 @@ func (c *client) AddNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) { if err != nil { return &api.Nat{}, err } + retNat := &api.Nat{ + TypeMeta: api.TypeMeta{Kind: api.NatKind}, + NatMeta: nat.NatMeta, + Status: api.ProtoStatusToStatus(res.Status), + } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.Nat{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError + return retNat, apierrors.ErrServerError } underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) if err != nil { - return &api.Nat{Status: api.ProtoStatusToStatus(res.Status)}, fmt.Errorf("error parsing underlay route: %w", err) + return retNat, fmt.Errorf("error parsing underlay route: %w", err) } - nat.Spec.UnderlayRoute = &underlayRoute - status := api.ProtoStatusToStatus(res.Status) - return &api.Nat{ - TypeMeta: api.TypeMeta{Kind: api.NatKind}, - NatMeta: nat.NatMeta, - Spec: nat.Spec, - Status: status, - }, nil + retNat.Spec = nat.Spec + retNat.Spec.UnderlayRoute = &underlayRoute + return retNat, nil } func (c *client) DeleteNat(ctx context.Context, interfaceID string) (*api.Nat, error) { @@ -635,10 +697,15 @@ func (c *client) DeleteNat(ctx context.Context, interfaceID string) (*api.Nat, e if err != nil { return &api.Nat{}, err } + retNat := &api.Nat{ + TypeMeta: api.TypeMeta{Kind: api.NatKind}, + NatMeta: api.NatMeta{InterfaceID: interfaceID}, + Status: api.ProtoStatusToStatus(res), + } if errorCode := res.GetError(); errorCode != 0 { - return &api.Nat{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError + return retNat, apierrors.ErrServerError } - return &api.Nat{Status: api.ProtoStatusToStatus(res)}, nil + return retNat, nil } func (c *client) AddNeighborNat(ctx context.Context, nNat *api.NeighborNat) (*api.NeighborNat, error) { @@ -656,15 +723,16 @@ func (c *client) AddNeighborNat(ctx context.Context, nNat *api.NeighborNat) (*ap if err != nil { return &api.NeighborNat{}, err } - if errorCode := res.GetError(); errorCode != 0 { - return &api.NeighborNat{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError - } - return &api.NeighborNat{ + retnNat := &api.NeighborNat{ TypeMeta: api.TypeMeta{Kind: api.NeighborNatKind}, NeighborNatMeta: nNat.NeighborNatMeta, - Spec: nNat.Spec, Status: api.ProtoStatusToStatus(res), - }, nil + } + if errorCode := res.GetError(); errorCode != 0 { + return retnNat, apierrors.ErrServerError + } + retnNat.Spec = nNat.Spec + return retnNat, nil } func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType string) (*api.NatList, error) { @@ -758,10 +826,15 @@ func (c *client) DeleteNeighborNat(ctx context.Context, neigbhorNat api.Neighbor if err != nil { return &api.NeighborNat{}, err } + nnat := &api.NeighborNat{ + TypeMeta: api.TypeMeta{Kind: api.NeighborNatKind}, + NeighborNatMeta: neigbhorNat.NeighborNatMeta, + Status: api.ProtoStatusToStatus(res), + } if errorCode := res.GetError(); errorCode != 0 { - return &api.NeighborNat{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError + return nnat, apierrors.ErrServerError } - return &api.NeighborNat{Status: api.ProtoStatusToStatus(res)}, nil + return nnat, nil } func (c *client) ListFirewallRules(ctx context.Context, interfaceID string) (*api.FirewallRuleList, error) { @@ -850,18 +923,16 @@ func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) if err != nil { return &api.FirewallRule{}, err } + retFwrule := &api.FirewallRule{ + TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, + FirewallRuleMeta: api.FirewallRuleMeta{InterfaceID: fwRule.InterfaceID}, + Spec: api.FirewallRuleSpec{RuleID: fwRule.Spec.RuleID}, + Status: api.ProtoStatusToStatus(res.Status)} if res.Status.Error != 0 { - return &api.FirewallRule{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError + return retFwrule, apierrors.ErrServerError } - - return &api.FirewallRule{ - TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, - FirewallRuleMeta: api.FirewallRuleMeta{ - InterfaceID: fwRule.InterfaceID, - }, - Spec: fwRule.Spec, - Status: api.ProtoStatusToStatus(res.Status), - }, nil + retFwrule.Spec = fwRule.Spec + return retFwrule, nil } func (c *client) GetFirewallRule(ctx context.Context, ruleID string, interfaceID string) (*api.FirewallRule, error) { @@ -873,7 +944,11 @@ func (c *client) GetFirewallRule(ctx context.Context, ruleID string, interfaceID return &api.FirewallRule{}, err } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.FirewallRule{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError + return &api.FirewallRule{ + TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, + FirewallRuleMeta: api.FirewallRuleMeta{InterfaceID: interfaceID}, + Spec: api.FirewallRuleSpec{RuleID: ruleID}, + Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError } return api.ProtoFwRuleToFwRule(res.Rule, interfaceID) @@ -887,10 +962,16 @@ func (c *client) DeleteFirewallRule(ctx context.Context, interfaceID string, rul if err != nil { return &api.FirewallRule{}, err } + retFwrule := &api.FirewallRule{ + TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, + FirewallRuleMeta: api.FirewallRuleMeta{InterfaceID: interfaceID}, + Spec: api.FirewallRuleSpec{RuleID: ruleID}, + Status: api.ProtoStatusToStatus(res), + } if errorCode := res.GetError(); errorCode != 0 { - return &api.FirewallRule{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError + return retFwrule, apierrors.ErrServerError } - return &api.FirewallRule{Status: api.ProtoStatusToStatus(res)}, nil + return retFwrule, nil } func (c *client) Initialized(ctx context.Context) (string, error) { @@ -920,13 +1001,14 @@ func (c *client) GetVni(ctx context.Context, vni uint32, vniType uint8) (*api.Vn if err != nil { return &api.Vni{}, err } + retVni := &api.Vni{ + TypeMeta: api.TypeMeta{Kind: api.VniKind}, + VniMeta: api.VniMeta{VNI: vni, VniType: vniType}, + Status: api.ProtoStatusToStatus(res.Status), + } if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.Vni{Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError + return retVni, apierrors.ErrServerError } - - return &api.Vni{TypeMeta: api.TypeMeta{Kind: api.VniKind}, - VniMeta: api.VniMeta{VNI: vni, VniType: vniType}, - Spec: api.VniSpec{InUse: res.InUse}, - Status: api.ProtoStatusToStatus(res.Status), - }, nil + retVni.Spec.InUse = res.InUse + return retVni, nil } From 161d317fd528503c4e3045c30899d2e875d0a323 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 13 Jun 2023 11:40:11 +0200 Subject: [PATCH 57/65] move the grpc client logic to net-dpservice-go --- .vscode/launch.json | 4 +- cmd/add.go | 6 +- cmd/add_firewall_rule.go | 4 +- cmd/add_loadbalancer_target.go | 4 +- cmd/add_nat.go | 4 +- cmd/add_neighbor_nat.go | 4 +- cmd/add_prefix.go | 4 +- cmd/add_route.go | 4 +- cmd/add_virtualip.go | 4 +- cmd/command.go | 1 + cmd/common.go | 6 +- cmd/create_interface.go | 4 +- cmd/create_loadbalancer.go | 4 +- cmd/create_loadbalancer_prefix.go | 4 +- cmd/delete.go | 6 +- cmd/delete_firewall_rule.go | 2 +- cmd/delete_interface.go | 2 +- cmd/delete_loadbalancer.go | 2 +- cmd/delete_loadbalancer_prefix.go | 2 +- cmd/delete_loadbalancer_target.go | 2 +- cmd/delete_nat.go | 2 +- cmd/delete_neighbor_nat.go | 4 +- cmd/delete_prefix.go | 2 +- cmd/delete_route.go | 2 +- cmd/delete_virtualip.go | 2 +- cmd/get_firewall_rule.go | 2 +- cmd/get_interface.go | 2 +- cmd/get_loadbalancer.go | 2 +- cmd/get_nat.go | 2 +- cmd/get_virtualip.go | 2 +- cmd/get_vni.go | 2 +- cmd/init.go | 2 +- cmd/initialized.go | 2 +- cmd/list_loadbalancer_targets.go | 2 +- cmd/list_nats.go | 4 +- netiputil/netiputil.go => cmd/reset.go | 41 +- cmd/reset_vni.go | 94 +++ dpdk/api/conversion.go | 354 --------- dpdk/api/errors/errors.go | 161 ---- dpdk/api/types.go | 546 ------------- dpdk/client/client.go | 1014 ------------------------ dpdk/client/dynamic/dynamic.go | 4 +- dpdk/{api => runtime}/register.go | 40 +- go.mod | 6 +- go.sum | 4 +- main.go | 2 +- renderer/renderer.go | 2 +- sources/sources.go | 11 +- 48 files changed, 206 insertions(+), 2180 deletions(-) rename netiputil/netiputil.go => cmd/reset.go (51%) create mode 100644 cmd/reset_vni.go delete mode 100644 dpdk/api/conversion.go delete mode 100644 dpdk/api/errors/errors.go delete mode 100644 dpdk/api/types.go delete mode 100644 dpdk/client/client.go rename dpdk/{api => runtime}/register.go (60%) diff --git a/.vscode/launch.json b/.vscode/launch.json index 808fd73..e4ab736 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -24,9 +24,9 @@ // "--priority=100", "--rule-id=9", "--src=1.1.1.1/32", "--protocol=tcp"] //"args": ["add", "nnat", "10.20.30.40", "--vni", "100", "--minport", "30000", "--maxport", "30100", "--underlayroute", "ff80::1"] //"args": ["add", "interface", "--id=vm4", "--ip=10.200.1.4", "--ip=2000:200:1::4", "--vni=200", "--device=net_tap5"] - "args": ["delete", "-f", "/tmp/vip.yaml"] + //"args": ["delete", "-f", "/tmp/vip.yaml"] //"args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=11"] - //"args": ["list", "interfaces", "--wide"] + "args": ["list", "interfaces", "--wide"] //"args": ["get", "vni", "--vni=100", "--vni-type=0"] //"args": ["list", "prefixes","--interface-id=vm1"] //"args": ["list", "lbtargets","--lb-id=4"] diff --git a/cmd/add.go b/cmd/add.go index 609c72d..fa9e23d 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -20,10 +20,10 @@ import ( "os" "reflect" - "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/dpdk/client/dynamic" + "github.com/onmetal/dpservice-cli/dpdk/runtime" "github.com/onmetal/dpservice-cli/sources" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" ) @@ -95,7 +95,7 @@ func RunAdd( return fmt.Errorf("error creating sources iterator: %w", err) } - objs, err := sources.CollectObjects(iterator, api.DefaultScheme) + objs, err := sources.CollectObjects(iterator, runtime.DefaultScheme) if err != nil { return fmt.Errorf("error collecting objects: %w", err) } diff --git a/cmd/add_firewall_rule.go b/cmd/add_firewall_rule.go index 17f0820..0303b0f 100644 --- a/cmd/add_firewall_rule.go +++ b/cmd/add_firewall_rule.go @@ -20,10 +20,10 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/api" + "github.com/onmetal/net-dpservice-go/errors" dpdkproto "github.com/onmetal/net-dpservice-go/proto" "github.com/spf13/cobra" "github.com/spf13/pflag" diff --git a/cmd/add_loadbalancer_target.go b/cmd/add_loadbalancer_target.go index 50ab308..e4139c4 100644 --- a/cmd/add_loadbalancer_target.go +++ b/cmd/add_loadbalancer_target.go @@ -20,10 +20,10 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/api" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/add_nat.go b/cmd/add_nat.go index a89e352..ce654ab 100644 --- a/cmd/add_nat.go +++ b/cmd/add_nat.go @@ -20,10 +20,10 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/api" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/add_neighbor_nat.go b/cmd/add_neighbor_nat.go index 621dca8..2031807 100644 --- a/cmd/add_neighbor_nat.go +++ b/cmd/add_neighbor_nat.go @@ -20,10 +20,10 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/api" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/add_prefix.go b/cmd/add_prefix.go index e4959dc..dcd0497 100644 --- a/cmd/add_prefix.go +++ b/cmd/add_prefix.go @@ -20,10 +20,10 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/api" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/add_route.go b/cmd/add_route.go index 6758c6a..0a45e53 100644 --- a/cmd/add_route.go +++ b/cmd/add_route.go @@ -20,10 +20,10 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/api" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/add_virtualip.go b/cmd/add_virtualip.go index f8d56f1..cdb9495 100644 --- a/cmd/add_virtualip.go +++ b/cmd/add_virtualip.go @@ -20,10 +20,10 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/api" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/command.go b/cmd/command.go index c1b763a..36a4db4 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -38,6 +38,7 @@ func Command() *cobra.Command { Get(dpdkClientOptions), List(dpdkClientOptions), Delete(dpdkClientOptions), + Reset(dpdkClientOptions), Initialized(dpdkClientOptions, rendererOptions), Init(dpdkClientOptions, rendererOptions), completionCmd, diff --git a/cmd/common.go b/cmd/common.go index 791dd55..39520c4 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -23,11 +23,11 @@ import ( "strconv" "time" - "github.com/onmetal/dpservice-cli/dpdk/api" - apierrors "github.com/onmetal/dpservice-cli/dpdk/api/errors" - "github.com/onmetal/dpservice-cli/dpdk/client" "github.com/onmetal/dpservice-cli/renderer" "github.com/onmetal/dpservice-cli/sources" + "github.com/onmetal/net-dpservice-go/api" + "github.com/onmetal/net-dpservice-go/client" + apierrors "github.com/onmetal/net-dpservice-go/errors" dpdkproto "github.com/onmetal/net-dpservice-go/proto" "github.com/spf13/cobra" "github.com/spf13/pflag" diff --git a/cmd/create_interface.go b/cmd/create_interface.go index 3367c5d..b213d34 100644 --- a/cmd/create_interface.go +++ b/cmd/create_interface.go @@ -20,10 +20,10 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/api" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/create_loadbalancer.go b/cmd/create_loadbalancer.go index cad6968..501ae14 100644 --- a/cmd/create_loadbalancer.go +++ b/cmd/create_loadbalancer.go @@ -20,10 +20,10 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/api" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/create_loadbalancer_prefix.go b/cmd/create_loadbalancer_prefix.go index 9ea9760..e763ec9 100644 --- a/cmd/create_loadbalancer_prefix.go +++ b/cmd/create_loadbalancer_prefix.go @@ -20,10 +20,10 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/api" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/delete.go b/cmd/delete.go index 8fe0add..f622c53 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -20,10 +20,10 @@ import ( "os" "reflect" - "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/dpdk/client/dynamic" + "github.com/onmetal/dpservice-cli/dpdk/runtime" "github.com/onmetal/dpservice-cli/sources" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" ) @@ -94,7 +94,7 @@ func RunDelete( return fmt.Errorf("error creating sources iterator: %w", err) } - objs, err := sources.CollectObjects(iterator, api.DefaultScheme) + objs, err := sources.CollectObjects(iterator, runtime.DefaultScheme) if err != nil { return fmt.Errorf("error collecting objects: %w", err) } diff --git a/cmd/delete_firewall_rule.go b/cmd/delete_firewall_rule.go index 19d443b..65c700d 100644 --- a/cmd/delete_firewall_rule.go +++ b/cmd/delete_firewall_rule.go @@ -19,8 +19,8 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/delete_interface.go b/cmd/delete_interface.go index 206fb5e..03e7457 100644 --- a/cmd/delete_interface.go +++ b/cmd/delete_interface.go @@ -19,8 +19,8 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/delete_loadbalancer.go b/cmd/delete_loadbalancer.go index ed21d29..6f30630 100644 --- a/cmd/delete_loadbalancer.go +++ b/cmd/delete_loadbalancer.go @@ -19,8 +19,8 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/delete_loadbalancer_prefix.go b/cmd/delete_loadbalancer_prefix.go index 2876f66..be2a7ef 100644 --- a/cmd/delete_loadbalancer_prefix.go +++ b/cmd/delete_loadbalancer_prefix.go @@ -20,9 +20,9 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/delete_loadbalancer_target.go b/cmd/delete_loadbalancer_target.go index 9a211d2..4259545 100644 --- a/cmd/delete_loadbalancer_target.go +++ b/cmd/delete_loadbalancer_target.go @@ -20,9 +20,9 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/delete_nat.go b/cmd/delete_nat.go index 4a05bb6..20de1ae 100644 --- a/cmd/delete_nat.go +++ b/cmd/delete_nat.go @@ -19,8 +19,8 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/delete_neighbor_nat.go b/cmd/delete_neighbor_nat.go index 5bda5b7..f73eba0 100644 --- a/cmd/delete_neighbor_nat.go +++ b/cmd/delete_neighbor_nat.go @@ -20,10 +20,10 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/api" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/delete_prefix.go b/cmd/delete_prefix.go index 4df0d79..ee85560 100644 --- a/cmd/delete_prefix.go +++ b/cmd/delete_prefix.go @@ -20,9 +20,9 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/delete_route.go b/cmd/delete_route.go index 8e02e35..a0b1e1b 100644 --- a/cmd/delete_route.go +++ b/cmd/delete_route.go @@ -20,9 +20,9 @@ import ( "net/netip" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/flag" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/delete_virtualip.go b/cmd/delete_virtualip.go index 2cdc5e9..65163f8 100644 --- a/cmd/delete_virtualip.go +++ b/cmd/delete_virtualip.go @@ -19,8 +19,8 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/get_firewall_rule.go b/cmd/get_firewall_rule.go index ab1243e..96cd00f 100644 --- a/cmd/get_firewall_rule.go +++ b/cmd/get_firewall_rule.go @@ -19,8 +19,8 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/get_interface.go b/cmd/get_interface.go index b97425d..2475f90 100644 --- a/cmd/get_interface.go +++ b/cmd/get_interface.go @@ -19,8 +19,8 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/get_loadbalancer.go b/cmd/get_loadbalancer.go index 84ef600..6d560e4 100644 --- a/cmd/get_loadbalancer.go +++ b/cmd/get_loadbalancer.go @@ -19,8 +19,8 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/get_nat.go b/cmd/get_nat.go index 171f695..b079da3 100644 --- a/cmd/get_nat.go +++ b/cmd/get_nat.go @@ -19,8 +19,8 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/get_virtualip.go b/cmd/get_virtualip.go index 62a588f..ff14031 100644 --- a/cmd/get_virtualip.go +++ b/cmd/get_virtualip.go @@ -19,8 +19,8 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/get_vni.go b/cmd/get_vni.go index 6fb1ab4..930cd2b 100644 --- a/cmd/get_vni.go +++ b/cmd/get_vni.go @@ -19,8 +19,8 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/init.go b/cmd/init.go index 2ccd573..1d0dac6 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -19,8 +19,8 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" dpdkproto "github.com/onmetal/net-dpservice-go/proto" "github.com/spf13/cobra" "github.com/spf13/pflag" diff --git a/cmd/initialized.go b/cmd/initialized.go index 5b81bf6..e511f62 100644 --- a/cmd/initialized.go +++ b/cmd/initialized.go @@ -19,7 +19,7 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/net-dpservice-go/api" "github.com/spf13/cobra" ) diff --git a/cmd/list_loadbalancer_targets.go b/cmd/list_loadbalancer_targets.go index e089e96..a88b2b0 100644 --- a/cmd/list_loadbalancer_targets.go +++ b/cmd/list_loadbalancer_targets.go @@ -19,8 +19,8 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/cmd/list_nats.go b/cmd/list_nats.go index ffffd44..c9d25da 100644 --- a/cmd/list_nats.go +++ b/cmd/list_nats.go @@ -19,8 +19,8 @@ import ( "fmt" "os" - "github.com/onmetal/dpservice-cli/dpdk/api" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" + "github.com/onmetal/net-dpservice-go/api" + "github.com/onmetal/net-dpservice-go/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) diff --git a/netiputil/netiputil.go b/cmd/reset.go similarity index 51% rename from netiputil/netiputil.go rename to cmd/reset.go index 46ebdbc..927f20b 100644 --- a/netiputil/netiputil.go +++ b/cmd/reset.go @@ -12,24 +12,35 @@ // See the License for the specific language governing permissions and // limitations under the License. -package netiputil +package cmd -import "net/netip" +import ( + "fmt" -func FindIPv4(ips []netip.Addr) netip.Addr { - for _, ip := range ips { - if ip.Is4() { - return ip - } + "github.com/spf13/cobra" +) + +func Reset(factory DPDKClientFactory) *cobra.Command { + rendererOptions := &RendererOptions{Output: "name"} + + cmd := &cobra.Command{ + Use: "reset", + Args: cobra.NoArgs, + RunE: SubcommandRequired, } - return netip.Addr{} -} -func FindIPv6(ips []netip.Addr) netip.Addr { - for _, ip := range ips { - if ip.Is6() { - return ip - } + rendererOptions.AddFlags(cmd.PersistentFlags()) + + subcommands := []*cobra.Command{ + ResetVni(factory, rendererOptions), } - return netip.Addr{} + + cmd.Short = fmt.Sprintf("Resets one of %v", CommandNames(subcommands)) + cmd.Long = fmt.Sprintf("Resets one of %v", CommandNames(subcommands)) + + cmd.AddCommand( + subcommands..., + ) + + return cmd } diff --git a/cmd/reset_vni.go b/cmd/reset_vni.go new file mode 100644 index 0000000..f207f9d --- /dev/null +++ b/cmd/reset_vni.go @@ -0,0 +1,94 @@ +// Copyright 2022 OnMetal authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/onmetal/dpservice-cli/util" + "github.com/onmetal/net-dpservice-go/errors" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func ResetVni(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory) *cobra.Command { + var ( + opts ResetVniOptions + ) + + cmd := &cobra.Command{ + Use: "vni <--vni> <--vni-type>", + Short: "Reset vni usage information", + Example: "dpservice-cli reset vni --vni=vm1 --vni-type=0", + Aliases: NatAliases, + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + + return RunResetVni( + cmd.Context(), + dpdkClientFactory, + rendererFactory, + opts, + ) + }, + } + + opts.AddFlags(cmd.Flags()) + + util.Must(opts.MarkRequiredFlags(cmd)) + + return cmd +} + +type ResetVniOptions struct { + VNI uint32 + VniType uint8 +} + +func (o *ResetVniOptions) AddFlags(fs *pflag.FlagSet) { + fs.Uint32Var(&o.VNI, "vni", o.VNI, "VNI to check.") + fs.Uint8Var(&o.VniType, "vni-type", o.VniType, "VNI Type: VniIpv4 = 0/VniIpv6 = 1.") +} + +func (o *ResetVniOptions) MarkRequiredFlags(cmd *cobra.Command) error { + for _, name := range []string{"vni", "vni-type"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } + return nil +} + +func RunResetVni( + ctx context.Context, + dpdkClientFactory DPDKClientFactory, + rendererFactory RendererFactory, + opts ResetVniOptions, +) error { + client, cleanup, err := dpdkClientFactory.NewClient(ctx) + if err != nil { + return fmt.Errorf("error creating dpdk client: %w", err) + } + defer DpdkClose(cleanup) + + vni, err := client.ResetVni(ctx, opts.VNI, opts.VniType) + if err != nil && err != errors.ErrServerError { + return fmt.Errorf("error resetting vni: %w", err) + } + + return rendererFactory.RenderObject("reset", os.Stdout, vni) +} diff --git a/dpdk/api/conversion.go b/dpdk/api/conversion.go deleted file mode 100644 index 0eb6b8b..0000000 --- a/dpdk/api/conversion.go +++ /dev/null @@ -1,354 +0,0 @@ -// Copyright 2022 OnMetal authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package api - -import ( - "fmt" - "net/netip" - "strconv" - "strings" - - proto "github.com/onmetal/net-dpservice-go/proto" -) - -func ProtoLoadBalancerToLoadBalancer(dpdkLB *proto.GetLoadBalancerResponse, lbID string) (*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.GetLbVipIP().Address); lbipString != "" { - var err error - lbip, err = netip.ParseAddr(string(dpdkLB.GetLbVipIP().Address)) - if err != nil { - return nil, fmt.Errorf("error parsing lb ip: %w", err) - } - } - var lbports = make([]LBPort, 0, len(dpdkLB.Lbports)) - var p LBPort - for _, lbport := range dpdkLB.Lbports { - p.Protocol = uint32(lbport.Protocol) - p.Port = lbport.Port - lbports = append(lbports, p) - } - - return &LoadBalancer{ - TypeMeta: TypeMeta{ - Kind: LoadBalancerKind, - }, - LoadBalancerMeta: LoadBalancerMeta{ - ID: lbID, - }, - Spec: LoadBalancerSpec{ - VNI: dpdkLB.Vni, - LbVipIP: &lbip, - Lbports: lbports, - UnderlayRoute: &underlayRoute, - }, - Status: Status{ - Error: dpdkLB.Status.Error, - Message: dpdkLB.Status.Message, - }, - }, nil -} - -func LbipToProtoLbip(lbip netip.Addr) *proto.LBIP { - return &proto.LBIP{IpVersion: NetIPAddrToProtoIPVersion(lbip), Address: []byte(lbip.String())} -} - -func ProtoLbipToLbip(protolbip proto.LBIP) *netip.Addr { - var ip netip.Addr - if lbipString := string(protolbip.Address); lbipString != "" { - var err error - ip, err = netip.ParseAddr(string(protolbip.Address)) - if err != nil { - return nil - } - } - return &ip -} - -func StringLbportToLbport(lbport string) (LBPort, error) { - p := strings.Split(lbport, "/") - protocolName := strings.ToLower(p[0]) - switch protocolName { - case "icmp", "tcp", "udp", "sctp": - protocolName = strings.ToUpper(protocolName) - case "icmpv6": - protocolName = "ICMPv6" - default: - return LBPort{}, fmt.Errorf("unsupported protocol") - } - protocol := proto.Protocol_value[protocolName] - port, err := strconv.Atoi(p[1]) - if err != nil { - return LBPort{}, fmt.Errorf("error parsing port number: %w", err) - } - return LBPort{Protocol: uint32(protocol), Port: uint32(port)}, nil -} - -func ProtoInterfaceToInterface(dpdkIface *proto.Interface) (*Interface, error) { - var ips []netip.Addr - - if ipv4String := string(dpdkIface.GetPrimaryIPv4Address()); ipv4String != "" { - ip, err := netip.ParseAddr(ipv4String) - if err != nil { - return nil, fmt.Errorf("error parsing primary ipv4: %w", err) - } - - ips = append(ips, ip) - } - - if ipv6String := string(dpdkIface.GetPrimaryIPv6Address()); ipv6String != "" { - ip, err := netip.ParseAddr(ipv6String) - if err != nil { - return nil, fmt.Errorf("error parsing primary ipv6: %w", err) - } - - ips = append(ips, ip) - } - - var underlayRoute netip.Addr - if underlayRouteString := string(dpdkIface.GetUnderlayRoute()); underlayRouteString != "" { - var err error - underlayRoute, err = netip.ParseAddr(string(dpdkIface.GetUnderlayRoute())) - if err != nil { - return nil, fmt.Errorf("error parsing underlay ip: %w", err) - } - } - - return &Interface{ - TypeMeta: TypeMeta{ - Kind: InterfaceKind, - }, - InterfaceMeta: InterfaceMeta{ - ID: string(dpdkIface.InterfaceID), - }, - Spec: InterfaceSpec{ - VNI: dpdkIface.GetVni(), - Device: dpdkIface.GetPciDpName(), - IPs: ips, - UnderlayRoute: &underlayRoute, - }, - }, nil -} - -func NetIPAddrToProtoIPVersion(addr netip.Addr) proto.IPVersion { - switch { - case addr.Is4(): - return proto.IPVersion_IPv4 - case addr.Is6(): - return proto.IPVersion_IPv6 - default: - return 0 - } -} - -func NetIPAddrToProtoIPConfig(addr netip.Addr) *proto.IPConfig { - if !addr.IsValid() { - return nil - } - - return &proto.IPConfig{ - IpVersion: NetIPAddrToProtoIPVersion(addr), - PrimaryAddress: []byte(addr.String()), - } -} - -func ProtoVirtualIPToVirtualIP(interfaceID string, dpdkVIP *proto.InterfaceVIPIP) (*VirtualIP, error) { - ip, err := netip.ParseAddr(string(dpdkVIP.GetAddress())) - if err != nil { - return nil, fmt.Errorf("error parsing virtual ip address: %w", err) - } - - underlayRoute, err := netip.ParseAddr(string(dpdkVIP.UnderlayRoute)) - if err != nil { - return nil, fmt.Errorf("error parsing underlay route: %w", err) - } - - return &VirtualIP{ - TypeMeta: TypeMeta{ - Kind: VirtualIPKind, - }, - VirtualIPMeta: VirtualIPMeta{ - InterfaceID: interfaceID, - }, - Spec: VirtualIPSpec{ - IP: ip, - UnderlayRoute: &underlayRoute, - }, - Status: ProtoStatusToStatus(dpdkVIP.Status), - }, nil -} - -func ProtoPrefixToPrefix(interfaceID string, dpdkPrefix *proto.Prefix) (*Prefix, error) { - addr, err := netip.ParseAddr(string(dpdkPrefix.GetAddress())) - if err != nil { - return nil, fmt.Errorf("error parsing dpdk prefix address: %w", err) - } - - prefix := netip.PrefixFrom(addr, int(dpdkPrefix.GetPrefixLength())) - - underlayRoute, err := netip.ParseAddr(string(dpdkPrefix.UnderlayRoute)) - if err != nil { - return nil, fmt.Errorf("error parsing underlay route: %w", err) - } - - return &Prefix{ - TypeMeta: TypeMeta{ - Kind: PrefixKind, - }, - PrefixMeta: PrefixMeta{ - InterfaceID: interfaceID, - }, - Spec: PrefixSpec{ - Prefix: prefix, - UnderlayRoute: &underlayRoute, - }, - }, nil -} - -func ProtoRouteToRoute(vni uint32, dpdkRoute *proto.Route) (*Route, error) { - prefixAddr, err := netip.ParseAddr(string(dpdkRoute.GetPrefix().GetAddress())) - if err != nil { - return nil, fmt.Errorf("error parsing prefix address: %w", err) - } - - prefix := netip.PrefixFrom(prefixAddr, int(dpdkRoute.GetPrefix().GetPrefixLength())) - - nextHopIP, err := netip.ParseAddr(string(dpdkRoute.GetNexthopAddress())) - if err != nil { - return nil, fmt.Errorf("error parsing netxt hop address: %w", err) - } - - return &Route{ - TypeMeta: TypeMeta{ - RouteKind, - }, - RouteMeta: RouteMeta{ - VNI: vni, - }, - Spec: RouteSpec{Prefix: &prefix, - NextHop: &RouteNextHop{ - VNI: dpdkRoute.GetNexthopVNI(), - IP: &nextHopIP, - }}, - }, nil -} - -func ProtoLBPrefixToProtoPrefix(lbprefix proto.LBPrefix) *proto.Prefix { - return &proto.Prefix{ - IpVersion: lbprefix.IpVersion, - Address: lbprefix.Address, - PrefixLength: lbprefix.PrefixLength, - UnderlayRoute: lbprefix.UnderlayRoute, - } -} - -func ProtoNatToNat(dpdkNat *proto.GetNATResponse, interfaceID string) (*Nat, error) { - var underlayRoute netip.Addr - if underlayRouteString := string(dpdkNat.GetUnderlayRoute()); underlayRouteString != "" { - var err error - underlayRoute, err = netip.ParseAddr(string(dpdkNat.GetUnderlayRoute())) - if err != nil { - return nil, fmt.Errorf("error parsing underlay ip: %w", err) - } - } - var natvipip netip.Addr - if natvipipString := string(dpdkNat.GetNatVIPIP().Address); natvipipString != "" { - var err error - natvipip, err = netip.ParseAddr(string(dpdkNat.GetNatVIPIP().Address)) - if err != nil { - return nil, fmt.Errorf("error parsing nat ip: %w", err) - } - } - - return &Nat{ - TypeMeta: TypeMeta{ - Kind: NatKind, - }, - NatMeta: NatMeta{ - InterfaceID: interfaceID, - }, - Spec: NatSpec{ - NatVIPIP: &natvipip, - MinPort: dpdkNat.MinPort, - MaxPort: dpdkNat.MaxPort, - UnderlayRoute: &underlayRoute, - }, - Status: Status{ - Error: dpdkNat.Status.Error, - Message: dpdkNat.Status.Message, - }, - }, nil -} - -func ProtoFwRuleToFwRule(dpdkFwRule *proto.FirewallRule, interfaceID string) (*FirewallRule, error) { - - srcPrefix, err := netip.ParsePrefix(string(dpdkFwRule.SourcePrefix.Address) + "/" + strconv.Itoa(int(dpdkFwRule.SourcePrefix.PrefixLength))) - if err != nil { - return nil, fmt.Errorf("error converting prefix: %w", err) - } - - dstPrefix, err := netip.ParsePrefix(string(dpdkFwRule.DestinationPrefix.Address) + "/" + strconv.Itoa(int(dpdkFwRule.DestinationPrefix.PrefixLength))) - if err != nil { - return nil, fmt.Errorf("error converting prefix: %w", err) - } - var direction, action, ipv string - if dpdkFwRule.Direction == 0 { - direction = "Ingress" - } else { - direction = "Egress" - } - if dpdkFwRule.Action == 0 { - action = "Drop" - } else { - action = "Accept" - } - if dpdkFwRule.IpVersion == 0 { - ipv = "IPv4" - } else { - ipv = "IPv6" - } - - return &FirewallRule{ - TypeMeta: TypeMeta{Kind: FirewallRuleKind}, - FirewallRuleMeta: FirewallRuleMeta{ - InterfaceID: interfaceID, - }, - Spec: FirewallRuleSpec{ - RuleID: string(dpdkFwRule.RuleID), - TrafficDirection: direction, - FirewallAction: action, - Priority: dpdkFwRule.Priority, - IpVersion: ipv, - SourcePrefix: &srcPrefix, - DestinationPrefix: &dstPrefix, - ProtocolFilter: dpdkFwRule.ProtocolFilter, - }, - }, nil -} - -func ProtoStatusToStatus(dpdkStatus *proto.Status) Status { - return Status{ - Error: dpdkStatus.Error, - Message: dpdkStatus.Message, - } -} diff --git a/dpdk/api/errors/errors.go b/dpdk/api/errors/errors.go deleted file mode 100644 index e07459c..0000000 --- a/dpdk/api/errors/errors.go +++ /dev/null @@ -1,161 +0,0 @@ -// Copyright 2022 OnMetal authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package errors - -import ( - "errors" - "fmt" -) - -const ( - ADD = 100 - ADD_IPV6_FORMAT = 101 - ADD_VM_NAME_ERR = 102 - ADD_VM_LPM4 = 104 - ADD_VM_LPM6 = 105 - ADD_VM_ADD_ROUT4 = 106 - ADD_VM_ADD_ROUT6 = 107 - ADD_VM_NO_VFS = 108 - ALREADY_ALLOCATED = 109 - CANT_GET_NAME = 110 - ADD_VM_VNF_ERROR = 111 - DEL = 150 - DEL_VM_NOT_FND = 151 - GET_VM_NOT_FND = 171 - LIST = 200 - ADD_RT = 250 - ADD_RT_FAIL4 = 251 - ADD_RT_FAIL6 = 252 - ADD_RT_NO_VM = 253 - DEL_RT = 300 - GET_NETNAT_ITER_ERROR = 349 - ADD_NAT = 350 - ADD_NAT_IP_EXISTS = 351 - ADD_NAT_ALLOC = 352 - ADD_NAT_ADD_KEY = 353 - ADD_NET_NAT_DATA = 354 - ADD_NETWORK_NAT = 355 - DEL_NETWORK_NAT = 356 - ADD_NETNAT_NONLOCAL = 357 - ADD_NETNAT_INVALID_PORT = 358 - ADD_NETNAT_DATA_NOT_FOUND = 359 - DEL_NETNAT_NONLOCAL = 360 - DEL_NETNAT_INVALID_PORT = 361 - DEL_NETNAT_ENTRY_NOT_FOUND = 362 - ADD_NETNAT_IP_EXISTS = 363 - ADD_NETNAT_KEY = 364 - ADD_NETNAT_ALLO_DATA = 365 - ADD_NETNAT_ADD_DATA = 366 - DEL_NETNAT_KEY_DELETED = 367 - GET_NETNAT_IPV6_UNSUPPORTED = 369 - ADD_NEIGHNAT_WRONGTYPE = 370 - DEL_NEIGHNAT_WRONGTYPE = 371 - ADD_NEIGHNAT_ENTRY_EXIST = 372 - ADD_NEIGHNAT_ALLOC = 373 - DEL_NEIGHNAT_ENTRY_NOFOUND = 374 - GET_NEIGHNAT_UNDER_IPV6 = 375 - GET_NETNAT_INFO_TYPE_UNKNOWN = 376 - ADD_NAT_VNF_ERR = 377 - ADD_DNAT = 400 - ADD_DNAT_IP_EXISTS = 401 - ADD_DNAT_ALLOC = 402 - ADD_DNAT_ADD_KEY = 403 - ADD_DNAT_ADD_DATA = 404 - DEL_NAT = 450 - DEL_NAT_NO_SNAT = 451 - GET_NAT = 500 - GET_NAT_NO_IP_SET = 501 - ADD_LB_VIP = 550 - ADD_LB_NO_VNI_EXIST = 551 - ADD_LB_UNSUPP_IP = 552 - DEL_LB_VIP = 600 - DEL_LB_NO_VNI_EXIST = 601 - DEL_LB_UNSUPP_IP = 602 - ADD_PFX = 650 - ADD_PFX_NO_VM = 651 - ADD_PFX_ROUTE = 652 - ADD_PFX_VNF_ERR = 653 - DEL_PFX = 700 - DEL_PFX_NO_VM = 701 - CREATE_LB_UNSUPP_IP = 750 - CREATE_LB_ERR = 751 - CREATE_LB_VNF_ERR = 752 - DEL_LB_ID_ERR = 755 - DEL_LB_BACK_IP_ERR = 756 - GET_LB_ID_ERR = 760 - GET_LB_BACK_IP_ERR = 761 - ADD_FWALL_ERR = 800 - ADD_FWALL_RULE_ERR = 801 - ADD_FWALL_NO_DROP_SUPPORT = 802 - ADD_FWALL_ID_EXISTS = 803 - GET_FWALL_ERR = 810 - GET_NO_FWALL_RULE_ERR = 811 - DEL_FWALL_ERR = 820 - DEL_NO_FWALL_RULE_ERR = 821 - - // os.Exit value - CLIENT_ERROR = 1 - SERVER_ERROR = 2 -) - -var ErrServerError = fmt.Errorf("server error") - -type StatusError struct { - errorCode int32 - message string -} - -func (s *StatusError) Message() string { - return s.message -} - -func (s *StatusError) ErrorCode() int32 { - return s.errorCode -} - -func (s *StatusError) Error() string { - if s.message != "" { - return fmt.Sprintf("[error code %d] %s", s.errorCode, s.message) - } - return fmt.Sprintf("error code %d", s.errorCode) -} - -func NewStatusError(errorCode int32, message string) *StatusError { - return &StatusError{ - errorCode: errorCode, - message: message, - } -} - -func IsStatusErrorCode(err error, errorCodes ...int32) bool { - statusError := &StatusError{} - if !errors.As(err, &statusError) { - return false - } - - for _, errorCode := range errorCodes { - if statusError.ErrorCode() == errorCode { - return true - } - } - return false -} - -func IgnoreStatusErrorCode(err error, errorCode int32) error { - if IsStatusErrorCode(err, errorCode) { - return nil - } - return err -} diff --git a/dpdk/api/types.go b/dpdk/api/types.go deleted file mode 100644 index 7f04b67..0000000 --- a/dpdk/api/types.go +++ /dev/null @@ -1,546 +0,0 @@ -// Copyright 2022 OnMetal authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package api - -import ( - "fmt" - "net/netip" - "reflect" - - proto "github.com/onmetal/net-dpservice-go/proto" -) - -type Object interface { - GetKind() string - GetName() string - GetStatus() Status -} - -type List interface { - GetItems() []Object -} - -type TypeMeta struct { - Kind string `json:"kind"` -} - -func (m *TypeMeta) GetKind() string { - return m.Kind -} - -type Status struct { - Error int32 `json:"error"` - Message string `json:"message"` -} - -func (status *Status) String() string { - if status.Error == 0 { - return status.Message - } - return fmt.Sprintf("Error: %d, Message: %s", status.Error, status.Message) -} - -type RouteList struct { - TypeMeta `json:",inline"` - RouteListMeta `json:"metadata"` - Status Status `json:"status"` - Items []Route `json:"items"` -} - -type RouteListMeta struct { - VNI uint32 `json:"vni"` -} - -func (l *RouteList) GetItems() []Object { - res := make([]Object, len(l.Items)) - for i := range l.Items { - res[i] = &l.Items[i] - } - return res -} - -type Route struct { - TypeMeta `json:",inline"` - RouteMeta `json:"metadata"` - Spec RouteSpec `json:"spec"` - Status Status `json:"status"` -} - -type RouteMeta struct { - VNI uint32 `json:"vni"` -} - -func (m *Route) GetName() string { - return fmt.Sprintf("%s-%d", m.Spec.Prefix, m.Spec.NextHop.VNI) -} - -func (m *Route) GetStatus() Status { - return m.Status -} - -type RouteSpec struct { - Prefix *netip.Prefix `json:"prefix,omitempty"` - NextHop *RouteNextHop `json:"nextHop,omitempty"` -} - -type RouteNextHop struct { - VNI uint32 `json:"vni"` - IP *netip.Addr `json:"ip,omitempty"` -} - -type PrefixList struct { - TypeMeta `json:",inline"` - PrefixListMeta `json:"metadata"` - Status Status `json:"status"` - Items []Prefix `json:"items"` -} - -type PrefixListMeta struct { - InterfaceID string `json:"interfaceID"` -} - -func (l *PrefixList) GetItems() []Object { - res := make([]Object, len(l.Items)) - for i := range l.Items { - res[i] = &l.Items[i] - } - return res -} - -type Prefix struct { - TypeMeta `json:",inline"` - PrefixMeta `json:"metadata"` - Spec PrefixSpec `json:"spec"` - Status Status `json:"status"` -} - -type PrefixMeta struct { - InterfaceID string `json:"interfaceID"` -} - -func (m *Prefix) GetName() string { - return m.Spec.Prefix.String() -} - -func (m *Prefix) GetStatus() Status { - return m.Status -} - -type PrefixSpec struct { - Prefix netip.Prefix `json:"prefix"` - UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` -} - -type VirtualIP struct { - TypeMeta `json:",inline"` - VirtualIPMeta `json:"metadata"` - Spec VirtualIPSpec `json:"spec"` - Status Status `json:"status"` -} - -type VirtualIPMeta struct { - InterfaceID string `json:"interfaceID"` -} - -func (m *VirtualIP) GetName() string { - return "on interface: " + m.VirtualIPMeta.InterfaceID -} - -func (m *VirtualIP) GetStatus() Status { - return m.Status -} - -type VirtualIPSpec struct { - IP netip.Addr `json:"ip"` - UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` -} - -// LoadBalancer section -type LoadBalancer struct { - TypeMeta `json:",inline"` - LoadBalancerMeta `json:"metadata"` - Spec LoadBalancerSpec `json:"spec"` - Status Status `json:"status"` -} - -type LoadBalancerMeta struct { - ID string `json:"id"` -} - -func (m *LoadBalancerMeta) GetName() string { - return m.ID -} - -func (m *LoadBalancer) GetStatus() Status { - return m.Status -} - -type LoadBalancerSpec struct { - VNI uint32 `json:"vni,omitempty"` - LbVipIP *netip.Addr `json:"lbVipIP,omitempty"` - Lbports []LBPort `json:"lbports,omitempty"` - UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` -} - -type LBPort struct { - Protocol uint32 `json:"protocol,omitempty"` - Port uint32 `json:"port,omitempty"` -} - -type LoadBalancerTarget struct { - TypeMeta `json:",inline"` - LoadBalancerTargetMeta `json:"metadata"` - Spec LoadBalancerTargetSpec `json:"spec"` - Status Status `json:"status"` -} - -type LoadBalancerTargetMeta struct { - LoadbalancerID string `json:"loadbalancerId"` -} - -func (m *LoadBalancerTarget) GetName() string { - return "on loadbalancer: " + m.LoadBalancerTargetMeta.LoadbalancerID -} - -func (m *LoadBalancerTarget) GetStatus() Status { - return m.Status -} - -type LoadBalancerTargetSpec struct { - TargetIP *netip.Addr `json:"targetIP,omitempty"` -} - -type LoadBalancerTargetList struct { - TypeMeta `json:",inline"` - LoadBalancerTargetListMeta `json:"metadata"` - Status Status `json:"status"` - Items []LoadBalancerTarget `json:"items"` -} - -type LoadBalancerTargetListMeta struct { - LoadBalancerID string `json:"loadbalancerID"` -} - -func (l *LoadBalancerTargetList) GetItems() []Object { - res := make([]Object, len(l.Items)) - for i := range l.Items { - res[i] = &l.Items[i] - } - return res -} - -type LoadBalancerPrefix struct { - TypeMeta `json:",inline"` - LoadBalancerPrefixMeta `json:"metadata"` - Spec LoadBalancerPrefixSpec `json:"spec"` - Status Status `json:"status"` -} - -type LoadBalancerPrefixMeta struct { - InterfaceID string `json:"interfaceID"` -} - -func (m *LoadBalancerPrefix) GetName() string { - return m.Spec.Prefix.String() -} - -func (m *LoadBalancerPrefix) GetStatus() Status { - return m.Status -} - -type LoadBalancerPrefixSpec struct { - Prefix netip.Prefix `json:"prefix"` - UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` -} - -// Interface section -type Interface struct { - TypeMeta `json:",inline"` - InterfaceMeta `json:"metadata"` - Spec InterfaceSpec `json:"spec"` - Status Status `json:"status"` -} - -type InterfaceMeta struct { - ID string `json:"id"` -} - -type PXE struct { - Server string `json:"server,omitempty"` - FileName string `json:"fileName,omitempty"` -} - -func (m *InterfaceMeta) GetName() string { - return m.ID -} - -func (m *Interface) GetStatus() Status { - return m.Status -} - -type InterfaceSpec struct { - VNI uint32 `json:"vni,omitempty"` - Device string `json:"device,omitempty"` - IPs []netip.Addr `json:"ips,omitempty"` - UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` - VirtualFunction *VirtualFunction `json:"virtualFunction,omitempty"` - PXE *PXE `json:"pxe,omitempty"` - Nat *Nat `json:"-"` - VIP *VirtualIP `json:"-"` -} - -type VirtualFunction struct { - Name string `json:"vfName,omitempty"` - Domain uint32 `json:"vfDomain,omitempty"` - Bus uint32 `json:"vfBus,omitempty"` - Slot uint32 `json:"vfSlot,omitempty"` - Function uint32 `json:"vfFunction,omitempty"` -} - -func (vf *VirtualFunction) String() string { - return fmt.Sprintf("Name: %s, Domain: %d, Bus: %d, Slot: %d, Function: %d", vf.Name, vf.Domain, vf.Bus, vf.Slot, vf.Function) -} - -type InterfaceList struct { - TypeMeta `json:",inline"` - InterfaceListMeta `json:"metadata"` - Status Status `json:"status"` - Items []Interface `json:"items"` -} - -type InterfaceListMeta struct { -} - -func (l *InterfaceList) GetItems() []Object { - res := make([]Object, len(l.Items)) - for i := range l.Items { - res[i] = &l.Items[i] - } - return res -} - -// NAT section -type Nat struct { - TypeMeta `json:",inline"` - NatMeta `json:"metadata"` - Spec NatSpec `json:"spec"` - Status Status `json:"status"` -} - -type NatMeta struct { - InterfaceID string `json:"interfaceID,omitempty"` -} - -func (m *NatMeta) GetName() string { - return m.InterfaceID -} - -func (m *Nat) GetStatus() Status { - return m.Status -} - -func (m *Nat) String() string { - return fmt.Sprintf("%s <%d, %d>", m.Spec.NatVIPIP, m.Spec.MinPort, m.Spec.MaxPort) -} - -type NatSpec struct { - NatVIPIP *netip.Addr `json:"natVIPIP,omitempty"` - MinPort uint32 `json:"minPort,omitempty"` - MaxPort uint32 `json:"maxPort,omitempty"` - UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` -} - -type NatList struct { - TypeMeta `json:",inline"` - NatListMeta `json:"metadata"` - Status Status `json:"status"` - Items []Nat `json:"items"` -} - -type NatListMeta struct { - NatIP *netip.Addr `json:"natIp,omitempty"` - NatInfoType string `json:"infoType,omitempty"` -} - -func (l *NatList) GetItems() []Object { - res := make([]Object, len(l.Items)) - for i := range l.Items { - res[i] = &l.Items[i] - } - return res -} - -type NeighborNat struct { - TypeMeta `json:",inline"` - NeighborNatMeta `json:"metadata"` - Spec NeighborNatSpec `json:"spec"` - Status Status `json:"status"` -} - -type NeighborNatMeta struct { - NatVIPIP *netip.Addr `json:"natVIPIP"` -} - -func (m *NeighborNatMeta) GetName() string { - return m.NatVIPIP.String() -} - -func (m *NeighborNat) GetStatus() Status { - return m.Status -} - -type NeighborNatSpec struct { - Vni uint32 `json:"vni,omitempty"` - MinPort uint32 `json:"minPort,omitempty"` - MaxPort uint32 `json:"maxPort,omitempty"` - UnderlayRoute *netip.Addr `json:"underlayRoute,omitempty"` -} - -// FirewallRule section -type FirewallRule struct { - TypeMeta `json:",inline"` - FirewallRuleMeta `json:"metadata"` - Spec FirewallRuleSpec `json:"spec"` - Status Status `json:"status"` -} - -type FirewallRuleMeta struct { - InterfaceID string `json:"interfaceID"` -} - -func (m *FirewallRule) GetName() string { - return m.FirewallRuleMeta.InterfaceID + "/" + m.Spec.RuleID -} - -func (m *FirewallRule) GetStatus() Status { - return m.Status -} - -type FirewallRuleSpec struct { - RuleID string `json:"ruleID"` - TrafficDirection string `json:"trafficDirection,omitempty"` - FirewallAction string `json:"firewallAction,omitempty"` - Priority uint32 `json:"priority,omitempty"` - IpVersion string `json:"ipVersion,omitempty"` - SourcePrefix *netip.Prefix `json:"sourcePrefix,omitempty"` - DestinationPrefix *netip.Prefix `json:"destinationPrefix,omitempty"` - ProtocolFilter *proto.ProtocolFilter `json:"protocolFilter,omitempty"` -} - -type FirewallRuleList struct { - TypeMeta `json:",inline"` - FirewallRuleListMeta `json:"metadata"` - Status Status `json:"status"` - Items []FirewallRule `json:"items"` -} - -type FirewallRuleListMeta struct { - InterfaceID string `json:"interfaceID"` -} - -func (l *FirewallRuleList) GetItems() []Object { - res := make([]Object, len(l.Items)) - for i := range l.Items { - res[i] = &l.Items[i] - } - return res -} - -type Init struct { - TypeMeta `json:",inline"` - InitMeta `json:"metadata"` - Spec InitSpec `json:"spec"` - Status Status `json:"status"` -} - -type InitMeta struct { -} - -type InitSpec struct { -} - -func (m *InitMeta) GetName() string { - return "init" -} - -func (m *Init) GetStatus() Status { - return m.Status -} - -type Initialized struct { - TypeMeta `json:",inline"` - InitializedMeta `json:"metadata"` - Spec InitializedSpec `json:"spec"` - Status Status `json:"status"` -} - -type InitializedMeta struct { -} - -type InitializedSpec struct { - UUID string `json:"uuid"` -} - -func (m *InitializedMeta) GetName() string { - return "initialized" -} - -func (m *Initialized) GetStatus() Status { - return Status{} -} - -type Vni struct { - TypeMeta `json:",inline"` - VniMeta `json:"metadata"` - Spec VniSpec `json:"spec"` - Status Status `json:"status"` -} - -type VniMeta struct { - VNI uint32 `json:"vni"` - VniType uint8 `json:"vniType"` -} - -type VniSpec struct { - InUse bool `json:"inUse"` -} - -func (m *VniMeta) GetName() string { - return "vni" -} - -func (m *Vni) GetStatus() Status { - return Status{} -} - -var ( - InterfaceKind = reflect.TypeOf(Interface{}).Name() - InterfaceListKind = reflect.TypeOf(InterfaceList{}).Name() - LoadBalancerKind = reflect.TypeOf(LoadBalancer{}).Name() - LoadBalancerTargetKind = reflect.TypeOf(LoadBalancerTarget{}).Name() - LoadBalancerTargetListKind = reflect.TypeOf(LoadBalancerTargetList{}).Name() - LoadBalancerPrefixKind = reflect.TypeOf(LoadBalancerPrefix{}).Name() - PrefixKind = reflect.TypeOf(Prefix{}).Name() - PrefixListKind = reflect.TypeOf(PrefixList{}).Name() - VirtualIPKind = reflect.TypeOf(VirtualIP{}).Name() - RouteKind = reflect.TypeOf(Route{}).Name() - RouteListKind = reflect.TypeOf(RouteList{}).Name() - NatKind = reflect.TypeOf(Nat{}).Name() - NatListKind = reflect.TypeOf(NatList{}).Name() - NeighborNatKind = reflect.TypeOf(NeighborNat{}).Name() - FirewallRuleKind = reflect.TypeOf(FirewallRule{}).Name() - FirewallRuleListKind = reflect.TypeOf(FirewallRuleList{}).Name() - VniKind = reflect.TypeOf(Vni{}).Name() -) diff --git a/dpdk/client/client.go b/dpdk/client/client.go deleted file mode 100644 index a32b153..0000000 --- a/dpdk/client/client.go +++ /dev/null @@ -1,1014 +0,0 @@ -// Copyright 2022 OnMetal authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package client - -import ( - "context" - "fmt" - "net/netip" - "strings" - - "github.com/onmetal/dpservice-cli/dpdk/api" - apierrors "github.com/onmetal/dpservice-cli/dpdk/api/errors" - "github.com/onmetal/dpservice-cli/netiputil" - dpdkproto "github.com/onmetal/net-dpservice-go/proto" -) - -type Client interface { - GetLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) - CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) (*api.LoadBalancer, error) - DeleteLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) - - ListLoadBalancerPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) - CreateLoadBalancerPrefix(ctx context.Context, prefix *api.LoadBalancerPrefix) (*api.LoadBalancerPrefix, error) - DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) (*api.LoadBalancerPrefix, error) - - GetLoadBalancerTargets(ctx context.Context, interfaceID string) (*api.LoadBalancerTargetList, error) - AddLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBalancerTarget) (*api.LoadBalancerTarget, error) - DeleteLoadBalancerTarget(ctx context.Context, id string, targetIP netip.Addr) (*api.LoadBalancerTarget, error) - - GetInterface(ctx context.Context, id string) (*api.Interface, error) - ListInterfaces(ctx context.Context) (*api.InterfaceList, error) - CreateInterface(ctx context.Context, iface *api.Interface) (*api.Interface, error) - DeleteInterface(ctx context.Context, id string) (*api.Interface, error) - - GetVirtualIP(ctx context.Context, interfaceID string) (*api.VirtualIP, error) - AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*api.VirtualIP, error) - DeleteVirtualIP(ctx context.Context, interfaceID string) (*api.VirtualIP, error) - - ListPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) - AddPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix, error) - DeletePrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) (*api.Prefix, error) - - ListRoutes(ctx context.Context, vni uint32) (*api.RouteList, error) - AddRoute(ctx context.Context, route *api.Route) (*api.Route, error) - DeleteRoute(ctx context.Context, vni uint32, prefix netip.Prefix) (*api.Route, error) - - GetNat(ctx context.Context, interfaceID string) (*api.Nat, error) - AddNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) - DeleteNat(ctx context.Context, interfaceID string) (*api.Nat, error) - - AddNeighborNat(ctx context.Context, nat *api.NeighborNat) (*api.NeighborNat, error) - GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType string) (*api.NatList, error) - DeleteNeighborNat(ctx context.Context, neigbhorNat api.NeighborNat) (*api.NeighborNat, error) - - ListFirewallRules(ctx context.Context, interfaceID string) (*api.FirewallRuleList, error) - AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) - GetFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) - DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) - - Initialized(ctx context.Context) (string, error) - Init(ctx context.Context, initConfig dpdkproto.InitConfig) (*api.Init, error) - GetVni(ctx context.Context, vni uint32, vniType uint8) (*api.Vni, error) -} - -type client struct { - dpdkproto.DPDKonmetalClient -} - -func NewClient(protoClient dpdkproto.DPDKonmetalClient) Client { - return &client{protoClient} -} - -func (c *client) GetLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) { - res, err := c.DPDKonmetalClient.GetLoadBalancer(ctx, &dpdkproto.GetLoadBalancerRequest{LoadBalancerID: []byte(id)}) - if err != nil { - return &api.LoadBalancer{}, err - } - retLoadBalancer := &api.LoadBalancer{ - TypeMeta: api.TypeMeta{Kind: api.LoadBalancerKind}, - LoadBalancerMeta: api.LoadBalancerMeta{ID: id}, - Status: api.ProtoStatusToStatus(res.Status), - } - if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return retLoadBalancer, apierrors.ErrServerError - } - return api.ProtoLoadBalancerToLoadBalancer(res, id) -} - -func (c *client) CreateLoadBalancer(ctx context.Context, lb *api.LoadBalancer) (*api.LoadBalancer, error) { - var lbPorts = make([]*dpdkproto.LBPort, 0, len(lb.Spec.Lbports)) - for _, p := range lb.Spec.Lbports { - lbPort := &dpdkproto.LBPort{Port: p.Port, Protocol: dpdkproto.Protocol(p.Protocol)} - lbPorts = append(lbPorts, lbPort) - } - res, err := c.DPDKonmetalClient.CreateLoadBalancer(ctx, &dpdkproto.CreateLoadBalancerRequest{ - LoadBalancerID: []byte(lb.LoadBalancerMeta.ID), - Vni: lb.Spec.VNI, - LbVipIP: api.LbipToProtoLbip(*lb.Spec.LbVipIP), - Lbports: lbPorts, - }) - if err != nil { - return &api.LoadBalancer{}, err - } - retLoadBalancer := &api.LoadBalancer{ - TypeMeta: api.TypeMeta{Kind: api.LoadBalancerKind}, - LoadBalancerMeta: lb.LoadBalancerMeta, - Status: api.ProtoStatusToStatus(res.Status), - } - if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return retLoadBalancer, apierrors.ErrServerError - } - - underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) - if err != nil { - return retLoadBalancer, fmt.Errorf("error parsing underlay route: %w", err) - } - retLoadBalancer.Spec = lb.Spec - retLoadBalancer.Spec.UnderlayRoute = &underlayRoute - - return retLoadBalancer, nil -} - -func (c *client) DeleteLoadBalancer(ctx context.Context, id string) (*api.LoadBalancer, error) { - res, err := c.DPDKonmetalClient.DeleteLoadBalancer(ctx, &dpdkproto.DeleteLoadBalancerRequest{LoadBalancerID: []byte(id)}) - if err != nil { - return &api.LoadBalancer{}, err - } - retLoadBalancer := &api.LoadBalancer{ - TypeMeta: api.TypeMeta{Kind: api.LoadBalancerKind}, - LoadBalancerMeta: api.LoadBalancerMeta{ID: id}, - Status: api.ProtoStatusToStatus(res), - } - if errorCode := res.GetError(); errorCode != 0 { - return retLoadBalancer, apierrors.ErrServerError - } - return retLoadBalancer, nil -} - -func (c *client) ListLoadBalancerPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) { - res, err := c.DPDKonmetalClient.ListInterfaceLoadBalancerPrefixes(ctx, &dpdkproto.ListInterfaceLoadBalancerPrefixesRequest{ - InterfaceID: []byte(interfaceID), - }) - if err != nil { - return nil, err - } - - prefixes := make([]api.Prefix, len(res.GetPrefixes())) - for i, dpdkPrefix := range res.GetPrefixes() { - prefix, err := api.ProtoPrefixToPrefix(interfaceID, api.ProtoLBPrefixToProtoPrefix(*dpdkPrefix)) - prefix.Kind = api.LoadBalancerPrefixKind - if err != nil { - return nil, err - } - - prefixes[i] = *prefix - } - - return &api.PrefixList{ - TypeMeta: api.TypeMeta{Kind: "LoadBalancerPrefixList"}, - PrefixListMeta: api.PrefixListMeta{InterfaceID: interfaceID}, - Items: prefixes, - }, nil -} - -func (c *client) CreateLoadBalancerPrefix(ctx context.Context, lbprefix *api.LoadBalancerPrefix) (*api.LoadBalancerPrefix, error) { - res, err := c.DPDKonmetalClient.CreateInterfaceLoadBalancerPrefix(ctx, &dpdkproto.CreateInterfaceLoadBalancerPrefixRequest{ - InterfaceID: &dpdkproto.InterfaceIDMsg{ - InterfaceID: []byte(lbprefix.InterfaceID), - }, - Prefix: &dpdkproto.Prefix{ - IpVersion: api.NetIPAddrToProtoIPVersion(lbprefix.Spec.Prefix.Addr()), - Address: []byte(lbprefix.Spec.Prefix.Addr().String()), - PrefixLength: uint32(lbprefix.Spec.Prefix.Bits()), - }, - }) - if err != nil { - return &api.LoadBalancerPrefix{}, err - } - retLBPrefix := &api.LoadBalancerPrefix{ - TypeMeta: api.TypeMeta{Kind: api.LoadBalancerPrefixKind}, - LoadBalancerPrefixMeta: lbprefix.LoadBalancerPrefixMeta, - Spec: api.LoadBalancerPrefixSpec{ - Prefix: lbprefix.Spec.Prefix, - }, - Status: api.ProtoStatusToStatus(res.Status), - } - if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return retLBPrefix, apierrors.ErrServerError - } - underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) - if err != nil { - return retLBPrefix, fmt.Errorf("error parsing underlay route: %w", err) - } - retLBPrefix.Spec.UnderlayRoute = &underlayRoute - return retLBPrefix, nil -} - -func (c *client) DeleteLoadBalancerPrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) (*api.LoadBalancerPrefix, error) { - res, err := c.DPDKonmetalClient.DeleteInterfaceLoadBalancerPrefix(ctx, &dpdkproto.DeleteInterfaceLoadBalancerPrefixRequest{ - InterfaceID: &dpdkproto.InterfaceIDMsg{ - InterfaceID: []byte(interfaceID), - }, - Prefix: &dpdkproto.Prefix{ - IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Addr()), - Address: []byte(prefix.Addr().String()), - PrefixLength: uint32(prefix.Bits()), - }, - }) - if err != nil { - return &api.LoadBalancerPrefix{}, err - } - retLBPrefix := &api.LoadBalancerPrefix{ - TypeMeta: api.TypeMeta{Kind: api.LoadBalancerPrefixKind}, - LoadBalancerPrefixMeta: api.LoadBalancerPrefixMeta{InterfaceID: interfaceID}, - Spec: api.LoadBalancerPrefixSpec{Prefix: prefix}, - Status: api.ProtoStatusToStatus(res), - } - if errorCode := res.GetError(); errorCode != 0 { - return retLBPrefix, apierrors.ErrServerError - } - return retLBPrefix, nil -} - -func (c *client) GetLoadBalancerTargets(ctx context.Context, loadBalancerID string) (*api.LoadBalancerTargetList, error) { - res, err := c.DPDKonmetalClient.GetLoadBalancerTargets(ctx, &dpdkproto.GetLoadBalancerTargetsRequest{ - LoadBalancerID: []byte(loadBalancerID), - }) - if err != nil { - return &api.LoadBalancerTargetList{}, err - } - if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.LoadBalancerTargetList{ - TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetListKind}, - Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError - } - - lbtargets := make([]api.LoadBalancerTarget, len(res.GetTargetIPs())) - for i, dpdkLBtarget := range res.GetTargetIPs() { - var lbtarget api.LoadBalancerTarget - lbtarget.TypeMeta.Kind = api.LoadBalancerTargetKind - lbtarget.Spec.TargetIP = api.ProtoLbipToLbip(*dpdkLBtarget) - lbtarget.LoadBalancerTargetMeta.LoadbalancerID = loadBalancerID - - lbtargets[i] = lbtarget - } - - return &api.LoadBalancerTargetList{ - TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetListKind}, - LoadBalancerTargetListMeta: api.LoadBalancerTargetListMeta{LoadBalancerID: loadBalancerID}, - Items: lbtargets, - // TODO server is not returning correct status - //Status: api.ProtoStatusToStatus(res.Status), - }, nil -} - -func (c *client) AddLoadBalancerTarget(ctx context.Context, lbtarget *api.LoadBalancerTarget) (*api.LoadBalancerTarget, error) { - res, err := c.DPDKonmetalClient.AddLoadBalancerTarget(ctx, &dpdkproto.AddLoadBalancerTargetRequest{ - LoadBalancerID: []byte(lbtarget.LoadBalancerTargetMeta.LoadbalancerID), - TargetIP: api.LbipToProtoLbip(*lbtarget.Spec.TargetIP), - }) - if err != nil { - return &api.LoadBalancerTarget{}, err - } - retLBTarget := &api.LoadBalancerTarget{ - TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, - LoadBalancerTargetMeta: lbtarget.LoadBalancerTargetMeta, - Status: api.ProtoStatusToStatus(res), - } - if errorCode := res.GetError(); errorCode != 0 { - return retLBTarget, apierrors.ErrServerError - } - retLBTarget.Spec = lbtarget.Spec - return retLBTarget, nil -} - -func (c *client) DeleteLoadBalancerTarget(ctx context.Context, lbid string, targetIP netip.Addr) (*api.LoadBalancerTarget, error) { - res, err := c.DPDKonmetalClient.DeleteLoadBalancerTarget(ctx, &dpdkproto.DeleteLoadBalancerTargetRequest{ - LoadBalancerID: []byte(lbid), - TargetIP: api.LbipToProtoLbip(targetIP), - }) - if err != nil { - return &api.LoadBalancerTarget{}, err - } - retLBTarget := &api.LoadBalancerTarget{ - TypeMeta: api.TypeMeta{Kind: api.LoadBalancerTargetKind}, - LoadBalancerTargetMeta: api.LoadBalancerTargetMeta{LoadbalancerID: lbid}, - Status: api.ProtoStatusToStatus(res), - } - if errorCode := res.GetError(); errorCode != 0 { - return retLBTarget, apierrors.ErrServerError - } - return retLBTarget, nil -} - -func (c *client) GetInterface(ctx context.Context, id string) (*api.Interface, error) { - res, err := c.DPDKonmetalClient.GetInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(id)}) - if err != nil { - return &api.Interface{}, err - } - if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.Interface{ - TypeMeta: api.TypeMeta{Kind: api.InterfaceKind}, - InterfaceMeta: api.InterfaceMeta{ID: id}, - Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError - } - return api.ProtoInterfaceToInterface(res.GetInterface()) -} - -func (c *client) ListInterfaces(ctx context.Context) (*api.InterfaceList, error) { - res, err := c.DPDKonmetalClient.ListInterfaces(ctx, &dpdkproto.Empty{}) - if err != nil { - return nil, err - } - - ifaces := make([]api.Interface, len(res.GetInterfaces())) - for i, dpdkIface := range res.GetInterfaces() { - iface, err := api.ProtoInterfaceToInterface(dpdkIface) - if err != nil { - return nil, err - } - - ifaces[i] = *iface - } - - return &api.InterfaceList{ - TypeMeta: api.TypeMeta{Kind: api.InterfaceListKind}, - Items: ifaces, - }, nil -} - -func (c *client) CreateInterface(ctx context.Context, iface *api.Interface) (*api.Interface, error) { - req := dpdkproto.CreateInterfaceRequest{ - InterfaceType: dpdkproto.InterfaceType_VirtualInterface, - InterfaceID: []byte(iface.ID), - Vni: iface.Spec.VNI, - Ipv4Config: api.NetIPAddrToProtoIPConfig(netiputil.FindIPv4(iface.Spec.IPs)), - Ipv6Config: api.NetIPAddrToProtoIPConfig(netiputil.FindIPv6(iface.Spec.IPs)), - DeviceName: iface.Spec.Device, - } - if iface.Spec.PXE.FileName != "" && iface.Spec.PXE.Server != "" { - req.Ipv4Config.PxeConfig = &dpdkproto.PXEConfig{NextServer: iface.Spec.PXE.Server, BootFileName: iface.Spec.PXE.FileName} - req.Ipv6Config.PxeConfig = &dpdkproto.PXEConfig{NextServer: iface.Spec.PXE.Server, BootFileName: iface.Spec.PXE.FileName} - } - - res, err := c.DPDKonmetalClient.CreateInterface(ctx, &req) - if err != nil { - return &api.Interface{}, err - } - retInterface := &api.Interface{ - TypeMeta: iface.TypeMeta, - InterfaceMeta: iface.InterfaceMeta, - Status: api.ProtoStatusToStatus(res.Response.Status), - } - if errorCode := res.GetResponse().GetStatus().GetError(); errorCode != 0 { - return retInterface, apierrors.ErrServerError - } - - underlayRoute, err := netip.ParseAddr(string(res.GetResponse().GetUnderlayRoute())) - if err != nil { - return retInterface, fmt.Errorf("error parsing underlay route: %w", err) - } - - return &api.Interface{ - TypeMeta: api.TypeMeta{Kind: api.InterfaceKind}, - InterfaceMeta: iface.InterfaceMeta, - Spec: api.InterfaceSpec{ - VNI: iface.Spec.VNI, - Device: iface.Spec.Device, - IPs: iface.Spec.IPs, - UnderlayRoute: &underlayRoute, - VirtualFunction: &api.VirtualFunction{ - Name: res.Vf.Name, - Domain: res.Vf.Domain, - Bus: res.Vf.Bus, - Slot: res.Vf.Slot, - Function: res.Vf.Function, - }, - PXE: &api.PXE{Server: iface.Spec.PXE.Server, FileName: iface.Spec.PXE.FileName}, - }, - Status: api.ProtoStatusToStatus(res.Response.Status), - }, nil -} - -func (c *client) DeleteInterface(ctx context.Context, id string) (*api.Interface, error) { - res, err := c.DPDKonmetalClient.DeleteInterface(ctx, &dpdkproto.InterfaceIDMsg{InterfaceID: []byte(id)}) - if err != nil { - return &api.Interface{}, err - } - retInterface := &api.Interface{ - TypeMeta: api.TypeMeta{Kind: api.InterfaceKind}, - InterfaceMeta: api.InterfaceMeta{ID: id}, - Status: api.ProtoStatusToStatus(res), - } - if errorCode := res.GetError(); errorCode != 0 { - return retInterface, apierrors.ErrServerError - } - return retInterface, nil -} - -func (c *client) GetVirtualIP(ctx context.Context, interfaceID string) (*api.VirtualIP, error) { - res, err := c.DPDKonmetalClient.GetInterfaceVIP(ctx, &dpdkproto.InterfaceIDMsg{ - InterfaceID: []byte(interfaceID), - }) - if err != nil { - return &api.VirtualIP{}, err - } - if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.VirtualIP{ - TypeMeta: api.TypeMeta{Kind: api.VirtualIPKind}, - VirtualIPMeta: api.VirtualIPMeta{InterfaceID: interfaceID}, - Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError - } - return api.ProtoVirtualIPToVirtualIP(interfaceID, res) -} - -func (c *client) AddVirtualIP(ctx context.Context, virtualIP *api.VirtualIP) (*api.VirtualIP, error) { - res, err := c.DPDKonmetalClient.AddInterfaceVIP(ctx, &dpdkproto.InterfaceVIPMsg{ - InterfaceID: []byte(virtualIP.InterfaceID), - InterfaceVIPIP: &dpdkproto.InterfaceVIPIP{ - IpVersion: api.NetIPAddrToProtoIPVersion(virtualIP.Spec.IP), - Address: []byte(virtualIP.Spec.IP.String()), - }, - }) - if err != nil { - return &api.VirtualIP{}, err - } - retVirtualIP := &api.VirtualIP{ - TypeMeta: api.TypeMeta{Kind: api.VirtualIPKind}, - VirtualIPMeta: virtualIP.VirtualIPMeta, - Spec: api.VirtualIPSpec{ - IP: virtualIP.Spec.IP, - }, - Status: api.ProtoStatusToStatus(res.Status), - } - if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return retVirtualIP, apierrors.ErrServerError - } - underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) - if err != nil { - return retVirtualIP, fmt.Errorf("error parsing underlay route: %w", err) - } - retVirtualIP.Spec.UnderlayRoute = &underlayRoute - return retVirtualIP, nil -} - -func (c *client) DeleteVirtualIP(ctx context.Context, interfaceID string) (*api.VirtualIP, error) { - res, err := c.DPDKonmetalClient.DeleteInterfaceVIP(ctx, &dpdkproto.InterfaceIDMsg{ - InterfaceID: []byte(interfaceID), - }) - if err != nil { - return &api.VirtualIP{}, err - } - retVirtualIP := &api.VirtualIP{ - TypeMeta: api.TypeMeta{Kind: api.VirtualIPKind}, - VirtualIPMeta: api.VirtualIPMeta{InterfaceID: interfaceID}, - } - if errorCode := res.GetError(); errorCode != 0 { - return retVirtualIP, apierrors.ErrServerError - } - return retVirtualIP, nil -} - -func (c *client) ListPrefixes(ctx context.Context, interfaceID string) (*api.PrefixList, error) { - res, err := c.DPDKonmetalClient.ListInterfacePrefixes(ctx, &dpdkproto.InterfaceIDMsg{ - InterfaceID: []byte(interfaceID), - }) - if err != nil { - return nil, err - } - - prefixes := make([]api.Prefix, len(res.GetPrefixes())) - for i, dpdkPrefix := range res.GetPrefixes() { - prefix, err := api.ProtoPrefixToPrefix(interfaceID, dpdkPrefix) - if err != nil { - return nil, err - } - - prefixes[i] = *prefix - } - - return &api.PrefixList{ - TypeMeta: api.TypeMeta{Kind: api.PrefixListKind}, - PrefixListMeta: api.PrefixListMeta{InterfaceID: interfaceID}, - Items: prefixes, - }, nil -} - -func (c *client) AddPrefix(ctx context.Context, prefix *api.Prefix) (*api.Prefix, error) { - res, err := c.DPDKonmetalClient.AddInterfacePrefix(ctx, &dpdkproto.InterfacePrefixMsg{ - InterfaceID: &dpdkproto.InterfaceIDMsg{ - InterfaceID: []byte(prefix.InterfaceID), - }, - Prefix: &dpdkproto.Prefix{ - IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Spec.Prefix.Addr()), - Address: []byte(prefix.Spec.Prefix.Addr().String()), - PrefixLength: uint32(prefix.Spec.Prefix.Bits()), - }, - }) - if err != nil { - return &api.Prefix{}, err - } - retPrefix := &api.Prefix{ - TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, - PrefixMeta: prefix.PrefixMeta, - Spec: api.PrefixSpec{Prefix: prefix.Spec.Prefix}, - Status: api.ProtoStatusToStatus(res.Status), - } - - if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return retPrefix, apierrors.ErrServerError - } - underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) - if err != nil { - return retPrefix, fmt.Errorf("error parsing underlay route: %w", err) - } - retPrefix.Spec.UnderlayRoute = &underlayRoute - return retPrefix, nil -} - -func (c *client) DeletePrefix(ctx context.Context, interfaceID string, prefix netip.Prefix) (*api.Prefix, error) { - res, err := c.DPDKonmetalClient.DeleteInterfacePrefix(ctx, &dpdkproto.InterfacePrefixMsg{ - InterfaceID: &dpdkproto.InterfaceIDMsg{ - InterfaceID: []byte(interfaceID), - }, - Prefix: &dpdkproto.Prefix{ - IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Addr()), - Address: []byte(prefix.Addr().String()), - PrefixLength: uint32(prefix.Bits()), - }, - }) - if err != nil { - return &api.Prefix{}, err - } - retPrefix := &api.Prefix{ - TypeMeta: api.TypeMeta{Kind: api.PrefixKind}, - PrefixMeta: api.PrefixMeta{InterfaceID: interfaceID}, - Spec: api.PrefixSpec{Prefix: prefix}, - Status: api.ProtoStatusToStatus(res), - } - if errorCode := res.GetError(); errorCode != 0 { - return retPrefix, apierrors.ErrServerError - } - return retPrefix, nil -} - -func (c *client) AddRoute(ctx context.Context, route *api.Route) (*api.Route, error) { - res, err := c.DPDKonmetalClient.AddRoute(ctx, &dpdkproto.VNIRouteMsg{ - Vni: &dpdkproto.VNIMsg{Vni: route.VNI}, - Route: &dpdkproto.Route{ - IpVersion: api.NetIPAddrToProtoIPVersion(*route.Spec.NextHop.IP), - Weight: 100, - Prefix: &dpdkproto.Prefix{ - IpVersion: api.NetIPAddrToProtoIPVersion(route.Spec.Prefix.Addr()), - Address: []byte(route.Spec.Prefix.Addr().String()), - PrefixLength: uint32(route.Spec.Prefix.Bits()), - }, - NexthopVNI: route.Spec.NextHop.VNI, - NexthopAddress: []byte(route.Spec.NextHop.IP.String()), - }, - }) - if err != nil { - return &api.Route{}, err - } - retRoute := &api.Route{ - TypeMeta: api.TypeMeta{Kind: api.RouteKind}, - RouteMeta: route.RouteMeta, - Spec: api.RouteSpec{ - Prefix: route.Spec.Prefix, - NextHop: &api.RouteNextHop{}}, - Status: api.ProtoStatusToStatus(res), - } - if errorCode := res.GetError(); errorCode != 0 { - return retRoute, apierrors.ErrServerError - } - retRoute.Spec = route.Spec - return retRoute, nil -} - -func (c *client) DeleteRoute(ctx context.Context, vni uint32, prefix netip.Prefix) (*api.Route, error) { - res, err := c.DPDKonmetalClient.DeleteRoute(ctx, &dpdkproto.VNIRouteMsg{ - Vni: &dpdkproto.VNIMsg{Vni: vni}, - Route: &dpdkproto.Route{ - IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Addr()), - Weight: 100, - Prefix: &dpdkproto.Prefix{ - IpVersion: api.NetIPAddrToProtoIPVersion(prefix.Addr()), - Address: []byte(prefix.Addr().String()), - PrefixLength: uint32(prefix.Bits()), - }, - }, - }) - if err != nil { - return &api.Route{}, err - } - retRoute := &api.Route{ - TypeMeta: api.TypeMeta{Kind: api.RouteKind}, - RouteMeta: api.RouteMeta{VNI: vni}, - Spec: api.RouteSpec{ - Prefix: &prefix, - NextHop: &api.RouteNextHop{}, - }, - Status: api.ProtoStatusToStatus(res), - } - if errorCode := res.GetError(); errorCode != 0 { - return retRoute, apierrors.ErrServerError - } - return retRoute, nil -} - -func (c *client) ListRoutes(ctx context.Context, vni uint32) (*api.RouteList, error) { - res, err := c.DPDKonmetalClient.ListRoutes(ctx, &dpdkproto.VNIMsg{ - Vni: vni, - }) - if err != nil { - return nil, err - } - - routes := make([]api.Route, len(res.GetRoutes())) - for i, dpdkRoute := range res.GetRoutes() { - route, err := api.ProtoRouteToRoute(vni, dpdkRoute) - if err != nil { - return nil, err - } - - routes[i] = *route - } - - return &api.RouteList{ - TypeMeta: api.TypeMeta{Kind: api.RouteListKind}, - RouteListMeta: api.RouteListMeta{VNI: vni}, - Items: routes, - }, nil -} - -func (c *client) GetNat(ctx context.Context, interfaceID string) (*api.Nat, error) { - res, err := c.DPDKonmetalClient.GetNAT(ctx, &dpdkproto.GetNATRequest{InterfaceID: []byte(interfaceID)}) - if err != nil { - return &api.Nat{}, err - } - if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.Nat{ - TypeMeta: api.TypeMeta{Kind: api.NatKind}, - NatMeta: api.NatMeta{InterfaceID: interfaceID}, - Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError - } - return api.ProtoNatToNat(res, interfaceID) -} - -func (c *client) AddNat(ctx context.Context, nat *api.Nat) (*api.Nat, error) { - res, err := c.DPDKonmetalClient.AddNAT(ctx, &dpdkproto.AddNATRequest{ - InterfaceID: []byte(nat.NatMeta.InterfaceID), - NatVIPIP: &dpdkproto.NATIP{ - IpVersion: api.NetIPAddrToProtoIPVersion(*nat.Spec.NatVIPIP), - Address: []byte(nat.Spec.NatVIPIP.String()), - }, - MinPort: nat.Spec.MinPort, - MaxPort: nat.Spec.MaxPort, - }) - if err != nil { - return &api.Nat{}, err - } - retNat := &api.Nat{ - TypeMeta: api.TypeMeta{Kind: api.NatKind}, - NatMeta: nat.NatMeta, - Status: api.ProtoStatusToStatus(res.Status), - } - if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return retNat, apierrors.ErrServerError - } - - underlayRoute, err := netip.ParseAddr(string(res.GetUnderlayRoute())) - if err != nil { - return retNat, fmt.Errorf("error parsing underlay route: %w", err) - } - - retNat.Spec = nat.Spec - retNat.Spec.UnderlayRoute = &underlayRoute - return retNat, nil -} - -func (c *client) DeleteNat(ctx context.Context, interfaceID string) (*api.Nat, error) { - res, err := c.DPDKonmetalClient.DeleteNAT(ctx, &dpdkproto.DeleteNATRequest{ - InterfaceID: []byte(interfaceID), - }) - if err != nil { - return &api.Nat{}, err - } - retNat := &api.Nat{ - TypeMeta: api.TypeMeta{Kind: api.NatKind}, - NatMeta: api.NatMeta{InterfaceID: interfaceID}, - Status: api.ProtoStatusToStatus(res), - } - if errorCode := res.GetError(); errorCode != 0 { - return retNat, apierrors.ErrServerError - } - return retNat, nil -} - -func (c *client) AddNeighborNat(ctx context.Context, nNat *api.NeighborNat) (*api.NeighborNat, error) { - - res, err := c.DPDKonmetalClient.AddNeighborNAT(ctx, &dpdkproto.AddNeighborNATRequest{ - NatVIPIP: &dpdkproto.NATIP{ - IpVersion: api.NetIPAddrToProtoIPVersion(*nNat.NeighborNatMeta.NatVIPIP), - Address: []byte(nNat.NeighborNatMeta.NatVIPIP.String()), - }, - Vni: nNat.Spec.Vni, - MinPort: nNat.Spec.MinPort, - MaxPort: nNat.Spec.MaxPort, - UnderlayRoute: []byte(nNat.Spec.UnderlayRoute.String()), - }) - if err != nil { - return &api.NeighborNat{}, err - } - retnNat := &api.NeighborNat{ - TypeMeta: api.TypeMeta{Kind: api.NeighborNatKind}, - NeighborNatMeta: nNat.NeighborNatMeta, - Status: api.ProtoStatusToStatus(res), - } - if errorCode := res.GetError(); errorCode != 0 { - return retnNat, apierrors.ErrServerError - } - retnNat.Spec = nNat.Spec - return retnNat, nil -} - -func (c *client) GetNATInfo(ctx context.Context, natVIPIP netip.Addr, natType string) (*api.NatList, error) { - var nType int32 - switch strings.ToLower(natType) { - case "local", "1": - nType = 1 - case "neigh", "2", "neighbor": - nType = 2 - case "any", "0", "": - nType = 0 - default: - return nil, fmt.Errorf("nat info type can be only: Any = 0/Local = 1/Neigh(bor) = 2") - } - - req := dpdkproto.GetNATInfoRequest{ - NatVIPIP: &dpdkproto.NATIP{IpVersion: api.NetIPAddrToProtoIPVersion(natVIPIP), - Address: []byte(natVIPIP.String()), - }, - NatInfoType: dpdkproto.NATInfoType(nType), - } - // nat info type not defined, try both types - res := &dpdkproto.GetNATInfoResponse{NatVIPIP: &dpdkproto.NATIP{}} - var err error - if nType == 0 { - req.NatInfoType = 1 - res1, err1 := c.DPDKonmetalClient.GetNATInfo(ctx, &req) - if err1 != nil { - return nil, err1 - } - req.NatInfoType = 2 - res2, err2 := c.DPDKonmetalClient.GetNATInfo(ctx, &req) - if err2 != nil { - return nil, err2 - } - res.NatInfoEntries = append(res.NatInfoEntries, res1.NatInfoEntries...) - res.NatInfoEntries = append(res.NatInfoEntries, res2.NatInfoEntries...) - res.NatVIPIP.Address = []byte(natVIPIP.String()) - } else { - res, err = c.DPDKonmetalClient.GetNATInfo(ctx, &req) - if err != nil { - return nil, err - } - } - - var nats = make([]api.Nat, len(res.NatInfoEntries)) - var nat api.Nat - for i, natInfoEntry := range res.GetNatInfoEntries() { - - var underlayRoute, vipIP netip.Addr - if natInfoEntry.UnderlayRoute != nil { - underlayRoute, err = netip.ParseAddr(string(natInfoEntry.GetUnderlayRoute())) - if err != nil { - return nil, fmt.Errorf("error parsing underlay route: %w", err) - } - nat.Spec.UnderlayRoute = &underlayRoute - vipIP, err = netip.ParseAddr(string(res.NatVIPIP.Address)) - if err != nil { - return nil, fmt.Errorf("error parsing vip ip: %w", err) - } - nat.Spec.NatVIPIP = &vipIP - } else if natInfoEntry.Address != nil { - vipIP, err = netip.ParseAddr(string(natInfoEntry.GetAddress())) - if err != nil { - return nil, fmt.Errorf("error parsing vip ip: %w", err) - } - nat.Spec.NatVIPIP = &vipIP - } - nat.Kind = api.NatKind - nat.Spec.MinPort = natInfoEntry.MinPort - nat.Spec.MaxPort = natInfoEntry.MaxPort - nats[i] = nat - } - return &api.NatList{ - TypeMeta: api.TypeMeta{Kind: api.NatListKind}, - NatListMeta: api.NatListMeta{NatIP: &natVIPIP, NatInfoType: natType}, - Items: nats, - }, nil -} - -func (c *client) DeleteNeighborNat(ctx context.Context, neigbhorNat api.NeighborNat) (*api.NeighborNat, error) { - res, err := c.DPDKonmetalClient.DeleteNeighborNAT(ctx, &dpdkproto.DeleteNeighborNATRequest{ - NatVIPIP: &dpdkproto.NATIP{ - IpVersion: api.NetIPAddrToProtoIPVersion(*neigbhorNat.NatVIPIP), - Address: []byte(neigbhorNat.NatVIPIP.String()), - }, - Vni: neigbhorNat.Spec.Vni, - MinPort: neigbhorNat.Spec.MinPort, - MaxPort: neigbhorNat.Spec.MaxPort, - }) - if err != nil { - return &api.NeighborNat{}, err - } - nnat := &api.NeighborNat{ - TypeMeta: api.TypeMeta{Kind: api.NeighborNatKind}, - NeighborNatMeta: neigbhorNat.NeighborNatMeta, - Status: api.ProtoStatusToStatus(res), - } - if errorCode := res.GetError(); errorCode != 0 { - return nnat, apierrors.ErrServerError - } - return nnat, nil -} - -func (c *client) ListFirewallRules(ctx context.Context, interfaceID string) (*api.FirewallRuleList, error) { - res, err := c.DPDKonmetalClient.ListFirewallRules(ctx, &dpdkproto.ListFirewallRulesRequest{ - InterfaceID: []byte(interfaceID), - }) - if err != nil { - return &api.FirewallRuleList{}, err - } - - fwRules := make([]api.FirewallRule, len(res.GetRules())) - for i, dpdkFwRule := range res.GetRules() { - fwRule, err := api.ProtoFwRuleToFwRule(dpdkFwRule, interfaceID) - if err != nil { - return &api.FirewallRuleList{}, err - } - fwRules[i] = *fwRule - } - - return &api.FirewallRuleList{ - TypeMeta: api.TypeMeta{Kind: api.FirewallRuleListKind}, - FirewallRuleListMeta: api.FirewallRuleListMeta{InterfaceID: interfaceID}, - Items: fwRules, - }, nil -} - -func (c *client) AddFirewallRule(ctx context.Context, fwRule *api.FirewallRule) (*api.FirewallRule, error) { - var action, direction, ipv uint8 - - switch strings.ToLower(fwRule.Spec.FirewallAction) { - case "accept", "allow", "1": - action = 1 - fwRule.Spec.FirewallAction = "Accept" - case "drop", "deny", "0": - action = 0 - fwRule.Spec.FirewallAction = "Drop" - default: - return &api.FirewallRule{}, fmt.Errorf("firewall action can be only: drop/deny/0|accept/allow/1") - } - - switch strings.ToLower(fwRule.Spec.TrafficDirection) { - case "ingress", "0": - direction = 0 - fwRule.Spec.TrafficDirection = "Ingress" - case "egress", "1": - direction = 1 - fwRule.Spec.TrafficDirection = "Egress" - default: - return &api.FirewallRule{}, fmt.Errorf("traffic direction can be only: Ingress = 0/Egress = 1") - } - - switch strings.ToLower(fwRule.Spec.IpVersion) { - case "ipv4", "0": - ipv = 0 - fwRule.Spec.IpVersion = "IPv4" - case "ipv6", "1": - ipv = 1 - fwRule.Spec.IpVersion = "IPv6" - default: - return &api.FirewallRule{}, fmt.Errorf("ip version can be only: IPv4 = 0/IPv6 = 1") - } - - req := dpdkproto.AddFirewallRuleRequest{ - InterfaceID: []byte(fwRule.FirewallRuleMeta.InterfaceID), - Rule: &dpdkproto.FirewallRule{ - RuleID: []byte(fwRule.Spec.RuleID), - Direction: dpdkproto.TrafficDirection(direction), - Action: dpdkproto.FirewallAction(action), - Priority: fwRule.Spec.Priority, - IpVersion: dpdkproto.IPVersion(ipv), - SourcePrefix: &dpdkproto.Prefix{ - IpVersion: dpdkproto.IPVersion(ipv), - Address: []byte(fwRule.Spec.SourcePrefix.Addr().String()), - PrefixLength: uint32(fwRule.Spec.SourcePrefix.Bits()), - }, - DestinationPrefix: &dpdkproto.Prefix{ - IpVersion: dpdkproto.IPVersion(ipv), - Address: []byte(fwRule.Spec.DestinationPrefix.Addr().String()), - PrefixLength: uint32(fwRule.Spec.DestinationPrefix.Bits()), - }, - ProtocolFilter: fwRule.Spec.ProtocolFilter, - }, - } - - res, err := c.DPDKonmetalClient.AddFirewallRule(ctx, &req) - if err != nil { - return &api.FirewallRule{}, err - } - retFwrule := &api.FirewallRule{ - TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, - FirewallRuleMeta: api.FirewallRuleMeta{InterfaceID: fwRule.InterfaceID}, - Spec: api.FirewallRuleSpec{RuleID: fwRule.Spec.RuleID}, - Status: api.ProtoStatusToStatus(res.Status)} - if res.Status.Error != 0 { - return retFwrule, apierrors.ErrServerError - } - retFwrule.Spec = fwRule.Spec - return retFwrule, nil -} - -func (c *client) GetFirewallRule(ctx context.Context, ruleID string, interfaceID string) (*api.FirewallRule, error) { - res, err := c.DPDKonmetalClient.GetFirewallRule(ctx, &dpdkproto.GetFirewallRuleRequest{ - InterfaceID: []byte(interfaceID), - RuleID: []byte(ruleID), - }) - if err != nil { - return &api.FirewallRule{}, err - } - if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return &api.FirewallRule{ - TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, - FirewallRuleMeta: api.FirewallRuleMeta{InterfaceID: interfaceID}, - Spec: api.FirewallRuleSpec{RuleID: ruleID}, - Status: api.ProtoStatusToStatus(res.Status)}, apierrors.ErrServerError - } - - return api.ProtoFwRuleToFwRule(res.Rule, interfaceID) -} - -func (c *client) DeleteFirewallRule(ctx context.Context, interfaceID string, ruleID string) (*api.FirewallRule, error) { - res, err := c.DPDKonmetalClient.DeleteFirewallRule(ctx, &dpdkproto.DeleteFirewallRuleRequest{ - InterfaceID: []byte(interfaceID), - RuleID: []byte(ruleID), - }) - if err != nil { - return &api.FirewallRule{}, err - } - retFwrule := &api.FirewallRule{ - TypeMeta: api.TypeMeta{Kind: api.FirewallRuleKind}, - FirewallRuleMeta: api.FirewallRuleMeta{InterfaceID: interfaceID}, - Spec: api.FirewallRuleSpec{RuleID: ruleID}, - Status: api.ProtoStatusToStatus(res), - } - if errorCode := res.GetError(); errorCode != 0 { - return retFwrule, apierrors.ErrServerError - } - return retFwrule, nil -} - -func (c *client) Initialized(ctx context.Context) (string, error) { - res, err := c.DPDKonmetalClient.Initialized(ctx, &dpdkproto.Empty{}) - if err != nil { - return "", err - } - return res.Uuid, nil -} - -func (c *client) Init(ctx context.Context, initConfig dpdkproto.InitConfig) (*api.Init, error) { - res, err := c.DPDKonmetalClient.Init(ctx, &initConfig) - if err != nil { - return &api.Init{}, err - } - if errorCode := res.GetError(); errorCode != 0 { - return &api.Init{Status: api.ProtoStatusToStatus(res)}, apierrors.ErrServerError - } - return &api.Init{TypeMeta: api.TypeMeta{Kind: "Init"}, Status: api.ProtoStatusToStatus(res)}, nil -} - -func (c *client) GetVni(ctx context.Context, vni uint32, vniType uint8) (*api.Vni, error) { - res, err := c.DPDKonmetalClient.IsVniInUse(ctx, &dpdkproto.IsVniInUseRequest{ - Vni: vni, - Type: dpdkproto.VniType(vniType), - }) - if err != nil { - return &api.Vni{}, err - } - retVni := &api.Vni{ - TypeMeta: api.TypeMeta{Kind: api.VniKind}, - VniMeta: api.VniMeta{VNI: vni, VniType: vniType}, - Status: api.ProtoStatusToStatus(res.Status), - } - if errorCode := res.GetStatus().GetError(); errorCode != 0 { - return retVni, apierrors.ErrServerError - } - retVni.Spec.InUse = res.InUse - return retVni, nil -} diff --git a/dpdk/client/dynamic/dynamic.go b/dpdk/client/dynamic/dynamic.go index 562edf8..c5c86bd 100644 --- a/dpdk/client/dynamic/dynamic.go +++ b/dpdk/client/dynamic/dynamic.go @@ -19,8 +19,8 @@ import ( "fmt" "net/netip" - "github.com/onmetal/dpservice-cli/dpdk/api" - structured "github.com/onmetal/dpservice-cli/dpdk/client" + "github.com/onmetal/net-dpservice-go/api" + structured "github.com/onmetal/net-dpservice-go/client" ) type ObjectKey interface { diff --git a/dpdk/api/register.go b/dpdk/runtime/register.go similarity index 60% rename from dpdk/api/register.go rename to dpdk/runtime/register.go index 5621bc9..434eef4 100644 --- a/dpdk/api/register.go +++ b/dpdk/runtime/register.go @@ -12,32 +12,30 @@ // See the License for the specific language governing permissions and // limitations under the License. -package api +package runtime -import ( - "github.com/onmetal/dpservice-cli/dpdk/runtime" -) +import "github.com/onmetal/net-dpservice-go/api" -var DefaultScheme = runtime.NewScheme() +var DefaultScheme = NewScheme() func init() { if err := DefaultScheme.Add( - &Interface{}, - &InterfaceList{}, - &Prefix{}, - &PrefixList{}, - &Route{}, - &RouteList{}, - &VirtualIP{}, - &LoadBalancer{}, - &LoadBalancerTarget{}, - &LoadBalancerPrefix{}, - &LoadBalancerTargetList{}, - &Nat{}, - &NatList{}, - &NeighborNat{}, - &FirewallRule{}, - &Vni{}, + &api.Interface{}, + &api.InterfaceList{}, + &api.Prefix{}, + &api.PrefixList{}, + &api.Route{}, + &api.RouteList{}, + &api.VirtualIP{}, + &api.LoadBalancer{}, + &api.LoadBalancerTarget{}, + &api.LoadBalancerPrefix{}, + &api.LoadBalancerTargetList{}, + &api.Nat{}, + &api.NatList{}, + &api.NeighborNat{}, + &api.FirewallRule{}, + &api.Vni{}, ); err != nil { panic(err) } diff --git a/go.mod b/go.mod index bbb8d3a..8e0a86b 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/ghodss/yaml v1.0.0 github.com/google/addlicense v1.1.1 github.com/jedib0t/go-pretty/v6 v6.3.9 - github.com/onmetal/net-dpservice-go v0.1.11 + github.com/onmetal/net-dpservice-go v0.1.13-0.20230613080142-e5672d5fcbd0 github.com/onsi/ginkgo/v2 v2.2.0 github.com/onsi/gomega v1.20.2 github.com/spf13/cobra v1.4.0 @@ -31,6 +31,4 @@ require ( google.golang.org/protobuf v1.28.1 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect -) - -replace github.com/onmetal/net-dpservice-go v0.1.1 => github.com/onmetal/net-dpservice-go v0.1.3-0.20220901075245-e11be6221ddd +) \ No newline at end of file diff --git a/go.sum b/go.sum index 43d1349..7873a22 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/onmetal/net-dpservice-go v0.1.11 h1:1YGfARGVkwJ6WG+6KcYla6cggYqif4yIoHtJS8jOysQ= -github.com/onmetal/net-dpservice-go v0.1.11/go.mod h1:dbItLY1P+nYl7/c04JdJTEaMCG6GLXgAZsMP/OuZBmg= +github.com/onmetal/net-dpservice-go v0.1.13-0.20230613080142-e5672d5fcbd0 h1:wXQtKZ2zDSiBRhtp75MYnw7M9zK0yrRdIGqefNWQukQ= +github.com/onmetal/net-dpservice-go v0.1.13-0.20230613080142-e5672d5fcbd0/go.mod h1:dbItLY1P+nYl7/c04JdJTEaMCG6GLXgAZsMP/OuZBmg= github.com/onsi/ginkgo/v2 v2.2.0 h1:3ZNA3L1c5FYDFTTxbFeVGGD8jYvjYauHD30YgLxVsNI= github.com/onsi/ginkgo/v2 v2.2.0/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk= github.com/onsi/gomega v1.20.2 h1:8uQq0zMgLEfa0vRrrBgaJF2gyW9Da9BmfGV+OyUzfkY= diff --git a/main.go b/main.go index ab66cb0..604f79b 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,7 @@ import ( "strconv" "github.com/onmetal/dpservice-cli/cmd" - "github.com/onmetal/dpservice-cli/dpdk/api/errors" + "github.com/onmetal/net-dpservice-go/errors" ) func main() { diff --git a/renderer/renderer.go b/renderer/renderer.go index 80249ac..f1881e0 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -25,7 +25,7 @@ import ( "github.com/ghodss/yaml" "github.com/jedib0t/go-pretty/v6/table" - "github.com/onmetal/dpservice-cli/dpdk/api" + "github.com/onmetal/net-dpservice-go/api" dpdkproto "github.com/onmetal/net-dpservice-go/proto" ) diff --git a/sources/sources.go b/sources/sources.go index cb6b19a..9524280 100644 --- a/sources/sources.go +++ b/sources/sources.go @@ -21,8 +21,7 @@ import ( "os" "path/filepath" - "github.com/onmetal/dpservice-cli/dpdk/api" - runtime2 "github.com/onmetal/dpservice-cli/dpdk/runtime" + "github.com/onmetal/dpservice-cli/dpdk/runtime" ) type Iterator struct { @@ -140,7 +139,7 @@ func (s *DirSource) Next() (ReadCloserExt, error) { return nil, io.EOF } -func IterateObjects(iterator *Iterator, scheme *runtime2.Scheme, f func(obj any) error) error { +func IterateObjects(iterator *Iterator, scheme *runtime.Scheme, f func(obj any) error) error { for { src, err := iterator.Next() if err != nil { @@ -159,12 +158,12 @@ func IterateObjects(iterator *Iterator, scheme *runtime2.Scheme, f func(obj any) break } - newDecoder, err := runtime2.NewExtDecoderFactory(rce.Ext()) + newDecoder, err := runtime.NewExtDecoderFactory(rce.Ext()) if err != nil { return err } - decoder := runtime2.NewKindDecoder(api.DefaultScheme, runtime2.NewPeekDecoder(rce, newDecoder)) + decoder := runtime.NewKindDecoder(runtime.DefaultScheme, runtime.NewPeekDecoder(rce, newDecoder)) for { obj, err := decoder.Next() if err != nil { @@ -183,7 +182,7 @@ func IterateObjects(iterator *Iterator, scheme *runtime2.Scheme, f func(obj any) return nil } -func CollectObjects(iterator *Iterator, scheme *runtime2.Scheme) ([]any, error) { +func CollectObjects(iterator *Iterator, scheme *runtime.Scheme) ([]any, error) { var objs []any if err := IterateObjects(iterator, scheme, func(obj any) error { objs = append(objs, obj) From ac064e22de5a94f26fdeba1ce5187f9deb011fc8 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 13 Jun 2023 14:54:19 +0200 Subject: [PATCH 58/65] change flag name from natip to nat-ip --- cmd/add_nat.go | 8 +++---- cmd/add_neighbor_nat.go | 8 +++---- cmd/delete_neighbor_nat.go | 8 +++---- cmd/reset_vni.go | 21 +++++++++++++++---- docs/commands/README.md | 6 +++--- docs/commands/dpservice-cli_add_nat.md | 6 +++--- .../commands/dpservice-cli_add_neighbornat.md | 6 +++--- .../dpservice-cli_delete_neighbornat.md | 6 +++--- 8 files changed, 41 insertions(+), 28 deletions(-) diff --git a/cmd/add_nat.go b/cmd/add_nat.go index ce654ab..19b349f 100644 --- a/cmd/add_nat.go +++ b/cmd/add_nat.go @@ -34,9 +34,9 @@ func AddNat(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFactory ) cmd := &cobra.Command{ - Use: "nat <--natip> <--minport> <--maxport>", + Use: "nat <--interface-id> <--nat-ip> <--minport> <--maxport>", Short: "Add a NAT to interface", - Example: "dpservice-cli add nat --interface-id=vm1 --natip=10.20.30.40 --minport=30000 --maxport=30100", + Example: "dpservice-cli add nat --interface-id=vm1 --nat-ip=10.20.30.40 --minport=30000 --maxport=30100", Aliases: NatAliases, Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { @@ -68,11 +68,11 @@ func (o *AddNatOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&o.InterfaceID, "interface-id", o.InterfaceID, "Interface ID where to add NAT.") fs.Uint32Var(&o.MinPort, "minport", o.MinPort, "MinPort of NAT.") fs.Uint32Var(&o.MaxPort, "maxport", o.MaxPort, "MaxPort of NAT.") - flag.AddrVar(fs, &o.NATVipIP, "natip", o.NATVipIP, "NAT IP to assign to the interface.") + flag.AddrVar(fs, &o.NATVipIP, "nat-ip", o.NATVipIP, "NAT IP to assign to the interface.") } func (o *AddNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"interface-id", "minport", "maxport", "natip"} { + for _, name := range []string{"interface-id", "minport", "maxport", "nat-ip"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } diff --git a/cmd/add_neighbor_nat.go b/cmd/add_neighbor_nat.go index 2031807..b08135c 100644 --- a/cmd/add_neighbor_nat.go +++ b/cmd/add_neighbor_nat.go @@ -34,9 +34,9 @@ func AddNeighborNat(dpdkClientFactory DPDKClientFactory, rendererFactory Rendere ) cmd := &cobra.Command{ - Use: "neighbornat <--natip> <--vni> <--minport> <--maxport> <--underlayroute>", + Use: "neighbornat <--nat-ip> <--vni> <--minport> <--maxport> <--underlayroute>", Short: "Add a Neighbor NAT", - Example: "dpservice-cli add neighbornat --natip=10.20.30.40 --vni=100 --minport=30000 --maxport=30100 --underlayroute=ff80::1", + Example: "dpservice-cli add neighbornat --nat-ip=10.20.30.40 --vni=100 --minport=30000 --maxport=30100 --underlayroute=ff80::1", Aliases: NeighborNatAliases, Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { @@ -66,7 +66,7 @@ type AddNeighborNatOptions struct { } func (o *AddNeighborNatOptions) AddFlags(fs *pflag.FlagSet) { - flag.AddrVar(fs, &o.NatIP, "natip", o.NatIP, "Neighbor NAT IP.") + flag.AddrVar(fs, &o.NatIP, "nat-ip", o.NatIP, "Neighbor NAT IP.") fs.Uint32Var(&o.Vni, "vni", o.Vni, "VNI of neighbor NAT.") fs.Uint32Var(&o.MinPort, "minport", o.MinPort, "MinPort of neighbor NAT.") fs.Uint32Var(&o.MaxPort, "maxport", o.MaxPort, "MaxPort of neighbor NAT.") @@ -74,7 +74,7 @@ func (o *AddNeighborNatOptions) AddFlags(fs *pflag.FlagSet) { } func (o *AddNeighborNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"natip", "vni", "minport", "maxport", "underlayroute"} { + for _, name := range []string{"nat-ip", "vni", "minport", "maxport", "underlayroute"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } diff --git a/cmd/delete_neighbor_nat.go b/cmd/delete_neighbor_nat.go index f73eba0..5b14a64 100644 --- a/cmd/delete_neighbor_nat.go +++ b/cmd/delete_neighbor_nat.go @@ -34,9 +34,9 @@ func DeleteNeighborNat(dpdkClientFactory DPDKClientFactory, rendererFactory Rend ) cmd := &cobra.Command{ - Use: "neighbornat <--natip> <--vni> <--minport> <--maxport>", + Use: "neighbornat <--nat-ip> <--vni> <--minport> <--maxport>", Short: "Delete neighbor nat", - Example: "dpservice-cli delete neighbornat --natip=10.20.30.40 --vni=100 --minport=30000 --maxport=30100", + Example: "dpservice-cli delete neighbornat --nat-ip=10.20.30.40 --vni=100 --minport=30000 --maxport=30100", Aliases: NeighborNatAliases, Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { @@ -65,14 +65,14 @@ type DeleteNeighborNatOptions struct { } func (o *DeleteNeighborNatOptions) AddFlags(fs *pflag.FlagSet) { - flag.AddrVar(fs, &o.NatIP, "natip", o.NatIP, "Neighbor NAT IP.") + flag.AddrVar(fs, &o.NatIP, "nat-ip", o.NatIP, "Neighbor NAT IP.") fs.Uint32Var(&o.Vni, "vni", o.Vni, "VNI of neighbor NAT.") fs.Uint32Var(&o.MinPort, "minport", o.MinPort, "MinPort of neighbor NAT.") fs.Uint32Var(&o.MaxPort, "maxport", o.MaxPort, "MaxPort of neighbor NAT.") } func (o *DeleteNeighborNatOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"natip", "vni", "minport", "maxport"} { + for _, name := range []string{"nat-ip", "vni", "minport", "maxport"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } diff --git a/cmd/reset_vni.go b/cmd/reset_vni.go index f207f9d..0fa9e7f 100644 --- a/cmd/reset_vni.go +++ b/cmd/reset_vni.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "os" + "strings" "github.com/onmetal/dpservice-cli/util" "github.com/onmetal/net-dpservice-go/errors" @@ -56,16 +57,16 @@ func ResetVni(dpdkClientFactory DPDKClientFactory, rendererFactory RendererFacto type ResetVniOptions struct { VNI uint32 - VniType uint8 + VniType string } func (o *ResetVniOptions) AddFlags(fs *pflag.FlagSet) { fs.Uint32Var(&o.VNI, "vni", o.VNI, "VNI to check.") - fs.Uint8Var(&o.VniType, "vni-type", o.VniType, "VNI Type: VniIpv4 = 0/VniIpv6 = 1.") + fs.StringVar(&o.VniType, "vni-type", "both", "VNI Type: ipv4 = 0/ipv6 = 1/both = 2.") } func (o *ResetVniOptions) MarkRequiredFlags(cmd *cobra.Command) error { - for _, name := range []string{"vni", "vni-type"} { + for _, name := range []string{"vni"} { if err := cmd.MarkFlagRequired(name); err != nil { return err } @@ -85,7 +86,19 @@ func RunResetVni( } defer DpdkClose(cleanup) - vni, err := client.ResetVni(ctx, opts.VNI, opts.VniType) + var vniType uint8 + switch strings.ToLower(opts.VniType) { + case "ipv4", "0": + vniType = 0 + case "ipv6", "1": + vniType = 1 + case "both", "2": + vniType = 2 + default: + return fmt.Errorf("VNI type can be only: ipv4 = 0/ipv6 = 1/both = 2") + } + + vni, err := client.ResetVni(ctx, opts.VNI, vniType) if err != nil && err != errors.ErrServerError { return fmt.Errorf("error resetting vni: %w", err) } diff --git a/docs/commands/README.md b/docs/commands/README.md index 99465a0..2b70af7 100644 --- a/docs/commands/README.md +++ b/docs/commands/README.md @@ -67,7 +67,7 @@ get virtualip --interface-id= ## Add/delete/list NAT IP (with port range) for the interface: ``` -add nat --interface-id= --natip= --minport= --maxport= +add nat --interface-id= --nat-ip= --minport= --maxport= delete nat --interface-id= get nat --interface-id= list nats @@ -75,8 +75,8 @@ list nats ## Add/delete/list neighbors (dp-services) with the same NAT IP: ``` -add neighbornat --natip= --vni= --minport= --maxport= --underlayroute= -delete neighbornat --natip= --vni= --minport= --maxport= +add neighbornat --nat-ip= --vni= --minport= --maxport= --underlayroute= +delete neighbornat --nat-ip= --vni= --minport= --maxport= get natinfo --nat-ip= --info-type= ``` diff --git a/docs/commands/dpservice-cli_add_nat.md b/docs/commands/dpservice-cli_add_nat.md index 4c55029..ed813f9 100644 --- a/docs/commands/dpservice-cli_add_nat.md +++ b/docs/commands/dpservice-cli_add_nat.md @@ -3,13 +3,13 @@ Add a NAT to interface ``` -dpservice-cli add nat <--natip> <--minport> <--maxport> [flags] +dpservice-cli add nat <--nat-ip> <--minport> <--maxport> [flags] ``` ### Examples ``` -dpservice-cli add nat --interface-id=vm1 --natip=10.20.30.40 --minport=30000 --maxport=30100 +dpservice-cli add nat --interface-id=vm1 --nat-ip=10.20.30.40 --minport=30000 --maxport=30100 ``` ### Options @@ -19,7 +19,7 @@ dpservice-cli add nat --interface-id=vm1 --natip=10.20.30.40 --minport=30000 --m --interface-id string Interface ID where to add NAT. --maxport uint32 MaxPort of NAT. --minport uint32 MinPort of NAT. - --natip ip NAT IP to assign to the interface. (default invalid IP) + --nat-ip ip NAT IP to assign to the interface. (default invalid IP) ``` ### Options inherited from parent commands diff --git a/docs/commands/dpservice-cli_add_neighbornat.md b/docs/commands/dpservice-cli_add_neighbornat.md index b1bc756..d616eb4 100644 --- a/docs/commands/dpservice-cli_add_neighbornat.md +++ b/docs/commands/dpservice-cli_add_neighbornat.md @@ -3,13 +3,13 @@ Add a Neighbor NAT ``` -dpservice-cli add neighbornat <--natip> <--vni> <--minport> <--maxport> <--underlayroute> [flags] +dpservice-cli add neighbornat <--nat-ip> <--vni> <--minport> <--maxport> <--underlayroute> [flags] ``` ### Examples ``` -dpservice-cli add neighbornat --natip=10.20.30.40 --vni=100 --minport=30000 --maxport=30100 --underlayroute=ff80::1 +dpservice-cli add neighbornat --nat-ip=10.20.30.40 --vni=100 --minport=30000 --maxport=30100 --underlayroute=ff80::1 ``` ### Options @@ -18,7 +18,7 @@ dpservice-cli add neighbornat --natip=10.20.30.40 --vni=100 --minport=30000 --ma -h, --help help for neighbornat --maxport uint32 MaxPort of neighbor NAT. --minport uint32 MinPort of neighbor NAT. - --natip ip Neighbor NAT IP. (default invalid IP) + --nat-ip ip Neighbor NAT IP. (default invalid IP) --underlayroute ip Underlay route of neighbor NAT. (default invalid IP) --vni uint32 VNI of neighbor NAT. ``` diff --git a/docs/commands/dpservice-cli_delete_neighbornat.md b/docs/commands/dpservice-cli_delete_neighbornat.md index 6ca448b..3076c37 100644 --- a/docs/commands/dpservice-cli_delete_neighbornat.md +++ b/docs/commands/dpservice-cli_delete_neighbornat.md @@ -3,13 +3,13 @@ Delete neighbor nat ``` -dpservice-cli delete neighbornat <--natip> <--vni> <--minport> <--maxport> [flags] +dpservice-cli delete neighbornat <--nat-ip> <--vni> <--minport> <--maxport> [flags] ``` ### Examples ``` -dpservice-cli delete neighbornat --natip=10.20.30.40 --vni=100 --minport=30000 --maxport=30100 +dpservice-cli delete neighbornat --nat-ip=10.20.30.40 --vni=100 --minport=30000 --maxport=30100 ``` ### Options @@ -18,7 +18,7 @@ dpservice-cli delete neighbornat --natip=10.20.30.40 --vni=100 --minport=30000 - -h, --help help for neighbornat --maxport uint32 MaxPort of neighbor NAT. --minport uint32 MinPort of neighbor NAT. - --natip ip Neighbor NAT IP. (default invalid IP) + --nat-ip ip Neighbor NAT IP. (default invalid IP) --vni uint32 VNI of neighbor NAT. ``` From a1cc1db294ee4d30712328c590c2df94c31b1ed8 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 13 Jun 2023 15:15:47 +0200 Subject: [PATCH 59/65] fix get natinfo output --- renderer/renderer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/renderer/renderer.go b/renderer/renderer.go index f1881e0..be58251 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -333,7 +333,7 @@ func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { // if command was natinfo } else { columns[i] = []any{nat.Spec.NatVIPIP, nat.Spec.MinPort, nat.Spec.MaxPort, nat.Spec.UnderlayRoute} - if len(nats) > 0 && nats[0].Spec.UnderlayRoute == nil { + if len(nats) > 0 && nats[i].Spec.UnderlayRoute == nil { columns[i] = append(columns[i], "Local") } else { columns[i] = append(columns[i], "Neighbor") From b6378c9e6684ca323e63c0d97291847a31915299 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Tue, 13 Jun 2023 15:58:38 +0200 Subject: [PATCH 60/65] change nat info table output --- go.mod | 2 +- go.sum | 6 ++++-- renderer/renderer.go | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 8e0a86b..7dd27ff 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/ghodss/yaml v1.0.0 github.com/google/addlicense v1.1.1 github.com/jedib0t/go-pretty/v6 v6.3.9 - github.com/onmetal/net-dpservice-go v0.1.13-0.20230613080142-e5672d5fcbd0 + github.com/onmetal/net-dpservice-go v0.1.13-0.20230613133937-066e40ed0eb3 github.com/onsi/ginkgo/v2 v2.2.0 github.com/onsi/gomega v1.20.2 github.com/spf13/cobra v1.4.0 diff --git a/go.sum b/go.sum index 7873a22..44494c0 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,10 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/onmetal/net-dpservice-go v0.1.13-0.20230613080142-e5672d5fcbd0 h1:wXQtKZ2zDSiBRhtp75MYnw7M9zK0yrRdIGqefNWQukQ= -github.com/onmetal/net-dpservice-go v0.1.13-0.20230613080142-e5672d5fcbd0/go.mod h1:dbItLY1P+nYl7/c04JdJTEaMCG6GLXgAZsMP/OuZBmg= +github.com/onmetal/net-dpservice-go v0.1.13-0.20230613133615-01fd5bbbc3d2 h1:obCbgkXzIzxGlv4XsbRWs6rEY4g5uMxHd2+TxfhXwNo= +github.com/onmetal/net-dpservice-go v0.1.13-0.20230613133615-01fd5bbbc3d2/go.mod h1:dbItLY1P+nYl7/c04JdJTEaMCG6GLXgAZsMP/OuZBmg= +github.com/onmetal/net-dpservice-go v0.1.13-0.20230613133937-066e40ed0eb3 h1:AZihpu9G709TNhLNc/7TnAVWaLWkP+1b4tKBWhS44z4= +github.com/onmetal/net-dpservice-go v0.1.13-0.20230613133937-066e40ed0eb3/go.mod h1:dbItLY1P+nYl7/c04JdJTEaMCG6GLXgAZsMP/OuZBmg= github.com/onsi/ginkgo/v2 v2.2.0 h1:3ZNA3L1c5FYDFTTxbFeVGGD8jYvjYauHD30YgLxVsNI= github.com/onsi/ginkgo/v2 v2.2.0/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk= github.com/onsi/gomega v1.20.2 h1:8uQq0zMgLEfa0vRrrBgaJF2gyW9Da9BmfGV+OyUzfkY= diff --git a/renderer/renderer.go b/renderer/renderer.go index be58251..7151fde 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -319,10 +319,10 @@ func (t defaultTableConverter) natTable(nats []api.Nat) (*TableData, error) { var headers []any // if command was nat or there are no nats if len(nats) > 0 && nats[0].InterfaceID != "" { - headers = []any{"InterfaceID", "NatIP", "MinPort", "MaxPort", "UnderlayRoute"} + headers = []any{"InterfaceID", "IP", "MinPort", "MaxPort", "UnderlayRoute"} // if command was natinfo } else { - headers = []any{"NatIP", "MinPort", "MaxPort", "UnderlayRoute", "NatInfoType"} + headers = []any{"IP", "MinPort", "MaxPort", "UnderlayRoute", "NatInfoType"} } columns := make([][]any, len(nats)) From 97767d8afef3999b43199df075eaf9d402f196b0 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Wed, 14 Jun 2023 14:19:44 +0200 Subject: [PATCH 61/65] fix output of add lbprefix command --- cmd/create_loadbalancer_prefix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/create_loadbalancer_prefix.go b/cmd/create_loadbalancer_prefix.go index e763ec9..2192951 100644 --- a/cmd/create_loadbalancer_prefix.go +++ b/cmd/create_loadbalancer_prefix.go @@ -103,5 +103,5 @@ func RunCreateLoadBalancerPrefix( return fmt.Errorf("error adding loadbalancer prefix: %w", err) } - return rendererFactory.RenderObject("added", os.Stdout, lbprefix) + return rendererFactory.RenderObject(fmt.Sprintf("added, underlay route: %s", lbprefix.Spec.UnderlayRoute), os.Stdout, lbprefix) } From 1b95806067c781d1b4f181264d35053cfb07afb7 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Wed, 14 Jun 2023 15:24:39 +0200 Subject: [PATCH 62/65] generate current docs files --- docs/commands/README.md | 6 +++ docs/commands/dpservice-cli.md | 6 ++- docs/commands/dpservice-cli_add.md | 3 +- .../dpservice-cli_add_firewallrule.md | 17 +++++---- docs/commands/dpservice-cli_add_interface.md | 5 ++- docs/commands/dpservice-cli_add_lbprefix.md | 5 ++- docs/commands/dpservice-cli_add_lbtarget.md | 5 ++- .../dpservice-cli_add_loadbalancer.md | 5 ++- docs/commands/dpservice-cli_add_nat.md | 9 +++-- .../commands/dpservice-cli_add_neighbornat.md | 7 ++-- docs/commands/dpservice-cli_add_prefix.md | 5 ++- docs/commands/dpservice-cli_add_route.md | 5 ++- docs/commands/dpservice-cli_add_virtualip.md | 5 ++- docs/commands/dpservice-cli_completion.md | 3 +- docs/commands/dpservice-cli_delete.md | 3 +- .../dpservice-cli_delete_firewallrule.md | 3 +- .../dpservice-cli_delete_interface.md | 3 +- .../commands/dpservice-cli_delete_lbprefix.md | 3 +- .../commands/dpservice-cli_delete_lbtarget.md | 3 +- .../dpservice-cli_delete_loadbalancer.md | 3 +- docs/commands/dpservice-cli_delete_nat.md | 3 +- .../dpservice-cli_delete_neighbornat.md | 5 ++- docs/commands/dpservice-cli_delete_prefix.md | 3 +- docs/commands/dpservice-cli_delete_route.md | 7 ++-- .../dpservice-cli_delete_virtualip.md | 3 +- docs/commands/dpservice-cli_get.md | 3 +- .../dpservice-cli_get_firewallrule.md | 5 ++- docs/commands/dpservice-cli_get_interface.md | 5 ++- .../dpservice-cli_get_loadbalancer.md | 5 ++- docs/commands/dpservice-cli_get_nat.md | 5 ++- docs/commands/dpservice-cli_get_natinfo.md | 9 +++-- docs/commands/dpservice-cli_get_virtualip.md | 5 ++- docs/commands/dpservice-cli_get_vni.md | 7 ++-- docs/commands/dpservice-cli_init.md | 3 +- docs/commands/dpservice-cli_initialized.md | 3 +- docs/commands/dpservice-cli_list.md | 9 +++-- .../dpservice-cli_list_firewallrules.md | 7 ++-- .../commands/dpservice-cli_list_interfaces.md | 7 ++-- .../commands/dpservice-cli_list_lbprefixes.md | 7 ++-- ...get.md => dpservice-cli_list_lbtargets.md} | 15 ++++---- docs/commands/dpservice-cli_list_nats.md | 7 ++-- docs/commands/dpservice-cli_list_prefixes.md | 7 ++-- docs/commands/dpservice-cli_list_routes.md | 7 ++-- docs/commands/dpservice-cli_reset.md | 34 +++++++++++++++++ docs/commands/dpservice-cli_reset_vni.md | 37 +++++++++++++++++++ go.mod | 2 +- go.sum | 2 - 47 files changed, 217 insertions(+), 99 deletions(-) rename docs/commands/{dpservice-cli_list_lbtarget.md => dpservice-cli_list_lbtargets.md} (60%) create mode 100644 docs/commands/dpservice-cli_reset.md create mode 100644 docs/commands/dpservice-cli_reset_vni.md diff --git a/docs/commands/README.md b/docs/commands/README.md index 2b70af7..f8b0019 100644 --- a/docs/commands/README.md +++ b/docs/commands/README.md @@ -86,4 +86,10 @@ add fwrule --interface-id= --action= --direction= --dst= delete firewallrule --rule-id= --interface-id= get fwrule --rule-id= --interface-id= list firewallrules --interface-id= +``` + +## Get/reset vni: +``` +get vni --vni= --vni-type= +reset vni --vni= --vni-type= ``` \ No newline at end of file diff --git a/docs/commands/dpservice-cli.md b/docs/commands/dpservice-cli.md index fdb2e7e..fbccaef 100644 --- a/docs/commands/dpservice-cli.md +++ b/docs/commands/dpservice-cli.md @@ -14,6 +14,7 @@ dpservice-cli [flags] -h, --help help for dpservice-cli -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO @@ -24,6 +25,7 @@ dpservice-cli [flags] * [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] * [dpservice-cli init](dpservice-cli_init.md) - Initial set up of the DPDK app * [dpservice-cli initialized](dpservice-cli_initialized.md) - Indicates if the DPDK app has been initialized already -* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtargets nats] +* [dpservice-cli reset](dpservice-cli_reset.md) - Resets one of [vni] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_add.md b/docs/commands/dpservice-cli_add.md index bb15c95..3943f36 100644 --- a/docs/commands/dpservice-cli_add.md +++ b/docs/commands/dpservice-cli_add.md @@ -24,6 +24,7 @@ dpservice-cli add [flags] --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO @@ -40,4 +41,4 @@ dpservice-cli add [flags] * [dpservice-cli add route](dpservice-cli_add_route.md) - Add a route * [dpservice-cli add virtualip](dpservice-cli_add_virtualip.md) - Add a virtual IP to interface. -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_add_firewallrule.md b/docs/commands/dpservice-cli_add_firewallrule.md index 7d82f65..3e0ef82 100644 --- a/docs/commands/dpservice-cli_add_firewallrule.md +++ b/docs/commands/dpservice-cli_add_firewallrule.md @@ -18,19 +18,19 @@ dpservice-cli add fwrule --interface-id=vm1 --action=1 --direction=1 --dst=5.5.5 --action string Firewall action: drop/deny/0|accept/allow/1 (Can be only "accept/allow/1" at the moment). --direction string Traffic direction of FW Rule: Ingress = 0/Egress = 1 --dst ipprefix Destination prefix (0.0.0.0 with prefix length 0 matches all destination IPs). (default invalid Prefix) - --dst-port-max int32 Destination Ports end. - --dst-port-min int32 Destination Ports start (-1 matches all destination ports). + --dst-port-max int32 Destination Ports end. (default -1) + --dst-port-min int32 Destination Ports start (-1 matches all destination ports). (default -1) -h, --help help for firewallrule - --icmp-code int32 ICMP code (-1 matches all ICMP Codes). - --icmp-type int32 ICMP type (-1 matches all ICMP Types). + --icmp-code int32 ICMP code (-1 matches all ICMP Codes). (default -1) + --icmp-type int32 ICMP type (-1 matches all ICMP Types). (default -1) --interface-id string InterfaceID of FW Rule. --ipver string IpVersion of FW Rule: IPv4 = 0/IPv6 = 1. --priority uint32 Priority of FW Rule. (For future use. No effect at the moment). (default 1000) --protocol string Protocol used icmp/tcp/udp (Not defining a protocol filter matches all protocols). --rule-id string RuleID of FW Rule. --src ipprefix Source prefix (0.0.0.0 with prefix length 0 matches all source IPs). (default invalid Prefix) - --src-port-max int32 Source Ports end. - --src-port-min int32 Source Ports start (-1 matches all source ports). + --src-port-max int32 Source Ports end. (default -1) + --src-port-min int32 Source Ports start (-1 matches all source ports). (default -1) ``` ### Options inherited from parent commands @@ -38,12 +38,13 @@ dpservice-cli add fwrule --interface-id=vm1 --action=1 --direction=1 --dst=5.5.5 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] (default "table") + -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_add_interface.md b/docs/commands/dpservice-cli_add_interface.md index 78ef0fb..04d1815 100644 --- a/docs/commands/dpservice-cli_add_interface.md +++ b/docs/commands/dpservice-cli_add_interface.md @@ -29,12 +29,13 @@ dpservice-cli add interface --id=vm4 --ip=10.200.1.4 --ip=2000:200:1::4 --vni=20 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] (default "table") + -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_add_lbprefix.md b/docs/commands/dpservice-cli_add_lbprefix.md index 591eeb7..e13db26 100644 --- a/docs/commands/dpservice-cli_add_lbprefix.md +++ b/docs/commands/dpservice-cli_add_lbprefix.md @@ -25,12 +25,13 @@ dpservice-cli add lbprefix --prefix=10.10.10.0/24 --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] (default "table") + -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_add_lbtarget.md b/docs/commands/dpservice-cli_add_lbtarget.md index a579f5a..edac936 100644 --- a/docs/commands/dpservice-cli_add_lbtarget.md +++ b/docs/commands/dpservice-cli_add_lbtarget.md @@ -25,12 +25,13 @@ dpservice-cli add lbtarget --target-ip=ff80::5 --lb-id=2 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] (default "table") + -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_add_loadbalancer.md b/docs/commands/dpservice-cli_add_loadbalancer.md index 7fe2aab..3b480b7 100644 --- a/docs/commands/dpservice-cli_add_loadbalancer.md +++ b/docs/commands/dpservice-cli_add_loadbalancer.md @@ -27,12 +27,13 @@ dpservice-cli add loadbalancer --id=4 --vni=100 --vip=10.20.30.40 --lbports=TCP/ ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] (default "table") + -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_add_nat.md b/docs/commands/dpservice-cli_add_nat.md index ed813f9..0ac41b5 100644 --- a/docs/commands/dpservice-cli_add_nat.md +++ b/docs/commands/dpservice-cli_add_nat.md @@ -3,7 +3,7 @@ Add a NAT to interface ``` -dpservice-cli add nat <--nat-ip> <--minport> <--maxport> [flags] +dpservice-cli add nat <--interface-id> <--nat-ip> <--minport> <--maxport> [flags] ``` ### Examples @@ -19,7 +19,7 @@ dpservice-cli add nat --interface-id=vm1 --nat-ip=10.20.30.40 --minport=30000 -- --interface-id string Interface ID where to add NAT. --maxport uint32 MaxPort of NAT. --minport uint32 MinPort of NAT. - --nat-ip ip NAT IP to assign to the interface. (default invalid IP) + --nat-ip ip NAT IP to assign to the interface. (default invalid IP) ``` ### Options inherited from parent commands @@ -27,12 +27,13 @@ dpservice-cli add nat --interface-id=vm1 --nat-ip=10.20.30.40 --minport=30000 -- ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] (default "table") + -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_add_neighbornat.md b/docs/commands/dpservice-cli_add_neighbornat.md index d616eb4..49fc1a0 100644 --- a/docs/commands/dpservice-cli_add_neighbornat.md +++ b/docs/commands/dpservice-cli_add_neighbornat.md @@ -18,7 +18,7 @@ dpservice-cli add neighbornat --nat-ip=10.20.30.40 --vni=100 --minport=30000 --m -h, --help help for neighbornat --maxport uint32 MaxPort of neighbor NAT. --minport uint32 MinPort of neighbor NAT. - --nat-ip ip Neighbor NAT IP. (default invalid IP) + --nat-ip ip Neighbor NAT IP. (default invalid IP) --underlayroute ip Underlay route of neighbor NAT. (default invalid IP) --vni uint32 VNI of neighbor NAT. ``` @@ -28,12 +28,13 @@ dpservice-cli add neighbornat --nat-ip=10.20.30.40 --vni=100 --minport=30000 --m ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] (default "table") + -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_add_prefix.md b/docs/commands/dpservice-cli_add_prefix.md index dd8d0ab..50c93d0 100644 --- a/docs/commands/dpservice-cli_add_prefix.md +++ b/docs/commands/dpservice-cli_add_prefix.md @@ -25,12 +25,13 @@ dpservice-cli add prefix --prefix=10.20.30.0/24 --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] (default "table") + -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_add_route.md b/docs/commands/dpservice-cli_add_route.md index 89bf142..0cb9393 100644 --- a/docs/commands/dpservice-cli_add_route.md +++ b/docs/commands/dpservice-cli_add_route.md @@ -27,12 +27,13 @@ dpservice-cli add route --prefix=10.100.3.0/24 --next-hop-vni=0 --next-hop-ip=fc ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] (default "table") + -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_add_virtualip.md b/docs/commands/dpservice-cli_add_virtualip.md index d656d8f..c66d2b6 100644 --- a/docs/commands/dpservice-cli_add_virtualip.md +++ b/docs/commands/dpservice-cli_add_virtualip.md @@ -25,12 +25,13 @@ dpservice-cli add virtualip --vip=20.20.20.20 --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] (default "table") + -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli add](dpservice-cli_add.md) - Creates one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_completion.md b/docs/commands/dpservice-cli_completion.md index 0d0378e..1204f00 100644 --- a/docs/commands/dpservice-cli_completion.md +++ b/docs/commands/dpservice-cli_completion.md @@ -61,10 +61,11 @@ dpservice-cli completion [bash|zsh|fish|powershell] --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli](dpservice-cli.md) - -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_delete.md b/docs/commands/dpservice-cli_delete.md index 548001a..24581ed 100644 --- a/docs/commands/dpservice-cli_delete.md +++ b/docs/commands/dpservice-cli_delete.md @@ -24,6 +24,7 @@ dpservice-cli delete [flags] --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO @@ -40,4 +41,4 @@ dpservice-cli delete [flags] * [dpservice-cli delete route](dpservice-cli_delete_route.md) - Delete a route * [dpservice-cli delete virtualip](dpservice-cli_delete_virtualip.md) - Delete virtual IP from interface -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_delete_firewallrule.md b/docs/commands/dpservice-cli_delete_firewallrule.md index 9c67bad..0c40bd0 100644 --- a/docs/commands/dpservice-cli_delete_firewallrule.md +++ b/docs/commands/dpservice-cli_delete_firewallrule.md @@ -27,10 +27,11 @@ dpservice-cli delete firewallrule --rule-id=1 --interface-id=vm1 --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_delete_interface.md b/docs/commands/dpservice-cli_delete_interface.md index 471a49c..d825322 100644 --- a/docs/commands/dpservice-cli_delete_interface.md +++ b/docs/commands/dpservice-cli_delete_interface.md @@ -26,10 +26,11 @@ dpservice-cli delete interface --id=vm1 --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_delete_lbprefix.md b/docs/commands/dpservice-cli_delete_lbprefix.md index 9259bb2..621f8fc 100644 --- a/docs/commands/dpservice-cli_delete_lbprefix.md +++ b/docs/commands/dpservice-cli_delete_lbprefix.md @@ -27,10 +27,11 @@ dpservice-cli delete lbprefix --prefix=ff80::1/64 --interface-id=vm1 --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_delete_lbtarget.md b/docs/commands/dpservice-cli_delete_lbtarget.md index 2ace014..68d2249 100644 --- a/docs/commands/dpservice-cli_delete_lbtarget.md +++ b/docs/commands/dpservice-cli_delete_lbtarget.md @@ -27,10 +27,11 @@ dpservice-cli delete lbtarget --target-ip=ff80::1 --lb-id=1 --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_delete_loadbalancer.md b/docs/commands/dpservice-cli_delete_loadbalancer.md index 88a9d87..07bf674 100644 --- a/docs/commands/dpservice-cli_delete_loadbalancer.md +++ b/docs/commands/dpservice-cli_delete_loadbalancer.md @@ -26,10 +26,11 @@ dpservice-cli delete loadbalancer --id=1 --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_delete_nat.md b/docs/commands/dpservice-cli_delete_nat.md index a681f6d..d4591e5 100644 --- a/docs/commands/dpservice-cli_delete_nat.md +++ b/docs/commands/dpservice-cli_delete_nat.md @@ -26,10 +26,11 @@ dpservice-cli delete nat --interface-id=vm1 --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_delete_neighbornat.md b/docs/commands/dpservice-cli_delete_neighbornat.md index 3076c37..f92192c 100644 --- a/docs/commands/dpservice-cli_delete_neighbornat.md +++ b/docs/commands/dpservice-cli_delete_neighbornat.md @@ -18,7 +18,7 @@ dpservice-cli delete neighbornat --nat-ip=10.20.30.40 --vni=100 --minport=30000 -h, --help help for neighbornat --maxport uint32 MaxPort of neighbor NAT. --minport uint32 MinPort of neighbor NAT. - --nat-ip ip Neighbor NAT IP. (default invalid IP) + --nat-ip ip Neighbor NAT IP. (default invalid IP) --vni uint32 VNI of neighbor NAT. ``` @@ -29,10 +29,11 @@ dpservice-cli delete neighbornat --nat-ip=10.20.30.40 --vni=100 --minport=30000 --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_delete_prefix.md b/docs/commands/dpservice-cli_delete_prefix.md index 4807047..44c1116 100644 --- a/docs/commands/dpservice-cli_delete_prefix.md +++ b/docs/commands/dpservice-cli_delete_prefix.md @@ -27,10 +27,11 @@ dpservice-cli delete prefix --prefix=10.20.30.0/24 --interface-id=vm1 --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_delete_route.md b/docs/commands/dpservice-cli_delete_route.md index c3f6b8b..3042db2 100644 --- a/docs/commands/dpservice-cli_delete_route.md +++ b/docs/commands/dpservice-cli_delete_route.md @@ -3,13 +3,13 @@ Delete a route ``` -dpservice-cli delete route <--prefix> <--next-hop-vni> <--next-hop-ip> <--vni> [flags] +dpservice-cli delete route <--prefix> <--vni> [flags] ``` ### Examples ``` -dpservice-cli delete route --prefix=10.100.2.0/24 --next-hop-vni=0 --next-hop-ip=fc00:2::64:0:1 --vni=100 +dpservice-cli delete route --prefix=10.100.2.0/24 --vni=100 ``` ### Options @@ -27,10 +27,11 @@ dpservice-cli delete route --prefix=10.100.2.0/24 --next-hop-vni=0 --next-hop-ip --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_delete_virtualip.md b/docs/commands/dpservice-cli_delete_virtualip.md index 341bb75..f1f4bc6 100644 --- a/docs/commands/dpservice-cli_delete_virtualip.md +++ b/docs/commands/dpservice-cli_delete_virtualip.md @@ -26,10 +26,11 @@ dpservice-cli delete virtualip --interface-id=vm1 --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] (default "name") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli delete](dpservice-cli_delete.md) - Deletes one of [interface prefix route virtualip loadbalancer lbprefix lbtarget nat neighbornat firewallrule] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_get.md b/docs/commands/dpservice-cli_get.md index 1ce0760..f4135bf 100644 --- a/docs/commands/dpservice-cli_get.md +++ b/docs/commands/dpservice-cli_get.md @@ -23,6 +23,7 @@ dpservice-cli get [flags] --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO @@ -36,4 +37,4 @@ dpservice-cli get [flags] * [dpservice-cli get virtualip](dpservice-cli_get_virtualip.md) - Get Virtual IP on interface * [dpservice-cli get vni](dpservice-cli_get_vni.md) - Get vni usage information -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_get_firewallrule.md b/docs/commands/dpservice-cli_get_firewallrule.md index bae6d35..178ebf4 100644 --- a/docs/commands/dpservice-cli_get_firewallrule.md +++ b/docs/commands/dpservice-cli_get_firewallrule.md @@ -25,12 +25,13 @@ dpservice-cli get fwrule --rule-id=1 --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_get_interface.md b/docs/commands/dpservice-cli_get_interface.md index d913cc2..b107e1b 100644 --- a/docs/commands/dpservice-cli_get_interface.md +++ b/docs/commands/dpservice-cli_get_interface.md @@ -24,12 +24,13 @@ dpservice-cli get interface --id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_get_loadbalancer.md b/docs/commands/dpservice-cli_get_loadbalancer.md index da060c9..6b1e5b2 100644 --- a/docs/commands/dpservice-cli_get_loadbalancer.md +++ b/docs/commands/dpservice-cli_get_loadbalancer.md @@ -24,12 +24,13 @@ dpservice-cli get loadbalancer --id=4 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_get_nat.md b/docs/commands/dpservice-cli_get_nat.md index 41006a8..42fdbb4 100644 --- a/docs/commands/dpservice-cli_get_nat.md +++ b/docs/commands/dpservice-cli_get_nat.md @@ -24,12 +24,13 @@ dpservice-cli get nat --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_get_natinfo.md b/docs/commands/dpservice-cli_get_natinfo.md index 42640ce..1854e7e 100644 --- a/docs/commands/dpservice-cli_get_natinfo.md +++ b/docs/commands/dpservice-cli_get_natinfo.md @@ -9,14 +9,14 @@ dpservice-cli get natinfo <--nat-ip> <--nat-type> [flags] ### Examples ``` -dpservice-cli get natinfo --nat-ip=10.20.30.40 --nat-type=1 +dpservice-cli get natinfo --nat-ip=10.20.30.40 --info-type=1 ``` ### Options ``` -h, --help help for natinfo - --info-type string NAT Info type: Local = 1/Neigh(bor) = 2 + --info-type string NAT Info type: Any = 0/Local = 1/Neigh(bor) = 2 --nat-ip ip NAT IP to get info for (default invalid IP) ``` @@ -25,12 +25,13 @@ dpservice-cli get natinfo --nat-ip=10.20.30.40 --nat-type=1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_get_virtualip.md b/docs/commands/dpservice-cli_get_virtualip.md index b37a2c2..e42cae7 100644 --- a/docs/commands/dpservice-cli_get_virtualip.md +++ b/docs/commands/dpservice-cli_get_virtualip.md @@ -24,12 +24,13 @@ dpservice-cli get virtualip --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_get_vni.md b/docs/commands/dpservice-cli_get_vni.md index 68b9c51..1de3fdd 100644 --- a/docs/commands/dpservice-cli_get_vni.md +++ b/docs/commands/dpservice-cli_get_vni.md @@ -17,7 +17,7 @@ dpservice-cli get vni --vni=vm1 --vni-type=0 ``` -h, --help help for vni --vni uint32 VNI to check. - --vni-type uint8 VNI Type. + --vni-type uint8 VNI Type: VniIpv4 = 0/VniIpv6 = 1. ``` ### Options inherited from parent commands @@ -25,12 +25,13 @@ dpservice-cli get vni --vni=vm1 --vni-type=0 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli get](dpservice-cli_get.md) - Gets one of [interface virtualip loadbalancer nat natinfo firewallrule vni] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_init.md b/docs/commands/dpservice-cli_init.md index 3c55296..d755439 100644 --- a/docs/commands/dpservice-cli_init.md +++ b/docs/commands/dpservice-cli_init.md @@ -25,10 +25,11 @@ dpservice-cli init --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli](dpservice-cli.md) - -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_initialized.md b/docs/commands/dpservice-cli_initialized.md index c1d0acf..c105217 100644 --- a/docs/commands/dpservice-cli_initialized.md +++ b/docs/commands/dpservice-cli_initialized.md @@ -25,10 +25,11 @@ dpservice-cli initialized --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO * [dpservice-cli](dpservice-cli.md) - -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_list.md b/docs/commands/dpservice-cli_list.md index f9375d8..0955660 100644 --- a/docs/commands/dpservice-cli_list.md +++ b/docs/commands/dpservice-cli_list.md @@ -1,10 +1,10 @@ ## dpservice-cli list -Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] +Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtargets nats] ### Synopsis -Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] +Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtargets nats] ``` dpservice-cli list [flags] @@ -23,6 +23,7 @@ dpservice-cli list [flags] --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) -o, --output string Output format. [json|yaml|table|name] --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO @@ -31,9 +32,9 @@ dpservice-cli list [flags] * [dpservice-cli list firewallrules](dpservice-cli_list_firewallrules.md) - List firewall rules on interface * [dpservice-cli list interfaces](dpservice-cli_list_interfaces.md) - List all interfaces * [dpservice-cli list lbprefixes](dpservice-cli_list_lbprefixes.md) - List loadbalancer prefixes on interface. -* [dpservice-cli list lbtarget](dpservice-cli_list_lbtarget.md) - List LoadBalancer Targets +* [dpservice-cli list lbtargets](dpservice-cli_list_lbtargets.md) - List LoadBalancer Targets * [dpservice-cli list nats](dpservice-cli_list_nats.md) - List all nats * [dpservice-cli list prefixes](dpservice-cli_list_prefixes.md) - List prefix(es) on interface. * [dpservice-cli list routes](dpservice-cli_list_routes.md) - List routes of specified VNI -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_list_firewallrules.md b/docs/commands/dpservice-cli_list_firewallrules.md index 6c65ed1..22bebe7 100644 --- a/docs/commands/dpservice-cli_list_firewallrules.md +++ b/docs/commands/dpservice-cli_list_firewallrules.md @@ -24,12 +24,13 @@ dpservice-cli list firewallrules --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO -* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtargets nats] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_list_interfaces.md b/docs/commands/dpservice-cli_list_interfaces.md index c2b735f..188a0b1 100644 --- a/docs/commands/dpservice-cli_list_interfaces.md +++ b/docs/commands/dpservice-cli_list_interfaces.md @@ -23,12 +23,13 @@ dpservice-cli list interfaces ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO -* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtargets nats] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_list_lbprefixes.md b/docs/commands/dpservice-cli_list_lbprefixes.md index b4f32de..92df72a 100644 --- a/docs/commands/dpservice-cli_list_lbprefixes.md +++ b/docs/commands/dpservice-cli_list_lbprefixes.md @@ -24,12 +24,13 @@ dpservice-cli list lbprefixes --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO -* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtargets nats] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_list_lbtarget.md b/docs/commands/dpservice-cli_list_lbtargets.md similarity index 60% rename from docs/commands/dpservice-cli_list_lbtarget.md rename to docs/commands/dpservice-cli_list_lbtargets.md index 69a3fbb..e2947fc 100644 --- a/docs/commands/dpservice-cli_list_lbtarget.md +++ b/docs/commands/dpservice-cli_list_lbtargets.md @@ -1,21 +1,21 @@ -## dpservice-cli list lbtarget +## dpservice-cli list lbtargets List LoadBalancer Targets ``` -dpservice-cli list lbtarget <--lb-id> [flags] +dpservice-cli list lbtargets <--lb-id> [flags] ``` ### Examples ``` -dpservice-cli list lbtarget --lb-id=1 +dpservice-cli list lbtargets --lb-id=1 ``` ### Options ``` - -h, --help help for lbtarget + -h, --help help for lbtargets --lb-id string ID of the loadbalancer to get the targets for. ``` @@ -24,12 +24,13 @@ dpservice-cli list lbtarget --lb-id=1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO -* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtargets nats] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_list_nats.md b/docs/commands/dpservice-cli_list_nats.md index 2e4be3a..f9ab248 100644 --- a/docs/commands/dpservice-cli_list_nats.md +++ b/docs/commands/dpservice-cli_list_nats.md @@ -23,12 +23,13 @@ dpservice-cli list nats ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO -* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtargets nats] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_list_prefixes.md b/docs/commands/dpservice-cli_list_prefixes.md index 3849c68..7a9bd8a 100644 --- a/docs/commands/dpservice-cli_list_prefixes.md +++ b/docs/commands/dpservice-cli_list_prefixes.md @@ -24,12 +24,13 @@ dpservice-cli list prefixes --interface-id=vm1 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO -* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtargets nats] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_list_routes.md b/docs/commands/dpservice-cli_list_routes.md index c009ae9..8362e41 100644 --- a/docs/commands/dpservice-cli_list_routes.md +++ b/docs/commands/dpservice-cli_list_routes.md @@ -24,12 +24,13 @@ dpservice-cli list routes --vni=100 ``` --address string net-dpservice address. (default "localhost:1337") --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) - -o, --output string Output format. [json|yaml|table|name] + -o, --output string Output format. [json|yaml|table|name] (default "table") --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. ``` ### SEE ALSO -* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtarget nats] +* [dpservice-cli list](dpservice-cli_list.md) - Lists one of [firewallrules interfaces prefixes lbprefixes routes lbtargets nats] -###### Auto generated by spf13/cobra on 30-May-2023 +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_reset.md b/docs/commands/dpservice-cli_reset.md new file mode 100644 index 0000000..a27832f --- /dev/null +++ b/docs/commands/dpservice-cli_reset.md @@ -0,0 +1,34 @@ +## dpservice-cli reset + +Resets one of [vni] + +### Synopsis + +Resets one of [vni] + +``` +dpservice-cli reset [flags] +``` + +### Options + +``` + -h, --help help for reset +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] + --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. +``` + +### SEE ALSO + +* [dpservice-cli](dpservice-cli.md) - +* [dpservice-cli reset vni](dpservice-cli_reset_vni.md) - Reset vni usage information + +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/docs/commands/dpservice-cli_reset_vni.md b/docs/commands/dpservice-cli_reset_vni.md new file mode 100644 index 0000000..119064f --- /dev/null +++ b/docs/commands/dpservice-cli_reset_vni.md @@ -0,0 +1,37 @@ +## dpservice-cli reset vni + +Reset vni usage information + +``` +dpservice-cli reset vni <--vni> <--vni-type> [flags] +``` + +### Examples + +``` +dpservice-cli reset vni --vni=vm1 --vni-type=0 +``` + +### Options + +``` + -h, --help help for vni + --vni uint32 VNI to check. + --vni-type string VNI Type: ipv4 = 0/ipv6 = 1/both = 2. (default "both") +``` + +### Options inherited from parent commands + +``` + --address string net-dpservice address. (default "localhost:1337") + --connect-timeout duration Timeout to connect to the net-dpservice. (default 4s) + -o, --output string Output format. [json|yaml|table|name] (default "name") + --pretty Whether to render pretty output. + -w, --wide Whether to render more info in table output. +``` + +### SEE ALSO + +* [dpservice-cli reset](dpservice-cli_reset.md) - Resets one of [vni] + +###### Auto generated by spf13/cobra on 14-Jun-2023 diff --git a/go.mod b/go.mod index 7dd27ff..8a31ee9 100644 --- a/go.mod +++ b/go.mod @@ -31,4 +31,4 @@ require ( google.golang.org/protobuf v1.28.1 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect -) \ No newline at end of file +) diff --git a/go.sum b/go.sum index 44494c0..1c008e5 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/onmetal/net-dpservice-go v0.1.13-0.20230613133615-01fd5bbbc3d2 h1:obCbgkXzIzxGlv4XsbRWs6rEY4g5uMxHd2+TxfhXwNo= -github.com/onmetal/net-dpservice-go v0.1.13-0.20230613133615-01fd5bbbc3d2/go.mod h1:dbItLY1P+nYl7/c04JdJTEaMCG6GLXgAZsMP/OuZBmg= github.com/onmetal/net-dpservice-go v0.1.13-0.20230613133937-066e40ed0eb3 h1:AZihpu9G709TNhLNc/7TnAVWaLWkP+1b4tKBWhS44z4= github.com/onmetal/net-dpservice-go v0.1.13-0.20230613133937-066e40ed0eb3/go.mod h1:dbItLY1P+nYl7/c04JdJTEaMCG6GLXgAZsMP/OuZBmg= github.com/onsi/ginkgo/v2 v2.2.0 h1:3ZNA3L1c5FYDFTTxbFeVGGD8jYvjYauHD30YgLxVsNI= From c5e90ea7a0929d6050fdeb102f8427bbba274381 Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 29 Jun 2023 09:32:22 +0200 Subject: [PATCH 63/65] bump net-dpservice-go to v0.1.13 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 8a31ee9..31a2d4e 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/ghodss/yaml v1.0.0 github.com/google/addlicense v1.1.1 github.com/jedib0t/go-pretty/v6 v6.3.9 - github.com/onmetal/net-dpservice-go v0.1.13-0.20230613133937-066e40ed0eb3 + github.com/onmetal/net-dpservice-go v0.1.13 github.com/onsi/ginkgo/v2 v2.2.0 github.com/onsi/gomega v1.20.2 github.com/spf13/cobra v1.4.0 diff --git a/go.sum b/go.sum index 1c008e5..c9b52df 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/onmetal/net-dpservice-go v0.1.13-0.20230613133937-066e40ed0eb3 h1:AZihpu9G709TNhLNc/7TnAVWaLWkP+1b4tKBWhS44z4= -github.com/onmetal/net-dpservice-go v0.1.13-0.20230613133937-066e40ed0eb3/go.mod h1:dbItLY1P+nYl7/c04JdJTEaMCG6GLXgAZsMP/OuZBmg= +github.com/onmetal/net-dpservice-go v0.1.13 h1:gMmFB9ky6jY18G7+jIB0l4WBdQT+xulG6Z4orp6+YHk= +github.com/onmetal/net-dpservice-go v0.1.13/go.mod h1:C6TEAtr2T3RJKTf0Js+2T9liU0lXZUdIFWUJvwHmEIE= github.com/onsi/ginkgo/v2 v2.2.0 h1:3ZNA3L1c5FYDFTTxbFeVGGD8jYvjYauHD30YgLxVsNI= github.com/onsi/ginkgo/v2 v2.2.0/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk= github.com/onsi/gomega v1.20.2 h1:8uQq0zMgLEfa0vRrrBgaJF2gyW9Da9BmfGV+OyUzfkY= From 8ecb4ea43cb317560682e1fdbfb4d1bc83178e0c Mon Sep 17 00:00:00 2001 From: Viliam Lorinc Date: Thu, 29 Jun 2023 11:27:45 +0200 Subject: [PATCH 64/65] fix missing error handling --- .vscode/launch.json | 3 ++- cmd/add_firewall_rule.go | 26 +++++++++++++++++++------- cmd/completion.go | 19 ++++++++++++++----- dpdk/runtime/decoder.go | 2 +- go.mod | 11 ++++++----- go.sum | 25 +++++++++++++------------ 6 files changed, 55 insertions(+), 31 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index e4ab736..69a19b9 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -24,9 +24,10 @@ // "--priority=100", "--rule-id=9", "--src=1.1.1.1/32", "--protocol=tcp"] //"args": ["add", "nnat", "10.20.30.40", "--vni", "100", "--minport", "30000", "--maxport", "30100", "--underlayroute", "ff80::1"] //"args": ["add", "interface", "--id=vm4", "--ip=10.200.1.4", "--ip=2000:200:1::4", "--vni=200", "--device=net_tap5"] + "args": ["add", "-f", "/tmp/int.json"] //"args": ["delete", "-f", "/tmp/vip.yaml"] //"args": ["get", "firewallrule", "--interface-id=vm1", "--rule-id=11"] - "args": ["list", "interfaces", "--wide"] + //"args": ["list", "interfaces", "--wide"] //"args": ["get", "vni", "--vni=100", "--vni-type=0"] //"args": ["list", "prefixes","--interface-id=vm1"] //"args": ["list", "lbtargets","--lb-id=4"] diff --git a/cmd/add_firewall_rule.go b/cmd/add_firewall_rule.go index 0303b0f..85fa45f 100644 --- a/cmd/add_firewall_rule.go +++ b/cmd/add_firewall_rule.go @@ -41,22 +41,34 @@ func AddFirewallRule(dpdkClientFactory DPDKClientFactory, rendererFactory Render Aliases: FirewallRuleAliases, Args: cobra.ExactArgs(0), // if protocol flag is set, require also additional flags - PreRun: func(cmd *cobra.Command, args []string) { + PreRunE: func(cmd *cobra.Command, args []string) error { filter, _ := cmd.Flags().GetString("protocol") switch filter { case "icmp", "1": - cmd.MarkFlagRequired("icmp-type") - cmd.MarkFlagRequired("icmp-code") + for _, name := range []string{"icmp-type", "icmp-code"} { + if err := cmd.MarkFlagRequired(name); err != nil { + return err + } + } case "tcp", "6", "udp", "17": - cmd.MarkFlagRequired("src-port-min") + if err := cmd.MarkFlagRequired("src-port-min"); err != nil { + return err + } if src, _ := cmd.Flags().GetInt32("src-port-min"); src != -1 { - cmd.MarkFlagRequired("src-port-max") + if err := cmd.MarkFlagRequired("src-port-max"); err != nil { + return err + } + } + if err := cmd.MarkFlagRequired("dst-port-min"); err != nil { + return err } - cmd.MarkFlagRequired("dst-port-min") if dst, _ := cmd.Flags().GetInt32("dst-port-min"); dst != -1 { - cmd.MarkFlagRequired("dst-port-max") + if err := cmd.MarkFlagRequired("dst-port-max"); err != nil { + return err + } } } + return nil }, RunE: func(cmd *cobra.Command, args []string) error { diff --git a/cmd/completion.go b/cmd/completion.go index a7e40f9..f163480 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -67,17 +67,26 @@ PowerShell: DisableFlagsInUseLine: true, ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { switch args[0] { case "bash": - cmd.Root().GenBashCompletion(os.Stdout) + if err := cmd.Root().GenBashCompletion(os.Stdout); err != nil { + return err + } case "zsh": - cmd.Root().GenZshCompletion(os.Stdout) + if err := cmd.Root().GenZshCompletion(os.Stdout); err != nil { + return err + } case "fish": - cmd.Root().GenFishCompletion(os.Stdout, true) + if err := cmd.Root().GenFishCompletion(os.Stdout, true); err != nil { + return err + } case "powershell": - cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) + if err := cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout); err != nil { + return err + } } + return nil }, } diff --git a/dpdk/runtime/decoder.go b/dpdk/runtime/decoder.go index db35e69..f56e120 100644 --- a/dpdk/runtime/decoder.go +++ b/dpdk/runtime/decoder.go @@ -93,7 +93,7 @@ func (d *KindDecoder) Next() (any, error) { if err != nil { return nil, fmt.Errorf("error marshaling %s: %w", obj.Kind, err) } - json.Unmarshal(jsonObj, res) + err = json.Unmarshal(jsonObj, res) if err != nil { return nil, fmt.Errorf("error unmarshaling %s: %w", obj.Kind, err) } diff --git a/go.mod b/go.mod index 31a2d4e..5522349 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/onmetal/net-dpservice-go v0.1.13 github.com/onsi/ginkgo/v2 v2.2.0 github.com/onsi/gomega v1.20.2 - github.com/spf13/cobra v1.4.0 + github.com/spf13/cobra v1.7.0 github.com/spf13/pflag v1.0.5 google.golang.org/grpc v1.53.0 gopkg.in/yaml.v2 v2.4.0 @@ -19,14 +19,15 @@ require ( github.com/bmatcuk/doublestar/v4 v4.0.2 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-cmp v0.5.9 // indirect - github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/kr/pretty v0.2.0 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect github.com/rivo/uniseg v0.2.0 // indirect + github.com/stretchr/testify v1.8.4 // indirect golang.org/x/net v0.5.0 // indirect - golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect - golang.org/x/sys v0.4.0 // indirect - golang.org/x/text v0.6.0 // indirect + golang.org/x/sync v0.2.0 // indirect + golang.org/x/sys v0.8.0 // indirect + golang.org/x/text v0.9.0 // indirect google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect google.golang.org/protobuf v1.28.1 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect diff --git a/go.sum b/go.sum index c9b52df..2f8a51f 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ github.com/bmatcuk/doublestar/v4 v4.0.2 h1:X0krlUVAVmtr2cRoTqR8aDMrDqnB36ht8wpWTiQ3jsA= github.com/bmatcuk/doublestar/v4 v4.0.2/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= -github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -14,8 +14,8 @@ github.com/google/addlicense v1.1.1/go.mod h1:Sm/DHu7Jk+T5miFHHehdIjbi4M5+dJDRS3 github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= -github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jedib0t/go-pretty/v6 v6.3.9 h1:GAK/1WJY9WVVrKd601HGB89ihLBDfJnUIJye31PY+uk= github.com/jedib0t/go-pretty/v6 v6.3.9/go.mod h1:MgmISkTWDSFu0xOqiZ0mKNntMQ2mDgOcwOkwBEkMDJI= github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= @@ -37,25 +37,26 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q= -github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.4 h1:wZRexSlwd7ZXfKINDLsO4r7WBt3gTKONc6K/VesHvHM= github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw= golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= +golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= -golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k= -golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f h1:BWUVssLB0HVOSY78gIdvk1dTVYtT1y8SBWtPYuTJ/6w= google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM= From cdcc5696d232c4add26698bd22eae8af89b9254c Mon Sep 17 00:00:00 2001 From: Viliam Lorinc <129966234+vlorinc@users.noreply.github.com> Date: Mon, 3 Jul 2023 08:32:47 +0200 Subject: [PATCH 65/65] Delete not needed file File was generated automatically and is not needed --- sudo | 1 - 1 file changed, 1 deletion(-) delete mode 100644 sudo diff --git a/sudo b/sudo deleted file mode 100644 index 2c5ecea..0000000 --- a/sudo +++ /dev/null @@ -1 +0,0 @@ -Error running command: accepts 1 arg(s), received 2