From beac61324f22d7c4a2a1080e77c56aa1450c2634 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Mon, 22 Apr 2024 11:56:47 +0800 Subject: [PATCH] feat: add pod antiaffinity (#233) Signed-off-by: Qi Zhang --- controllers/handler/api.go | 8 +++++--- controllers/handler/app-ui.go | 1 + controllers/handler/common.go | 27 ++++++++++++++++++++++++++- controllers/handler/hub.go | 4 +++- controllers/handler/mq.go | 4 +++- controllers/handler/resource_proxy.go | 1 + controllers/handler/webcli.go | 4 +++- controllers/handler/worker.go | 4 +++- 8 files changed, 45 insertions(+), 8 deletions(-) diff --git a/controllers/handler/api.go b/controllers/handler/api.go index e983b344..11bd9389 100644 --- a/controllers/handler/api.go +++ b/controllers/handler/api.go @@ -3,14 +3,15 @@ package handler import ( "context" "fmt" + "os" + "strconv" + "strings" + checksqllite "github.com/goodrain/rainbond-operator/util/check-sqllite" "github.com/goodrain/rainbond-operator/util/k8sutil" "github.com/goodrain/rainbond-operator/util/rbdutil" "github.com/sirupsen/logrus" utilversion "k8s.io/apimachinery/pkg/util/version" - "os" - "strconv" - "strings" rainbondv1alpha1 "github.com/goodrain/rainbond-operator/api/v1alpha1" "github.com/goodrain/rainbond-operator/util/commonutil" @@ -238,6 +239,7 @@ func (a *api) deployment() client.Object { Spec: corev1.PodSpec{ ImagePullSecrets: imagePullSecrets(a.component, a.cluster), TerminationGracePeriodSeconds: commonutil.Int64(0), + Affinity: antiAffinityForRequiredNodes([]string{APIName}), Containers: []corev1.Container{ { Name: APIName, diff --git a/controllers/handler/app-ui.go b/controllers/handler/app-ui.go index 75ef4527..44282639 100644 --- a/controllers/handler/app-ui.go +++ b/controllers/handler/app-ui.go @@ -244,6 +244,7 @@ func (a *appui) deploymentForAppUI() client.Object { Spec: corev1.PodSpec{ ImagePullSecrets: imagePullSecrets(a.component, a.cluster), TerminationGracePeriodSeconds: commonutil.Int64(0), + Affinity: antiAffinityForRequiredNodes([]string{AppUIName}), Containers: []corev1.Container{ { Name: AppUIName, diff --git a/controllers/handler/common.go b/controllers/handler/common.go index d53c5bf9..327c97d8 100644 --- a/controllers/handler/common.go +++ b/controllers/handler/common.go @@ -4,12 +4,13 @@ import ( "context" "errors" "fmt" - "k8s.io/apimachinery/pkg/util/intstr" "os" "path" "strconv" "strings" + "k8s.io/apimachinery/pkg/util/intstr" + "github.com/goodrain/rainbond-operator/util/commonutil" "github.com/goodrain/rainbond-operator/util/constants" "github.com/goodrain/rainbond-operator/util/rbdutil" @@ -353,6 +354,30 @@ func affinityForRequiredNodes(nodeNames []string) *corev1.Affinity { } } +func antiAffinityForRequiredNodes(componentLabelName []string) *corev1.Affinity { + return &corev1.Affinity{ + PodAntiAffinity: &corev1.PodAntiAffinity{ + PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{ + { + Weight: 100, + PodAffinityTerm: corev1.PodAffinityTerm{ + LabelSelector: &metav1.LabelSelector{ + MatchExpressions: []metav1.LabelSelectorRequirement{ + { + Key: "name", + Operator: metav1.LabelSelectorOpIn, + Values: componentLabelName, + }, + }, + }, + TopologyKey: "kubernetes.io/hostname,k3s.io/hostname", + }, + }, + }, + }, + } +} + func copyLabels(m map[string]string) map[string]string { cp := make(map[string]string) for k, v := range m { diff --git a/controllers/handler/hub.go b/controllers/handler/hub.go index 4a92572e..0911d02a 100644 --- a/controllers/handler/hub.go +++ b/controllers/handler/hub.go @@ -3,11 +3,12 @@ package handler import ( "context" "fmt" + "os/exec" + "github.com/sirupsen/logrus" networkingv1beta1 "k8s.io/api/networking/v1beta1" k8sErrors "k8s.io/apimachinery/pkg/api/errors" utilversion "k8s.io/apimachinery/pkg/util/version" - "os/exec" rainbondv1alpha1 "github.com/goodrain/rainbond-operator/api/v1alpha1" "github.com/goodrain/rainbond-operator/util/commonutil" @@ -181,6 +182,7 @@ func (h *hub) deployment() client.Object { Spec: corev1.PodSpec{ ImagePullSecrets: imagePullSecrets(h.component, h.cluster), TerminationGracePeriodSeconds: commonutil.Int64(0), + Affinity: antiAffinityForRequiredNodes([]string{HubName}), Containers: []corev1.Container{ { Name: "rbd-hub", diff --git a/controllers/handler/mq.go b/controllers/handler/mq.go index 889b857e..5b4dfa71 100644 --- a/controllers/handler/mq.go +++ b/controllers/handler/mq.go @@ -3,9 +3,10 @@ package handler import ( "context" "fmt" - "k8s.io/apimachinery/pkg/util/intstr" "strings" + "k8s.io/apimachinery/pkg/util/intstr" + rainbondv1alpha1 "github.com/goodrain/rainbond-operator/api/v1alpha1" "github.com/goodrain/rainbond-operator/util/commonutil" appsv1 "k8s.io/api/apps/v1" @@ -111,6 +112,7 @@ func (m *mq) deployment() client.Object { Spec: corev1.PodSpec{ TerminationGracePeriodSeconds: commonutil.Int64(0), ImagePullSecrets: imagePullSecrets(m.component, m.cluster), + Affinity: antiAffinityForRequiredNodes([]string{MQName}), Containers: []corev1.Container{ { Name: MQName, diff --git a/controllers/handler/resource_proxy.go b/controllers/handler/resource_proxy.go index aaac3602..e54a7c1b 100644 --- a/controllers/handler/resource_proxy.go +++ b/controllers/handler/resource_proxy.go @@ -110,6 +110,7 @@ func (r *resourceProxy) resource() []client.Object { Spec: corev1.PodSpec{ ImagePullSecrets: imagePullSecrets(r.component, r.cluster), TerminationGracePeriodSeconds: commonutil.Int64(0), + Affinity: antiAffinityForRequiredNodes([]string{ResourceProxyName}), Containers: []corev1.Container{ { Name: ResourceProxyName, diff --git a/controllers/handler/webcli.go b/controllers/handler/webcli.go index a1c10c06..f9ef0e6c 100644 --- a/controllers/handler/webcli.go +++ b/controllers/handler/webcli.go @@ -3,9 +3,10 @@ package handler import ( "context" "fmt" - "k8s.io/apimachinery/pkg/util/intstr" "strings" + "k8s.io/apimachinery/pkg/util/intstr" + "github.com/goodrain/rainbond-operator/util/commonutil" rainbondv1alpha1 "github.com/goodrain/rainbond-operator/api/v1alpha1" @@ -103,6 +104,7 @@ func (w *webcli) deployment() client.Object { ImagePullSecrets: imagePullSecrets(w.component, w.cluster), ServiceAccountName: "rainbond-operator", TerminationGracePeriodSeconds: commonutil.Int64(0), + Affinity: antiAffinityForRequiredNodes([]string{WebCliName}), Containers: []corev1.Container{ { Name: WebCliName, diff --git a/controllers/handler/worker.go b/controllers/handler/worker.go index b32aec24..bee4888f 100644 --- a/controllers/handler/worker.go +++ b/controllers/handler/worker.go @@ -3,10 +3,11 @@ package handler import ( "context" "fmt" - "k8s.io/apimachinery/pkg/util/intstr" "path" "strings" + "k8s.io/apimachinery/pkg/util/intstr" + checksqllite "github.com/goodrain/rainbond-operator/util/check-sqllite" "github.com/goodrain/rainbond-operator/util/probeutil" @@ -224,6 +225,7 @@ func (w *worker) deployment() client.Object { TerminationGracePeriodSeconds: commonutil.Int64(0), ServiceAccountName: "rainbond-operator", ImagePullSecrets: imagePullSecrets(w.component, w.cluster), + Affinity: antiAffinityForRequiredNodes([]string{WorkerName}), Containers: []corev1.Container{ { Name: WorkerName,