Skip to content

Commit

Permalink
fixup! fixup! liqoctl add network configuration commands
Browse files Browse the repository at this point in the history
  • Loading branch information
aleoli committed Oct 4, 2023
1 parent 0ff165f commit 838fe87
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 145 deletions.
15 changes: 15 additions & 0 deletions cmd/liqoctl/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

Expand Down
2 changes: 0 additions & 2 deletions cmd/liqoctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ 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"
)

var liqoctl string

var liqoResources = []rest.APIProvider{
virtualnode.VirtualNode,
localconfiguration.LocalConfiguration,
configuration.Configuration,
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/liqoctl/network/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
24 changes: 17 additions & 7 deletions pkg/liqoctl/rest/configuration/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
},
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
6 changes: 4 additions & 2 deletions pkg/liqoctl/rest/configuration/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}
}
28 changes: 0 additions & 28 deletions pkg/liqoctl/rest/localconfiguration/delete.go

This file was deleted.

38 changes: 0 additions & 38 deletions pkg/liqoctl/rest/localconfiguration/types.go

This file was deleted.

28 changes: 0 additions & 28 deletions pkg/liqoctl/rest/localconfiguration/update.go

This file was deleted.

17 changes: 13 additions & 4 deletions pkg/liqoctl/rest/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -58,13 +59,21 @@ 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
Create(ctx context.Context, options *CreateOptions) *cobra.Command
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package localconfiguration
package virtualnode

import (
"context"
Expand All @@ -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")
}

0 comments on commit 838fe87

Please sign in to comment.