From 633b3668bf1c7560d39d923d25c67fe8613ea169 Mon Sep 17 00:00:00 2001 From: Francesco Torta <62566275+fra98@users.noreply.github.com> Date: Fri, 27 Oct 2023 15:22:50 +0200 Subject: [PATCH] Check --- pkg/liqoctl/network/cluster.go | 25 +++++++++++++++++++++++++ pkg/liqoctl/network/handler.go | 10 ++++++++++ pkg/liqoctl/rest/configuration/utils.go | 14 ++++++++++++++ pkg/liqoctl/wait/wait.go | 10 +++------- 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/pkg/liqoctl/network/cluster.go b/pkg/liqoctl/network/cluster.go index 0a35c298a4..28c2604564 100644 --- a/pkg/liqoctl/network/cluster.go +++ b/pkg/liqoctl/network/cluster.go @@ -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" @@ -158,6 +160,29 @@ 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") diff --git a/pkg/liqoctl/network/handler.go b/pkg/liqoctl/network/handler.go index b0ef0955a3..e469cdcab0 100644 --- a/pkg/liqoctl/network/handler.go +++ b/pkg/liqoctl/network/handler.go @@ -157,6 +157,16 @@ func (o *Options) RunConnect(ctx context.Context) error { return err } + // Check if the Networking is initialized on cluster 1 + if err := cluster1.CheckNetworkInitialized(ctx, cluster2.clusterIdentity); err != nil { + return err + } + + // Check if the Networking is initialized on cluster 2 + if err := cluster2.CheckNetworkInitialized(ctx, cluster1.clusterIdentity); err != nil { + return err + } + // Create gateway server on cluster 1 gwServer, err := cluster1.EnsureGatewayServer(ctx, gatewayserver.DefaultGatewayServerName(cluster2.clusterIdentity), diff --git a/pkg/liqoctl/rest/configuration/utils.go b/pkg/liqoctl/rest/configuration/utils.go index c32ebc7a15..3009b6a379 100644 --- a/pkg/liqoctl/rest/configuration/utils.go +++ b/pkg/liqoctl/rest/configuration/utils.go @@ -21,6 +21,7 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1" @@ -75,3 +76,16 @@ func ForgeConfigurationForRemoteCluster(ctx context.Context, cl client.Client, func DefaultConfigurationName(remoteClusterIdentity *discoveryv1alpha1.ClusterIdentity) string { return remoteClusterIdentity.ClusterName } + +// IsConfigurationStatusSet check if a Configuration is ready by checking if its status is correctly set. +func IsConfigurationStatusSet(ctx context.Context, cl client.Client, name, namespace string) (bool, error) { + conf := &networkingv1alpha1.Configuration{} + if err := cl.Get(ctx, types.NamespacedName{Name: name, Namespace: namespace}, conf); err != nil { + return false, err + } + + return conf.Status.Remote != nil && + conf.Status.Remote.CIDR.Pod.String() != "" && + conf.Status.Remote.CIDR.External.String() != "", + nil +} diff --git a/pkg/liqoctl/wait/wait.go b/pkg/liqoctl/wait/wait.go index 2c8ef8a82d..ff031b000d 100644 --- a/pkg/liqoctl/wait/wait.go +++ b/pkg/liqoctl/wait/wait.go @@ -30,6 +30,7 @@ import ( "github.com/liqotech/liqo/pkg/consts" "github.com/liqotech/liqo/pkg/liqoctl/factory" "github.com/liqotech/liqo/pkg/liqoctl/output" + "github.com/liqotech/liqo/pkg/liqoctl/rest/configuration" "github.com/liqotech/liqo/pkg/utils" fcutils "github.com/liqotech/liqo/pkg/utils/foreignCluster" getters "github.com/liqotech/liqo/pkg/utils/getters" @@ -206,16 +207,11 @@ func (w *Waiter) ForUnoffloading(ctx context.Context, namespace string) error { func (w *Waiter) ForConfiguration(ctx context.Context, conf *networkingv1alpha1.Configuration) error { s := w.Printer.StartSpinner("Waiting for configuration to be applied") err := wait.PollUntilContextCancel(ctx, 1*time.Second, true, func(ctx context.Context) (done bool, err error) { - currentConf := &networkingv1alpha1.Configuration{} - err = w.CRClient.Get(ctx, client.ObjectKey{Name: conf.Name, Namespace: conf.Namespace}, currentConf) + ok, err := configuration.IsConfigurationStatusSet(ctx, w.CRClient, conf.Name, conf.Namespace) if err != nil { return false, client.IgnoreNotFound(err) } - - return currentConf.Status.Remote != nil && - currentConf.Status.Remote.CIDR.Pod.String() != "" && - currentConf.Status.Remote.CIDR.External.String() != "", - nil + return ok, nil }) if err != nil { s.Fail(fmt.Sprintf("Failed waiting for configuration to be applied: %s", output.PrettyErr(err)))