Skip to content

Commit

Permalink
Refactor of VirtualNode-controller.
Browse files Browse the repository at this point in the history
This commit allows to manage in a simpler way the NamespaceMap lifecycle.
  • Loading branch information
Andreagit97 committed Jun 22, 2021
1 parent 3c09091 commit c85ce9d
Show file tree
Hide file tree
Showing 10 changed files with 496 additions and 736 deletions.
4 changes: 2 additions & 2 deletions pkg/consts/namespace_mapping.go
Expand Up @@ -3,8 +3,8 @@ package consts
const (
// RemoteClusterID is used to obtain cluster-id from different Liqo resources.
RemoteClusterID = "cluster-id" // "remote.liqo.io/clusterId"
// MapNamespaceName is the namespace where NamespaceMap are created.
MapNamespaceName = "default"
// TechnicalNamespace is the namespace where NamespaceMap are created.
TechnicalNamespace = "default"
// TypeLabel is the key of a Liqo label that identifies different types of nodes.
TypeLabel = "liqo.io/type"
// TypeNode is the value of a Liqo label that identifies Liqo virtual nodes.
Expand Down
@@ -0,0 +1,34 @@
package virtualnodectrl

import (
"context"

corev1 "k8s.io/api/core/v1"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlutils "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

func (r *VirtualNodeReconciler) checkVirtualNodeFinalizerPresence(ctx context.Context, n *corev1.Node) error {
if !ctrlutils.ContainsFinalizer(n, virtualNodeControllerFinalizer) {
original := n.DeepCopy()
ctrlutils.AddFinalizer(n, virtualNodeControllerFinalizer)
if err := r.Patch(ctx, n, client.MergeFrom(original)); err != nil {
klog.Errorf("%s --> Unable to add finalizer to the virtual-node '%s'", err, n.GetName())
return err
}
klog.Infof("Finalizer correctly added on the virtual-node '%s'", n.GetName())
}
return nil
}

func (r *VirtualNodeReconciler) removeVirtualNodeFinalizer(ctx context.Context, n *corev1.Node) error {
original := n.DeepCopy()
ctrlutils.RemoveFinalizer(n, virtualNodeControllerFinalizer)
if err := r.Patch(ctx, n, client.MergeFrom(original)); err != nil {
klog.Errorf("%s --> Unable to remove finalizer from the virtual-node '%s'", err, n.GetName())
return err
}
klog.Infof("Finalizer is correctly removed from the virtual-node '%s'", n.GetName())
return nil
}
@@ -0,0 +1,56 @@
package virtualnodectrl

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
ctrlutils "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

mapsv1alpha1 "github.com/liqotech/liqo/apis/virtualKubelet/v1alpha1"
liqoconst "github.com/liqotech/liqo/pkg/consts"

"sigs.k8s.io/controller-runtime/pkg/client"
)

// create a new NamespaceMap with OwnerReference.
func (r *VirtualNodeReconciler) createNamespaceMap(ctx context.Context, n *corev1.Node) error {
nm := &mapsv1alpha1.NamespaceMap{
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-", n.GetAnnotations()[liqoconst.RemoteClusterID]),
Namespace: liqoconst.TechnicalNamespace,
Labels: map[string]string{
liqoconst.RemoteClusterID: n.GetAnnotations()[liqoconst.RemoteClusterID],
},
},
}

if err := ctrlutils.SetControllerReference(n, nm, r.Scheme); err != nil {
return err
}

if err := r.Create(ctx, nm); err != nil {
klog.Errorf("%s --> Problems in NamespaceMap creation for the virtual node '%s'", err, n.GetName())
return err
}
klog.Infof(" Create the NamespaceMap '%s' for the virtual node '%s'", nm.GetName(), n.GetName())
return nil
}

// This function manages NamespaceMaps Lifecycle on the basis of NamespaceMaps' number.
func (r *VirtualNodeReconciler) checkNamespaceMapPresence(ctx context.Context, n *corev1.Node) error {
nms := &mapsv1alpha1.NamespaceMapList{}
if err := r.List(ctx, nms, client.InNamespace(liqoconst.TechnicalNamespace),
client.MatchingLabels{liqoconst.RemoteClusterID: n.GetAnnotations()[liqoconst.RemoteClusterID]}); err != nil {
klog.Errorf("%s --> Unable to List NamespaceMaps of virtual-node '%s'", err, n.GetName())
return err
}

if len(nms.Items) == 0 {
return r.createNamespaceMap(ctx, n)
}

return nil
}

This file was deleted.

0 comments on commit c85ce9d

Please sign in to comment.