From 2e250d60f42642ac8dd24a797a252c311f614347 Mon Sep 17 00:00:00 2001 From: Philipp Eberle Date: Tue, 3 Sep 2024 10:27:35 +0200 Subject: [PATCH 1/4] Add option to disable the load balancers (by modifying the selectors) --- api/v1/postgres_types.go | 23 +++++++++++++++++-- api/v1/zz_generated.deepcopy.go | 5 ++++ .../bases/database.fits.cloud_postgres.yaml | 2 ++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/api/v1/postgres_types.go b/api/v1/postgres_types.go index 53dd67cc..7ee03cf8 100644 --- a/api/v1/postgres_types.go +++ b/api/v1/postgres_types.go @@ -69,6 +69,7 @@ const ( SpiloRoleLabelValueMaster = "master" SpiloRoleLabelValueStandbyLeader = "standby_leader" StatefulsetPodNameLabelName = "statefulset.kubernetes.io/pod-name" + ClusterNameLabelName = "cluster-name" teamIDPrefix = "pg" @@ -84,6 +85,7 @@ const ( defaultPostgresParamValueWalKeepSegments = "64" defaultPostgresParamValueWalKeepSize = "1GB" defaultPostgresParamValuePGStatStatementsMax = "500" + defaultSelectorDisableValue = "selector-disabled" // PostgresAutoAssignedIPNamePrefix a prefix to add to the generated random name PostgresAutoAssignedIPNamePrefix = "pgaas-autoassign-" @@ -207,6 +209,9 @@ type PostgresSpec struct { // DedicatedLoadBalancerPort The port to use for the load balancer DedicatedLoadBalancerPort *int32 `json:"dedicatedLoadBalancerPort,omitempty"` + + // DisableLoadBalancers enable or disable the Load Balancers (Services) + DisableLoadBalancers *bool `json:"disableLoadBalancers,omitempty"` } // AccessList defines the type of restrictions to access the database @@ -381,7 +386,7 @@ func (p *Postgres) ToSharedSvcLB(lbIP string, lbPort int32, enableStandbyLeaderS lb.Spec.Selector = map[string]string{ ApplicationLabelName: ApplicationLabelValue, - "cluster-name": p.ToPeripheralResourceName(), + ClusterNameLabelName: p.ToPeripheralResourceName(), "team": p.generateTeamID(), } if p.IsReplicationPrimaryOrStandalone() { @@ -397,6 +402,9 @@ func (p *Postgres) ToSharedSvcLB(lbIP string, lbPort int32, enableStandbyLeaderS lb.Spec.Selector[StatefulsetPodNameLabelName] = p.ToPeripheralResourceName() + "-0" } } + if p.DisableLoadBalancers() { + lb.Spec.Selector[ClusterNameLabelName] = defaultSelectorDisableValue + } if len(lbIP) > 0 { // if no ip is set, a new loadbalancer will be created automatically @@ -475,7 +483,7 @@ func (p *Postgres) ToDedicatedSvcLB(lbIP string, lbPort int32, standbyClustersSo lb.Spec.Selector = map[string]string{ ApplicationLabelName: ApplicationLabelValue, - "cluster-name": p.ToPeripheralResourceName(), + ClusterNameLabelName: p.ToPeripheralResourceName(), "team": p.generateTeamID(), } if p.IsReplicationPrimaryOrStandalone() { @@ -484,6 +492,9 @@ func (p *Postgres) ToDedicatedSvcLB(lbIP string, lbPort int32, standbyClustersSo // select the first pod in the statefulset lb.Spec.Selector[StatefulsetPodNameLabelName] = p.ToPeripheralResourceName() + "-0" } + if p.DisableLoadBalancers() { + lb.Spec.Selector[ClusterNameLabelName] = defaultSelectorDisableValue + } if len(lbIP) > 0 { lb.Spec.LoadBalancerIP = lbIP @@ -1064,3 +1075,11 @@ func (p *Postgres) ToStandbyClusterEgressCWNP() (*firewall.ClusterwideNetworkPol return standbyEgressCWNP, nil } + +func (p *Postgres) DisableLoadBalancers() bool { + if p.Spec.DisableLoadBalancers == nil { + return false + } + + return *p.Spec.DisableLoadBalancers +} diff --git a/api/v1/zz_generated.deepcopy.go b/api/v1/zz_generated.deepcopy.go index 0e5dbcef..54b29514 100644 --- a/api/v1/zz_generated.deepcopy.go +++ b/api/v1/zz_generated.deepcopy.go @@ -193,6 +193,11 @@ func (in *PostgresSpec) DeepCopyInto(out *PostgresSpec) { *out = new(int32) **out = **in } + if in.DisableLoadBalancers != nil { + in, out := &in.DisableLoadBalancers, &out.DisableLoadBalancers + *out = new(bool) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PostgresSpec. diff --git a/config/crd/bases/database.fits.cloud_postgres.yaml b/config/crd/bases/database.fits.cloud_postgres.yaml index abcbb05d..875973c8 100644 --- a/config/crd/bases/database.fits.cloud_postgres.yaml +++ b/config/crd/bases/database.fits.cloud_postgres.yaml @@ -113,6 +113,8 @@ spec: description: description: Description type: string + disableLoadBalancers: + type: boolean maintenance: description: |- todo: add default From 069989129f9e31fff85c2698172a90247fa1a171 Mon Sep 17 00:00:00 2001 From: Philipp Eberle Date: Tue, 3 Sep 2024 10:28:01 +0200 Subject: [PATCH 2/4] Test forced pod restart (to terminate existing connections) --- controllers/postgres_controller.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/controllers/postgres_controller.go b/controllers/postgres_controller.go index 87a6ec25..311fc975 100644 --- a/controllers/postgres_controller.go +++ b/controllers/postgres_controller.go @@ -539,6 +539,11 @@ func (r *PostgresReconciler) updatePodEnvironmentConfigMap(log logr.Logger, ctx data["STANDBY_WALG_DOWNLOAD_CONCURRENCY"] = downloadConcurrency } + // TODO do we really want to *always* force a restart? + if p.DisableLoadBalancers() { + data["POSTGRESLET_LOADBALANCERS_DISABLED"] = "true" + } + cm := &corev1.ConfigMap{} ns := types.NamespacedName{ Name: operatormanager.PodEnvCMName, From f27523c727b52e86c3101326abcc0cea0289d4d2 Mon Sep 17 00:00:00 2001 From: Philipp Eberle Date: Fri, 15 Nov 2024 09:46:27 +0100 Subject: [PATCH 3/4] Disable pod restart --- controllers/postgres_controller.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/controllers/postgres_controller.go b/controllers/postgres_controller.go index 311fc975..87a6ec25 100644 --- a/controllers/postgres_controller.go +++ b/controllers/postgres_controller.go @@ -539,11 +539,6 @@ func (r *PostgresReconciler) updatePodEnvironmentConfigMap(log logr.Logger, ctx data["STANDBY_WALG_DOWNLOAD_CONCURRENCY"] = downloadConcurrency } - // TODO do we really want to *always* force a restart? - if p.DisableLoadBalancers() { - data["POSTGRESLET_LOADBALANCERS_DISABLED"] = "true" - } - cm := &corev1.ConfigMap{} ns := types.NamespacedName{ Name: operatormanager.PodEnvCMName, From 04eeb2a00c240a94772907298a441e3bf453e2c2 Mon Sep 17 00:00:00 2001 From: Philipp Eberle Date: Fri, 15 Nov 2024 09:53:46 +0100 Subject: [PATCH 4/4] Add generated crd --- config/crd/bases/database.fits.cloud_postgres.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/crd/bases/database.fits.cloud_postgres.yaml b/config/crd/bases/database.fits.cloud_postgres.yaml index 875973c8..22afd875 100644 --- a/config/crd/bases/database.fits.cloud_postgres.yaml +++ b/config/crd/bases/database.fits.cloud_postgres.yaml @@ -114,6 +114,8 @@ spec: description: Description type: string disableLoadBalancers: + description: DisableLoadBalancers enable or disable the Load Balancers + (Services) type: boolean maintenance: description: |-