Skip to content

Commit

Permalink
Liqoctl network delete resources
Browse files Browse the repository at this point in the history
  • Loading branch information
fra98 committed Nov 9, 2023
1 parent 46948de commit d4ddede
Show file tree
Hide file tree
Showing 19 changed files with 695 additions and 92 deletions.
67 changes: 64 additions & 3 deletions cmd/liqoctl/cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,23 @@ import (

const liqoctlNetworkLongHelp = `Manage liqo networking.`

const liqoctlNetworkInitLongHelp = `Initialize the liqo networking between two clusters.`
const liqoctlNetworkInitLongHelp = `Initialize the liqo networking between two clusters.
It generates all network configurations required to connect the two clusters.`

const liqoctlNetworkResetLongHelp = `Tear down all liqo networking between two clusters.
It disconnects the two clusters and remove network configurations generated with the *network init* command.`

const liqoctlNetworConnectLongHelp = `Connect two clusters using liqo networking.
This command creates the Gateways to connect the two clusters.
Run this command after inizialiting the network using the *network init* command.`

const liqoctlNetworkDisconnectLongHelp = `Disconnect two clusters.
It deletes the Gateways, but keeps the network configurations generated with the *network init* command.`

func newNetworkCommand(ctx context.Context, f *factory.Factory) *cobra.Command {
options := network.NewOptions(f)
options.RemoteFactory = factory.NewForRemote()
Expand Down Expand Up @@ -75,7 +86,9 @@ func newNetworkCommand(ctx context.Context, f *factory.Factory) *cobra.Command {
completion.Namespaces(ctx, options.RemoteFactory, completion.NoLimit)))

cmd.AddCommand(newNetworkInitCommand(ctx, options))
cmd.AddCommand(newNetworkResetCommand(ctx, options))
cmd.AddCommand(newNetworkConnectCommand(ctx, options))
cmd.AddCommand(newNetworkDisconnectCommand(ctx, options))

return cmd
}
Expand All @@ -88,7 +101,31 @@ func newNetworkInitCommand(ctx context.Context, options *network.Options) *cobra
Args: cobra.NoArgs,

Run: func(cmd *cobra.Command, args []string) {
output.ExitOnErr(options.RunInit(ctx))
err := options.RunInit(ctx)
if err != nil {
options.LocalFactory.Printer.CheckErr(
fmt.Errorf("`network init` failed: issue `network reset` to cleanup the environment"))
}
output.ExitOnErr(err)
},
}

return cmd
}

func newNetworkResetCommand(ctx context.Context, options *network.Options) *cobra.Command {
cmd := &cobra.Command{
Use: "reset",
Short: "Tear down liqo networking between two clusters (disconnect and remove network configurations)",
Long: WithTemplate(liqoctlNetworkResetLongHelp),
Args: cobra.NoArgs,

PreRun: func(cmd *cobra.Command, args []string) {
output.ExitOnErr(options.LocalFactory.Printer.AskConfirm("reset", options.LocalFactory.SkipConfirm))
},

Run: func(cmd *cobra.Command, args []string) {
output.ExitOnErr(options.RunReset(ctx))
},
}

Expand All @@ -103,7 +140,12 @@ func newNetworkConnectCommand(ctx context.Context, options *network.Options) *co
Args: cobra.NoArgs,

Run: func(cmd *cobra.Command, args []string) {
output.ExitOnErr(options.RunConnect(ctx))
err := options.RunConnect(ctx)
if err != nil {
options.LocalFactory.Printer.CheckErr(
fmt.Errorf("`network connect` failed: issue `network disconnect` to cleanup the environment"))
}
output.ExitOnErr(err)
},
}

Expand Down Expand Up @@ -137,3 +179,22 @@ func newNetworkConnectCommand(ctx context.Context, options *network.Options) *co

return cmd
}

func newNetworkDisconnectCommand(ctx context.Context, options *network.Options) *cobra.Command {
cmd := &cobra.Command{
Use: "disconnect",
Short: "Disconnect two clusters",
Long: WithTemplate(liqoctlNetworkDisconnectLongHelp),
Args: cobra.NoArgs,

PreRun: func(cmd *cobra.Command, args []string) {
output.ExitOnErr(options.LocalFactory.Printer.AskConfirm("disconnect", options.LocalFactory.SkipConfirm))
},

Run: func(cmd *cobra.Command, args []string) {
output.ExitOnErr(options.RunDisconnect(ctx))
},
}

return cmd
}
77 changes: 75 additions & 2 deletions pkg/liqoctl/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ func PVCs(ctx context.Context, f *factory.Factory, argsLimit int) FnType {
return common(ctx, f, argsLimit, retriever)
}

// Gateway returns a function to autocomplete Gateway (server or client) names.
func Gateway(ctx context.Context, f *factory.Factory, argsLimit int) FnType {
// Gateways returns a function to autocomplete Gateway (server or client) names.
func Gateways(ctx context.Context, f *factory.Factory, argsLimit int) FnType {
retriever := func(ctx context.Context, f *factory.Factory) ([]string, error) {
var names []string

Expand All @@ -303,6 +303,79 @@ func Gateway(ctx context.Context, f *factory.Factory, argsLimit int) FnType {
for i := range gwClients.Items {
names = append(names, gwClients.Items[i].Name)
}

return names, nil
}

return common(ctx, f, argsLimit, retriever)
}

// GatewayServers returns a function to autocomplete GatewayServers names.
func GatewayServers(ctx context.Context, f *factory.Factory, argsLimit int) FnType {
retriever := func(ctx context.Context, f *factory.Factory) ([]string, error) {
var gwServers networkingv1alpha1.GatewayServerList
if err := f.CRClient.List(ctx, &gwServers, client.InNamespace(f.Namespace)); err != nil {
return nil, err
}

var names []string
for i := range gwServers.Items {
names = append(names, gwServers.Items[i].Name)
}
return names, nil
}

return common(ctx, f, argsLimit, retriever)
}

// GatewayClients returns a function to autocomplete GatewayClients names.
func GatewayClients(ctx context.Context, f *factory.Factory, argsLimit int) FnType {
retriever := func(ctx context.Context, f *factory.Factory) ([]string, error) {
var gwClients networkingv1alpha1.GatewayClientList
if err := f.CRClient.List(ctx, &gwClients, client.InNamespace(f.Namespace)); err != nil {
return nil, err
}

var names []string
for i := range gwClients.Items {
names = append(names, gwClients.Items[i].Name)
}
return names, nil
}

return common(ctx, f, argsLimit, retriever)
}

// PublicKeys returns a function to autocomplete PublicKeys names.
func PublicKeys(ctx context.Context, f *factory.Factory, argsLimit int) FnType {
retriever := func(ctx context.Context, f *factory.Factory) ([]string, error) {
var publicKeys networkingv1alpha1.PublicKeyList
if err := f.CRClient.List(ctx, &publicKeys, client.InNamespace(f.Namespace)); err != nil {
return nil, err
}

var names []string
for i := range publicKeys.Items {
names = append(names, publicKeys.Items[i].Name)
}
return names, nil
}

return common(ctx, f, argsLimit, retriever)
}

// Configurations returns a function to autocomplete Configurations names.
func Configurations(ctx context.Context, f *factory.Factory, argsLimit int) FnType {
retriever := func(ctx context.Context, f *factory.Factory) ([]string, error) {
var configurations networkingv1alpha1.ConfigurationList
if err := f.CRClient.List(ctx, &configurations, client.InNamespace(f.Namespace)); err != nil {
return nil, err
}

var names []string
for i := range configurations.Items {
names = append(names, configurations.Items[i].Name)
}
return names, nil
}

Expand Down
Loading

0 comments on commit d4ddede

Please sign in to comment.