Skip to content

Commit

Permalink
Deletion of remote namespace conditions in the NamespaceOffloading Re…
Browse files Browse the repository at this point in the history
…source

This commit allows a better management for the remoteNamespaceConditions deletion during the peering deletion phase.
  • Loading branch information
Andreagit97 authored and adamjensenbot committed Jul 15, 2021
1 parent adbb394 commit 7200a77
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func (r *NamespaceOffloadingReconciler) getClusterIDMap(ctx context.Context) (ma
return nil, err
}

clusterIDMap := make(map[string]*mapsv1alpha1.NamespaceMap)
if len(nms.Items) == 0 {
klog.Info("No NamespaceMaps at the moment in the cluster")
return nil, nil
return clusterIDMap, nil
}

clusterIDMap := make(map[string]*mapsv1alpha1.NamespaceMap)
for i := range nms.Items {
clusterIDMap[nms.Items[i].Labels[liqoconst.RemoteClusterID]] = &nms.Items[i]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ func (r *NamespaceOffloadingReconciler) Reconcile(ctx context.Context, req ctrl.
if err != nil {
return ctrl.Result{}, err
}
// There are no NamespaceMap in the cluster
if namespaceMapNumber == 0 {
return ctrl.Result{}, nil
}

// If deletion timestamp is set, it starts deletion logic and waits until all remote Namespaces
// associated with this resource are deleted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,22 @@ func setNamespaceOffloadingStatus(noff *offv1alpha1.NamespaceOffloading) {
klog.Infof("The OffloadingStatus for the NamespaceOffloading in the namespace '%s' is set to '%s'",
noff.Namespace, noff.Status.OffloadingPhase)
}

// ensureRemoteConditionsConsistence checks for every remote condition of the NamespaceOffloading resource that the
// corresponding NamespaceMap is still there. If the peering is deleted also the corresponding remote condition
// must be deleted.
func ensureRemoteConditionsConsistence(noff *offv1alpha1.NamespaceOffloading, nml *mapsv1alpha1.NamespaceMapList) {
for remoteClusterID := range noff.Status.RemoteNamespacesConditions {
presence := false
for i := range nml.Items {
if nodeClusterID, ok := nml.Items[i].Labels[liqoconst.RemoteClusterID]; ok && remoteClusterID == nodeClusterID {
presence = true
break
}
}
if !presence {
delete(noff.Status.RemoteNamespacesConditions, remoteClusterID)
klog.Infof("The remoteNamespaceCondition for the remote cluster '%s' is no more necessary", remoteClusterID)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"

offv1alpha1 "github.com/liqotech/liqo/apis/offloading/v1alpha1"
mapsv1alpha1 "github.com/liqotech/liqo/apis/virtualKubelet/v1alpha1"
Expand Down Expand Up @@ -72,13 +70,10 @@ func (r *OffloadingStatusReconciler) Reconcile(ctx context.Context, req ctrl.Req
return ctrl.Result{}, err
}

if len(namespaceMapList.Items) == 0 {
klog.Info("No NamespaceMaps in the cluster at the moment")
return ctrl.Result{RequeueAfter: r.RequeueTime}, nil
}

original := namespaceOffloading.DeepCopy()

ensureRemoteConditionsConsistence(namespaceOffloading, namespaceMapList)

setRemoteConditionsForEveryCluster(namespaceOffloading, namespaceMapList)

setNamespaceOffloadingStatus(namespaceOffloading)
Expand All @@ -93,19 +88,9 @@ func (r *OffloadingStatusReconciler) Reconcile(ctx context.Context, req ctrl.Req
return ctrl.Result{RequeueAfter: r.RequeueTime}, nil
}

// checkNamespaceOffloadingStatus calls Reconcile only when a new NamespaceOffloading is created.
func checkNamespaceOffloadingStatus() predicate.Predicate {
return predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
return false
},
}
}

// SetupWithManager reconciles when a new NamespaceOffloading is created.
func (r *OffloadingStatusReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&offv1alpha1.NamespaceOffloading{}).
WithEventFilter(checkNamespaceOffloadingStatus()).
Complete(r)
}
Original file line number Diff line number Diff line change
Expand Up @@ -399,5 +399,66 @@ var _ = Describe("Namespace controller", func() {

})

It(" TEST 6: Delete a NamespaceMap and check if the corresponding remote condition is deleted", func() {

// The namespace name is associated with the test number
namespace6Name := "namespace6"
By(fmt.Sprintf(" 1 - Creating the namespace '%s'", namespace6Name))
namespace6 := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace6Name,
},
}
Expect(homeClient.Create(context.TODO(), namespace6)).To(BeNil())

By(" 2 - Creating the associated NamespaceOffloading")
namespaceOffloading6 := &offv1alpha1.NamespaceOffloading{
ObjectMeta: metav1.ObjectMeta{
Name: liqoconst.DefaultNamespaceOffloadingName,
Namespace: namespace6Name,
},
Spec: offv1alpha1.NamespaceOffloadingSpec{
NamespaceMappingStrategy: offv1alpha1.EnforceSameNameMappingStrategyType,
PodOffloadingStrategy: offv1alpha1.LocalAndRemotePodOffloadingStrategyType,
ClusterSelector: corev1.NodeSelector{NodeSelectorTerms: []corev1.NodeSelectorTerm{}},
},
}
Expect(homeClient.Create(context.TODO(), namespaceOffloading6)).To(BeNil())

By(" 3 - Get NamespaceMap associated to remote cluster 2 and delete it")
Eventually(func() error {
if err := homeClient.List(context.TODO(), nms, client.MatchingLabels{liqoconst.RemoteClusterID: remoteClusterId2}); err != nil {
return err
}
if len(nms.Items) == 0 {
return nil
}
_ = homeClient.Delete(context.TODO(), nms.Items[0].DeepCopy())
return fmt.Errorf("the namespaceMap deletion is still in progress")
}, timeout, interval).Should(BeNil())

By(" 4 - Checking Terminating status of the NamespaceOffloading and the remote conditions")
namespaceOffloading6 = &offv1alpha1.NamespaceOffloading{}
Eventually(func() error {
if err := homeClient.Get(context.TODO(), types.NamespacedName{
Name: liqoconst.DefaultNamespaceOffloadingName,
Namespace: namespace6Name}, namespaceOffloading6); err != nil {
return err
}
if len(namespaceOffloading6.Status.RemoteNamespacesConditions) != mapNumber-1 {
return fmt.Errorf("there are still '%d' remoteNamespaceCondition",
len(namespaceOffloading6.Status.RemoteNamespacesConditions))
}
if len(namespaceOffloading6.Status.RemoteNamespacesConditions[remoteClusterId1]) != 1 {
return fmt.Errorf("the remote condition associated with the namespaceMap 1 is not present")
}
if len(namespaceOffloading6.Status.RemoteNamespacesConditions[remoteClusterId3]) != 1 {
return fmt.Errorf("the remote condition associated with the namespaceMap 3 is not present")
}
return nil
}, timeout, interval).Should(BeNil())

})

})
})
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,19 @@ func (r *ResourceOfferReconciler) Reconcile(ctx context.Context, req ctrl.Reques
case kubeletDeletePhaseDrainingNode:
// set virtual kubelet in deleting phase
resourceOffer.Status.VirtualKubeletStatus = sharingv1alpha1.VirtualKubeletStatusDeleting
return result, nil
case kubeletDeletePhaseNone:
break
// create the virtual kubelet deployment
if err = r.createVirtualKubeletDeployment(ctx, &resourceOffer); err != nil {
klog.Error(err)
return ctrl.Result{}, err
}
return result, nil
default:
err = fmt.Errorf("unknown deleting phase %v", deletingPhase)
klog.Error(err)
return result, err
}

// create the virtual kubelet deployment
if err = r.createVirtualKubeletDeployment(ctx, &resourceOffer); err != nil {
klog.Error(err)
return ctrl.Result{}, err
}

return result, nil
}

// SetupWithManager sets up the controller with the Manager.
Expand Down

0 comments on commit 7200a77

Please sign in to comment.