Skip to content

Commit

Permalink
change technical namespace in tenant namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreagit97 committed Jun 30, 2021
1 parent b0edf16 commit 431848e
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 107 deletions.
1 change: 0 additions & 1 deletion Makefile
Expand Up @@ -90,7 +90,6 @@ ifeq (, $(shell which gci))
}
endif


# Run go fmt against code
fmt: gci
go fmt ./...
Expand Down
6 changes: 2 additions & 4 deletions cmd/advertisement-operator/main.go
Expand Up @@ -68,12 +68,10 @@ func init() {
_ = advtypes.AddToScheme(scheme)
_ = netv1alpha1.AddToScheme(scheme)
_ = discoveryv1alpha1.AddToScheme(scheme)

_ = capsulev1alpha1.AddToScheme(scheme)

_ = offloadingv1alpha1.AddToScheme(scheme)

_ = virtualkubeletv1alpha1.AddToScheme(scheme)

_ = capsulev1alpha1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}

Expand Down
Expand Up @@ -24,8 +24,8 @@ const (

// This function creates a remote Namespace inside the remote cluster, if it doesn't exist yet.
// The right client to use is chosen by means of NamespaceMap's cluster-id.
func (r *NamespaceMapReconciler) createRemoteNamespace(ctx context.Context, clusterID, remoteName string) error {
if err := r.checkRemoteClientPresence(ctx, clusterID); err != nil {
func (r *NamespaceMapReconciler) createRemoteNamespace(ctx context.Context, remoteClusterID, remoteName string) error {
if err := r.checkRemoteClientPresence(ctx, remoteClusterID); err != nil {
return err
}

Expand All @@ -40,20 +40,22 @@ func (r *NamespaceMapReconciler) createRemoteNamespace(ctx context.Context, clus
},
}

if err := r.RemoteClients[clusterID].Create(ctx, remoteNamespace); err != nil {
if err := r.RemoteClients[remoteClusterID].Create(ctx, remoteNamespace); err != nil {
if apierrors.IsAlreadyExists(err) {
if errGet := r.RemoteClients[clusterID].Get(ctx, types.NamespacedName{Name: remoteName}, remoteNamespace); errGet == nil {
if errGet := r.RemoteClients[remoteClusterID].Get(ctx, types.NamespacedName{Name: remoteName}, remoteNamespace); errGet == nil {
if value, ok := remoteNamespace.Annotations[annotationKey]; ok && value == remoteNamespaceAnnotationValue {
klog.Infof("the namespace '%s' has already been created inside the remote cluster '%s'",
remoteNamespace.Name, clusterID)
remoteNamespace.Name, remoteClusterID)
return nil
}
}
}
klog.Errorf("%s -> Namespace '%s' cannot be created inside cluster '%s'", err,
remoteName, remoteClusterID)
return err
}

klog.Infof("The namespace '%s' is created inside the remote cluster: '%s'", remoteNamespace.Name, clusterID)
klog.Infof("The namespace '%s' is created inside the remote cluster: '%s'", remoteNamespace.Name, remoteClusterID)
return nil
}

Expand All @@ -68,8 +70,6 @@ func (r *NamespaceMapReconciler) ensureRemoteNamespacesExistence(ctx context.Con
Phase: mapsv1alpha1.MappingCreationLoopBackOff,
}
errorCondition = true
klog.Errorf("%s -> Namespace '%s' cannot be created inside cluster '%s'", err,
localName, nm.Labels[liqoconst.RemoteClusterID])
continue
}
nm.Status.CurrentMapping[localName] = mapsv1alpha1.RemoteNamespaceStatus{
Expand All @@ -83,15 +83,15 @@ func (r *NamespaceMapReconciler) ensureRemoteNamespacesExistence(ctx context.Con
// This function deletes a remote Namespace from the remote cluster, the right client to use is chosen
// by NamespaceMap's cluster-id. This function return nil (success) only when the remote Namespace is really deleted,
// so the get Api returns a "NotFound".
func (r *NamespaceMapReconciler) deleteRemoteNamespace(ctx context.Context, clusterID, remoteName string) error {
if err := r.checkRemoteClientPresence(ctx, clusterID); err != nil {
func (r *NamespaceMapReconciler) deleteRemoteNamespace(ctx context.Context, remoteClusterID, remoteName string) error {
if err := r.checkRemoteClientPresence(ctx, remoteClusterID); err != nil {
return err
}

remoteNamespace := &corev1.Namespace{}
if err := r.RemoteClients[clusterID].Get(ctx, types.NamespacedName{Name: remoteName}, remoteNamespace); err != nil {
if err := r.RemoteClients[remoteClusterID].Get(ctx, types.NamespacedName{Name: remoteName}, remoteNamespace); err != nil {
if apierrors.IsNotFound(err) {
klog.Infof("The namespace '%s' is correctly deleted from the remote cluster: '%s'", remoteName, clusterID)
klog.Infof("The namespace '%s' is correctly deleted from the remote cluster: '%s'", remoteName, remoteClusterID)
return nil
}
klog.Errorf("unable to get the remote namespace '%s'", remoteName)
Expand All @@ -102,18 +102,18 @@ func (r *NamespaceMapReconciler) deleteRemoteNamespace(ctx context.Context, clus
annotationKey := fmt.Sprintf("%s-%s/%s", remoteNamespaceAnnotationPrefix, r.LocalClusterID, liqoSuffix)
if value, ok := remoteNamespace.Annotations[annotationKey]; !ok || value != remoteNamespaceAnnotationValue {
klog.Infof("No remote namespaces with name '%s' created through Liqo mechanism inside the remote cluster: '%s'",
remoteName, clusterID)
remoteName, remoteClusterID)
return nil
}

if remoteNamespace.DeletionTimestamp.IsZero() {
if err := r.RemoteClients[clusterID].Delete(ctx, remoteNamespace); err != nil {
klog.Errorf("unable to delete the namespace '%s' from the remote cluster: '%s'", remoteName, clusterID)
if err := r.RemoteClients[remoteClusterID].Delete(ctx, remoteNamespace); err != nil {
klog.Errorf("unable to delete the namespace '%s' from the remote cluster: '%s'", remoteName, remoteClusterID)
return err
}
klog.Infof("The deletion timestamp is correctly set on the namespace '%s'", remoteName)
}
return fmt.Errorf("the remote Namespace '%s' inside the cluster '%s' is undergoing graceful termination", remoteName, clusterID)
return fmt.Errorf("the remote Namespace '%s' inside the cluster '%s' is undergoing graceful termination", remoteName, remoteClusterID)
}

// If DesiredMapping field has less entries than CurrentMapping, is necessary to remove some remote namespaces.
Expand All @@ -130,7 +130,6 @@ func (r *NamespaceMapReconciler) ensureRemoteNamespacesDeletion(ctx context.Cont
Phase: mapsv1alpha1.MappingTerminating,
}
errorCondition = true
klog.Info(err)
continue
}
delete(nm.Status.CurrentMapping, localName)
Expand Down
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/klog"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlutils "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
Expand Down Expand Up @@ -104,7 +105,6 @@ func manageDesiredMappings() predicate.Predicate {
// SetupWithManager monitors only updates on NamespaceMap.
func (r *NamespaceMapReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&mapsv1alpha1.NamespaceMap{}).
WithEventFilter(manageDesiredMappings()).
For(&mapsv1alpha1.NamespaceMap{}, builder.WithPredicates(manageDesiredMappings())).
Complete(r)
}
Expand Up @@ -59,8 +59,7 @@ func (r *NamespaceOffloadingReconciler) initialConfiguration(ctx context.Context
Key: liqoconst.TypeLabel,
Operator: corev1.NodeSelectorOpIn,
Values: []string{liqoconst.TypeNode},
},
},
}},
},
}}
}
Expand Down
Expand Up @@ -20,7 +20,7 @@ func (r *VirtualNodeReconciler) createNamespaceMap(ctx context.Context, n *corev
nm := &mapsv1alpha1.NamespaceMap{
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-", n.Annotations[liqoconst.RemoteClusterID]),
Namespace: r.getLocalTechnicalNamespaceName(n.Annotations[liqoconst.RemoteClusterID]),
Namespace: r.getLocalTenantNamespaceName(n.Annotations[liqoconst.RemoteClusterID]),
Labels: map[string]string{
liqoconst.RemoteClusterID: n.Annotations[liqoconst.RemoteClusterID],
},
Expand All @@ -42,12 +42,12 @@ func (r *VirtualNodeReconciler) createNamespaceMap(ctx context.Context, n *corev
// ensureNamespaceMapPresence creates a new NamespaceMap associated with that virtual-node if it is not already present.
func (r *VirtualNodeReconciler) ensureNamespaceMapPresence(ctx context.Context, n *corev1.Node) error {
// Only when the NamespaceMap is created for the first time it is necessary to check the presence of the local
// Technical namespace's name.
if err := r.checkLocalTechnicalNamespaceNamePresence(ctx, n.Annotations[liqoconst.RemoteClusterID]); err != nil {
// Tenant namespace's name.
if err := r.checkLocalTenantNamespaceNamePresence(ctx, n.Annotations[liqoconst.RemoteClusterID]); err != nil {
return err
}
nms := &mapsv1alpha1.NamespaceMapList{}
if err := r.List(ctx, nms, client.InNamespace(r.getLocalTechnicalNamespaceName(n.Annotations[liqoconst.RemoteClusterID])),
if err := r.List(ctx, nms, client.InNamespace(r.getLocalTenantNamespaceName(n.Annotations[liqoconst.RemoteClusterID])),
client.MatchingLabels{liqoconst.RemoteClusterID: n.Annotations[liqoconst.RemoteClusterID]}); err != nil {
klog.Errorf("%s --> Unable to List NamespaceMaps of the virtual-node '%s'", err, n.GetName())
return err
Expand All @@ -60,35 +60,35 @@ func (r *VirtualNodeReconciler) ensureNamespaceMapPresence(ctx context.Context,
return nil
}

// checkLocalTechnicalNamespaceNamePresence checks if the local technical namespace's name for the cluster with
// `remoteClusterID` clusterID is already present in the map r.LocalTechnicalNamespacesNames.
func (r *VirtualNodeReconciler) checkLocalTechnicalNamespaceNamePresence(ctx context.Context, remoteClusterID string) error {
if r.LocalTechnicalNamespacesNames == nil {
r.LocalTechnicalNamespacesNames = map[string]string{}
// checkLocalTenantNamespaceNamePresence checks if the local tenant namespace's name for the cluster with
// `remoteClusterID` clusterID is already present in the map r.LocalTenantNamespacesNames.
func (r *VirtualNodeReconciler) checkLocalTenantNamespaceNamePresence(ctx context.Context, remoteClusterID string) error {
if r.LocalTenantNamespacesNames == nil {
r.LocalTenantNamespacesNames = map[string]string{}
}

if _, ok := r.LocalTechnicalNamespacesNames[remoteClusterID]; !ok {
if _, ok := r.LocalTenantNamespacesNames[remoteClusterID]; !ok {
fc, err := foreignclusterutils.GetForeignClusterByID(ctx, r.Client, remoteClusterID)
if err != nil {
return err
}

if fc.Status.TenantControlNamespace.Local == "" {
err = fmt.Errorf("there is no technical namespace associated with the peering with the remote cluster '%s'",
err = fmt.Errorf("there is no tenant namespace associated with the peering with the remote cluster '%s'",
remoteClusterID)
klog.Error(err)
return err
}

r.LocalTechnicalNamespacesNames[remoteClusterID] = fc.Status.TenantControlNamespace.Local
klog.Infof("The Technical namespace '%s' associated with the peering with the remote cluster '%s' is added to the Map",
r.LocalTenantNamespacesNames[remoteClusterID] = fc.Status.TenantControlNamespace.Local
klog.Infof("The Tenant namespace '%s' associated with the peering with the remote cluster '%s' is added to the Map",
fc.Status.TenantControlNamespace.Local, remoteClusterID)
}
return nil
}

// getLocalTechnicalNamespaceName provides the name of the local technical namespace, given the remoteClusterID
// getLocalTenantNamespaceName provides the name of the local tenant namespace, given the remoteClusterID
// associated with a peering.
func (r *VirtualNodeReconciler) getLocalTechnicalNamespaceName(remoteClusterID string) string {
return r.LocalTechnicalNamespacesNames[remoteClusterID]
func (r *VirtualNodeReconciler) getLocalTenantNamespaceName(remoteClusterID string) string {
return r.LocalTenantNamespacesNames[remoteClusterID]
}
80 changes: 40 additions & 40 deletions pkg/liqo-controller-manager/virtualNode-controller/suite_test.go
Expand Up @@ -33,7 +33,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"

discoveryV1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
mapsv1alpha1 "github.com/liqotech/liqo/apis/virtualKubelet/v1alpha1"
liqoconst "github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/discovery"
Expand All @@ -50,28 +50,28 @@ const (
nameVirtualNode1 = "virtual-node-1"
nameVirtualNode2 = "virtual-node-2"
nameSimpleNode = "simple-node"
remoteClusterId1 = "6a0e9f-b52-4ed0"
remoteClusterId2 = "899890-dsd-323"
remoteClusterID1 = "6a0e9f-b52-4ed0"
remoteClusterID2 = "899890-dsd-323"
remoteClusterIdSimpleNode = "909030-sd-3231"
technicalNamespaceNameID1 = "liqo-technical-namespace-1"
technicalNamespaceNameID2 = "liqo-technical-namespace-2"
tenantNamespaceNameID1 = "liqo-tenant-namespace-1"
tenantNamespaceNameID2 = "liqo-tenant-namespace-2"
offloadingCluster1Label1 = "offloading.liqo.io/cluster-1"
offloadingCluster1Label2 = "offloading.liqo.io/AWS"
timeout = time.Second * 10
interval = time.Millisecond * 250
)

var (
nms *mapsv1alpha1.NamespaceMapList
virtualNode1 *corev1.Node
virtualNode2 *corev1.Node
simpleNode *corev1.Node
technicalNamespace1 *corev1.Namespace
technicalNamespace2 *corev1.Namespace
foreignCluster1 *discoveryV1alpha1.ForeignCluster
foreignCluster2 *discoveryV1alpha1.ForeignCluster
flags *flag.FlagSet
buffer *bytes.Buffer
nms *mapsv1alpha1.NamespaceMapList
virtualNode1 *corev1.Node
virtualNode2 *corev1.Node
simpleNode *corev1.Node
tenantNamespace1 *corev1.Namespace
tenantNamespace2 *corev1.Namespace
foreignCluster1 *discoveryv1alpha1.ForeignCluster
foreignCluster2 *discoveryv1alpha1.ForeignCluster
flags *flag.FlagSet
buffer *bytes.Buffer
)

func TestAPIs(t *testing.T) {
Expand Down Expand Up @@ -108,7 +108,7 @@ var _ = BeforeSuite(func(done Done) {
err = mapsv1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

err = discoveryV1alpha1.AddToScheme(scheme.Scheme)
err = discoveryv1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
// +kubebuilder:scaffold:scheme

Expand All @@ -134,57 +134,57 @@ var _ = BeforeSuite(func(done Done) {

nms = &mapsv1alpha1.NamespaceMapList{}

technicalNamespace1 = &corev1.Namespace{
tenantNamespace1 = &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: technicalNamespaceNameID1,
Name: tenantNamespaceNameID1,
},
}

technicalNamespace2 = &corev1.Namespace{
tenantNamespace2 = &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: technicalNamespaceNameID2,
Name: tenantNamespaceNameID2,
},
}

foreignCluster1 = &discoveryV1alpha1.ForeignCluster{
foreignCluster1 = &discoveryv1alpha1.ForeignCluster{
ObjectMeta: metav1.ObjectMeta{
Name: remoteClusterId1,
Name: remoteClusterID1,
Labels: map[string]string{
discovery.ClusterIDLabel: remoteClusterId1,
discovery.ClusterIDLabel: remoteClusterID1,
},
},
}

foreignCluster2 = &discoveryV1alpha1.ForeignCluster{
foreignCluster2 = &discoveryv1alpha1.ForeignCluster{
ObjectMeta: metav1.ObjectMeta{
Name: remoteClusterId2,
Name: remoteClusterID2,
Labels: map[string]string{
discovery.ClusterIDLabel: remoteClusterId2,
discovery.ClusterIDLabel: remoteClusterID2,
},
},
}

// create the 2 technical namespaces and the foreignClusters.
// create the 2 tenant namespaces and the foreignClusters.
Expect(k8sClient.Create(context.TODO(), foreignCluster1)).To(Succeed())
Expect(k8sClient.Create(context.TODO(), foreignCluster2)).To(Succeed())
Expect(k8sClient.Create(context.TODO(), technicalNamespace1)).To(Succeed())
Expect(k8sClient.Create(context.TODO(), technicalNamespace2)).To(Succeed())

fc := &discoveryV1alpha1.ForeignCluster{}
Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: remoteClusterId1}, fc)).To(Succeed())
fc.Status = discoveryV1alpha1.ForeignClusterStatus{
TenantControlNamespace: discoveryV1alpha1.TenantControlNamespace{
Local: technicalNamespaceNameID1,
Expect(k8sClient.Create(context.TODO(), tenantNamespace1)).To(Succeed())
Expect(k8sClient.Create(context.TODO(), tenantNamespace2)).To(Succeed())

fc := &discoveryv1alpha1.ForeignCluster{}
Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: remoteClusterID1}, fc)).To(Succeed())
fc.Status = discoveryv1alpha1.ForeignClusterStatus{
TenantControlNamespace: discoveryv1alpha1.TenantControlNamespace{
Local: tenantNamespaceNameID1,
Remote: "remote",
},
}
Expect(k8sClient.Status().Update(context.TODO(), fc)).To(Succeed())

fc = &discoveryV1alpha1.ForeignCluster{}
Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: remoteClusterId2}, fc)).To(Succeed())
fc.Status = discoveryV1alpha1.ForeignClusterStatus{
TenantControlNamespace: discoveryV1alpha1.TenantControlNamespace{
Local: technicalNamespaceNameID2,
fc = &discoveryv1alpha1.ForeignCluster{}
Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: remoteClusterID2}, fc)).To(Succeed())
fc.Status = discoveryv1alpha1.ForeignClusterStatus{
TenantControlNamespace: discoveryv1alpha1.TenantControlNamespace{
Local: tenantNamespaceNameID2,
Remote: "remote",
},
}
Expand Down
Expand Up @@ -41,8 +41,8 @@ const (
type VirtualNodeReconciler struct {
client.Client
Scheme *runtime.Scheme
// key = clusterID, value = technicalNamesapceName
LocalTechnicalNamespacesNames map[string]string
// key = clusterID, value = tenantNamesapceName
LocalTenantNamespacesNames map[string]string
}

// cluster-role
Expand Down

0 comments on commit 431848e

Please sign in to comment.