Skip to content

Commit 5b05650

Browse files
authored
Broaden Svc Selector (#395)
* Broaden selector in service * Force single instance for standby * Revert "Force single instance for standby" This reverts commit 6a8c229. * Select the first pod for standby dbs, ignore spilo-role * Remove incorrect comment * Make the use of the existing selector via a new feature flag * Cleanup pod-name from selector * Use const instead of string literal * Properly clean up the selector in database switch * reduce, reuse, recycle * move new var into own line
1 parent 433715e commit 5b05650

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

api/v1/postgres_types.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const (
6868
SpiloRoleLabelName = "spilo-role"
6969
SpiloRoleLabelValueMaster = "master"
7070
SpiloRoleLabelValueStandbyLeader = "standby_leader"
71+
StatefulsetPodNameLabelName = "statefulset.kubernetes.io/pod-name"
7172

7273
teamIDPrefix = "pg"
7374

@@ -321,7 +322,7 @@ func (p *Postgres) ToKey() *types.NamespacedName {
321322
}
322323
}
323324

324-
func (p *Postgres) ToSvcLB(lbIP string, lbPort int32, enableStandbyLeaderSelector bool) *corev1.Service {
325+
func (p *Postgres) ToSvcLB(lbIP string, lbPort int32, enableStandbyLeaderSelector bool, enableLegacyStandbySelector bool) *corev1.Service {
325326
lb := &corev1.Service{}
326327
lb.Spec.Type = "LoadBalancer"
327328

@@ -342,16 +343,24 @@ func (p *Postgres) ToSvcLB(lbIP string, lbPort int32, enableStandbyLeaderSelecto
342343
port.TargetPort = intstr.FromInt(5432)
343344
lb.Spec.Ports = []corev1.ServicePort{port}
344345

345-
spiloRole := SpiloRoleLabelValueMaster
346-
if enableStandbyLeaderSelector && !p.IsReplicationPrimary() {
347-
spiloRole = SpiloRoleLabelValueStandbyLeader
348-
}
349346
lb.Spec.Selector = map[string]string{
350347
ApplicationLabelName: ApplicationLabelValue,
351348
"cluster-name": p.ToPeripheralResourceName(),
352-
SpiloRoleLabelName: spiloRole,
353349
"team": p.generateTeamID(),
354350
}
351+
if p.IsReplicationPrimary() {
352+
lb.Spec.Selector[SpiloRoleLabelName] = SpiloRoleLabelValueMaster
353+
} else {
354+
if enableStandbyLeaderSelector {
355+
// Only set this value when we are NOT a primary and the StandbyLeaderSelector is enabled.
356+
lb.Spec.Selector[SpiloRoleLabelName] = SpiloRoleLabelValueStandbyLeader
357+
} else if enableLegacyStandbySelector {
358+
lb.Spec.Selector[SpiloRoleLabelName] = SpiloRoleLabelValueMaster
359+
} else {
360+
// select the first pod in the statefulset
361+
lb.Spec.Selector[StatefulsetPodNameLabelName] = p.ToPeripheralResourceName() + "-0"
362+
}
363+
}
355364

356365
if len(lbIP) > 0 {
357366
// if no ip is set, a new loadbalancer will be created automatically

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const (
6161
patroniRetryTimeoutFlg = "patroni-retry-timeout"
6262
enableStandbyLeaderSelectorFlg = "enable-standby-leader-selector"
6363
ControlPlaneNamespaceFlg = "control-plane-namespace"
64+
enableLegacyStandbySelectorFlg = "enable-legacy-standby-selector"
6465
deployEtcdFlg = "deploy-etcd"
6566
etcdImageFlg = "etcd-image"
6667
etcdBackupSidecarImageFlg = "etcd-backup-sidecar-image"
@@ -85,6 +86,7 @@ func init() {
8586
}
8687

8788
func main() {
89+
8890
var (
8991
metricsAddrCtrlMgr string
9092
metricsAddrSvcMgr string
@@ -112,6 +114,7 @@ func main() {
112114
enableNetPol bool
113115
enablePodAntiaffinity bool
114116
enableStandbyLeaderSelector bool
117+
enableLegacyStandbySelector bool
115118
deployEtcd bool
116119

117120
portRangeStart int
@@ -220,6 +223,9 @@ func main() {
220223
viper.SetDefault(ControlPlaneNamespaceFlg, "metal-extension-postgres")
221224
controlPlaneNamespace = viper.GetString(ControlPlaneNamespaceFlg)
222225

226+
viper.SetDefault(enableLegacyStandbySelectorFlg, false)
227+
enableLegacyStandbySelector = viper.GetBool(enableLegacyStandbySelectorFlg)
228+
223229
viper.SetDefault(deployEtcdFlg, false)
224230
deployEtcd = viper.GetBool(deployEtcdFlg)
225231

@@ -262,6 +268,7 @@ func main() {
262268
patroniRetryTimeoutFlg, patroniRetryTimeout,
263269
enableStandbyLeaderSelectorFlg, enableStandbyLeaderSelector,
264270
ControlPlaneNamespaceFlg, controlPlaneNamespace,
271+
enableLegacyStandbySelectorFlg, enableLegacyStandbySelector,
265272
deployEtcdFlg, deployEtcd,
266273
etcdImageFlg, etcdImage,
267274
etcdBackupSidecarImageFlg, etcdBackupSidecarImage,
@@ -348,6 +355,7 @@ func main() {
348355
PortRangeStart: int32(portRangeStart),
349356
PortRangeSize: int32(portRangeSize),
350357
EnableStandbyLeaderSelector: enableStandbyLeaderSelector,
358+
EnableLegacyStandbySelector: enableLegacyStandbySelector,
351359
}
352360
if err = (&controllers.PostgresReconciler{
353361
CtrlClient: ctrlPlaneClusterMgr.GetClient(),

pkg/lbmanager/lbmanager.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Options struct {
1616
PortRangeStart int32
1717
PortRangeSize int32
1818
EnableStandbyLeaderSelector bool
19+
EnableLegacyStandbySelector bool
1920
}
2021

2122
// LBManager Responsible for the creation and deletion of externally accessible Services to access the Postgresql clusters managed by the Postgreslet.
@@ -59,18 +60,14 @@ func (m *LBManager) CreateSvcLBIfNone(ctx context.Context, in *api.Postgres) err
5960
lbIPToUse = ""
6061
}
6162

62-
if err := m.Create(ctx, in.ToSvcLB(lbIPToUse, nextFreePort, m.options.EnableStandbyLeaderSelector)); err != nil {
63+
if err := m.Create(ctx, in.ToSvcLB(lbIPToUse, nextFreePort, m.options.EnableStandbyLeaderSelector, m.options.EnableLegacyStandbySelector)); err != nil {
6364
return fmt.Errorf("failed to create Service of type LoadBalancer: %w", err)
6465
}
6566
return nil
6667
}
6768

6869
// update the selector, and only the selector (we do NOT want the change the ip or port here!!!)
69-
if m.options.EnableStandbyLeaderSelector && !in.IsReplicationPrimary() {
70-
svc.Spec.Selector[api.SpiloRoleLabelName] = api.SpiloRoleLabelValueStandbyLeader
71-
} else {
72-
svc.Spec.Selector[api.SpiloRoleLabelName] = api.SpiloRoleLabelValueMaster
73-
}
70+
svc.Spec.Selector = in.ToSvcLB("", 0, m.options.EnableStandbyLeaderSelector, m.options.EnableLegacyStandbySelector).Spec.Selector
7471

7572
if err := m.Update(ctx, svc); err != nil {
7673
return fmt.Errorf("failed to update Service of type LoadBalancer: %w", err)

0 commit comments

Comments
 (0)