Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This controller gets a just created NamespaceOffloading resource and adds entries to NamespaceMaps (Spec->DesiredMappings) in order to require the creation of remote namespaces, based on requirements specified by users. When the NamespaceOffloading resource is deleted also the remote namespaces are requested to be deleted, so the entries in NamespaceMaps are removed.
- Loading branch information
1 parent
58a7dbd
commit 703c0d0
Showing
14 changed files
with
1,394 additions
and
56 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
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
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
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
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
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
85 changes: 85 additions & 0 deletions
85
pkg/liqo-controller-manager/namespaceOffloading-controller/clusterselector_management.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,85 @@ | ||
package namespaceoffloadingctrl | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
k8shelper "k8s.io/component-helpers/scheduling/corev1" | ||
"k8s.io/klog" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
offv1alpha1 "github.com/liqotech/liqo/apis/offloading/v1alpha1" | ||
mapsv1alpha1 "github.com/liqotech/liqo/apis/virtualKubelet/v1alpha1" | ||
liqoconst "github.com/liqotech/liqo/pkg/consts" | ||
) | ||
|
||
func (r *NamespaceOffloadingReconciler) selectCompliantVirtualNodes(noff *offv1alpha1.NamespaceOffloading, | ||
clusterIDMap map[string]*mapsv1alpha1.NamespaceMap) error { | ||
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 err | ||
} | ||
|
||
// If here there are no virtual nodes is an error because it means that in the cluster there are NamespaceMap | ||
// but not their associated virtual nodes | ||
if len(virtualNodes.Items) == 0 { | ||
err := fmt.Errorf(" No VirtualNodes at the moment in the cluster") | ||
klog.Info(err) | ||
return err | ||
} | ||
|
||
errorCondition := false | ||
for i := range virtualNodes.Items { | ||
match, err := k8shelper.MatchNodeSelectorTerms(&virtualNodes.Items[i], &noff.Spec.ClusterSelector) | ||
if err != nil { | ||
klog.Infof("%s -> Unable to offload the namespace '%s', there is an error in ClusterSelectorField", | ||
err, noff.Namespace) | ||
patch := noff.DeepCopy() | ||
if noff.Annotations == nil { | ||
noff.Annotations = map[string]string{} | ||
} | ||
noff.Annotations[liqoconst.SchedulingLiqoLabel] = fmt.Sprintf("Invalid Cluster Selector : %s", err) | ||
if err = r.Patch(context.TODO(), noff, client.MergeFrom(patch)); err != nil { | ||
klog.Errorf("%s -> unable to add the liqo scheduling annotation to the NamespaceOffloading in the namespace '%s'", | ||
err, noff.Namespace) | ||
return err | ||
} | ||
klog.Infof("The liqo scheduling annotation is correctly added to the NamespaceOffloading in the namespace '%s'", | ||
noff.Namespace) | ||
break | ||
} | ||
if match { | ||
if err = addDesiredMapping(r.Client, noff.Namespace, noff.Status.RemoteNamespaceName, | ||
clusterIDMap[virtualNodes.Items[i].Annotations[liqoconst.RemoteClusterID]]); err != nil { | ||
errorCondition = true | ||
continue | ||
} | ||
delete(clusterIDMap, virtualNodes.Items[i].Annotations[liqoconst.RemoteClusterID]) | ||
} | ||
} | ||
if errorCondition { | ||
err := fmt.Errorf("some desiredMappings has not been added") | ||
klog.Error(err) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (r *NamespaceOffloadingReconciler) enforceClusterSelector(noff *offv1alpha1.NamespaceOffloading, | ||
clusterIDMap map[string]*mapsv1alpha1.NamespaceMap) error { | ||
if noff.Spec.ClusterSelector.Size() == 0 { | ||
klog.Infof(" The namespace '%s' is requested to be offloaded on all remote clusters", noff.Namespace) | ||
if err := addDesiredMappings(r.Client, noff.Namespace, noff.Status.RemoteNamespaceName, clusterIDMap); err != nil { | ||
return err | ||
} | ||
for key := range clusterIDMap { | ||
delete(clusterIDMap, key) | ||
} | ||
return nil | ||
} | ||
|
||
return r.selectCompliantVirtualNodes(noff, clusterIDMap) | ||
} |
3 changes: 3 additions & 0 deletions
3
pkg/liqo-controller-manager/namespaceOffloading-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,3 @@ | ||
// Package namespaceoffloadingctrl contains NamespaceOffloading Controller logic and some functions for adding | ||
// DesiredMappings to NamespaceMaps Spec | ||
package namespaceoffloadingctrl |
90 changes: 90 additions & 0 deletions
90
pkg/liqo-controller-manager/namespaceOffloading-controller/manage_desiredmappings.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,90 @@ | ||
/* | ||
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 namespaceoffloadingctrl | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"k8s.io/klog" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
mapsv1alpha1 "github.com/liqotech/liqo/apis/virtualKubelet/v1alpha1" | ||
) | ||
|
||
// Removes right entry from one NamespaceMap, if present. | ||
func removeDesiredMapping(c client.Client, localName string, nm *mapsv1alpha1.NamespaceMap) error { | ||
if _, ok := nm.Spec.DesiredMapping[localName]; ok { | ||
patch := nm.DeepCopy() | ||
delete(nm.Spec.DesiredMapping, localName) | ||
if err := c.Patch(context.TODO(), nm, client.MergeFrom(patch)); err != nil { | ||
klog.Errorf("%s --> Unable to patch NamespaceMap '%s'", err, nm.GetName()) | ||
return err | ||
} | ||
klog.Infof(" Entry for the namespace '%s' is correctly deleted from the NamespaceMap '%s'", nm.GetName(), nm.GetName()) | ||
} | ||
return nil | ||
} | ||
|
||
// Removes right entries from more than one NamespaceMap (it depends on len(nms)). | ||
func removeDesiredMappings(c client.Client, localName string, nms map[string]*mapsv1alpha1.NamespaceMap) error { | ||
errorCondition := false | ||
for _, nm := range nms { | ||
if err := removeDesiredMapping(c, localName, nm); err != nil { | ||
errorCondition = true | ||
} | ||
} | ||
if errorCondition { | ||
err := fmt.Errorf("some desiredMappings has not been deleted") | ||
klog.Error(err) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Adds right entry on one NamespaceMap, if it isn't already there. | ||
func addDesiredMapping(c client.Client, localName, remoteName string, | ||
nm *mapsv1alpha1.NamespaceMap) error { | ||
if nm.Spec.DesiredMapping == nil { | ||
nm.Spec.DesiredMapping = map[string]string{} | ||
} | ||
|
||
if _, ok := nm.Spec.DesiredMapping[localName]; !ok { | ||
patch := nm.DeepCopy() | ||
nm.Spec.DesiredMapping[localName] = remoteName | ||
if err := c.Patch(context.TODO(), nm, client.MergeFrom(patch)); err != nil { | ||
klog.Errorf("%s --> Unable to add entry for namespace '%s' on NamespaceMap '%s'", | ||
err, localName, nm.GetName()) | ||
return err | ||
} | ||
klog.Infof("Entry for the namespace '%s' is successfully added on the NamespaceMap '%s' ", localName, nm.GetName()) | ||
} | ||
return nil | ||
} | ||
|
||
// Adds right entries on more than one NamespaceMap (it depends on len(nms)). | ||
func addDesiredMappings(c client.Client, localName, remoteName string, | ||
nms map[string]*mapsv1alpha1.NamespaceMap) error { | ||
errorCondition := false | ||
for _, nm := range nms { | ||
if err := addDesiredMapping(c, localName, remoteName, nm); err != nil { | ||
errorCondition = true | ||
} | ||
} | ||
if errorCondition { | ||
err := fmt.Errorf("some desiredMappings has not been added") | ||
klog.Error(err) | ||
return err | ||
} | ||
return nil | ||
} |
51 changes: 51 additions & 0 deletions
51
pkg/liqo-controller-manager/namespaceOffloading-controller/manage_liqo_scheduling_label.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,51 @@ | ||
package namespaceoffloadingctrl | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/klog" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
liqoconst "github.com/liqotech/liqo/pkg/consts" | ||
) | ||
|
||
func addLiqoSchedulingLabel(ctx context.Context, c client.Client, namespaceName string) error { | ||
namespace := &corev1.Namespace{} | ||
if err := c.Get(ctx, types.NamespacedName{Name: namespaceName}, namespace); err != nil { | ||
klog.Errorf("%s --> Unable to get the namespace '%s'", err, namespaceName) | ||
return err | ||
} | ||
|
||
if value, ok := namespace.Labels[liqoconst.SchedulingLiqoLabel]; !ok || value != liqoconst.SchedulingLiqoLabelValue { | ||
if namespace.Labels == nil { | ||
namespace.Labels = map[string]string{} | ||
} | ||
namespace.Labels[liqoconst.SchedulingLiqoLabel] = liqoconst.SchedulingLiqoLabelValue | ||
if err := c.Update(ctx, namespace); err != nil { | ||
klog.Errorf(" %s --> Unable to add liqo scheduling label to the namespace '%s'", err, namespace.GetName()) | ||
return err | ||
} | ||
klog.Infof(" Liqo scheduling label successfully added to the namespace '%s'", namespace.GetName()) | ||
} | ||
return nil | ||
} | ||
|
||
func removeLiqoSchedulingLabel(ctx context.Context, c client.Client, namespaceName string) error { | ||
namespace := &corev1.Namespace{} | ||
if err := c.Get(ctx, types.NamespacedName{Name: namespaceName}, namespace); err != nil { | ||
klog.Errorf("%s --> Unable to get the namespace '%s'", err, namespaceName) | ||
return err | ||
} | ||
|
||
if value, ok := namespace.Labels[liqoconst.SchedulingLiqoLabel]; ok && value == liqoconst.SchedulingLiqoLabelValue { | ||
delete(namespace.Labels, liqoconst.SchedulingLiqoLabel) | ||
if err := c.Update(ctx, namespace); err != nil { | ||
klog.Errorf(" %s --> Unable to remove Liqo scheduling label from the namespace '%s'", err, namespace.GetName()) | ||
return err | ||
} | ||
klog.Infof(" Liqo scheduling label successfully removed from the namespace '%s'", namespace.GetName()) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.