|
| 1 | +// Copyright 2019-2021 The Liqo Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package offload |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + |
| 20 | + "github.com/spf13/cobra" |
| 21 | + corev1 "k8s.io/api/core/v1" |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + "k8s.io/klog/v2" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 26 | + |
| 27 | + offloadingv1alpha1 "github.com/liqotech/liqo/apis/offloading/v1alpha1" |
| 28 | + "github.com/liqotech/liqo/pkg/consts" |
| 29 | + "github.com/liqotech/liqo/pkg/liqoctl/common" |
| 30 | + argsutils "github.com/liqotech/liqo/pkg/utils/args" |
| 31 | + logsutils "github.com/liqotech/liqo/pkg/utils/logs" |
| 32 | +) |
| 33 | + |
| 34 | +// HandleOffloadCommand implements the "offload namespace" command. |
| 35 | +// It forges and createOrUpdate a namespaceOffloading resource for a given namespace according to the flag values. |
| 36 | +func HandleOffloadCommand(ctx context.Context, command *cobra.Command, args []string) error { |
| 37 | + if !klog.V(4).Enabled() { |
| 38 | + klog.SetLogFilter(logsutils.LogFilter{}) |
| 39 | + } |
| 40 | + |
| 41 | + config, err := common.GetLiqoctlRestConf() |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + k8sClient, err := client.New(config, client.Options{}) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + var acceptedClusterLabels argsutils.StringMap |
| 52 | + if err := acceptedClusterLabels.Set(command.Flag(AcceptedLabelsFlag).Value.String()); err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + var deniedClusterLabels argsutils.StringMap |
| 56 | + if err := deniedClusterLabels.Set(command.Flag(DeniedLabelsFlag).Value.String()); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + nsOffloading := forgeNamespaceOffloading(command, args, acceptedClusterLabels, deniedClusterLabels) |
| 61 | + |
| 62 | + _, err = controllerutil.CreateOrUpdate(ctx, k8sClient, nsOffloading, func() error { |
| 63 | + nsOffloading.Spec.PodOffloadingStrategy = forgePodOffloadingStrategy(command) |
| 64 | + nsOffloading.Spec.NamespaceMappingStrategy = forgeNamespaceMappingStrategy(command) |
| 65 | + nsOffloading.Spec.ClusterSelector = forgeClusterSelector(acceptedClusterLabels, deniedClusterLabels) |
| 66 | + return nil |
| 67 | + }) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func forgeNamespaceOffloading(command *cobra.Command, args []string, |
| 76 | + acceptedLabels, deniedLabels argsutils.StringMap) *offloadingv1alpha1.NamespaceOffloading { |
| 77 | + return &offloadingv1alpha1.NamespaceOffloading{ |
| 78 | + ObjectMeta: metav1.ObjectMeta{ |
| 79 | + Name: consts.DefaultNamespaceOffloadingName, |
| 80 | + Namespace: args[1], |
| 81 | + }, |
| 82 | + Spec: offloadingv1alpha1.NamespaceOffloadingSpec{ |
| 83 | + NamespaceMappingStrategy: forgeNamespaceMappingStrategy(command), |
| 84 | + PodOffloadingStrategy: forgePodOffloadingStrategy(command), |
| 85 | + ClusterSelector: forgeClusterSelector(acceptedLabels, deniedLabels), |
| 86 | + }, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func forgeClusterSelector(acceptedLabels, deniedLabels argsutils.StringMap) corev1.NodeSelector { |
| 91 | + var l []corev1.NodeSelectorTerm |
| 92 | + |
| 93 | + // No acceptedLabels are defined, backing to the default behavior |
| 94 | + if len(acceptedLabels.StringMap) == 0 && len(deniedLabels.StringMap) == 0 { |
| 95 | + return corev1.NodeSelector{ |
| 96 | + NodeSelectorTerms: []corev1.NodeSelectorTerm{{ |
| 97 | + MatchExpressions: []corev1.NodeSelectorRequirement{{ |
| 98 | + Key: consts.TypeLabel, |
| 99 | + Operator: corev1.NodeSelectorOpIn, |
| 100 | + Values: []string{consts.TypeNode}, |
| 101 | + }}}, |
| 102 | + }, |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + for k, v := range acceptedLabels.StringMap { |
| 107 | + l = append(l, corev1.NodeSelectorTerm{ |
| 108 | + MatchExpressions: []corev1.NodeSelectorRequirement{{ |
| 109 | + Key: k, |
| 110 | + Operator: corev1.NodeSelectorOpIn, |
| 111 | + Values: []string{v}, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }) |
| 115 | + } |
| 116 | + |
| 117 | + for k, v := range deniedLabels.StringMap { |
| 118 | + l = append(l, corev1.NodeSelectorTerm{ |
| 119 | + MatchExpressions: []corev1.NodeSelectorRequirement{{ |
| 120 | + Key: k, |
| 121 | + Operator: corev1.NodeSelectorOpNotIn, |
| 122 | + Values: []string{v}, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }) |
| 126 | + } |
| 127 | + |
| 128 | + return corev1.NodeSelector{NodeSelectorTerms: l} |
| 129 | +} |
| 130 | + |
| 131 | +func forgePodOffloadingStrategy(command *cobra.Command) offloadingv1alpha1.PodOffloadingStrategyType { |
| 132 | + return offloadingv1alpha1.PodOffloadingStrategyType(command.Flag(PodOffloadingStrategyFlag).Value.String()) |
| 133 | +} |
| 134 | + |
| 135 | +func forgeNamespaceMappingStrategy(command *cobra.Command) offloadingv1alpha1.NamespaceMappingStrategyType { |
| 136 | + return offloadingv1alpha1.NamespaceMappingStrategyType(command.Flag(NamespaceMappingStrategyFlag).Value.String()) |
| 137 | +} |
0 commit comments