Skip to content

Commit

Permalink
fix namespace translation in resources replication
Browse files Browse the repository at this point in the history
  • Loading branch information
aleoli committed Aug 2, 2021
1 parent a19e7f1 commit 2681ff6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
28 changes: 19 additions & 9 deletions internal/crdReplicator/crdReplicator-operator.go
Expand Up @@ -196,7 +196,9 @@ func (c *Controller) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
c.ClusterID, req.NamespacedName, remoteClusterID, err)
return result, nil
}
return result, c.setUpConnectionToPeeringCluster(config, remoteClusterID, &fc)

c.setUpConnectionToPeeringCluster(config, &fc)
return result, nil
}

func (c *Controller) SetupWithManager(mgr ctrl.Manager) error {
Expand Down Expand Up @@ -227,20 +229,29 @@ func (c *Controller) updateForeignCluster(ctx context.Context,
})
}

func (c *Controller) setUpConnectionToPeeringCluster(config *rest.Config, remoteClusterID string, fc *discoveryv1alpha1.ForeignCluster) error {
c.LocalToRemoteNamespaceMapper[fc.Status.TenantNamespace.Local] = fc.Status.TenantNamespace.Remote
c.RemoteToLocalNamespaceMapper[fc.Status.TenantNamespace.Remote] = fc.Status.TenantNamespace.Local
c.ClusterIDToLocalNamespaceMapper[fc.Spec.ClusterIdentity.ClusterID] = fc.Status.TenantNamespace.Local
c.ClusterIDToRemoteNamespaceMapper[fc.Spec.ClusterIdentity.ClusterID] = fc.Status.TenantNamespace.Remote
func (c *Controller) setUpConnectionToPeeringCluster(config *rest.Config, fc *discoveryv1alpha1.ForeignCluster) {
remoteClusterID := fc.Spec.ClusterIdentity.ClusterID
localNamespace := fc.Status.TenantNamespace.Local
remoteNamespace := fc.Status.TenantNamespace.Remote
keyedRemoteNamespace := remoteNamespaceKeyer(remoteClusterID, remoteNamespace)
klog.V(3).Infof("%s -> setting up mapping local namespace %s to %s",
remoteClusterID, localNamespace, remoteNamespace)
c.LocalToRemoteNamespaceMapper[localNamespace] = remoteNamespace

klog.V(3).Infof("%s -> setting up mapping remote namespace %s to %s",
remoteClusterID, remoteNamespace, localNamespace)
c.RemoteToLocalNamespaceMapper[keyedRemoteNamespace] = localNamespace

c.ClusterIDToLocalNamespaceMapper[remoteClusterID] = localNamespace
c.ClusterIDToRemoteNamespaceMapper[remoteClusterID] = remoteNamespace

// check if the dynamic dynamic client exists
if _, ok := c.RemoteDynClients[remoteClusterID]; !ok {
dynClient, err := dynamic.NewForConfig(config)
if err != nil {
klog.Errorf("%s -> unable to create dynamic client in order to create the dynamic shared informer factory: %s", remoteClusterID, err)
// we don't need to immediately requeue the foreign cluster but wait for the next re-sync
return nil
return
} else {
klog.Infof("%s -> dynamic client created", remoteClusterID)
}
Expand All @@ -253,7 +264,6 @@ func (c *Controller) setUpConnectionToPeeringCluster(config *rest.Config, remote
c.RemoteDynSharedInformerFactory[remoteClusterID] = f
klog.Infof("%s -> dynamic shared informer factory created", remoteClusterID)
}
return nil
}

func (c *Controller) SetLabelsForRemoteResources(options *metav1.ListOptions) {
Expand Down Expand Up @@ -325,7 +335,7 @@ func (c *Controller) RemoteResourceModifiedHandler(obj *unstructured.Unstructure
name := obj.GetName()
namespace := obj.GetNamespace()

localNamespace := c.remoteToLocalNamespace(namespace)
localNamespace := c.remoteToLocalNamespace(remoteClusterID, namespace)

localDynClient := c.LocalDynClient
clusterID := c.ClusterID
Expand Down
7 changes: 4 additions & 3 deletions internal/crdReplicator/crdReplicator-operator_test.go
Expand Up @@ -499,26 +499,27 @@ func TestGetStatus(t *testing.T) {
func TestNamespaceTranslation(t *testing.T) {
d := getCRDReplicator()

remoteClusterID := "cluster-id"
localNamespace := "local"
remoteNamespace := "remote"
otherNamespace := "other"

d.LocalToRemoteNamespaceMapper[localNamespace] = remoteNamespace
d.RemoteToLocalNamespaceMapper[remoteNamespace] = localNamespace
d.RemoteToLocalNamespaceMapper[remoteNamespaceKeyer(remoteClusterID, remoteNamespace)] = localNamespace

// namespaces present in the map

mappedNamespace := d.localToRemoteNamespace(localNamespace)
assert.Equal(t, mappedNamespace, remoteNamespace, "these namespace names have to be equal")

demappedNamespace := d.remoteToLocalNamespace(mappedNamespace)
demappedNamespace := d.remoteToLocalNamespace(remoteClusterID, mappedNamespace)
assert.Equal(t, demappedNamespace, localNamespace, "these namespace names have to be equal")

// namespaces not present in the map

mappedNamespace = d.localToRemoteNamespace(otherNamespace)
assert.Equal(t, mappedNamespace, otherNamespace, "these namespace names have to be equal")

demappedNamespace = d.remoteToLocalNamespace(mappedNamespace)
demappedNamespace = d.remoteToLocalNamespace(remoteClusterID, mappedNamespace)
assert.Equal(t, demappedNamespace, otherNamespace, "these namespace names have to be equal")
}
12 changes: 8 additions & 4 deletions internal/crdReplicator/namespaceTranslation.go
Expand Up @@ -15,20 +15,20 @@ func (c *Controller) localToRemoteNamespace(namespace string) string {
if ns, ok := c.LocalToRemoteNamespaceMapper[namespace]; ok {
return ns
}
klog.V(5).Infof("local namespace %v translation not found, returning the original namespace", namespace)
klog.Warningf("local namespace %v translation not found, returning the original namespace", namespace)
return namespace
}

func (c *Controller) remoteToLocalNamespace(namespace string) string {
func (c *Controller) remoteToLocalNamespace(remoteClusterID, namespace string) string {
if namespace == "" {
// if the namespaces is empty, the resource is cluster scoped, so we do no need namespace translations
return namespace
}

if ns, ok := c.RemoteToLocalNamespaceMapper[namespace]; ok {
if ns, ok := c.RemoteToLocalNamespaceMapper[remoteNamespaceKeyer(remoteClusterID, namespace)]; ok {
return ns
}
klog.V(5).Infof("remote namespace %v translation not found, returning the original namespace", namespace)
klog.Warningf("remote namespace %v translation not found, returning the original namespace", namespace)
return namespace
}

Expand All @@ -49,3 +49,7 @@ func (c *Controller) clusterIDToLocalNamespace(clusterID string) (string, error)
klog.Error(err)
return "", err
}

func remoteNamespaceKeyer(remoteClusterID, namespace string) string {
return fmt.Sprintf("%s/%s", remoteClusterID, namespace)
}

0 comments on commit 2681ff6

Please sign in to comment.