Skip to content

Commit 405eede

Browse files
mhuinTristanCacqueray
authored andcommitted
zookeeper: add default PodAntiAffinity toggle in CR
Add a CR parameter that gives a deployer the ability to set a simple PodAntiAffinity policy on the zookeeper replicas, ensuring the pods run on distinct nodes. It is disabled by default. Change-Id: I93b1987759184951dc84222f77c04777fe46f8f6
1 parent 799aa1c commit 405eede

6 files changed

Lines changed: 61 additions & 4 deletions

File tree

api/v1/softwarefactory_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,11 @@ type ZookeeperSpec struct {
498498
// +kubebuilder:validation:Optional
499499
// +kubebuilder:default={"memory": "2Gi", "cpu": "500m"}
500500
Limits *LimitsSpec `json:"limits"`
501+
// Enable node anti-affinity, defaults to false so as not to fail on minikube deployments. This should
502+
// be set to true on production deployments with at least 3 nodes available.
503+
// +kubebuilder:default:=false
504+
// +optional
505+
NodeAntiAffinityEnabled *bool `json:"nodeAntiAffinityEnabled,omitempty"`
501506
}
502507

503508
type CodesearchSpec struct {

api/v1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/sf.softwarefactory-project.io_softwarefactories.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,12 @@ spec:
510510
- cpu
511511
- memory
512512
type: object
513+
nodeAntiAffinityEnabled:
514+
default: false
515+
description: |-
516+
Enable node anti-affinity, defaults to false so as not to fail on minikube deployments. This should
517+
be set to true on production deployments with at least 3 nodes available.
518+
type: boolean
513519
storage:
514520
properties:
515521
className:

controllers/zookeeper.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,41 @@ func (r *SFController) DeployZookeeper() bool {
256256

257257
zk.Spec.Template.Spec.HostAliases = base.CreateHostAliases(r.cr.Spec.HostAliases)
258258

259+
// Node Anti-Affinity
260+
//
261+
// TODO: changes to this should appear in the dry-run/verbose output, and respect user-defined affinities
262+
//
263+
// Also note that to disable this after it's been enabled (but why would you ?) you will have to
264+
// 1. remove the setting in the SF CR
265+
// 2. manually edit the zookeeper statefulset to remove the PodAntiAffinity setting
266+
// This is because we do not want to interfere with custom-set rules that may be set OoB.
267+
aaEnabled := false
268+
if r.cr.Spec.Zookeeper.NodeAntiAffinityEnabled != nil {
269+
aaEnabled = *r.cr.Spec.Zookeeper.NodeAntiAffinityEnabled
270+
}
271+
if aaEnabled {
272+
zkAALabelSelector := metav1.LabelSelector{
273+
MatchLabels: map[string]string{
274+
"app": "sf",
275+
"run": ZookeeperIdent,
276+
},
277+
}
278+
zkRequiredDuringSchedulingIgnoredDuringExecution := []apiv1.PodAffinityTerm{
279+
{
280+
LabelSelector: &zkAALabelSelector,
281+
TopologyKey: "kubernetes.io/hostname",
282+
},
283+
}
284+
zkPodAntiAffinity := apiv1.PodAntiAffinity{
285+
RequiredDuringSchedulingIgnoredDuringExecution: zkRequiredDuringSchedulingIgnoredDuringExecution,
286+
}
287+
zkAffinity := apiv1.Affinity{
288+
PodAntiAffinity: &zkPodAntiAffinity,
289+
}
290+
zk.Spec.Template.Spec.Affinity = &zkAffinity
291+
}
292+
293+
// we want to ensure the replica count is always at 3
259294
replicaCount := int32(ZookeeperReplicas)
260295

261296
// If we are bumping from 1 to 3 replicas we need to backup zookeeper first.

docs/reference/CHANGELOG.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ All notable changes to this project will be documented in this file.
88
### Changed
99

1010
- Deploy zookeeper as a 3-replica ensemble to ensure high availability. Ensure you have enough resources on the cluster to deploy 3 zookeeper pods.
11-
While the process handles snapshotting Zookeeper's data and restoring it to all pods after the extra replicas rollout, you are **strongly** advised
12-
to perform a backup with the sf-operator CLI before upgrading to avoid accidental data loss.
13-
PKI certificates will be recreated to reflect the new ensemble setup.
11+
While the upgrade process handles snapshotting Zookeeper's data and restoring it to all pods after the extra replicas rollout,
12+
**You are strongly advised to backup your zookeeper data before upgrading to avoid accidental data loss.**
13+
The PKI certificates will be recreated to reflect the new ensemble setup, and the zookeeper statefulset will be re-rolled out with its updated
14+
configuration.
15+
It is advised to shut down any zookeeper clients (specifically zuul executors) prior to the upgrade to avoid connection loss exceptions.
1416
- Added `PodDisruptionBudget` and `RollingUpdate` strategy to the Zookeeper controller, in accordance with zookeeper
15-
being now deployed as a 3-replica ensemble (see below).
17+
being now deployed as a 3-replica ensemble.
18+
- Added a PodAntiAffinity toggle for Zookeeper in the Custom Resource Definition (disabled by default). If enabled, the Zookeeper
19+
statefulset will be configured so that its pods spawn on distinct worker nodes. Make sure your deployment cluster has at least three
20+
available nodes, otherwise Software Factory will fail to reconcile when the toggle is enabled.
1621

1722
### Deprecated
1823
### Removed

docs/reference/api/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ _Appears in:_
549549
| --- | --- | --- |
550550
| `storage` _[StorageSpec](#storagespec)_ | | -|
551551
| `limits` _[LimitsSpec](#limitsspec)_ | Memory/CPU Limit | {map[cpu:500m memory:2Gi]}|
552+
| `nodeAntiAffinityEnabled` _boolean_ | Enable node anti-affinity, defaults to false so as not to fail on minikube deployments. This should be set to true on production deployments with at least 3 nodes available. | {false}|
552553

553554

554555
#### ZuulExecutorSpec

0 commit comments

Comments
 (0)