Skip to content

Commit

Permalink
feat: add pod antiaffinity (#233)
Browse files Browse the repository at this point in the history
Signed-off-by: Qi Zhang <smallqi1@163.com>
  • Loading branch information
zzzhangqi authored Apr 22, 2024
1 parent c55a560 commit beac613
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 8 deletions.
8 changes: 5 additions & 3 deletions controllers/handler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions controllers/handler/app-ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 26 additions & 1 deletion controllers/handler/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion controllers/handler/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion controllers/handler/mq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions controllers/handler/resource_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion controllers/handler/webcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion controllers/handler/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit beac613

Please sign in to comment.