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 Oct 27, 2023
1 parent 920a5cc commit fd77f16
Show file tree
Hide file tree
Showing 19 changed files with 614 additions and 89 deletions.
36 changes: 36 additions & 0 deletions cmd/liqoctl/cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ const liqoctlNetworkLongHelp = `Manage liqo networking.`

const liqoctlNetworkInitLongHelp = `Initialize the liqo networking between two clusters.`

const liqoctlNetworkResetLongHelp = `Reset the liqo networking between two clusters.`

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

const liqoctlNetworkDisconnectLongHelp = `Disconnect networking between two clusters.`

func newNetworkCommand(ctx context.Context, f *factory.Factory) *cobra.Command {
options := network.NewOptions(f)
options.RemoteFactory = factory.NewForRemote()
Expand Down Expand Up @@ -75,7 +79,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 @@ -95,6 +101,21 @@ func newNetworkInitCommand(ctx context.Context, options *network.Options) *cobra
return cmd
}

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

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

return cmd
}

func newNetworkConnectCommand(ctx context.Context, options *network.Options) *cobra.Command {
cmd := &cobra.Command{
Use: "connect",
Expand Down Expand Up @@ -137,3 +158,18 @@ 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 using liqo networking",
Long: WithTemplate(liqoctlNetworkDisconnectLongHelp),
Args: cobra.NoArgs,

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
111 changes: 97 additions & 14 deletions pkg/liqoctl/network/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"fmt"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
Expand Down Expand Up @@ -121,7 +123,7 @@ func (c *Cluster) SetNamespaces(ctx context.Context) error {
func (c *Cluster) SetLocalConfiguration(ctx context.Context) error {
// Get network configuration.
s := c.local.Printer.StartSpinner("Retrieving network configuration")
conf, err := configuration.ForgeLocalConfiguration(ctx, c.local.CRClient, c.local.Namespace, c.local.LiqoNamespace)
conf, err := configuration.ForgeConfigurationForRemoteCluster(ctx, c.local.CRClient, c.local.Namespace, 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 Expand Up @@ -158,53 +160,77 @@ func (c *Cluster) SetupConfiguration(ctx context.Context, conf *networkingv1alph
return nil
}

// CheckNetworkInitialized checks if the network is initialized correctly.
func (c *Cluster) CheckNetworkInitialized(ctx context.Context, remoteClusterIdentity *discoveryv1alpha1.ClusterIdentity) error {
s := c.local.Printer.StartSpinner("Checking network is initialized correctly")

confReady, err := configuration.IsConfigurationStatusSet(ctx, c.local.CRClient,
configuration.DefaultConfigurationName(remoteClusterIdentity), c.local.Namespace)
switch {
case client.IgnoreNotFound(err) != nil:
s.Fail(fmt.Sprintf("An error occurred while checking network Configuration: %v", output.PrettyErr(err)))
return err
case apierrors.IsNotFound(err):
s.Fail(fmt.Sprintf("Network Configuration not found. Initialize the network first with `liqoctl network init`: %v", output.PrettyErr(err)))
return err
case !confReady:
err := fmt.Errorf("network Configuration status is not set yet. Retry later or initialize the network again with `liqoctl network init`")
s.Fail(err)
return err
}

s.Success("Network correctly initialized")
return nil
}

// EnsureGatewayServer create or updates a GatewayServer.
func (c *Cluster) EnsureGatewayServer(ctx context.Context, name string, opts *gatewayserver.ForgeOptions) (*networkingv1alpha1.GatewayServer, error) {
s := c.local.Printer.StartSpinner("Setting up Gateway Server")
s := c.local.Printer.StartSpinner("Setting up gateway server")
gwServer, err := gatewayserver.ForgeGatewayServer(name, c.local.Namespace, opts)
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while forging gatewayserver: %v", output.PrettyErr(err)))
s.Fail(fmt.Sprintf("An error occurred while forging gateway server: %v", output.PrettyErr(err)))
return nil, err
}
_, err = controllerutil.CreateOrUpdate(ctx, c.local.CRClient, gwServer, func() error {
return gatewayserver.MutateGatewayServer(gwServer, opts)
})
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while setting up gatewayserver: %v", output.PrettyErr(err)))
s.Fail(fmt.Sprintf("An error occurred while setting up gateway server: %v", output.PrettyErr(err)))
return nil, err
}

s.Success("Gatewayserver correctly set up")
s.Success("Gateway server correctly set up")
return gwServer, nil
}

// EnsureGatewayClient create or updates a GatewayClient.
func (c *Cluster) EnsureGatewayClient(ctx context.Context, name string, opts *gatewayclient.ForgeOptions) (*networkingv1alpha1.GatewayClient, error) {
s := c.local.Printer.StartSpinner("Setting up Gateway Client")
s := c.local.Printer.StartSpinner("Setting up gateway client")
gwClient, err := gatewayclient.ForgeGatewayClient(name, c.local.Namespace, opts)
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while forging gatewayclient: %v", output.PrettyErr(err)))
s.Fail(fmt.Sprintf("An error occurred while forging gateway client: %v", output.PrettyErr(err)))
return nil, err
}
_, err = controllerutil.CreateOrUpdate(ctx, c.local.CRClient, gwClient, func() error {
return gatewayclient.MutateGatewayClient(gwClient, opts)
})
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while setting up gatewayclient: %v", output.PrettyErr(err)))
s.Fail(fmt.Sprintf("An error occurred while setting up gateway client: %v", output.PrettyErr(err)))
return nil, err
}

s.Success("Gatewayclient correctly set up")
s.Success("Gateway client correctly set up")
return gwClient, nil
}

// EnsurePublicKey create or updates a PublicKey.
func (c *Cluster) EnsurePublicKey(ctx context.Context, remoteClusterIdentity *discoveryv1alpha1.ClusterIdentity,
key []byte, ownerGateway metav1.Object) error {
s := c.local.Printer.StartSpinner("Creating PublicKey")
pubKey, err := publickey.ForgePublicKey(remoteClusterIdentity.ClusterName, c.local.Namespace, remoteClusterIdentity.ClusterID, key)
s := c.local.Printer.StartSpinner("Creating public key")
pubKey, err := publickey.ForgePublicKey(publickey.DefaultPublicKeyName(remoteClusterIdentity), c.local.Namespace,
remoteClusterIdentity.ClusterID, key)
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while forging publickey: %v", output.PrettyErr(err)))
s.Fail(fmt.Sprintf("An error occurred while forging public key: %v", output.PrettyErr(err)))
return err
}
_, err = controllerutil.CreateOrUpdate(ctx, c.local.CRClient, pubKey, func() error {
Expand All @@ -214,10 +240,67 @@ func (c *Cluster) EnsurePublicKey(ctx context.Context, remoteClusterIdentity *di
return controllerutil.SetOwnerReference(ownerGateway, pubKey, c.local.CRClient.Scheme())
})
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while creating publickey: %v", output.PrettyErr(err)))
s.Fail(fmt.Sprintf("An error occurred while creating public key: %v", output.PrettyErr(err)))
return err
}

s.Success("Public key correctly created")
return nil
}

// DeleteConfiguration deletes a Configuration.
func (c *Cluster) DeleteConfiguration(ctx context.Context, name string) error {
s := c.local.Printer.StartSpinner("Deleting network configuration")

conf := &networkingv1alpha1.Configuration{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: c.local.Namespace,
},
}
if err := c.local.CRClient.Delete(ctx, conf); err != nil {
s.Fail(fmt.Sprintf("An error occurred while deleting network configuration: %v", output.PrettyErr(err)))
return err
}

s.Success("Network configuration correctly deleted")
return nil
}

// DeleteGatewayServer deletes a GatewayServer.
func (c *Cluster) DeleteGatewayServer(ctx context.Context, name string) error {
s := c.local.Printer.StartSpinner("Deleting gateway server")

gwServer := &networkingv1alpha1.GatewayServer{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: c.local.Namespace,
},
}
if err := c.local.CRClient.Delete(ctx, gwServer); err != nil {
s.Fail(fmt.Sprintf("An error occurred while deleting gateway server: %v", output.PrettyErr(err)))
return err
}

s.Success("Gateway server correctly deleted")
return nil
}

// DeleteGatewayClient deletes a GatewayClient.
func (c *Cluster) DeleteGatewayClient(ctx context.Context, name string) error {
s := c.local.Printer.StartSpinner("Deleting gateway client")

gwClient := &networkingv1alpha1.GatewayClient{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: c.local.Namespace,
},
}
if err := c.local.CRClient.Delete(ctx, gwClient); err != nil {
s.Fail(fmt.Sprintf("An error occurred while deleting gateway client: %v", output.PrettyErr(err)))
return err
}

s.Success("PublicKey correctly created")
s.Success("Gateway client correctly deleted")
return nil
}
Loading

0 comments on commit fd77f16

Please sign in to comment.