Skip to content

Commit

Permalink
add label with two values: active and standby for the gateway replicas.
Browse files Browse the repository at this point in the history
  • Loading branch information
alacuku committed Jul 12, 2021
1 parent 29724b8 commit 0d48de9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 53 deletions.
13 changes: 11 additions & 2 deletions deployments/liqo/templates/_helpers.tpl
Expand Up @@ -96,10 +96,19 @@ Create the file name of a cluster role starting from a prefix, it accepts a dict
{{- end }}

{{/*
Gateway pod labels
Gateway pod labels.
If you change any value here, please make sure that you change it also in che source code.
*/}}
{{- define "liqo.gatewayPodLabels" -}}
net.liqo.io/gatewayPod: "true"
net.liqo.io/gateway: "standby"
{{- end }}

{{/*
Label selector used by the gateway service to select the right gateway pod.
If you change any value here, please make sure that you change it also in che source code.
*/}}
{{- define "liqo.gatewaySelector" -}}
net.liqo.io/gateway: "active"
{{- end }}

{{/*
Expand Down
5 changes: 1 addition & 4 deletions deployments/liqo/templates/liqo-gateway-service.yaml
Expand Up @@ -19,8 +19,5 @@ spec:
port: 5871
targetPort: 5871
protocol: UDP
- name: wireguard-overlay
port: 51871
protocol: UDP
selector:
{{- include "liqo.gatewayPodLabels" $gatewayConfig | nindent 4 }}
{{- include "liqo.gatewaySelector" $gatewayConfig | nindent 4 }}
57 changes: 32 additions & 25 deletions internal/liqonet/tunnel-operator/labelerOperator.go
Expand Up @@ -13,22 +13,26 @@ import (
liqoutils "github.com/liqotech/liqo/pkg/liqonet/utils"
)

var (
// This labels are the ones set during the deployment of liqo using the helm chart.
const (
// These labels are the ones set during the deployment of liqo using the helm chart.
// Any change to those labels on the helm chart has also to be reflected here.
podInstanceLabelKey = "app.kubernetes.io/instance"
podInstanceLabelValue = "liqo-gateway"
podNameLabelKey = "app.kubernetes.io/name"
podNameLabelValue = "gateway"
serviceSelectorLabelKey = "net.liqo.io/gatewayPod"
serviceSelectorLabelValue = "true"
// LabelSelector instructs the informer to only cache the pod objects that satisfies the selector.
podComponentLabelKey = "app.kubernetes.io/component"
podComponentLabelValue = "networking"
podNameLabelKey = "app.kubernetes.io/name"
podNameLabelValue = "gateway"
gatewayLabelKey = "net.liqo.io/gateway"
gatewayStatusActive = "active"
gatewayStatusStandby = "standby"
)

var (
// LabelSelector instructs the informer to only cache the pod objects that satisfy the selector.
// Only the pod objects with the right labels will be reconciled.
LabelSelector = cache.SelectorsByObject{
&corev1.Pod{}: {
Label: labels.SelectorFromSet(labels.Set{
podInstanceLabelKey: podInstanceLabelValue,
podNameLabelKey: podNameLabelValue,
podComponentLabelKey: podComponentLabelValue,
podNameLabelKey: podNameLabelValue,
}),
},
}
Expand All @@ -49,7 +53,7 @@ func NewLabelerController(podIP string, cl client.Client) *LabelerController {
}

// Reconcile for a given pod, replica of the current operator, it checks if it is the current pod
// meaning the pod where this code is running. If yes, then it adds a the label to the pod if it does
// meaning the pod where this code is running. If yes, then it adds a label to the pod if it does
// not have it. If the pod is not the current one the operator makes sure that it does not have the
// label by removing it if present.
func (lbc *LabelerController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand All @@ -59,27 +63,30 @@ func (lbc *LabelerController) Reconcile(ctx context.Context, req ctrl.Request) (
klog.Errorf("an error occurred while getting pod {%s}: %v", req.NamespacedName, err)
return ctrl.Result{}, client.IgnoreNotFound(err)
}
// If it is our pod/current pod then add the right label.
// If it is our pod/current pod then ensure that the labels values is set to "active".
if lbc.PodIP == pod.Status.PodIP {
if liqoutils.AddLabelToObj(pod, serviceSelectorLabelKey, serviceSelectorLabelValue) {
if liqoutils.AddLabelToObj(pod, gatewayLabelKey, gatewayStatusActive) {
if err := lbc.Update(ctx, pod); err != nil {
klog.Errorf("an error occurred while adding selector label to pod {%s}: %v", req.String(), err)
klog.Errorf("an error occurred while updating value of label {%s} to {%s} for pod {%s}: %v",
gatewayLabelKey, gatewayStatusActive, req.String(), err)
return ctrl.Result{}, err
}
klog.Infof("successfully added label {%s: %s} to pod {%s}",
serviceSelectorLabelKey, serviceSelectorLabelValue, req.String())
klog.Infof("successfully updated label {%s: %s} for pod {%s}",
gatewayLabelKey, gatewayStatusActive, req.String())
}
return ctrl.Result{}, nil
}
// Make sure that the other replicas does not have the selector label.
if val := liqoutils.GetLabelValueFromObj(pod, serviceSelectorLabelKey); val != "" {
delete(pod.GetLabels(), serviceSelectorLabelKey)
if err := lbc.Update(ctx, pod); err != nil {
klog.Errorf("an error occurred while removing selector label to pod {%s}: %v", req.String(), err)
return ctrl.Result{}, err
// Make sure that the other replicas has the label set to "standby".
if val := liqoutils.GetLabelValueFromObj(pod, gatewayLabelKey); val == gatewayStatusActive {
if liqoutils.AddLabelToObj(pod, gatewayLabelKey, gatewayStatusStandby) {
if err := lbc.Update(ctx, pod); err != nil {
klog.Errorf("an error occurred while updating value of label {%s} to {%s} for pod {%s}: %v",
gatewayLabelKey, gatewayStatusStandby, req.String(), err)
return ctrl.Result{}, err
}
klog.Infof("successfully updated label {%s: %s} for pod {%s}",
gatewayLabelKey, gatewayStatusStandby, req.String())
}
klog.Infof("successfully removed label {%s: %s} to pod {%s}",
serviceSelectorLabelKey, serviceSelectorLabelValue, req.String())
}
return ctrl.Result{}, nil
}
Expand Down
41 changes: 19 additions & 22 deletions internal/liqonet/tunnel-operator/labelerOperator_test.go
Expand Up @@ -42,16 +42,10 @@ var _ = Describe("LabelerOperator", func() {
Namespace: labelerReq.Namespace,
},
Spec: corev1.PodSpec{
NodeName: "overlaytestnodename",
Containers: []corev1.Container{
{
Name: "busybox",
Image: "busybox",
ImagePullPolicy: corev1.PullIfNotPresent,
Command: []string{
"sleep",
"3600",
},
Name: "busybox",
Image: "busybox",
},
},
},
Expand Down Expand Up @@ -97,19 +91,19 @@ var _ = Describe("LabelerOperator", func() {
if err != nil {
return err
}
if newPod.GetLabels()[serviceSelectorLabelKey] != serviceSelectorLabelValue {
return fmt.Errorf(" error: label %s is different than %s", newPod.GetLabels()[serviceSelectorLabelKey], serviceSelectorLabelValue)
if newPod.GetLabels()[gatewayLabelKey] != gatewayStatusActive {
return fmt.Errorf(" error: label %s is different than %s", newPod.GetLabels()[gatewayLabelKey], gatewayStatusActive)
}
return nil
}).Should(BeNil())
})

It("pod does have the label, should not change the pod", func() {
const podName = "current-pod-with-labels"
const podName = "current-pod-active"
labelerReq.Name = podName
labelerTestPod.Name = podName
labelerTestPod.SetLabels(map[string]string{
serviceSelectorLabelKey: serviceSelectorLabelValue,
gatewayLabelKey: gatewayStatusActive,
})
Eventually(func() error { return k8sClient.Create(context.TODO(), labelerTestPod) }).Should(BeNil())
newPod := &corev1.Pod{}
Expand All @@ -134,19 +128,22 @@ var _ = Describe("LabelerOperator", func() {
if err != nil {
return err
}
if newPod.GetLabels()[serviceSelectorLabelKey] != serviceSelectorLabelValue {
return fmt.Errorf(" error: label %s is different than %s", newPod.GetLabels()[serviceSelectorLabelKey], serviceSelectorLabelValue)
if newPod.GetLabels()[gatewayLabelKey] != gatewayStatusActive {
return fmt.Errorf(" error: label %s is different than %s", newPod.GetLabels()[gatewayLabelKey], gatewayStatusActive)
}
return nil
}).Should(BeNil())
})
})

Context("when the pod is not the current one", func() {
It("pod does not have the label, does nothing", func() {
const podName = "other-pod-without-labels"
It("pod is already in standby, does nothing", func() {
const podName = "other-pod-standby"
labelerReq.Name = podName
labelerTestPod.Name = podName
labelerTestPod.SetLabels(map[string]string{
gatewayLabelKey: gatewayStatusStandby,
})
Eventually(func() error { return k8sClient.Create(context.TODO(), labelerTestPod) }).Should(BeNil())
newPod := &corev1.Pod{}
Eventually(func() error { return k8sClient.Get(context.TODO(), labelerReq.NamespacedName, newPod) }).Should(BeNil())
Expand All @@ -167,12 +164,12 @@ var _ = Describe("LabelerOperator", func() {
Eventually(func() error { _, err := lbc.Reconcile(context.TODO(), labelerReq); return err }).Should(BeNil())
})

It("pod does have the label, should remove the label", func() {
const podName = "other-pod-with-labels"
It("label is set to {active}, should set it to {standby} ", func() {
const podName = "other-pod-active"
labelerReq.Name = podName
labelerTestPod.Name = podName
labelerTestPod.SetLabels(map[string]string{
serviceSelectorLabelKey: serviceSelectorLabelValue,
gatewayLabelKey: gatewayStatusActive,
})
Eventually(func() error { return k8sClient.Create(context.TODO(), labelerTestPod) }).Should(BeNil())
newPod := &corev1.Pod{}
Expand All @@ -197,15 +194,15 @@ var _ = Describe("LabelerOperator", func() {
if err != nil {
return err
}
if newPod.GetLabels()[serviceSelectorLabelKey] != "" {
return fmt.Errorf(" error: label %s is different than empty string", newPod.GetLabels()[serviceSelectorLabelKey])
if newPod.GetLabels()[gatewayLabelKey] != gatewayStatusStandby {
return fmt.Errorf(" error: label %s is different than %s string", newPod.GetLabels()[gatewayLabelKey], gatewayStatusStandby)
}
return nil
}).Should(BeNil())
})
})

Context("pod does not exit", func() {
Context("pod does not exist", func() {
It("shold return nil", func() {
const podName = "pod-does-not-exist"
labelerReq.Name = podName
Expand Down

0 comments on commit 0d48de9

Please sign in to comment.