Skip to content

Commit 27775eb

Browse files
informalictajanikow
authored andcommitted
Add node affinity for members pod (#494)
1 parent 458c933 commit 27775eb

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

pkg/util/k8sutil/affinity.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
package k8sutil
2424

2525
import (
26-
"k8s.io/api/core/v1"
26+
v1 "k8s.io/api/core/v1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
)
2929

@@ -32,6 +32,21 @@ import (
3232
// affinityWithRole contains the role to configure affinity with.
3333
func createAffinity(deploymentName, role string, required bool, affinityWithRole string) *v1.Affinity {
3434
a := &v1.Affinity{
35+
NodeAffinity: &v1.NodeAffinity{
36+
RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
37+
NodeSelectorTerms: []v1.NodeSelectorTerm{
38+
{
39+
MatchExpressions: []v1.NodeSelectorRequirement{
40+
{
41+
Key: "beta.kubernetes.io/arch",
42+
Operator: "In",
43+
Values: []string{"amd64"},
44+
},
45+
},
46+
},
47+
},
48+
},
49+
},
3550
PodAntiAffinity: &v1.PodAntiAffinity{},
3651
}
3752
labels := LabelsForDeployment(deploymentName, role)
@@ -72,5 +87,6 @@ func createAffinity(deploymentName, role string, required bool, affinityWithRole
7287
})
7388
}
7489
}
90+
7591
return a
7692
}

pkg/util/k8sutil/affinity_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,29 @@ package k8sutil
2525
import (
2626
"testing"
2727

28+
v1 "k8s.io/api/core/v1"
29+
2830
"github.com/stretchr/testify/assert"
2931
"github.com/stretchr/testify/require"
3032
)
3133

3234
// TestCreateAffinity tests createAffinity
3335
func TestCreateAffinity(t *testing.T) {
36+
expectedNodeAffinity := &v1.NodeAffinity{
37+
RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
38+
NodeSelectorTerms: []v1.NodeSelectorTerm{
39+
{
40+
MatchExpressions: []v1.NodeSelectorRequirement{
41+
{
42+
Key: "beta.kubernetes.io/arch",
43+
Operator: "In",
44+
Values: []string{"amd64"},
45+
},
46+
},
47+
},
48+
},
49+
},
50+
}
3451
// Required
3552
a := createAffinity("test", "role", true, "")
3653
assert.Nil(t, a.PodAffinity)
@@ -42,6 +59,7 @@ func TestCreateAffinity(t *testing.T) {
4259
assert.Equal(t, "test", a.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchLabels["arango_deployment"])
4360
assert.Equal(t, "arangodb", a.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchLabels["app"])
4461
assert.Equal(t, "role", a.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchLabels["role"])
62+
assert.Equal(t, expectedNodeAffinity, a.NodeAffinity)
4563

4664
// Require & affinity with role dbserver
4765
a = createAffinity("test", "role", true, "dbserver")
@@ -53,6 +71,7 @@ func TestCreateAffinity(t *testing.T) {
5371
assert.Equal(t, "test", a.PodAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchLabels["arango_deployment"])
5472
assert.Equal(t, "arangodb", a.PodAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchLabels["app"])
5573
assert.Equal(t, "dbserver", a.PodAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchLabels["role"])
74+
assert.Equal(t, expectedNodeAffinity, a.NodeAffinity)
5675

5776
require.NotNil(t, a.PodAntiAffinity)
5877
require.Len(t, a.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution, 1)
@@ -62,6 +81,7 @@ func TestCreateAffinity(t *testing.T) {
6281
assert.Equal(t, "test", a.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchLabels["arango_deployment"])
6382
assert.Equal(t, "arangodb", a.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchLabels["app"])
6483
assert.Equal(t, "role", a.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchLabels["role"])
84+
assert.Equal(t, expectedNodeAffinity, a.NodeAffinity)
6585

6686
// Not Required
6787
a = createAffinity("test", "role", false, "")
@@ -74,6 +94,7 @@ func TestCreateAffinity(t *testing.T) {
7494
assert.Equal(t, "test", a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.LabelSelector.MatchLabels["arango_deployment"])
7595
assert.Equal(t, "arangodb", a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.LabelSelector.MatchLabels["app"])
7696
assert.Equal(t, "role", a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.LabelSelector.MatchLabels["role"])
97+
assert.Equal(t, expectedNodeAffinity, a.NodeAffinity)
7798

7899
// Not Required & affinity with role dbserver
79100
a = createAffinity("test", "role", false, "dbserver")
@@ -85,6 +106,7 @@ func TestCreateAffinity(t *testing.T) {
85106
assert.Equal(t, "test", a.PodAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.LabelSelector.MatchLabels["arango_deployment"])
86107
assert.Equal(t, "arangodb", a.PodAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.LabelSelector.MatchLabels["app"])
87108
assert.Equal(t, "dbserver", a.PodAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.LabelSelector.MatchLabels["role"])
109+
assert.Equal(t, expectedNodeAffinity, a.NodeAffinity)
88110

89111
require.NotNil(t, a.PodAntiAffinity)
90112
require.Len(t, a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution, 1)
@@ -94,4 +116,5 @@ func TestCreateAffinity(t *testing.T) {
94116
assert.Equal(t, "test", a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.LabelSelector.MatchLabels["arango_deployment"])
95117
assert.Equal(t, "arangodb", a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.LabelSelector.MatchLabels["app"])
96118
assert.Equal(t, "role", a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.LabelSelector.MatchLabels["role"])
119+
assert.Equal(t, expectedNodeAffinity, a.NodeAffinity)
97120
}

0 commit comments

Comments
 (0)