Skip to content

Commit

Permalink
New OffloadingStatus Controller
Browse files Browse the repository at this point in the history
This new controller waits for all remote conditions of the offloaded namespace, and when the controller is able to get all of them, it sets the global offloading status according to these remote conditions.
  • Loading branch information
Andreagit97 committed May 24, 2021
1 parent 0c73e78 commit c7f625f
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/consts/namespaceMapping.go
Expand Up @@ -13,4 +13,11 @@ const (
NamespaceMapControllerFinalizer = "namespacemap-controller.liqo.io/finalizer"
// DocumentationURL is the URL to official Liqo Documentation.
DocumentationURL = "https://doc.liqo.io/"
// DefaultNameNamespaceOffloading is the default name of NamespaceOffloading resources, every namespace that have
// to be offloaded with Liqo, must have a NamespaceOffloading resource with this name.
DefaultNameNamespaceOffloading = "offloading"
// EnablingLiqoLabel is necessary in order to allow Pods to be scheduled on remote clusters.
EnablingLiqoLabel = "liqo.io/enabled"
// EnablingLiqoLabelValue unique value allowed for EnablingLiqoLabel.
EnablingLiqoLabelValue = "true"
)
@@ -0,0 +1,2 @@
// Package offloadingstatuscontroller contains OffloadingStatus Controller logic
package offloadingstatuscontroller
@@ -0,0 +1,134 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package offloadingstatuscontroller

import (
"context"
"reflect"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog"
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"
liqoconst "github.com/liqotech/liqo/pkg/consts"
liqoutils "github.com/liqotech/liqo/pkg/utils"
)

// OffloadingStatusReconciler checks the status of all remote namespaces associated with this
// namespaceOffloading Resource, and sets the global offloading status only when there is a feedback
// from all remote clusters.
type OffloadingStatusReconciler struct {
client.Client
Scheme *runtime.Scheme
RemoteClustersNumber int
}

// Reconcile checks if all remote Namespaces have been created with success or not.
func (r *OffloadingStatusReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
namespaceOffloading := &offv1alpha1.NamespaceOffloading{}
if err := r.Get(ctx, types.NamespacedName{
Namespace: req.Namespace,
Name: liqoconst.DefaultNameNamespaceOffloading,
}, namespaceOffloading); err != nil {
if apierrors.IsNotFound(err) {
klog.Infof("There is no NamespaceOffloading resource in Namespace '%s'", req.Namespace)
return ctrl.Result{}, nil
}
klog.Errorf("%s --> Unable to get namespaceOffloading for the namespace '%s'", err, req.Namespace)
return ctrl.Result{}, err
}

if r.RemoteClustersNumber == 0 {
virtualNodes := &corev1.NodeList{}
if err := r.List(context.TODO(), virtualNodes,
client.MatchingLabels{liqoconst.TypeLabel: liqoconst.TypeNode}); err != nil {
klog.Error(err, " --> Unable to List all virtual nodes")
return ctrl.Result{}, err
}
if len(virtualNodes.Items) == 0 {
klog.Info("No VirtualNodes at the moment in the cluster")
return ctrl.Result{}, nil
}
r.RemoteClustersNumber = len(virtualNodes.Items)
}

// if every remote Namespace has a condition
if len(namespaceOffloading.Status.RemoteNamespacesConditions) == r.RemoteClustersNumber {
ready := 0
notReady := 0

for i := range namespaceOffloading.Status.RemoteNamespacesConditions {
condition := liqoutils.GetNamespaceOffloadingCondition(namespaceOffloading.Status.RemoteNamespacesConditions[i],
offv1alpha1.NamespaceReady)
if condition == nil {
continue
}
if condition.Status == corev1.ConditionTrue {
ready++
} else {
notReady++
}
}

switch {
case ready == 0:
namespaceOffloading.Status.OffloadingPhase = offv1alpha1.AllFailedOffloadingPhaseType
case notReady == 0:
namespaceOffloading.Status.OffloadingPhase = offv1alpha1.ReadyOffloadingPhaseType
default:
namespaceOffloading.Status.OffloadingPhase = offv1alpha1.SomeFailedOffloadingPhaseType
}

if err := r.Update(ctx, namespaceOffloading); err != nil {
klog.Errorf("%s --> Unable to update namespaceOffloading for the namespace '%s'",
err, namespaceOffloading.Namespace)
return ctrl.Result{}, err
}
}

return ctrl.Result{}, nil
}

func checkNamespaceOffloadingStatus() predicate.Predicate {
return predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
// reconciles only when RemoteNamespacesConditions are updated
return !reflect.DeepEqual(e.ObjectOld.(*offv1alpha1.NamespaceOffloading).Status.RemoteNamespacesConditions,
e.ObjectNew.(*offv1alpha1.NamespaceOffloading).Status.RemoteNamespacesConditions) &&
e.ObjectNew.(*offv1alpha1.NamespaceOffloading).Status.OffloadingPhase != offv1alpha1.NoClusterSelectedOffloadingPhaseType
},
CreateFunc: func(e event.CreateEvent) bool {
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
return false
},
}
}

// SetupWithManager reconciles only on NamespaceOffloading Status updates.
func (r *OffloadingStatusReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&offv1alpha1.NamespaceOffloading{}).
WithEventFilter(checkNamespaceOffloadingStatus()).
Complete(r)
}

0 comments on commit c7f625f

Please sign in to comment.