Skip to content

Commit 2782a60

Browse files
LoadBalancer Kill Switch (#581)
* Add option to disable the load balancers (by modifying the selectors) * Test forced pod restart (to terminate existing connections) * Disable pod restart * Add generated crd --------- Co-authored-by: Ulrich Schreiner <[email protected]>
1 parent 5cea697 commit 2782a60

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

api/v1/postgres_types.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const (
6969
SpiloRoleLabelValueMaster = "master"
7070
SpiloRoleLabelValueStandbyLeader = "standby_leader"
7171
StatefulsetPodNameLabelName = "statefulset.kubernetes.io/pod-name"
72+
ClusterNameLabelName = "cluster-name"
7273

7374
teamIDPrefix = "pg"
7475

@@ -84,6 +85,7 @@ const (
8485
defaultPostgresParamValueWalKeepSegments = "64"
8586
defaultPostgresParamValueWalKeepSize = "1GB"
8687
defaultPostgresParamValuePGStatStatementsMax = "500"
88+
defaultSelectorDisableValue = "selector-disabled"
8789
defaultPostgresParamValuePasswordEncryption = "scram-sha-256" // nolint
8890
defaultPostgresParamValueLogMinErrorStatement = "WARNING"
8991
defaultPostgresParamValueLogErrorVerbosity = "VERBOSE"
@@ -211,6 +213,9 @@ type PostgresSpec struct {
211213

212214
// DedicatedLoadBalancerPort The port to use for the load balancer
213215
DedicatedLoadBalancerPort *int32 `json:"dedicatedLoadBalancerPort,omitempty"`
216+
217+
// DisableLoadBalancers enable or disable the Load Balancers (Services)
218+
DisableLoadBalancers *bool `json:"disableLoadBalancers,omitempty"`
214219
}
215220

216221
// AccessList defines the type of restrictions to access the database
@@ -385,7 +390,7 @@ func (p *Postgres) ToSharedSvcLB(lbIP string, lbPort int32, enableStandbyLeaderS
385390

386391
lb.Spec.Selector = map[string]string{
387392
ApplicationLabelName: ApplicationLabelValue,
388-
"cluster-name": p.ToPeripheralResourceName(),
393+
ClusterNameLabelName: p.ToPeripheralResourceName(),
389394
"team": p.generateTeamID(),
390395
}
391396
if p.IsReplicationPrimaryOrStandalone() {
@@ -401,6 +406,9 @@ func (p *Postgres) ToSharedSvcLB(lbIP string, lbPort int32, enableStandbyLeaderS
401406
lb.Spec.Selector[StatefulsetPodNameLabelName] = p.ToPeripheralResourceName() + "-0"
402407
}
403408
}
409+
if p.DisableLoadBalancers() {
410+
lb.Spec.Selector[ClusterNameLabelName] = defaultSelectorDisableValue
411+
}
404412

405413
if len(lbIP) > 0 {
406414
// if no ip is set, a new loadbalancer will be created automatically
@@ -479,7 +487,7 @@ func (p *Postgres) ToDedicatedSvcLB(lbIP string, lbPort int32, standbyClustersSo
479487

480488
lb.Spec.Selector = map[string]string{
481489
ApplicationLabelName: ApplicationLabelValue,
482-
"cluster-name": p.ToPeripheralResourceName(),
490+
ClusterNameLabelName: p.ToPeripheralResourceName(),
483491
"team": p.generateTeamID(),
484492
}
485493
if p.IsReplicationPrimaryOrStandalone() {
@@ -488,6 +496,9 @@ func (p *Postgres) ToDedicatedSvcLB(lbIP string, lbPort int32, standbyClustersSo
488496
// select the first pod in the statefulset
489497
lb.Spec.Selector[StatefulsetPodNameLabelName] = p.ToPeripheralResourceName() + "-0"
490498
}
499+
if p.DisableLoadBalancers() {
500+
lb.Spec.Selector[ClusterNameLabelName] = defaultSelectorDisableValue
501+
}
491502

492503
if len(lbIP) > 0 {
493504
lb.Spec.LoadBalancerIP = lbIP
@@ -1068,3 +1079,11 @@ func (p *Postgres) ToStandbyClusterEgressCWNP() (*firewall.ClusterwideNetworkPol
10681079

10691080
return standbyEgressCWNP, nil
10701081
}
1082+
1083+
func (p *Postgres) DisableLoadBalancers() bool {
1084+
if p.Spec.DisableLoadBalancers == nil {
1085+
return false
1086+
}
1087+
1088+
return *p.Spec.DisableLoadBalancers
1089+
}

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/database.fits.cloud_postgres.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ spec:
113113
description:
114114
description: Description
115115
type: string
116+
disableLoadBalancers:
117+
description: DisableLoadBalancers enable or disable the Load Balancers
118+
(Services)
119+
type: boolean
116120
maintenance:
117121
description: |-
118122
todo: add default

0 commit comments

Comments
 (0)