Skip to content

Commit 3ca26d0

Browse files
b1zzuFxKu
andauthored
Make PodDisruptionBudget master label selector optional (#2364)
* Make PDB master label selector optional * Update pkg/apis/acid.zalan.do/v1/crds.go --------- Co-authored-by: Felix Kunde <[email protected]>
1 parent 182a6d9 commit 3ca26d0

File tree

8 files changed

+52
-5
lines changed

8 files changed

+52
-5
lines changed

docs/reference/operator_parameters.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ configuration they are grouped under the `kubernetes` key.
323323
replaced by the cluster name. Only the `{cluster}` placeholders is allowed in
324324
the template.
325325

326+
* **pdb_master_label_selector**
327+
By default the PDB will match the master role hence preventing nodes to be
328+
drained if the node_readiness_label is not used. This option if set to `false`
329+
will not add the `spilo-role=master` selector to the PDB.
330+
326331
* **enable_pod_disruption_budget**
327332
PDB is enabled by default to protect the cluster from voluntarily disruptions
328333
and hence unwanted DB downtime. However, on some cloud providers it could be
@@ -431,7 +436,7 @@ configuration they are grouped under the `kubernetes` key.
431436
environment if they not if conflict with the environment variables generated
432437
by the operator. The WAL location (bucket path) can be overridden, though.
433438
The default is empty.
434-
439+
435440
* **pod_environment_secret**
436441
similar to pod_environment_configmap but referencing a secret with custom
437442
environment variables. Because the secret is not allowed to exist in a
@@ -577,7 +582,7 @@ effect, and the parameters are grouped under the `timeouts` key in the
577582
CRD-based configuration.
578583

579584
* **PatroniAPICheckInterval**
580-
the interval between consecutive attempts waiting for the return of
585+
the interval between consecutive attempts waiting for the return of
581586
Patroni Api. The default is `1s`.
582587

583588
* **PatroniAPICheckTimeout**
@@ -651,7 +656,7 @@ In the CRD-based configuration they are grouped under the `load_balancer` key.
651656
balancers. Allowed values are `Cluster` (default) and `Local`.
652657

653658
* **master_dns_name_format**
654-
defines the DNS name string template for the master load balancer cluster.
659+
defines the DNS name string template for the master load balancer cluster.
655660
The default is `{cluster}.{namespace}.{hostedzone}`, where `{cluster}` is
656661
replaced by the cluster name, `{namespace}` is replaced with the namespace
657662
and `{hostedzone}` is replaced with the hosted zone (the value of the
@@ -816,7 +821,7 @@ grouped under the `logical_backup` key.
816821
is specified, no argument will be passed to `aws s3` command. Default: "AES256".
817822

818823
* **logical_backup_s3_retention_time**
819-
Specify a retention time for logical backups stored in S3. Backups older than the specified retention
824+
Specify a retention time for logical backups stored in S3. Backups older than the specified retention
820825
time will be deleted after a new backup was uploaded. If empty, all backups will be kept. Example values are
821826
"3 days", "2 weeks", or "1 month". The default is empty.
822827

pkg/apis/acid.zalan.do/v1/crds.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,9 @@ var OperatorConfigCRDResourceValidation = apiextv1.CustomResourceValidation{
13941394
"pdb_name_format": {
13951395
Type: "string",
13961396
},
1397+
"pdb_master_label_selector": {
1398+
Type: "boolean",
1399+
},
13971400
"persistent_volume_claim_retention_policy": {
13981401
Type: "object",
13991402
Properties: map[string]apiextv1.JSONSchemaProps{

pkg/apis/acid.zalan.do/v1/operator_configuration_type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type KubernetesMetaConfiguration struct {
6868
AdditionalPodCapabilities []string `json:"additional_pod_capabilities,omitempty"`
6969
WatchedNamespace string `json:"watched_namespace,omitempty"`
7070
PDBNameFormat config.StringTemplate `json:"pdb_name_format,omitempty"`
71+
PDBMasterLabelSelector *bool `json:"pdb_master_label_selector,omitempty"`
7172
EnablePodDisruptionBudget *bool `json:"enable_pod_disruption_budget,omitempty"`
7273
StorageResizeMode string `json:"storage_resize_mode,omitempty"`
7374
EnableInitContainers *bool `json:"enable_init_containers,omitempty"`

pkg/apis/acid.zalan.do/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.

pkg/cluster/k8sres.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2150,12 +2150,19 @@ func (c *Cluster) generateStandbyEnvironment(description *acidv1.StandbyDescript
21502150
func (c *Cluster) generatePodDisruptionBudget() *policyv1.PodDisruptionBudget {
21512151
minAvailable := intstr.FromInt(1)
21522152
pdbEnabled := c.OpConfig.EnablePodDisruptionBudget
2153+
pdbMasterLabelSelector := c.OpConfig.PDBMasterLabelSelector
21532154

21542155
// if PodDisruptionBudget is disabled or if there are no DB pods, set the budget to 0.
21552156
if (pdbEnabled != nil && !(*pdbEnabled)) || c.Spec.NumberOfInstances <= 0 {
21562157
minAvailable = intstr.FromInt(0)
21572158
}
21582159

2160+
// define label selector and add the master role selector if enabled
2161+
labels := c.labelsSet(false)
2162+
if pdbMasterLabelSelector == nil || *c.OpConfig.PDBMasterLabelSelector {
2163+
labels[c.OpConfig.PodRoleLabel] = string(Master)
2164+
}
2165+
21592166
return &policyv1.PodDisruptionBudget{
21602167
ObjectMeta: metav1.ObjectMeta{
21612168
Name: c.podDisruptionBudgetName(),
@@ -2166,7 +2173,7 @@ func (c *Cluster) generatePodDisruptionBudget() *policyv1.PodDisruptionBudget {
21662173
Spec: policyv1.PodDisruptionBudgetSpec{
21672174
MinAvailable: &minAvailable,
21682175
Selector: &metav1.LabelSelector{
2169-
MatchLabels: c.roleLabelsSet(false, Master),
2176+
MatchLabels: labels,
21702177
},
21712178
},
21722179
}

pkg/cluster/k8sres_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,6 +2379,30 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
23792379
},
23802380
},
23812381
},
2382+
// With PDBMasterLabelSelector disabled.
2383+
{
2384+
New(
2385+
Config{OpConfig: config.Config{Resources: config.Resources{ClusterNameLabel: "cluster-name", PodRoleLabel: "spilo-role"}, PDBNameFormat: "postgres-{cluster}-pdb", PDBMasterLabelSelector: util.False()}},
2386+
k8sutil.KubernetesClient{},
2387+
acidv1.Postgresql{
2388+
ObjectMeta: metav1.ObjectMeta{Name: "myapp-database", Namespace: "myapp"},
2389+
Spec: acidv1.PostgresSpec{TeamID: "myapp", NumberOfInstances: 3}},
2390+
logger,
2391+
eventRecorder),
2392+
policyv1.PodDisruptionBudget{
2393+
ObjectMeta: metav1.ObjectMeta{
2394+
Name: "postgres-myapp-database-pdb",
2395+
Namespace: "myapp",
2396+
Labels: map[string]string{"team": "myapp", "cluster-name": "myapp-database"},
2397+
},
2398+
Spec: policyv1.PodDisruptionBudgetSpec{
2399+
MinAvailable: util.ToIntStr(1),
2400+
Selector: &metav1.LabelSelector{
2401+
MatchLabels: map[string]string{"cluster-name": "myapp-database"},
2402+
},
2403+
},
2404+
},
2405+
},
23822406
}
23832407

23842408
for _, tt := range tests {

pkg/controller/operator_config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func (c *Controller) importConfigurationFromCRD(fromCRD *acidv1.OperatorConfigur
8282
result.ClusterDomain = util.Coalesce(fromCRD.Kubernetes.ClusterDomain, "cluster.local")
8383
result.WatchedNamespace = fromCRD.Kubernetes.WatchedNamespace
8484
result.PDBNameFormat = fromCRD.Kubernetes.PDBNameFormat
85+
result.PDBMasterLabelSelector = util.CoalesceBool(fromCRD.Kubernetes.PDBMasterLabelSelector, util.True())
8586
result.EnablePodDisruptionBudget = util.CoalesceBool(fromCRD.Kubernetes.EnablePodDisruptionBudget, util.True())
8687
result.StorageResizeMode = util.Coalesce(fromCRD.Kubernetes.StorageResizeMode, "pvc")
8788
result.EnableInitContainers = util.CoalesceBool(fromCRD.Kubernetes.EnableInitContainers, util.True())

pkg/util/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ type Config struct {
220220
ReplicaDNSNameFormat StringTemplate `name:"replica_dns_name_format" default:"{cluster}-repl.{namespace}.{hostedzone}"`
221221
ReplicaLegacyDNSNameFormat StringTemplate `name:"replica_legacy_dns_name_format" default:"{cluster}-repl.{team}.{hostedzone}"`
222222
PDBNameFormat StringTemplate `name:"pdb_name_format" default:"postgres-{cluster}-pdb"`
223+
PDBMasterLabelSelector *bool `name:"pdb_master_label_selector" default:"true"`
223224
EnablePodDisruptionBudget *bool `name:"enable_pod_disruption_budget" default:"true"`
224225
EnableInitContainers *bool `name:"enable_init_containers" default:"true"`
225226
EnableSidecars *bool `name:"enable_sidecars" default:"true"`

0 commit comments

Comments
 (0)