Skip to content

Commit

Permalink
Network reset and disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
fra98 committed Oct 25, 2023
1 parent aa7181a commit 6481c5a
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 65 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 @@ -136,3 +157,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
}
86 changes: 72 additions & 14 deletions pkg/liqoctl/network/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,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 @@ -160,51 +160,52 @@ func (c *Cluster) SetupConfiguration(ctx context.Context, conf *networkingv1alph

// 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 +215,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("PublicKey correctly created")
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("Gateway client correctly deleted")
return nil
}
57 changes: 55 additions & 2 deletions pkg/liqoctl/network/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1"
"github.com/liqotech/liqo/pkg/liqoctl/factory"
"github.com/liqotech/liqo/pkg/liqoctl/rest/configuration"
"github.com/liqotech/liqo/pkg/liqoctl/rest/gatewayclient"
"github.com/liqotech/liqo/pkg/liqoctl/rest/gatewayserver"
"github.com/liqotech/liqo/pkg/liqoctl/rest/publickey"
Expand Down Expand Up @@ -113,6 +114,32 @@ func (o *Options) RunInit(ctx context.Context) error {
return nil
}

// RunReset reset the liqo networking between two clusters.
func (o *Options) RunReset(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, o.Timeout)
defer cancel()

// Create and initialize cluster 1.
cluster1 := NewCluster(o.LocalFactory, o.RemoteFactory)
if err := cluster1.Init(ctx); err != nil {
return err
}

// Create and initialize cluster 2.
cluster2 := NewCluster(o.RemoteFactory, o.LocalFactory)
if err := cluster2.Init(ctx); err != nil {
return err
}

// Delete Configuration on cluster 1
if err := cluster1.DeleteConfiguration(ctx, configuration.DefaultConfigurationName(cluster2.clusterIdentity)); err != nil {
return err
}

// Delete Configuration on cluster 2
return cluster2.DeleteConfiguration(ctx, configuration.DefaultConfigurationName(cluster1.clusterIdentity))
}

// RunConnect connect two clusters using liqo networking.
func (o *Options) RunConnect(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, o.Timeout)
Expand All @@ -132,7 +159,7 @@ func (o *Options) RunConnect(ctx context.Context) error {

// Create gateway server on cluster 1
gwServer, err := cluster1.EnsureGatewayServer(ctx,
cluster2.clusterIdentity.ClusterName,
gatewayserver.DefaultGatewayServerName(cluster2.clusterIdentity),
o.newGatewayServerForgeOptions(o.LocalFactory.KubeClient, cluster2.clusterIdentity.ClusterID))
if err != nil {
return err
Expand All @@ -145,7 +172,7 @@ func (o *Options) RunConnect(ctx context.Context) error {

// Create gateway client on cluster 2
gwClient, err := cluster2.EnsureGatewayClient(ctx,
cluster1.clusterIdentity.ClusterName,
gatewayclient.DefaultGatewayClientName(cluster1.clusterIdentity),
o.newGatewayClientForgeOptions(o.RemoteFactory.KubeClient, cluster1.clusterIdentity.ClusterID, gwServer.Status.Endpoint))
if err != nil {
return err
Expand Down Expand Up @@ -209,6 +236,32 @@ func (o *Options) RunConnect(ctx context.Context) error {
return nil
}

// RunDisconnect remove networking between two clusters.
func (o *Options) RunDisconnect(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, o.Timeout)
defer cancel()

// Create and initialize cluster 1.
cluster1 := NewCluster(o.LocalFactory, o.RemoteFactory)
if err := cluster1.Init(ctx); err != nil {
return err
}

// Create and initialize cluster 2.
cluster2 := NewCluster(o.RemoteFactory, o.LocalFactory)
if err := cluster2.Init(ctx); err != nil {
return err
}

// Delete gateway server on cluster 1
if err := cluster1.DeleteGatewayServer(ctx, gatewayserver.DefaultGatewayServerName(cluster2.clusterIdentity)); err != nil {
return err
}

// Delete gateway client on cluster 2
return cluster2.DeleteGatewayClient(ctx, gatewayclient.DefaultGatewayClientName(cluster1.clusterIdentity))
}

func (o *Options) newGatewayServerForgeOptions(kubeClient kubernetes.Interface, remoteClusterID string) *gatewayserver.ForgeOptions {
return &gatewayserver.ForgeOptions{
KubeClient: kubeClient,
Expand Down
49 changes: 1 addition & 48 deletions pkg/liqoctl/rest/configuration/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,12 @@ import (
"fmt"

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1"
liqoconsts "github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/liqoctl/completion"
"github.com/liqotech/liqo/pkg/liqoctl/output"
"github.com/liqotech/liqo/pkg/liqoctl/rest"
liqoutils "github.com/liqotech/liqo/pkg/utils"
"github.com/liqotech/liqo/pkg/utils/args"
liqogetters "github.com/liqotech/liqo/pkg/utils/getters"
)

const liqoctlGenerateConfigHelp = `Generate the local network configuration to be applied to other clusters.`
Expand Down Expand Up @@ -71,7 +63,7 @@ func (o *Options) Generate(ctx context.Context, options *rest.GenerateOptions) *
func (o *Options) handleGenerate(ctx context.Context) error {
opts := o.generateOptions

conf, err := ForgeLocalConfiguration(ctx, opts.CRClient, opts.Namespace, opts.LiqoNamespace)
conf, err := ForgeConfigurationForRemoteCluster(ctx, opts.CRClient, opts.Namespace, opts.LiqoNamespace)
if err != nil {
opts.Printer.CheckErr(fmt.Errorf("unable to forge local configuration: %w", err))
return err
Expand All @@ -80,42 +72,3 @@ func (o *Options) handleGenerate(ctx context.Context) error {
opts.Printer.CheckErr(o.output(conf))
return nil
}

// ForgeLocalConfiguration creates a local configuration starting from the cluster identity and the IPAM storage.
func ForgeLocalConfiguration(ctx context.Context, cl client.Client, namespace, liqoNamespace string) (*networkingv1alpha1.Configuration, error) {
clusterIdentity, err := liqoutils.GetClusterIdentityWithControllerClient(ctx, cl, liqoNamespace)
if err != nil {
return nil, fmt.Errorf("unable to get cluster identity: %w", err)
}

ipamStorage, err := liqogetters.GetIPAMStorageByLabel(ctx, cl, labels.NewSelector())
if err != nil {
return nil, fmt.Errorf("unable to get IPAM storage: %w", err)
}

cnf := &networkingv1alpha1.Configuration{
TypeMeta: metav1.TypeMeta{
Kind: networkingv1alpha1.ConfigurationKind,
APIVersion: networkingv1alpha1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: clusterIdentity.ClusterName,
Labels: map[string]string{
liqoconsts.RemoteClusterID: clusterIdentity.ClusterID,
},
},
Spec: networkingv1alpha1.ConfigurationSpec{
Remote: networkingv1alpha1.ClusterConfig{
CIDR: networkingv1alpha1.ClusterConfigCIDR{
Pod: networkingv1alpha1.CIDR(ipamStorage.Spec.PodCIDR),
External: networkingv1alpha1.CIDR(ipamStorage.Spec.ExternalCIDR),
},
},
},
}

if namespace != "" && namespace != corev1.NamespaceDefault {
cnf.Namespace = namespace
}
return cnf, nil
}
Loading

0 comments on commit 6481c5a

Please sign in to comment.