Skip to content

Commit

Permalink
Merge pull request #43 from spotahome/devops-742-add-node-affinity
Browse files Browse the repository at this point in the history
Devops 742 add node affinity
  • Loading branch information
jchanam authored Apr 17, 2018
2 parents 2c0492b + c36aec0 commit d396a7f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ go:
go_import_path: github.com/spotahome/redis-operator

before_install:
- curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
- curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.25.2/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
- curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v1.9.4/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/

env:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION := 0.2.2
VERSION := 0.2.3

# Name of this service/application
SERVICE_NAME := redis-operator
Expand Down
5 changes: 5 additions & 0 deletions api/redisfailover/v1alpha2/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1alpha2

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

Expand All @@ -26,6 +27,10 @@ type RedisFailoverSpec struct {
// HardAntiAffinity defines if the PodAntiAffinity on the deployments and
// statefulsets has to be hard (it's soft by default)
HardAntiAffinity bool `json:"hardAntiAffinity,omitempty"`

// NodeAffinity defines the rules for scheduling the Redis and Sentinel
// nodes
NodeAffinity *corev1.NodeAffinity `json:"nodeAffinity,omitempty"`
}

// RedisSettings defines the specification of the redis cluster
Expand Down
53 changes: 27 additions & 26 deletions operator/redisfailover/service/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ func generateRedisStatefulSet(rf *redisfailoverv1alpha2.RedisFailover, labels ma
Labels: labels,
},
Spec: corev1.PodSpec{
Affinity: createPodAntiAffinity(rf.Spec.HardAntiAffinity, labels),
Affinity: &corev1.Affinity{
NodeAffinity: rf.Spec.NodeAffinity,
PodAntiAffinity: createPodAntiAffinity(rf.Spec.HardAntiAffinity, labels),
},
Containers: []corev1.Container{
{
Name: "redis",
Expand Down Expand Up @@ -250,7 +253,10 @@ func generateSentinelDeployment(rf *redisfailoverv1alpha2.RedisFailover, labels
Labels: labels,
},
Spec: corev1.PodSpec{
Affinity: createPodAntiAffinity(rf.Spec.HardAntiAffinity, labels),
Affinity: &corev1.Affinity{
NodeAffinity: rf.Spec.NodeAffinity,
PodAntiAffinity: createPodAntiAffinity(rf.Spec.HardAntiAffinity, labels),
},
InitContainers: []corev1.Container{
{
Name: "sentinel-config-copy",
Expand Down Expand Up @@ -417,12 +423,11 @@ func createRedisExporterContainer() corev1.Container {
Env: []corev1.EnvVar{
{
Name: "REDIS_ALIAS",
ValueFrom:
&corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
},
},
Ports: []corev1.ContainerPort{
Expand Down Expand Up @@ -464,34 +469,30 @@ func createRedisExporterContainer() corev1.Container {
}
}

func createPodAntiAffinity(hard bool, labels map[string]string) *corev1.Affinity {
func createPodAntiAffinity(hard bool, labels map[string]string) *corev1.PodAntiAffinity {
if hard {
// Return a HARD anti-affinity (no same pods on one node)
return &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
{
TopologyKey: hostnameTopologyKey,
LabelSelector: &metav1.LabelSelector{
MatchLabels: labels,
},
return &corev1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
{
TopologyKey: hostnameTopologyKey,
LabelSelector: &metav1.LabelSelector{
MatchLabels: labels,
},
},
},
}
}

// Return a SOFT anti-affinity
return &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
{
Weight: 100,
PodAffinityTerm: corev1.PodAffinityTerm{
TopologyKey: hostnameTopologyKey,
LabelSelector: &metav1.LabelSelector{
MatchLabels: labels,
},
return &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
{
Weight: 100,
PodAffinityTerm: corev1.PodAffinityTerm{
TopologyKey: hostnameTopologyKey,
LabelSelector: &metav1.LabelSelector{
MatchLabels: labels,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion scripts/integration-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function cleanup {
trap cleanup EXIT

echo "=> Preparing minikube for running integration tests"
$SUDO minikube start --vm-driver=none
$SUDO minikube start --vm-driver=none --kubernetes-version=v1.9.4

echo "=> Waiting for minikube to start"
sleep 30
Expand Down

0 comments on commit d396a7f

Please sign in to comment.