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

Pod affinity #8

Merged
merged 3 commits into from
Feb 26, 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
9 changes: 6 additions & 3 deletions pkg/deployment/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ func (d *Deployment) createReadinessProbe(apiObject *api.ArangoDeployment, group
// ensurePods creates all Pods listed in member status
func (d *Deployment) ensurePods(apiObject *api.ArangoDeployment) error {
kubecli := d.deps.KubeCli
owner := apiObject.AsOwner()

if err := apiObject.ForeachServerGroup(func(group api.ServerGroup, spec api.ServerGroupSpec, status *api.MemberStatusList) error {
for _, m := range *status {
Expand All @@ -305,7 +304,7 @@ func (d *Deployment) ensurePods(apiObject *api.ArangoDeployment) error {
if err != nil {
return maskAny(err)
}
if err := k8sutil.CreateArangodPod(kubecli, apiObject, role, m.ID, m.PersistentVolumeClaimName, apiObject.Spec.Image, apiObject.Spec.ImagePullPolicy, args, env, livenessProbe, readinessProbe, owner); err != nil {
if err := k8sutil.CreateArangodPod(kubecli, apiObject.Spec.IsDevelopment(), apiObject, role, m.ID, m.PersistentVolumeClaimName, apiObject.Spec.Image, apiObject.Spec.ImagePullPolicy, args, env, livenessProbe, readinessProbe); err != nil {
return maskAny(err)
}
} else if group.IsArangosync() {
Expand All @@ -315,7 +314,11 @@ func (d *Deployment) ensurePods(apiObject *api.ArangoDeployment) error {
if err != nil {
return maskAny(err)
}
if err := k8sutil.CreateArangoSyncPod(kubecli, apiObject, role, m.ID, apiObject.Spec.Sync.Image, apiObject.Spec.Sync.ImagePullPolicy, args, env, livenessProbe, owner); err != nil {
affinityWithRole := ""
if group == api.ServerGroupSyncWorkers {
affinityWithRole = api.ServerGroupDBServers.AsRole()
}
if err := k8sutil.CreateArangoSyncPod(kubecli, apiObject.Spec.IsDevelopment(), apiObject, role, m.ID, apiObject.Spec.Sync.Image, apiObject.Spec.Sync.ImagePullPolicy, args, env, livenessProbe, affinityWithRole); err != nil {
return maskAny(err)
}
}
Expand Down
76 changes: 76 additions & 0 deletions pkg/util/k8sutil/affinity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// DISCLAIMER
//
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Ewout Prangsma
//

package k8sutil

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

// createAffinity creates pod anti-affinity for the given role.
// role contains the name of the role to configure any-affinity with.
// affinityWithRole contains the role to configure affinity with.
func createAffinity(deploymentName, role string, required bool, affinityWithRole string) *v1.Affinity {
a := &v1.Affinity{
PodAntiAffinity: &v1.PodAntiAffinity{},
}
labels := LabelsForDeployment(deploymentName, role)
labelSelector := &metav1.LabelSelector{
MatchLabels: labels,
}
if required {
a.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution = append(a.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution, v1.PodAffinityTerm{
LabelSelector: labelSelector,
TopologyKey: TopologyKeyHostname,
})
} else {
a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution = append(a.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution, v1.WeightedPodAffinityTerm{
Weight: 1,
PodAffinityTerm: v1.PodAffinityTerm{
LabelSelector: labelSelector,
TopologyKey: TopologyKeyHostname,
},
})
}
if affinityWithRole != "" {
a.PodAffinity = &v1.PodAffinity{}
labelSelector := &metav1.LabelSelector{
MatchLabels: LabelsForDeployment(deploymentName, affinityWithRole),
}
if required {
a.PodAffinity.RequiredDuringSchedulingIgnoredDuringExecution = append(a.PodAffinity.RequiredDuringSchedulingIgnoredDuringExecution, v1.PodAffinityTerm{
LabelSelector: labelSelector,
TopologyKey: TopologyKeyHostname,
})
} else {
a.PodAffinity.PreferredDuringSchedulingIgnoredDuringExecution = append(a.PodAffinity.PreferredDuringSchedulingIgnoredDuringExecution, v1.WeightedPodAffinityTerm{
Weight: 1,
PodAffinityTerm: v1.PodAffinityTerm{
LabelSelector: labelSelector,
TopologyKey: TopologyKeyHostname,
},
})
}
}
return a
}
1 change: 1 addition & 0 deletions pkg/util/k8sutil/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ const (
// K8s constants
ClusterIPNone = "None"
TolerateUnreadyEndpointsAnnotation = "service.alpha.kubernetes.io/tolerate-unready-endpoints"
TopologyKeyHostname = "kubernetes.io/hostname"
)
18 changes: 12 additions & 6 deletions pkg/util/k8sutil/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func newPod(deploymentName, ns, role, id string) v1.Pod {
// CreateArangodPod creates a Pod that runs `arangod`.
// If the pod already exists, nil is returned.
// If another error occurs, that error is returned.
func CreateArangodPod(kubecli kubernetes.Interface, deployment metav1.Object, role, id, pvcName, image string, imagePullPolicy v1.PullPolicy,
args []string, env map[string]string, livenessProbe *HTTPProbeConfig, readinessProbe *HTTPProbeConfig, owner metav1.OwnerReference) error {
func CreateArangodPod(kubecli kubernetes.Interface, developmentMode bool, deployment APIObject, role, id, pvcName, image string, imagePullPolicy v1.PullPolicy,
args []string, env map[string]string, livenessProbe *HTTPProbeConfig, readinessProbe *HTTPProbeConfig) error {
// Prepare basic pod
p := newPod(deployment.GetName(), deployment.GetNamespace(), role, id)

Expand Down Expand Up @@ -157,7 +157,10 @@ func CreateArangodPod(kubecli kubernetes.Interface, deployment metav1.Object, ro
p.Spec.Volumes = append(p.Spec.Volumes, vol)
}

if err := createPod(kubecli, &p, deployment.GetNamespace(), owner); err != nil {
// Add (anti-)affinity
p.Spec.Affinity = createAffinity(deployment.GetName(), role, !developmentMode, "")

if err := createPod(kubecli, &p, deployment.GetNamespace(), deployment.AsOwner()); err != nil {
return maskAny(err)
}
return nil
Expand All @@ -166,16 +169,19 @@ func CreateArangodPod(kubecli kubernetes.Interface, deployment metav1.Object, ro
// CreateArangoSyncPod creates a Pod that runs `arangosync`.
// If the pod already exists, nil is returned.
// If another error occurs, that error is returned.
func CreateArangoSyncPod(kubecli kubernetes.Interface, deployment metav1.Object, role, id, image string, imagePullPolicy v1.PullPolicy,
args []string, env map[string]string, livenessProbe *HTTPProbeConfig, owner metav1.OwnerReference) error {
func CreateArangoSyncPod(kubecli kubernetes.Interface, developmentMode bool, deployment APIObject, role, id, image string, imagePullPolicy v1.PullPolicy,
args []string, env map[string]string, livenessProbe *HTTPProbeConfig, affinityWithRole string) error {
// Prepare basic pod
p := newPod(deployment.GetName(), deployment.GetNamespace(), role, id)

// Add arangosync container
c := arangosyncContainer(p.GetName(), image, imagePullPolicy, args, env, livenessProbe)
p.Spec.Containers = append(p.Spec.Containers, c)

if err := createPod(kubecli, &p, deployment.GetNamespace(), owner); err != nil {
// Add (anti-)affinity
p.Spec.Affinity = createAffinity(deployment.GetName(), role, !developmentMode, affinityWithRole)

if err := createPod(kubecli, &p, deployment.GetNamespace(), deployment.AsOwner()); err != nil {
return maskAny(err)
}
return nil
Expand Down