Skip to content

Commit b02c4c4

Browse files
sumanthravipatiSumanth Ravipati
andauthored
CLD-478: Default affinity code changes commit
Co-authored-by: Sumanth Ravipati <[email protected]>
1 parent c1b41dd commit b02c4c4

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

charts/values.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,19 @@ auth:
5555

5656
# Configure Affinity property for scheduling pods to nodes
5757
# ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity
58-
affinity: {}
58+
# Preferred anti affinity rule to schedule one marklogic pod per worker node
59+
affinity:
60+
podAntiAffinity:
61+
preferredDuringSchedulingIgnoredDuringExecution:
62+
- weight: 100
63+
podAffinityTerm:
64+
labelSelector:
65+
matchExpressions:
66+
- key: app.kubernetes.io/name
67+
operator: In
68+
values:
69+
- marklogic
70+
topologyKey: kubernetes.io/hostname
5971

6072
# Configure NodeSelector property for scheduling pods to nodes
6173
# ref: https://kubernetes.io/docs/tasks/configure-pod-container/assign-pods-nodes/#create-a-pod-that-gets-scheduled-to-your-chosen-node
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package template_test
2+
3+
import (
4+
"path/filepath"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
appsv1 "k8s.io/api/apps/v1"
10+
11+
"github.com/gruntwork-io/terratest/modules/helm"
12+
"github.com/gruntwork-io/terratest/modules/k8s"
13+
"github.com/gruntwork-io/terratest/modules/random"
14+
)
15+
16+
func TestChartTemplatePodAntiAffinityClass(t *testing.T) {
17+
t.Parallel()
18+
19+
// Path to the helm chart we will test
20+
helmChartPath, err := filepath.Abs("../../charts")
21+
releaseName := "marklogic-pod-antiaffinity-test"
22+
t.Log(helmChartPath, releaseName)
23+
require.NoError(t, err)
24+
25+
// Set up the namespace; confirm that the template renders the expected value for the namespace.
26+
namespaceName := "marklogic-" + strings.ToLower(random.UniqueId())
27+
t.Logf("Namespace: %s\n", namespaceName)
28+
29+
// Setup the args for helm install
30+
options := &helm.Options{
31+
SetValues: map[string]string{
32+
"image.repository": "marklogicdb/marklogic-db",
33+
"image.tag": "latest",
34+
"persistence.enabled": "false",
35+
},
36+
KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
37+
}
38+
39+
// render the tempate
40+
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/statefulset.yaml"})
41+
42+
var statefulset appsv1.StatefulSet
43+
helm.UnmarshalK8SYaml(t, output, &statefulset)
44+
45+
// Verify the name and namespace matches
46+
require.Equal(t, namespaceName, statefulset.Namespace)
47+
48+
// Verify the pod anti affinity rule is set
49+
expectedLabelSelectorKey := "app.kubernetes.io/name"
50+
expectedLabelSelectorValue := "marklogic"
51+
expectedTopologyKey := "kubernetes.io/hostname"
52+
statefulSetAffinityRule := statefulset.Spec.Template.Spec.Affinity.PodAntiAffinity
53+
affinityPreferredRule := statefulSetAffinityRule.PreferredDuringSchedulingIgnoredDuringExecution
54+
actualLabelSelectorKey := affinityPreferredRule[0].PodAffinityTerm.LabelSelector.MatchExpressions[0].Key
55+
actualLabelSelectorValue := affinityPreferredRule[0].PodAffinityTerm.LabelSelector.MatchExpressions[0].Values[0]
56+
actualTopologyKey := affinityPreferredRule[0].PodAffinityTerm.TopologyKey
57+
require.Equal(t, actualLabelSelectorKey, expectedLabelSelectorKey)
58+
require.Equal(t, actualLabelSelectorValue, expectedLabelSelectorValue)
59+
require.Equal(t, actualTopologyKey, expectedTopologyKey)
60+
}

0 commit comments

Comments
 (0)