Skip to content

Commit

Permalink
Check
Browse files Browse the repository at this point in the history
  • Loading branch information
fra98 committed Oct 27, 2023
1 parent 9e92bb9 commit 633b366
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
25 changes: 25 additions & 0 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 @@ -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")
Expand Down
10 changes: 10 additions & 0 deletions pkg/liqoctl/network/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
14 changes: 14 additions & 0 deletions pkg/liqoctl/rest/configuration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
10 changes: 3 additions & 7 deletions pkg/liqoctl/wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)))
Expand Down

0 comments on commit 633b366

Please sign in to comment.