Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: peer command default network resources namespace #2846

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions pkg/liqoctl/network/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type Cluster struct {
remote *factory.Factory
waiter *wait.Waiter

localNetworkNamespace string
remoteNetworkNamespace string

localNamespaceManager tenantnamespace.Manager
remoteNamespaceManager tenantnamespace.Manager

Expand Down Expand Up @@ -113,7 +116,10 @@ func (c *Cluster) SetNamespaces(ctx context.Context, createTenantNs bool) error
}
}

if c.local.Namespace == "" || c.local.Namespace == corev1.NamespaceDefault {
switch c.local.Namespace {
case "", corev1.NamespaceDefault:
// If the local namespace is not set or it is the default one, create or retrieve the tenant namespace and
// set it as the local network namespace.
var localTenantNs *corev1.Namespace

if createTenantNs {
Expand All @@ -130,13 +136,16 @@ func (c *Cluster) SetNamespaces(ctx context.Context, createTenantNs bool) error
}
}

// Set the local namespace to the tenant namespace.
c.local.Namespace = localTenantNs.Name
c.localNetworkNamespace = localTenantNs.Name
default:
c.localNetworkNamespace = c.local.Namespace
}

if c.remote.Namespace == "" || c.remote.Namespace == corev1.NamespaceDefault {
switch c.remote.Namespace {
case "", corev1.NamespaceDefault:
// If the remote namespace is not set or it is the default one, create or retrieve the tenant namespace and
// set it as the remote network namespace.
var remoteTenantNs *corev1.Namespace

if createTenantNs {
remoteTenantNs, err = c.remoteNamespaceManager.CreateNamespace(ctx, c.localClusterID)
if err != nil {
Expand All @@ -151,7 +160,9 @@ func (c *Cluster) SetNamespaces(ctx context.Context, createTenantNs bool) error
}
}

c.remote.Namespace = remoteTenantNs.Name
c.remoteNetworkNamespace = remoteTenantNs.Name
default:
c.remoteNetworkNamespace = c.remote.Namespace
}

return nil
Expand All @@ -161,7 +172,7 @@ func (c *Cluster) SetNamespaces(ctx context.Context, createTenantNs bool) error
func (c *Cluster) SetLocalConfiguration(ctx context.Context) error {
// Get network configuration.
s := c.local.Printer.StartSpinner("Retrieving network configuration")
conf, err := forge.ConfigurationForRemoteCluster(ctx, c.local.CRClient, c.local.Namespace, c.local.LiqoNamespace)
conf, err := forge.ConfigurationForRemoteCluster(ctx, c.local.CRClient, c.localNetworkNamespace, c.local.LiqoNamespace)
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while retrieving network configuration: %v", output.PrettyErr(err)))
return err
Expand All @@ -175,7 +186,7 @@ func (c *Cluster) SetLocalConfiguration(ctx context.Context) error {
// SetupConfiguration sets up the network configuration.
func (c *Cluster) SetupConfiguration(ctx context.Context, conf *networkingv1beta1.Configuration) error {
s := c.local.Printer.StartSpinner("Setting up network configuration")
conf.Namespace = c.local.Namespace
conf.Namespace = c.localNetworkNamespace
confCopy := conf.DeepCopy()
_, err := resource.CreateOrUpdate(ctx, c.local.CRClient, conf, func() error {
if conf.Labels == nil {
Expand Down Expand Up @@ -387,7 +398,7 @@ func (c *Cluster) EnsureGatewayServer(ctx context.Context, opts *forge.GwServerO
}

// Forge GatewayServer.
gwServer, err = forge.GatewayServer(c.local.Namespace, name, opts)
gwServer, err = forge.GatewayServer(c.localNetworkNamespace, name, opts)
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while forging gateway server: %v", output.PrettyErr(err)))
return nil, err
Expand Down Expand Up @@ -437,7 +448,7 @@ func (c *Cluster) EnsureGatewayClient(ctx context.Context, opts *forge.GwClientO
name = &gwClient.Name // if the GatewayClient already exists, keep its name
}

gwClient, err = forge.GatewayClient(c.local.Namespace, name, opts)
gwClient, err = forge.GatewayClient(c.localNetworkNamespace, name, opts)
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while forging gateway client: %v", output.PrettyErr(err)))
return nil, err
Expand Down Expand Up @@ -469,7 +480,7 @@ func (c *Cluster) EnsurePublicKey(ctx context.Context, remoteClusterID liqov1bet
name = &pk.Name // if the PublicKey already exists, keep its name
}

pubKey, err := forge.PublicKey(c.local.Namespace, name, remoteClusterID, key)
pubKey, err := forge.PublicKey(c.localNetworkNamespace, name, remoteClusterID, key)
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while forging public key: %v", output.PrettyErr(err)))
return err
Expand Down
7 changes: 7 additions & 0 deletions pkg/liqoctl/peer/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func (o *Options) RunPeer(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, o.Timeout)
defer cancel()

// To ease the experience for most users, we disable the namespace and remote-namespace flags
// so that resources are created according to the default Liqo logic.
// Advanced users can use the individual commands (e.g., liqoctl init, liqoctl connect, etc..) to
// customize the namespaces according to their needs (e.g., networking resources in a specific namespace).
o.LocalFactory.Namespace = ""
o.RemoteFactory.Namespace = ""

// Ensure networking
if !o.NetworkingDisabled {
if err := ensureNetworking(ctx, o); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/liqoctl/unpeer/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func (o *Options) RunUnpeer(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, o.Timeout)
defer cancel()

// To ease the experience for most users, we disable the namespace and remote-namespace flags
// so that resources are created according to the default Liqo logic.
// Advanced users can use the individual commands (e.g., liqoctl reset, liqoctl disconnect, etc..) to
// customize the namespaces according to their needs (e.g., networking resources in a specific namespace).
o.LocalFactory.Namespace = ""
o.RemoteFactory.Namespace = ""

// Get consumer clusterID
o.consumerClusterID, err = liqoutils.GetClusterIDWithControllerClient(ctx, o.LocalFactory.CRClient, o.LocalFactory.LiqoNamespace)
if err != nil {
Expand Down
Loading