Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
0c73e78
commit c7f625f
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
pkg/liqo-controller-manager/offloadingStatus-controller/doc.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package offloadingstatuscontroller contains OffloadingStatus Controller logic | ||
package offloadingstatuscontroller |
134 changes: 134 additions & 0 deletions
134
pkg/liqo-controller-manager/offloadingStatus-controller/offloadingStatus_controller.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |