diff --git a/pkg/liqoctl/authenticate/cluster.go b/pkg/liqoctl/authenticate/cluster.go index 76f1ba1a95..deaf04484f 100644 --- a/pkg/liqoctl/authenticate/cluster.go +++ b/pkg/liqoctl/authenticate/cluster.go @@ -73,11 +73,12 @@ func (c *Cluster) EnsureTenantNamespace(ctx context.Context, remoteClusterID liq c.RemoteClusterID = remoteClusterID - if _, err := c.localNamespaceManager.CreateNamespace(ctx, c.RemoteClusterID); err != nil { + tenantNs, err := c.localNamespaceManager.CreateNamespace(ctx, c.RemoteClusterID) + if err != nil { s.Fail(fmt.Sprintf("An error occurred while ensuring tenant namespace: %v", output.PrettyErr(err))) return err } - c.TenantNamespace = tenantnamespace.GetNameForNamespace(c.RemoteClusterID) + c.TenantNamespace = tenantNs.Name s.Success("Tenant namespace correctly ensured") diff --git a/pkg/liqoctl/network/cluster.go b/pkg/liqoctl/network/cluster.go index b7a499827d..52b366ba0d 100644 --- a/pkg/liqoctl/network/cluster.go +++ b/pkg/liqoctl/network/cluster.go @@ -100,6 +100,8 @@ func (c *Cluster) SetClusterIDs(ctx context.Context) error { // unless the user has explicitly set custom namespaces with the `--namespace` and/or `--remote-namespace` flags. // All the external network resources will be created in these namespaces in their respective clusters. func (c *Cluster) SetNamespaces(ctx context.Context, createTenantNs bool) error { + var err error + if c.localClusterID == "" || c.remoteClusterID == "" { if err := c.SetClusterIDs(ctx); err != nil { return err @@ -107,23 +109,44 @@ func (c *Cluster) SetNamespaces(ctx context.Context, createTenantNs bool) error } if c.local.Namespace == "" || c.local.Namespace == corev1.NamespaceDefault { + var localTenantNs *corev1.Namespace + if createTenantNs { - if _, err := c.localNamespaceManager.CreateNamespace(ctx, c.remoteClusterID); err != nil { - c.local.Printer.CheckErr(fmt.Errorf("an error occurred while creating tenant namespace: %v", output.PrettyErr(err))) + localTenantNs, err = c.localNamespaceManager.CreateNamespace(ctx, c.remoteClusterID) + if err != nil { + c.local.Printer.CheckErr(fmt.Errorf("an error occurred while creating local tenant namespace: %v", output.PrettyErr(err))) + return err + } + } else { + localTenantNs, err = c.localNamespaceManager.GetNamespace(ctx, c.remoteClusterID) + if err != nil { + c.local.Printer.CheckErr(fmt.Errorf("an error occurred while retrieving local tenant namespace: %v", output.PrettyErr(err))) return err } } - c.local.Namespace = tenantnamespace.GetNameForNamespace(c.remoteClusterID) + + // Set the local namespace to the tenant namespace. + c.local.Namespace = localTenantNs.Name } if c.remote.Namespace == "" || c.remote.Namespace == corev1.NamespaceDefault { + var remoteTenantNs *corev1.Namespace + if createTenantNs { - if _, err := c.remoteNamespaceManager.CreateNamespace(ctx, c.localClusterID); err != nil { - c.remote.Printer.CheckErr(fmt.Errorf("an error occurred while creating tenant namespace: %v", output.PrettyErr(err))) + remoteTenantNs, err = c.remoteNamespaceManager.CreateNamespace(ctx, c.localClusterID) + if err != nil { + c.remote.Printer.CheckErr(fmt.Errorf("an error occurred while creating remote tenant namespace: %v", output.PrettyErr(err))) + return err + } + } else { + remoteTenantNs, err = c.remoteNamespaceManager.GetNamespace(ctx, c.localClusterID) + if err != nil { + c.remote.Printer.CheckErr(fmt.Errorf("an error occurred while retrieving remote tenant namespace: %v", output.PrettyErr(err))) return err } } - c.remote.Namespace = tenantnamespace.GetNameForNamespace(c.localClusterID) + + c.remote.Namespace = remoteTenantNs.Name } return nil diff --git a/pkg/tenantNamespace/namespaceManager.go b/pkg/tenantNamespace/namespaceManager.go index 33b021e67e..6643279dde 100644 --- a/pkg/tenantNamespace/namespaceManager.go +++ b/pkg/tenantNamespace/namespaceManager.go @@ -75,7 +75,7 @@ func NewCachedManager(ctx context.Context, client kubernetes.Interface, scheme * func(lo *metav1.ListOptions) { lo.LabelSelector = labels.NewSelector().Add(*req).String() }, )) namespaceLister := factory.Core().V1().Namespaces().Lister() - listNamespaces := func(ctx context.Context, selector labels.Selector) (ret []*v1.Namespace, err error) { + listNamespaces := func(_ context.Context, selector labels.Selector) (ret []*v1.Namespace, err error) { return namespaceLister.List(selector) } @@ -117,8 +117,8 @@ func (nm *tenantNamespaceManager) CreateNamespace(ctx context.Context, cluster l // ForgeNamespace returns a Tenant Namespace resource object given name and clusterid. func (nm *tenantNamespaceManager) ForgeNamespace(cluster liqov1beta1.ClusterID, name *string) *v1.Namespace { - // If no name is provided use the default one provided by the GetNameForNamespace() function - nsname := GetNameForNamespace(cluster) + // If no name is provided use the default one provided by the getNameForNamespace() function + nsname := getNameForNamespace(cluster) if name != nil { nsname = *name } @@ -153,7 +153,7 @@ func (nm *tenantNamespaceManager) GetNamespace(ctx context.Context, cluster liqo } if nItems := len(namespaces); nItems == 0 { - err = kerrors.NewNotFound(v1.Resource("Namespace"), GetNameForNamespace(cluster)) + err = kerrors.NewNotFound(v1.Resource("Namespace"), string(cluster)) // do not log it always, since it is also used in the preliminary stage of the create method klog.V(4).Info(err) return nil, err @@ -165,7 +165,7 @@ func (nm *tenantNamespaceManager) GetNamespace(ctx context.Context, cluster liqo return namespaces[0].DeepCopy(), nil } -// GetNameForNamespace given a cluster identity it returns the name of the tenant namespace for the cluster. -func GetNameForNamespace(cluster liqov1beta1.ClusterID) string { +// getNameForNamespace given a cluster identity it returns the default name of the tenant namespace for the cluster. +func getNameForNamespace(cluster liqov1beta1.ClusterID) string { return fmt.Sprintf("%s-%s", NamePrefix, foreignclusterutils.UniqueName(cluster)) } diff --git a/test/e2e/cruise/multiplevirtualnodes/multiple_virtualnodes_test.go b/test/e2e/cruise/multiplevirtualnodes/multiple_virtualnodes_test.go index 012d537ad5..db254712a8 100644 --- a/test/e2e/cruise/multiplevirtualnodes/multiple_virtualnodes_test.go +++ b/test/e2e/cruise/multiplevirtualnodes/multiple_virtualnodes_test.go @@ -67,8 +67,7 @@ var ( return *req } - createTestResourceSlice = func(cl client.Client, name string, providerClusterID liqov1beta1.ClusterID, createVirtualNode bool) { - tenantNs := tenantnamespace.GetNameForNamespace(providerClusterID) + createTestResourceSlice = func(cl client.Client, name, tenantNs string, providerClusterID liqov1beta1.ClusterID, createVirtualNode bool) { resSlice := forge.ResourceSlice(name, tenantNs) if resSlice.Labels == nil { resSlice.Labels = map[string]string{} @@ -175,10 +174,14 @@ var _ = Describe("Liqo E2E", func() { for i := range testContext.Clusters { if testContext.Clusters[i].Role == liqov1beta1.ConsumerRole { consumer := testContext.Clusters[i] + nsManager := tenantnamespace.NewManager(consumer.NativeClient, consumer.ControllerClient.Scheme()) for j := range testContext.Clusters { if testContext.Clusters[j].Role == liqov1beta1.ProviderRole { provider := testContext.Clusters[j] - createTestResourceSlice(consumer.ControllerClient, fmt.Sprintf("rs-test-%s", provider.Cluster), provider.Cluster, true) + tenantNs, err := nsManager.GetNamespace(ctx, provider.Cluster) + Expect(err).To(Not(HaveOccurred())) + createTestResourceSlice(consumer.ControllerClient, + fmt.Sprintf("rs-test-%s", provider.Cluster), tenantNs.Name, provider.Cluster, true) } } }