diff --git a/pkg/liqo-controller-manager/namespaceOffloading-controller/clusterselector_management.go b/pkg/liqo-controller-manager/namespaceOffloading-controller/clusterselector_management.go index a42e22abe4..a97aaa9f74 100644 --- a/pkg/liqo-controller-manager/namespaceOffloading-controller/clusterselector_management.go +++ b/pkg/liqo-controller-manager/namespaceOffloading-controller/clusterselector_management.go @@ -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] } diff --git a/pkg/liqo-controller-manager/namespaceOffloading-controller/namespaceoffloading_controller.go b/pkg/liqo-controller-manager/namespaceOffloading-controller/namespaceoffloading_controller.go index 45bd19a854..79ef4969aa 100644 --- a/pkg/liqo-controller-manager/namespaceOffloading-controller/namespaceoffloading_controller.go +++ b/pkg/liqo-controller-manager/namespaceOffloading-controller/namespaceoffloading_controller.go @@ -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. diff --git a/pkg/liqo-controller-manager/offloadingStatus-controller/namespaceoffloading_status_management.go b/pkg/liqo-controller-manager/offloadingStatus-controller/namespaceoffloading_status_management.go index ae23848662..a6faed7e71 100644 --- a/pkg/liqo-controller-manager/offloadingStatus-controller/namespaceoffloading_status_management.go +++ b/pkg/liqo-controller-manager/offloadingStatus-controller/namespaceoffloading_status_management.go @@ -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) + } + } +} diff --git a/pkg/liqo-controller-manager/offloadingStatus-controller/offloadingstatus_controller.go b/pkg/liqo-controller-manager/offloadingStatus-controller/offloadingstatus_controller.go index 9a70512ded..bbca941298 100644 --- a/pkg/liqo-controller-manager/offloadingStatus-controller/offloadingstatus_controller.go +++ b/pkg/liqo-controller-manager/offloadingStatus-controller/offloadingstatus_controller.go @@ -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" @@ -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) @@ -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) } diff --git a/pkg/liqo-controller-manager/offloadingStatus-controller/offloadingstatus_controller_test.go b/pkg/liqo-controller-manager/offloadingStatus-controller/offloadingstatus_controller_test.go index c69547f122..10f78d1291 100644 --- a/pkg/liqo-controller-manager/offloadingStatus-controller/offloadingstatus_controller_test.go +++ b/pkg/liqo-controller-manager/offloadingStatus-controller/offloadingstatus_controller_test.go @@ -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()) + + }) + }) }) diff --git a/pkg/liqo-controller-manager/resourceoffer-controller/resourceoffer_controller.go b/pkg/liqo-controller-manager/resourceoffer-controller/resourceoffer_controller.go index 9a41c3023d..b9462f14ed 100644 --- a/pkg/liqo-controller-manager/resourceoffer-controller/resourceoffer_controller.go +++ b/pkg/liqo-controller-manager/resourceoffer-controller/resourceoffer_controller.go @@ -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.