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

Devops 657 add flag hard antiaffinity #17

Merged
merged 7 commits into from
Jan 16, 2018
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
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# The following are targers that do not exist in the filesystem as real files and should be always executed by make
.PHONY: default build deps-development docker-build shell run image unit-test test generate go-generate get-deps update-deps
.PHONY: default build deps-development docker-build shell run image unit-test test generate go-generate get-deps update-deps testing
VERSION := 0.1.5

# Name of this service/application
Expand Down Expand Up @@ -81,6 +81,10 @@ image: deps-development
-t $(REPOSITORY):$(BRANCH) \
-f $(APP_DIR)/Dockerfile \
.

testing: image
Copy link

@TimJones TimJones Jan 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this committed by accident? If not, it should be named push or something to indicate function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I want it to be there and make developing easyer

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You missed the 2nd part of the comment :(

docker push $(REPOSITORY):$(BRANCH)

tag:
git tag $(VERSION)

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,22 @@ metadata:
name: myredisfailover
namespace: mynamespace
spec:
hardAntiAffinity: false # Optional. Value by default. If true, the pods will not be scheduled on the same node.
sentinel:
replicas: 3 # Optional. Value by default, can be set higher.
resources: # Optional. If not set, it won't be defined on created reosurces
replicas: 3 # Optional. Value by default, can be set higher.
resources: # Optional. If not set, it won't be defined on created reosurces
requests:
cpu: 100m
limits:
memory: 100Mi
redis:
replicas: 3 # Optional. Value by default, can be set higher.
resources: # Optional. If not set, it won't be defined on created reosurces
replicas: 3 # Optional. Value by default, can be set higher.
resources: # Optional. If not set, it won't be defined on created reosurces
requests:
cpu: 100m
limits:
memory: 100Mi
exporter: false # Optional. False by default. Adds a redis-exporter container to export metrics.
exporter: false # Optional. False by default. Adds a redis-exporter container to export metrics.
~~~~

## Creation pipeline
Expand Down Expand Up @@ -125,4 +126,3 @@ You can do the following commands with make:
`make update-deps`
* Build the app image.
`make image`

65 changes: 41 additions & 24 deletions pkg/failover/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,18 +429,7 @@ func (r *RedisFailoverKubeClient) CreateSentinelDeployment(rf *RedisFailover) er
Labels: labels,
},
Spec: v1.PodSpec{
Affinity: &v1.Affinity{
PodAntiAffinity: &v1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []v1.PodAffinityTerm{
v1.PodAffinityTerm{
TopologyKey: hostnameTopologyKey,
LabelSelector: &metav1.LabelSelector{
MatchLabels: labels,
},
},
},
},
},
Affinity: createPodAntiAffinity(rf.Spec.HardAntiAffinity, labels),
InitContainers: []v1.Container{
v1.Container{
Name: "sentinel-config",
Expand Down Expand Up @@ -579,18 +568,7 @@ func (r *RedisFailoverKubeClient) CreateRedisStatefulset(rf *RedisFailover) erro
Labels: labels,
},
Spec: v1.PodSpec{
Affinity: &v1.Affinity{
PodAntiAffinity: &v1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []v1.PodAffinityTerm{
v1.PodAffinityTerm{
TopologyKey: hostnameTopologyKey,
LabelSelector: &metav1.LabelSelector{
MatchLabels: labels,
},
},
},
},
},
Affinity: createPodAntiAffinity(rf.Spec.HardAntiAffinity, labels),
InitContainers: []v1.Container{
v1.Container{
Name: "redis-config",
Expand Down Expand Up @@ -761,6 +739,7 @@ func (r *RedisFailoverKubeClient) createPodDisruptionBudget(rf *RedisFailover, n
func (r *RedisFailoverKubeClient) UpdateSentinelDeployment(rf *RedisFailover) error {
namespace := rf.Metadata.Namespace
logger := r.logger.WithField(logNameField, rf.Metadata.Name).WithField(logNamespaceField, rf.Metadata.Namespace)
labels := generateLabels(sentinelRoleName, rf.Metadata.Name)

quorum := rf.GetQuorum()
replicas := rf.Spec.Sentinel.Replicas
Expand All @@ -784,6 +763,7 @@ func (r *RedisFailoverKubeClient) UpdateSentinelDeployment(rf *RedisFailover) er
oldSD.Spec.Template.Spec.InitContainers[0].Env = initEnv
oldSD.Spec.Template.Spec.Containers[0].Image = getRedisImage(rf)
oldSD.Spec.Template.Spec.Containers[0].Resources = getSentinelResources(rf.Spec)
oldSD.Spec.Template.Spec.Affinity = createPodAntiAffinity(rf.Spec.HardAntiAffinity, labels)

if err := r.waitForStatefulset(r.GetRedisName(rf), namespace, rf.Spec.Redis.Replicas, logger); err != nil {
return err
Expand All @@ -800,6 +780,7 @@ func (r *RedisFailoverKubeClient) UpdateSentinelDeployment(rf *RedisFailover) er
func (r *RedisFailoverKubeClient) UpdateRedisStatefulset(rf *RedisFailover) error {
namespace := rf.Metadata.Namespace
logger := r.logger.WithField(logNameField, rf.Metadata.Name).WithField(logNamespaceField, rf.Metadata.Namespace)
labels := generateLabels(redisRoleName, rf.Metadata.Name)

replicas := rf.Spec.Redis.Replicas

Expand All @@ -811,6 +792,7 @@ func (r *RedisFailoverKubeClient) UpdateRedisStatefulset(rf *RedisFailover) erro
oldSS.Spec.Replicas = &replicas
oldSS.Spec.Template.Spec.Containers[0].Resources = getRedisResources(rf.Spec)
oldSS.Spec.Template.Spec.Containers[0].Image = getRedisImage(rf)
oldSS.Spec.Template.Spec.Affinity = createPodAntiAffinity(rf.Spec.HardAntiAffinity, labels)

if rf.Spec.Redis.Exporter {
found := false
Expand Down Expand Up @@ -1024,3 +1006,38 @@ func createRedisExporterContainer() v1.Container {
},
}
}

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

// Return a SOFT anti-affinity
return &v1.Affinity{
PodAntiAffinity: &v1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []v1.WeightedPodAffinityTerm{
v1.WeightedPodAffinityTerm{
Weight: 100,
PodAffinityTerm: v1.PodAffinityTerm{
TopologyKey: hostnameTopologyKey,
LabelSelector: &metav1.LabelSelector{
MatchLabels: labels,
},
},
},
},
},
}
}
Loading