Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pod-anti-affinity #21

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/v1/geminicluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@
return false
}

func (cluster *GeminiCluster) GetEnableAffinity() bool {
return cluster.Spec.Affinity.EnablePodAntiAffinity

Check warning on line 219 in api/v1/geminicluster_types.go

View check run for this annotation

Codecov / codecov/patch

api/v1/geminicluster_types.go#L218-L219

Added lines #L218 - L219 were not covered by tests
}

func (cluster *GeminiCluster) GetAdminUserSecretName() string {
return fmt.Sprintf("%v%v", cluster.Name, AdminUserSecretSuffix)
}
Expand Down
65 changes: 37 additions & 28 deletions controllers/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"github.com/openGemini/openGemini-operator/pkg/opengemini/meta"
"github.com/openGemini/openGemini-operator/pkg/opengemini/sql"
"github.com/openGemini/openGemini-operator/pkg/opengemini/store"
"github.com/openGemini/openGemini-operator/pkg/specs"
"github.com/openGemini/openGemini-operator/pkg/utils"
)

Expand Down Expand Up @@ -113,31 +114,35 @@
) {
sts.Annotations = utils.MergeLabels(
cluster.Spec.Metadata.GetAnnotationsOrNil())

baseLabels := map[string]string{
opengeminiv1.LabelCluster: cluster.Name,
opengeminiv1.LabelInstanceSet: setName,
}
matchLabels := utils.MergeLabels(
baseLabels,
map[string]string{
opengeminiv1.LabelInstance: sts.Name,
},
)

Check warning on line 128 in controllers/instance.go

View check run for this annotation

Codecov / codecov/patch

controllers/instance.go#L117-L128

Added lines #L117 - L128 were not covered by tests
sts.Labels = utils.MergeLabels(
cluster.Spec.Metadata.GetLabelsOrNil(),
map[string]string{
opengeminiv1.LabelCluster: cluster.Name,
opengeminiv1.LabelInstanceSet: setName,
opengeminiv1.LabelInstance: sts.Name,
})
matchLabels,
)

Check warning on line 132 in controllers/instance.go

View check run for this annotation

Codecov / codecov/patch

controllers/instance.go#L131-L132

Added lines #L131 - L132 were not covered by tests
sts.Spec.Selector = &metav1.LabelSelector{
MatchLabels: map[string]string{
opengeminiv1.LabelCluster: cluster.Name,
opengeminiv1.LabelInstanceSet: setName,
opengeminiv1.LabelInstance: sts.Name,
},
MatchLabels: matchLabels,

Check warning on line 134 in controllers/instance.go

View check run for this annotation

Codecov / codecov/patch

controllers/instance.go#L134

Added line #L134 was not covered by tests
}
sts.Spec.Template.Annotations = utils.MergeLabels(
cluster.Spec.Metadata.GetAnnotationsOrNil(),
)
sts.Spec.Template.Labels = utils.MergeLabels(
cluster.Spec.Metadata.GetLabelsOrNil(),
matchLabels,

Check warning on line 141 in controllers/instance.go

View check run for this annotation

Codecov / codecov/patch

controllers/instance.go#L141

Added line #L141 was not covered by tests
map[string]string{
opengeminiv1.LabelCluster: cluster.Name,
opengeminiv1.LabelInstanceSet: setName,
opengeminiv1.LabelInstance: sts.Name,
opengeminiv1.LabelConfigHash: cluster.Status.AppliedConfigHash,
opengeminiv1.LabelConfigHash: cluster.Status.AppliedConfigHash,

Check warning on line 143 in controllers/instance.go

View check run for this annotation

Codecov / codecov/patch

controllers/instance.go#L143

Added line #L143 was not covered by tests
})
sts.Spec.Template.Spec.Affinity = specs.CreateAffinity(cluster.GetEnableAffinity(), baseLabels)

Check warning on line 145 in controllers/instance.go

View check run for this annotation

Codecov / codecov/patch

controllers/instance.go#L145

Added line #L145 was not covered by tests
sts.Spec.Template.Spec.RestartPolicy = corev1.RestartPolicyAlways
sts.Spec.Template.Spec.ShareProcessNamespace = &[]bool{true}[0]
sts.Spec.Template.Spec.EnableServiceLinks = &[]bool{false}[0]
Expand All @@ -154,31 +159,35 @@
) {
deploy.Annotations = utils.MergeLabels(
cluster.Spec.Metadata.GetAnnotationsOrNil())

baseLabels := map[string]string{
opengeminiv1.LabelCluster: cluster.Name,
opengeminiv1.LabelInstanceSet: setName,
}
matchLabels := utils.MergeLabels(
baseLabels,
map[string]string{
opengeminiv1.LabelInstance: deploy.Name,
},
)

Check warning on line 173 in controllers/instance.go

View check run for this annotation

Codecov / codecov/patch

controllers/instance.go#L162-L173

Added lines #L162 - L173 were not covered by tests
deploy.Labels = utils.MergeLabels(
cluster.Spec.Metadata.GetLabelsOrNil(),
map[string]string{
opengeminiv1.LabelCluster: cluster.Name,
opengeminiv1.LabelInstanceSet: setName,
opengeminiv1.LabelInstance: deploy.Name,
})
matchLabels,
)

Check warning on line 177 in controllers/instance.go

View check run for this annotation

Codecov / codecov/patch

controllers/instance.go#L176-L177

Added lines #L176 - L177 were not covered by tests
deploy.Spec.Selector = &metav1.LabelSelector{
MatchLabels: map[string]string{
opengeminiv1.LabelCluster: cluster.Name,
opengeminiv1.LabelInstanceSet: setName,
opengeminiv1.LabelInstance: deploy.Name,
},
MatchLabels: matchLabels,

Check warning on line 179 in controllers/instance.go

View check run for this annotation

Codecov / codecov/patch

controllers/instance.go#L179

Added line #L179 was not covered by tests
}
deploy.Spec.Template.Annotations = utils.MergeLabels(
cluster.Spec.Metadata.GetAnnotationsOrNil(),
)
deploy.Spec.Template.Labels = utils.MergeLabels(
cluster.Spec.Metadata.GetLabelsOrNil(),
matchLabels,

Check warning on line 186 in controllers/instance.go

View check run for this annotation

Codecov / codecov/patch

controllers/instance.go#L186

Added line #L186 was not covered by tests
map[string]string{
opengeminiv1.LabelCluster: cluster.Name,
opengeminiv1.LabelInstanceSet: setName,
opengeminiv1.LabelInstance: deploy.Name,
opengeminiv1.LabelConfigHash: cluster.Status.AppliedConfigHash,
opengeminiv1.LabelConfigHash: cluster.Status.AppliedConfigHash,

Check warning on line 188 in controllers/instance.go

View check run for this annotation

Codecov / codecov/patch

controllers/instance.go#L188

Added line #L188 was not covered by tests
})
deploy.Spec.Template.Spec.Affinity = specs.CreateAffinity(cluster.GetEnableAffinity(), baseLabels)

Check warning on line 190 in controllers/instance.go

View check run for this annotation

Codecov / codecov/patch

controllers/instance.go#L190

Added line #L190 was not covered by tests
deploy.Spec.Template.Spec.RestartPolicy = corev1.RestartPolicyAlways
deploy.Spec.Template.Spec.ShareProcessNamespace = &[]bool{true}[0]
deploy.Spec.Template.Spec.EnableServiceLinks = &[]bool{false}[0]
Expand Down
35 changes: 35 additions & 0 deletions pkg/specs/affinity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package specs

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const defaultAffinityWeight = 100

func CreateAffinity(enable bool, matchLabels map[string]string) *corev1.Affinity {
affinity := corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{},
}

affinityTerm := corev1.PodAffinityTerm{
TopologyKey: corev1.LabelHostname,
LabelSelector: &metav1.LabelSelector{
MatchLabels: matchLabels,
},
}

if enable {
affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution = []corev1.PodAffinityTerm{
affinityTerm,
}
} else {
affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution = []corev1.WeightedPodAffinityTerm{
{
Weight: defaultAffinityWeight,
PodAffinityTerm: affinityTerm,
},
}
}
return &affinity

Check warning on line 34 in pkg/specs/affinity.go

View check run for this annotation

Codecov / codecov/patch

pkg/specs/affinity.go#L10-L34

Added lines #L10 - L34 were not covered by tests
}
Loading