Skip to content
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
18 changes: 17 additions & 1 deletion pkg/util/k8sutil/affinity.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package k8sutil

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

Expand All @@ -32,6 +32,21 @@ import (
// affinityWithRole contains the role to configure affinity with.
func createAffinity(deploymentName, role string, required bool, affinityWithRole string) *v1.Affinity {
a := &v1.Affinity{
NodeAffinity: &v1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
NodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchExpressions: []v1.NodeSelectorRequirement{
{
Key: "beta.kubernetes.io/arch",
Operator: "In",
Values: []string{"amd64"},
},
},
},
},
},
},
PodAntiAffinity: &v1.PodAntiAffinity{},
}
labels := LabelsForDeployment(deploymentName, role)
Expand Down Expand Up @@ -72,5 +87,6 @@ func createAffinity(deploymentName, role string, required bool, affinityWithRole
})
}
}

return a
}
23 changes: 23 additions & 0 deletions pkg/util/k8sutil/affinity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,29 @@ package k8sutil
import (
"testing"

v1 "k8s.io/api/core/v1"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestCreateAffinity tests createAffinity
func TestCreateAffinity(t *testing.T) {
expectedNodeAffinity := &v1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
NodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchExpressions: []v1.NodeSelectorRequirement{
{
Key: "beta.kubernetes.io/arch",
Operator: "In",
Values: []string{"amd64"},
},
},
},
},
},
}
// Required
a := createAffinity("test", "role", true, "")
assert.Nil(t, a.PodAffinity)
Expand All @@ -42,6 +59,7 @@ func TestCreateAffinity(t *testing.T) {
assert.Equal(t, "test", a.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchLabels["arango_deployment"])
assert.Equal(t, "arangodb", a.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchLabels["app"])
assert.Equal(t, "role", a.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchLabels["role"])
assert.Equal(t, expectedNodeAffinity, a.NodeAffinity)

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

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

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

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

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