Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add Pod Anti Affinity
 * use `cluster_labels` for pod anti affinity
 * use `node_readiness_label` when defined
 * added property `enable_pod_antiaffinity` to operator config
 * added property `pod_antiaffinity_topology_key`
  • Loading branch information
ocaner-biz committed Feb 15, 2019
commit d5481e8ab05061dbd2d9039fdce4d9103082260b
32 changes: 20 additions & 12 deletions pkg/cluster/k8sres.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,24 +290,24 @@ func nodeAffinity(nodeReadinessLabel map[string]string) *v1.Affinity {
}
}

func generatePodAffinity(team string, version string) *v1.Affinity {
func generatePodAffinity(labels labels.Set, topologyKey string, nodeAffinity *v1.Affinity) *v1.Affinity {
// generate pod anti affinity to avoid multiple on instances on the same node
matchLabels := make(map[string]string)

matchLabels["application"] = "spilo"
matchLabels["team"] = team
matchLabels["version"] = version

return &v1.Affinity{
podAffinity := v1.Affinity{
PodAntiAffinity: &v1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []v1.PodAffinityTerm{{
LabelSelector: &metav1.LabelSelector{
MatchLabels: matchLabels,
MatchLabels: labels,
},
TopologyKey: "kubernetes.io/hostname",
TopologyKey: topologyKey,
}},
},
}

if nodeAffinity != nil && nodeAffinity.NodeAffinity != nil {
podAffinity.NodeAffinity = nodeAffinity.NodeAffinity
}

return &podAffinity
}

func tolerations(tolerationsSpec *[]v1.Toleration, podToleration map[string]string) []v1.Toleration {
Expand Down Expand Up @@ -439,6 +439,8 @@ func generatePodTemplate(
kubeIAMRole string,
priorityClassName string,
shmVolume bool,
podAntiAffinity bool,
podAntiAffinityTopologyKey string,
) (*v1.PodTemplateSpec, error) {

terminateGracePeriodSeconds := terminateGracePeriod
Expand All @@ -457,7 +459,11 @@ func generatePodTemplate(
addShmVolume(&podSpec)
}

podSpec.Affinity = generatePodAffinity(labels.Get("team"), labels.Get("version"))
if podAntiAffinity {
podSpec.Affinity = generatePodAffinity(labels, podAntiAffinityTopologyKey, nodeAffinity)
} else if nodeAffinity != nil {
podSpec.Affinity = nodeAffinity
}

if priorityClassName != "" {
podSpec.PriorityClassName = priorityClassName
Expand Down Expand Up @@ -831,7 +837,9 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*v1beta1.State
c.OpConfig.PodServiceAccountName,
c.OpConfig.KubeIAMRole,
effectivePodPriorityClassName,
mountShmVolumeNeeded(c.OpConfig, spec)); err != nil {
mountShmVolumeNeeded(c.OpConfig, spec),
c.OpConfig.EnablePodAntiAffinity,
c.OpConfig.PodAntiAffinityTopologyKey); err != nil {
return nil, fmt.Errorf("could not generate pod template: %v", err)
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/util/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ type Config struct {
EnableMasterLoadBalancer bool `name:"enable_master_load_balancer" default:"true"`
EnableReplicaLoadBalancer bool `name:"enable_replica_load_balancer" default:"false"`
CustomServiceAnnotations map[string]string `name:"custom_service_annotations"`
EnablePodAntiAffinity bool `name:"enable_pod_antiaffinity" default:"false"`
PodAntiAffinityTopologyKey string `name:"pod_antiaffinity_topology_key" default:"kubernetes.io/hostname"`
// deprecated and kept for backward compatibility
EnableLoadBalancer *bool `name:"enable_load_balancer"`
MasterDNSNameFormat StringTemplate `name:"master_dns_name_format" default:"{cluster}.{team}.{hostedzone}"`
Expand Down