From 838fe87d07835f0476cf3af2cf47b6e1a2f24ee2 Mon Sep 17 00:00:00 2001 From: Alessandro Olivero Date: Wed, 4 Oct 2023 10:54:19 +0200 Subject: [PATCH] fixup! fixup! liqoctl add network configuration commands --- cmd/liqoctl/cmd/generate.go | 15 +++++++ cmd/liqoctl/cmd/root.go | 2 - pkg/liqoctl/network/cluster.go | 4 +- pkg/liqoctl/rest/configuration/create.go | 24 +++++++--- .../get.go => configuration/generate.go} | 44 ++++++------------- pkg/liqoctl/rest/configuration/types.go | 6 ++- pkg/liqoctl/rest/localconfiguration/delete.go | 28 ------------ pkg/liqoctl/rest/localconfiguration/types.go | 38 ---------------- pkg/liqoctl/rest/localconfiguration/update.go | 28 ------------ pkg/liqoctl/rest/types.go | 17 +++++-- .../create.go => virtualnode/generate.go} | 6 +-- 11 files changed, 67 insertions(+), 145 deletions(-) rename pkg/liqoctl/rest/{localconfiguration/get.go => configuration/generate.go} (75%) delete mode 100644 pkg/liqoctl/rest/localconfiguration/delete.go delete mode 100644 pkg/liqoctl/rest/localconfiguration/types.go delete mode 100644 pkg/liqoctl/rest/localconfiguration/update.go rename pkg/liqoctl/rest/{localconfiguration/create.go => virtualnode/generate.go} (82%) diff --git a/cmd/liqoctl/cmd/generate.go b/cmd/liqoctl/cmd/generate.go index 76b7b4a271..aed6827da5 100644 --- a/cmd/liqoctl/cmd/generate.go +++ b/cmd/liqoctl/cmd/generate.go @@ -23,6 +23,7 @@ import ( "github.com/liqotech/liqo/pkg/liqoctl/factory" "github.com/liqotech/liqo/pkg/liqoctl/generate" "github.com/liqotech/liqo/pkg/liqoctl/output" + "github.com/liqotech/liqo/pkg/liqoctl/rest" ) const liqoctlGeneratePeerLongHelp = `Generate the command to execute on another cluster to peer with the local cluster. @@ -49,6 +50,20 @@ func newGenerateCommand(ctx context.Context, f *factory.Factory) *cobra.Command } cmd.AddCommand(newGeneratePeerCommand(ctx, f)) + + options := &rest.GenerateOptions{ + Factory: f, + } + + for _, r := range liqoResources { + api := r() + + apiOptions := api.APIOptions() + if apiOptions.EnableGenerate { + cmd.AddCommand(api.Generate(ctx, options)) + } + } + return cmd } diff --git a/cmd/liqoctl/cmd/root.go b/cmd/liqoctl/cmd/root.go index b4d86324f6..17dd1e5486 100644 --- a/cmd/liqoctl/cmd/root.go +++ b/cmd/liqoctl/cmd/root.go @@ -36,7 +36,6 @@ import ( "github.com/liqotech/liqo/pkg/liqoctl/get" "github.com/liqotech/liqo/pkg/liqoctl/rest" "github.com/liqotech/liqo/pkg/liqoctl/rest/configuration" - "github.com/liqotech/liqo/pkg/liqoctl/rest/localconfiguration" "github.com/liqotech/liqo/pkg/liqoctl/rest/virtualnode" ) @@ -44,7 +43,6 @@ var liqoctl string var liqoResources = []rest.APIProvider{ virtualnode.VirtualNode, - localconfiguration.LocalConfiguration, configuration.Configuration, } diff --git a/pkg/liqoctl/network/cluster.go b/pkg/liqoctl/network/cluster.go index 7cb3e76af5..b77aca6331 100644 --- a/pkg/liqoctl/network/cluster.go +++ b/pkg/liqoctl/network/cluster.go @@ -26,7 +26,7 @@ import ( "github.com/liqotech/liqo/pkg/discovery" "github.com/liqotech/liqo/pkg/liqoctl/factory" "github.com/liqotech/liqo/pkg/liqoctl/output" - "github.com/liqotech/liqo/pkg/liqoctl/rest/localconfiguration" + "github.com/liqotech/liqo/pkg/liqoctl/rest/configuration" "github.com/liqotech/liqo/pkg/liqoctl/wait" liqogetters "github.com/liqotech/liqo/pkg/utils/getters" liqolabels "github.com/liqotech/liqo/pkg/utils/labels" @@ -81,7 +81,7 @@ func (c *Cluster) Init(ctx context.Context) error { // Get network configuration. s = c.local.Printer.StartSpinner("Retrieving network configuration") - conf, err := localconfiguration.ForgeLocalConfiguration(ctx, c.local.CRClient, c.local.LiqoNamespace) + conf, err := configuration.ForgeLocalConfiguration(ctx, c.local.CRClient, c.local.LiqoNamespace) if err != nil { s.Fail(fmt.Sprintf("An error occurred while retrieving network configuration: %v", output.PrettyErr(err))) return err diff --git a/pkg/liqoctl/rest/configuration/create.go b/pkg/liqoctl/rest/configuration/create.go index 90af75bf81..159a6f15c4 100644 --- a/pkg/liqoctl/rest/configuration/create.go +++ b/pkg/liqoctl/rest/configuration/create.go @@ -90,7 +90,7 @@ func (o *Options) Create(ctx context.Context, options *rest.CreateOptions) *cobr func (o *Options) handleCreate(ctx context.Context) error { opts := o.createOptions if opts.OutputFormat != "" { - opts.Printer.CheckErr(o.output()) + opts.Printer.CheckErr(o.output(nil)) return nil } @@ -162,19 +162,29 @@ func mutateConfiguration(conf *networkingv1alpha1.Configuration, clusterID, podC } // output implements the logic to output the generated VirtualNode resource. -func (o *Options) output() error { - opts := o.createOptions +func (o *Options) output(conf *networkingv1alpha1.Configuration) error { + var outputFormat string + switch { + case o.createOptions != nil: + outputFormat = o.createOptions.OutputFormat + case o.generateOptions != nil: + outputFormat = o.generateOptions.OutputFormat + default: + return fmt.Errorf("unable to determine output format") + } var printer printers.ResourcePrinter - switch opts.OutputFormat { + switch outputFormat { case "yaml": printer = &printers.YAMLPrinter{} case "json": printer = &printers.JSONPrinter{} default: - return fmt.Errorf("unsupported output format %q", opts.OutputFormat) + return fmt.Errorf("unsupported output format %q", outputFormat) } - conf := forgeConfiguration(o.createOptions.Name, o.createOptions.Namespace, - o.ClusterID, o.PodCIDR.String(), o.ExternalCIDR.String()) + if conf == nil { + conf = forgeConfiguration(o.createOptions.Name, o.createOptions.Namespace, + o.ClusterID, o.PodCIDR.String(), o.ExternalCIDR.String()) + } return printer.PrintObj(conf, os.Stdout) } diff --git a/pkg/liqoctl/rest/localconfiguration/get.go b/pkg/liqoctl/rest/configuration/generate.go similarity index 75% rename from pkg/liqoctl/rest/localconfiguration/get.go rename to pkg/liqoctl/rest/configuration/generate.go index 251cfac221..874c0f159f 100644 --- a/pkg/liqoctl/rest/localconfiguration/get.go +++ b/pkg/liqoctl/rest/configuration/generate.go @@ -12,18 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. -package localconfiguration +package configuration import ( "context" "fmt" - "os" "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/util/runtime" - "k8s.io/cli-runtime/pkg/printers" "sigs.k8s.io/controller-runtime/pkg/client" networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1" @@ -36,28 +34,28 @@ import ( liqogetters "github.com/liqotech/liqo/pkg/utils/getters" ) -const liqoctlGetLocalConfigongHelp = `Retrieve the local network configuration.` +const liqoctlGenerateConfigHelp = `Generate the local network configuration to be applied to other clusters.` -// Get implements the get command. -func (o *Options) Get(ctx context.Context, options *rest.GetOptions) *cobra.Command { +// Generate generates a Configuration. +func (o *Options) Generate(ctx context.Context, options *rest.GenerateOptions) *cobra.Command { outputFormat := args.NewEnum([]string{"json", "yaml"}, "yaml") - o.getOptions = options + o.generateOptions = options cmd := &cobra.Command{ - Use: "localconfiguration", - Aliases: []string{"localconfig", "lc", "localconfigurations"}, - Short: "Get the local network configuration", - Long: liqoctlGetLocalConfigongHelp, + Use: "configuration", + Aliases: []string{"config", "configurations"}, + Short: "Generate a Configuration", + Long: liqoctlGenerateConfigHelp, Args: cobra.NoArgs, PreRun: func(cmd *cobra.Command, args []string) { options.OutputFormat = outputFormat.Value - o.getOptions = options + o.generateOptions = options }, Run: func(cmd *cobra.Command, args []string) { - output.ExitOnErr(o.handleGet(ctx)) + output.ExitOnErr(o.handleGenerate(ctx)) }, } @@ -69,8 +67,8 @@ func (o *Options) Get(ctx context.Context, options *rest.GetOptions) *cobra.Comm return cmd } -func (o *Options) handleGet(ctx context.Context) error { - opts := o.getOptions +func (o *Options) handleGenerate(ctx context.Context) error { + opts := o.generateOptions conf, err := ForgeLocalConfiguration(ctx, opts.CRClient, opts.LiqoNamespace) if err != nil { @@ -115,19 +113,3 @@ func ForgeLocalConfiguration(ctx context.Context, cl client.Client, liqoNamespac }, }, nil } - -// output implements the logic to output the local configuration. -func (o *Options) output(conf *networkingv1alpha1.Configuration) error { - opts := o.getOptions - var printer printers.ResourcePrinter - switch opts.OutputFormat { - case "yaml": - printer = &printers.YAMLPrinter{} - case "json": - printer = &printers.JSONPrinter{} - default: - return fmt.Errorf("unsupported output format %q", opts.OutputFormat) - } - - return printer.PrintObj(conf, os.Stdout) -} diff --git a/pkg/liqoctl/rest/configuration/types.go b/pkg/liqoctl/rest/configuration/types.go index 66d959cdea..a1b50eabc5 100644 --- a/pkg/liqoctl/rest/configuration/types.go +++ b/pkg/liqoctl/rest/configuration/types.go @@ -21,7 +21,8 @@ import ( // Options encapsulates the arguments of the configuration command. type Options struct { - createOptions *rest.CreateOptions + createOptions *rest.CreateOptions + generateOptions *rest.GenerateOptions ClusterID string PodCIDR args.CIDR @@ -39,6 +40,7 @@ func Configuration() rest.API { // APIOptions returns the APIOptions for the configuration API. func (o *Options) APIOptions() *rest.APIOptions { return &rest.APIOptions{ - EnableCreate: true, + EnableCreate: true, + EnableGenerate: true, } } diff --git a/pkg/liqoctl/rest/localconfiguration/delete.go b/pkg/liqoctl/rest/localconfiguration/delete.go deleted file mode 100644 index 5e8c40d17c..0000000000 --- a/pkg/liqoctl/rest/localconfiguration/delete.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2019-2023 The Liqo 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 localconfiguration - -import ( - "context" - - "github.com/spf13/cobra" - - "github.com/liqotech/liqo/pkg/liqoctl/rest" -) - -// Delete deletes a LocalConfiguration. -func (o *Options) Delete(_ context.Context, _ *rest.DeleteOptions) *cobra.Command { - panic("not implemented") -} diff --git a/pkg/liqoctl/rest/localconfiguration/types.go b/pkg/liqoctl/rest/localconfiguration/types.go deleted file mode 100644 index 004cacae72..0000000000 --- a/pkg/liqoctl/rest/localconfiguration/types.go +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2019-2023 The Liqo 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 localconfiguration - -import ( - "github.com/liqotech/liqo/pkg/liqoctl/rest" -) - -// Options encapsulates the arguments of the localconfiguration command. -type Options struct { - getOptions *rest.GetOptions -} - -var _ rest.API = &Options{} - -// LocalConfiguration returns the rest API for the localconfiguration command. -func LocalConfiguration() rest.API { - return &Options{} -} - -// APIOptions returns the APIOptions for the localconfiguration API. -func (o *Options) APIOptions() *rest.APIOptions { - return &rest.APIOptions{ - EnableGet: true, - } -} diff --git a/pkg/liqoctl/rest/localconfiguration/update.go b/pkg/liqoctl/rest/localconfiguration/update.go deleted file mode 100644 index bd47a050d6..0000000000 --- a/pkg/liqoctl/rest/localconfiguration/update.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2019-2023 The Liqo 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 localconfiguration - -import ( - "context" - - "github.com/spf13/cobra" - - "github.com/liqotech/liqo/pkg/liqoctl/rest" -) - -// Update implements the update command. -func (o *Options) Update(_ context.Context, _ *rest.UpdateOptions) *cobra.Command { - panic("not implemented") -} diff --git a/pkg/liqoctl/rest/types.go b/pkg/liqoctl/rest/types.go index b7d654a6f5..4a8c4ab2e2 100644 --- a/pkg/liqoctl/rest/types.go +++ b/pkg/liqoctl/rest/types.go @@ -24,10 +24,11 @@ import ( // APIOptions contains the options for the API. type APIOptions struct { - EnableCreate bool - EnableDelete bool - EnableGet bool - EnableUpdate bool + EnableCreate bool + EnableDelete bool + EnableGet bool + EnableUpdate bool + EnableGenerate bool } // CreateOptions contains the options for the create API. @@ -58,6 +59,13 @@ type UpdateOptions struct { *factory.Factory } +// GenerateOptions contains the options for the generate API. +type GenerateOptions struct { + *factory.Factory + + OutputFormat string +} + // API is the interface that must be implemented by the API. type API interface { APIOptions() *APIOptions @@ -65,6 +73,7 @@ type API interface { Delete(ctx context.Context, options *DeleteOptions) *cobra.Command Get(ctx context.Context, options *GetOptions) *cobra.Command Update(ctx context.Context, options *UpdateOptions) *cobra.Command + Generate(ctx context.Context, options *GenerateOptions) *cobra.Command } // APIProvider is the function that returns the API. diff --git a/pkg/liqoctl/rest/localconfiguration/create.go b/pkg/liqoctl/rest/virtualnode/generate.go similarity index 82% rename from pkg/liqoctl/rest/localconfiguration/create.go rename to pkg/liqoctl/rest/virtualnode/generate.go index 801bb40d2f..6fc114e796 100644 --- a/pkg/liqoctl/rest/localconfiguration/create.go +++ b/pkg/liqoctl/rest/virtualnode/generate.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package localconfiguration +package virtualnode import ( "context" @@ -22,7 +22,7 @@ import ( "github.com/liqotech/liqo/pkg/liqoctl/rest" ) -// Create creates a LocalConfiguration. -func (o *Options) Create(_ context.Context, _ *rest.CreateOptions) *cobra.Command { +// Generate generates a VirtualNode. +func (o *Options) Generate(_ context.Context, _ *rest.GenerateOptions) *cobra.Command { panic("not implemented") }