Skip to content

Commit

Permalink
fix: retrieve configurations resources by labels
Browse files Browse the repository at this point in the history
  • Loading branch information
fra98 committed Oct 9, 2024
1 parent cf7fd91 commit 49c2ef5
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
identitymanager "github.com/liqotech/liqo/pkg/identityManager"
)

// GenerateKubeconfigSecretName generates the name of the kubeconfig secret associated to an identity.
func GenerateKubeconfigSecretName(identity *authv1beta1.Identity) string {
// generateKubeconfigSecretName generates the name of the kubeconfig secret associated to an identity.
func generateKubeconfigSecretName(identity *authv1beta1.Identity) string {
return "kubeconfig-" + identity.Name
}

Expand All @@ -39,7 +39,7 @@ func KubeconfigSecret(identity *authv1beta1.Identity) *corev1.Secret {
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: GenerateKubeconfigSecretName(identity),
Name: generateKubeconfigSecretName(identity),
Namespace: identity.Namespace,
},
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/liqo-controller-manager/authentication/forge/nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"github.com/liqotech/liqo/pkg/consts"
)

// GenerateNonceSecretName generates the name of the Secret object to store the nonce.
func GenerateNonceSecretName() string {
// generateNonceSecretName generates the name of the Secret object to store the nonce.
func generateNonceSecretName() string {
return "liqo-nonce"
}

Expand All @@ -35,7 +35,7 @@ func Nonce(tenantNamespace string) *corev1.Secret {
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: GenerateNonceSecretName(),
Name: generateNonceSecretName(),
Namespace: tenantNamespace,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"github.com/liqotech/liqo/pkg/consts"
)

// GenerateSignedNonceSecretName generates the name of the Secret object to store the signed nonce.
func GenerateSignedNonceSecretName() string {
// generateSignedNonceSecretName generates the name of the Secret object to store the signed nonce.
func generateSignedNonceSecretName() string {
return "liqo-signed-nonce"
}

Expand All @@ -35,7 +35,7 @@ func SignedNonce(remoteClusterID liqov1beta1.ClusterID, tenantNamespace, nonce s
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: GenerateSignedNonceSecretName(),
Name: generateSignedNonceSecretName(),
Namespace: tenantNamespace,
Labels: map[string]string{
consts.SignedNonceSecretLabelKey: "true",
Expand Down
6 changes: 3 additions & 3 deletions pkg/liqo-controller-manager/networking/forge/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
ipamutils "github.com/liqotech/liqo/pkg/utils/ipam"
)

// DefaultConfigurationName returns the default name for a Configuration.
func DefaultConfigurationName(remoteClusterID liqov1beta1.ClusterID) string {
// defaultConfigurationName returns the default name for a Configuration.
func defaultConfigurationName(remoteClusterID liqov1beta1.ClusterID) string {
return string(remoteClusterID)
}

Expand Down Expand Up @@ -91,7 +91,7 @@ func ConfigurationForRemoteCluster(ctx context.Context, cl client.Client,
APIVersion: networkingv1beta1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: DefaultConfigurationName(clusterID),
Name: defaultConfigurationName(clusterID),
Labels: map[string]string{
consts.RemoteClusterID: string(clusterID),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package configuration
package utils

import (
"context"

"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

networkingv1beta1 "github.com/liqotech/liqo/apis/networking/v1beta1"
)

// 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 := &networkingv1beta1.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
func IsConfigurationStatusSet(confStatus networkingv1beta1.ConfigurationStatus) bool {
return confStatus.Remote != nil &&
confStatus.Remote.CIDR.Pod.String() != "" &&
confStatus.Remote.CIDR.External.String() != ""
}
16 changes: 16 additions & 0 deletions pkg/liqo-controller-manager/networking/utils/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2019-2024 The Liqo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package utils contains utility functions for the networking module.
package utils
28 changes: 17 additions & 11 deletions pkg/liqoctl/network/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
"github.com/liqotech/liqo/pkg/consts"
gwforge "github.com/liqotech/liqo/pkg/gateway/forge"
"github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/forge"
networkingutils "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/utils"
"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/liqoctl/wait"
tenantnamespace "github.com/liqotech/liqo/pkg/tenantNamespace"
liqoutils "github.com/liqotech/liqo/pkg/utils"
Expand Down Expand Up @@ -198,16 +198,18 @@ func (c *Cluster) SetupConfiguration(ctx context.Context, conf *networkingv1beta
func (c *Cluster) CheckNetworkInitialized(ctx context.Context, remoteClusterID liqov1beta1.ClusterID) error {
s := c.local.Printer.StartSpinner("Checking network is initialized correctly")

confReady, err := configuration.IsConfigurationStatusSet(ctx, c.local.CRClient,
forge.DefaultConfigurationName(remoteClusterID), c.local.Namespace)
// Get the network Configuration.
conf, err := getters.GetConfigurationByClusterID(ctx, c.local.CRClient, remoteClusterID)
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:
}

if !networkingutils.IsConfigurationStatusSet(conf.Status) {
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
Expand Down Expand Up @@ -344,17 +346,21 @@ func (c *Cluster) EnsurePublicKey(ctx context.Context, remoteClusterID liqov1bet
}

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

conf := &networkingv1beta1.Configuration{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: c.local.Namespace,
},
// Retrieve Configuration.
conf, err := getters.GetConfigurationByClusterID(ctx, c.local.CRClient, remoteClusterID)
if client.IgnoreNotFound(err) != nil {
s.Fail("An error occurred while retrieving network configuration: ", output.PrettyErr(err))
return err
} else if apierrors.IsNotFound(err) {
s.Success("Network configuration already deleted")
return nil
}

err := c.local.CRClient.Delete(ctx, conf)
// Delete Configuration.
err = c.local.CRClient.Delete(ctx, conf)
switch {
case client.IgnoreNotFound(err) != nil:
s.Fail("An error occurred while deleting network configuration: ", output.PrettyErr(err))
Expand Down
8 changes: 4 additions & 4 deletions pkg/liqoctl/network/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ func (o *Options) RunInit(ctx context.Context) error {

if o.Wait {
// Wait for cluster 1 to be ready.
if err := cluster1.waiter.ForConfiguration(ctx, cluster2.networkConfiguration); err != nil {
if err := cluster1.waiter.ForConfiguration(ctx, cluster2.localClusterID); err != nil {
return err
}

// Wait for cluster 2 to be ready.
if err := cluster2.waiter.ForConfiguration(ctx, cluster1.networkConfiguration); err != nil {
if err := cluster2.waiter.ForConfiguration(ctx, cluster1.localClusterID); err != nil {
return err
}
}
Expand Down Expand Up @@ -140,12 +140,12 @@ func (o *Options) RunReset(ctx context.Context) error {
}

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

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

// RunConnect connect two clusters using liqo networking.
Expand Down
8 changes: 4 additions & 4 deletions pkg/liqoctl/wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ import (
"github.com/liqotech/liqo/pkg/gateway/forge"
"github.com/liqotech/liqo/pkg/liqo-controller-manager/authentication"
authgetters "github.com/liqotech/liqo/pkg/liqo-controller-manager/authentication/getters"
networkingutils "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/utils"
"github.com/liqotech/liqo/pkg/liqoctl/factory"
"github.com/liqotech/liqo/pkg/liqoctl/output"
"github.com/liqotech/liqo/pkg/liqoctl/rest/configuration"
tenantnamespace "github.com/liqotech/liqo/pkg/tenantNamespace"
"github.com/liqotech/liqo/pkg/utils"
fcutils "github.com/liqotech/liqo/pkg/utils/foreigncluster"
Expand Down Expand Up @@ -185,14 +185,14 @@ func (w *Waiter) ForUnoffloading(ctx context.Context, namespace string) error {

// ForConfiguration waits until the status on the Configuration resource states that the configuration has been
// successfully applied.
func (w *Waiter) ForConfiguration(ctx context.Context, conf *networkingv1beta1.Configuration) error {
func (w *Waiter) ForConfiguration(ctx context.Context, remoteClusterID liqov1beta1.ClusterID) 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) {
ok, err := configuration.IsConfigurationStatusSet(ctx, w.CRClient, conf.Name, conf.Namespace)
conf, err := getters.GetConfigurationByClusterID(ctx, w.CRClient, remoteClusterID)
if err != nil {
return false, client.IgnoreNotFound(err)
}
return ok, nil
return networkingutils.IsConfigurationStatusSet(conf.Status), 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 49c2ef5

Please sign in to comment.