Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Anti Affinity rules added to deployment spec #202

Merged
merged 2 commits into from
Jun 6, 2022
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
3 changes: 3 additions & 0 deletions .changelog/202.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
Define anti-affinity rules so that the scheduler will attempt to evenly spread gateway pods across all available nodes
```
2 changes: 2 additions & 0 deletions internal/k8s/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const (

consulCALocalPath = "/consul/tls"
consulCALocalFile = consulCALocalPath + "/ca.pem"

k8sHostnameTopologyKey = "kubernetes.io/hostname"
)

type Builder interface {
Expand Down
17 changes: 17 additions & 0 deletions internal/k8s/builder/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,24 @@ func (b *GatewayDeploymentBuilder) podSpec() corev1.PodSpec {
defaultServiceAccount = b.gateway.Name
}

labels := utils.LabelsForGateway(b.gateway)

return corev1.PodSpec{
Affinity: &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
{
Weight: 1,
PodAffinityTerm: corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: labels,
},
TopologyKey: k8sHostnameTopologyKey,
},
},
},
},
},
NodeSelector: b.gwConfig.Spec.NodeSelector,
ServiceAccountName: orDefault(b.gwConfig.Spec.ConsulSpec.AuthSpec.Account, defaultServiceAccount),
// the init container copies the binary into the
Expand Down
12 changes: 12 additions & 0 deletions internal/k8s/builder/testdata/clusterip.deployment.golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ spec:
api-gateway.consul.hashicorp.com/name: test-clusterip
api-gateway.consul.hashicorp.com/namespace: ""
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- podAffinityTerm:
labelSelector:
matchLabels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-clusterip
api-gateway.consul.hashicorp.com/namespace: ""
topologyKey: kubernetes.io/hostname
weight: 1
containers:
- command:
- /bin/sh
Expand Down
12 changes: 12 additions & 0 deletions internal/k8s/builder/testdata/loadbalancer.deployment.golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ spec:
api-gateway.consul.hashicorp.com/name: test-loadbalancer
api-gateway.consul.hashicorp.com/namespace: ""
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- podAffinityTerm:
labelSelector:
matchLabels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-loadbalancer
api-gateway.consul.hashicorp.com/namespace: ""
topologyKey: kubernetes.io/hostname
weight: 1
containers:
- command:
- /bin/sh
Expand Down
22 changes: 17 additions & 5 deletions internal/k8s/builder/testdata/max-instances.deployment.golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ metadata:
labels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-multiple-instances
api-gateway.consul.hashicorp.com/name: test-max-instances
api-gateway.consul.hashicorp.com/namespace: ""
name: test-multiple-instances
name: test-max-instances
spec:
replicas: 5
selector:
matchLabels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-multiple-instances
api-gateway.consul.hashicorp.com/name: test-max-instances
api-gateway.consul.hashicorp.com/namespace: ""
strategy: {}
template:
Expand All @@ -23,9 +23,21 @@ spec:
labels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-multiple-instances
api-gateway.consul.hashicorp.com/name: test-max-instances
api-gateway.consul.hashicorp.com/namespace: ""
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- podAffinityTerm:
labelSelector:
matchLabels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-max-instances
api-gateway.consul.hashicorp.com/namespace: ""
topologyKey: kubernetes.io/hostname
weight: 1
containers:
- command:
- /bin/sh
Expand All @@ -36,7 +48,7 @@ spec:
exec /bootstrap/consul-api-gateway exec -log-json \
-log-level info \
-gateway-host "$(IP)" \
-gateway-name test-multiple-instances \
-gateway-name test-max-instances \
-gateway-namespace test \
-consul-http-address $(HOST_IP) \
-consul-http-port 8500 \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ metadata:
labels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-multiple-instances
api-gateway.consul.hashicorp.com/name: test-max-instances
api-gateway.consul.hashicorp.com/namespace: ""
name: test-multiple-instances
name: test-max-instances
spec:
ports:
- name: http
Expand All @@ -19,7 +19,7 @@ spec:
selector:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-multiple-instances
api-gateway.consul.hashicorp.com/name: test-max-instances
api-gateway.consul.hashicorp.com/namespace: ""
type: ClusterIP
status:
Expand Down
2 changes: 1 addition & 1 deletion internal/k8s/builder/testdata/max-instances.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ spec:
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: Gateway
metadata:
name: test-multiple-instances
name: test-max-instances
spec:
gatewayClassName: test-gateway-class
listeners:
Expand Down
22 changes: 17 additions & 5 deletions internal/k8s/builder/testdata/min-instances.deployment.golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ metadata:
labels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-multiple-instances
api-gateway.consul.hashicorp.com/name: test-min-instances
api-gateway.consul.hashicorp.com/namespace: ""
name: test-multiple-instances
name: test-min-instances
spec:
replicas: 5
selector:
matchLabels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-multiple-instances
api-gateway.consul.hashicorp.com/name: test-min-instances
api-gateway.consul.hashicorp.com/namespace: ""
strategy: {}
template:
Expand All @@ -23,9 +23,21 @@ spec:
labels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-multiple-instances
api-gateway.consul.hashicorp.com/name: test-min-instances
api-gateway.consul.hashicorp.com/namespace: ""
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- podAffinityTerm:
labelSelector:
matchLabels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-min-instances
api-gateway.consul.hashicorp.com/namespace: ""
topologyKey: kubernetes.io/hostname
weight: 1
containers:
- command:
- /bin/sh
Expand All @@ -36,7 +48,7 @@ spec:
exec /bootstrap/consul-api-gateway exec -log-json \
-log-level info \
-gateway-host "$(IP)" \
-gateway-name test-multiple-instances \
-gateway-name test-min-instances \
-gateway-namespace test \
-consul-http-address $(HOST_IP) \
-consul-http-port 8500 \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ metadata:
labels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-multiple-instances
api-gateway.consul.hashicorp.com/name: test-min-instances
api-gateway.consul.hashicorp.com/namespace: ""
name: test-multiple-instances
name: test-min-instances
spec:
ports:
- name: http
Expand All @@ -19,7 +19,7 @@ spec:
selector:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-multiple-instances
api-gateway.consul.hashicorp.com/name: test-min-instances
api-gateway.consul.hashicorp.com/namespace: ""
type: ClusterIP
status:
Expand Down
2 changes: 1 addition & 1 deletion internal/k8s/builder/testdata/min-instances.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ spec:
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: Gateway
metadata:
name: test-multiple-instances
name: test-min-instances
spec:
gatewayClassName: test-gateway-class
listeners:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ spec:
api-gateway.consul.hashicorp.com/name: test-multiple-instances
api-gateway.consul.hashicorp.com/namespace: ""
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- podAffinityTerm:
labelSelector:
matchLabels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-multiple-instances
api-gateway.consul.hashicorp.com/namespace: ""
topologyKey: kubernetes.io/hostname
weight: 1
containers:
- command:
- /bin/sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ spec:
api-gateway.consul.hashicorp.com/name: test-static-mapping
api-gateway.consul.hashicorp.com/namespace: ""
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- podAffinityTerm:
labelSelector:
matchLabels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: test-static-mapping
api-gateway.consul.hashicorp.com/namespace: ""
topologyKey: kubernetes.io/hostname
weight: 1
containers:
- command:
- /bin/sh
Expand Down
12 changes: 12 additions & 0 deletions internal/k8s/builder/testdata/tls-cert.deployment.golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ spec:
api-gateway.consul.hashicorp.com/name: tls-cert-test
api-gateway.consul.hashicorp.com/namespace: ""
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- podAffinityTerm:
labelSelector:
matchLabels:
api-gateway.consul.hashicorp.com/created: "-62135596800"
api-gateway.consul.hashicorp.com/managed: "true"
api-gateway.consul.hashicorp.com/name: tls-cert-test
api-gateway.consul.hashicorp.com/namespace: ""
topologyKey: kubernetes.io/hostname
weight: 1
containers:
- command:
- /bin/sh
Expand Down