Skip to content

Commit 618c94d

Browse files
authored
Add LoadBalancerSourceRanges (#461)
* Add LoadBalancerSourceRanges * Only apply fallback rule if no other rule is set * Make the use LBSourceRanges configurable
1 parent 48cd617 commit 618c94d

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

api/v1/postgres_types.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func (p *Postgres) ToKey() *types.NamespacedName {
322322
}
323323
}
324324

325-
func (p *Postgres) ToSvcLB(lbIP string, lbPort int32, enableStandbyLeaderSelector bool, enableLegacyStandbySelector bool) *corev1.Service {
325+
func (p *Postgres) ToSvcLB(lbIP string, lbPort int32, enableStandbyLeaderSelector bool, enableLegacyStandbySelector bool, standbyClustersSourceRanges []string) *corev1.Service {
326326
lb := &corev1.Service{}
327327
lb.Spec.Type = "LoadBalancer"
328328

@@ -334,7 +334,20 @@ func (p *Postgres) ToSvcLB(lbIP string, lbPort int32, enableStandbyLeaderSelecto
334334
lb.Name = p.ToSvcLBName()
335335
lb.SetLabels(SvcLoadBalancerLabel)
336336

337-
// svc.Spec.LoadBalancerSourceRanges // todo: Do we need to set this?
337+
lbsr := []string{}
338+
if p.HasSourceRanges() {
339+
for _, src := range p.Spec.AccessList.SourceRanges {
340+
lbsr = append(lbsr, src)
341+
}
342+
}
343+
for _, scsr := range standbyClustersSourceRanges {
344+
lbsr = append(lbsr, scsr)
345+
}
346+
if len(lbsr) == 0 {
347+
// block by default
348+
lbsr = append(lbsr, "255.255.255.255/32")
349+
}
350+
lb.Spec.LoadBalancerSourceRanges = lbsr
338351

339352
port := corev1.ServicePort{}
340353
port.Name = "postgresql"

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const (
6868
etcdBackupSecretNameFlg = "etcd-backup-secret-name" // nolint
6969
etcdPSPNameFlg = "etcd-psp-name"
7070
postgresletFullnameFlg = "postgreslet-fullname"
71+
enableLBSourceRangesFlg = "enable-lb-source-ranges"
7172
)
7273

7374
var (
@@ -116,6 +117,7 @@ func main() {
116117
enableStandbyLeaderSelector bool
117118
enableLegacyStandbySelector bool
118119
deployEtcd bool
120+
enableLBSourceRanges bool
119121

120122
portRangeStart int
121123
portRangeSize int
@@ -240,6 +242,9 @@ func main() {
240242
viper.SetDefault(postgresletFullnameFlg, partitionID) // fall back to partition id
241243
postgresletFullname = viper.GetString(postgresletFullnameFlg)
242244

245+
viper.SetDefault(enableLBSourceRangesFlg, true)
246+
enableLBSourceRanges = viper.GetBool(enableLBSourceRangesFlg)
247+
243248
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
244249

245250
ctrl.Log.Info("flag",
@@ -275,6 +280,7 @@ func main() {
275280
etcdBackupSecretNameFlg, etcdBackupSecretName,
276281
etcdPSPNameFlg, etcdPSPName,
277282
postgresletFullnameFlg, postgresletFullname,
283+
enableLBSourceRangesFlg, enableLBSourceRanges,
278284
)
279285

280286
svcClusterConf := ctrl.GetConfigOrDie()
@@ -356,6 +362,8 @@ func main() {
356362
PortRangeSize: int32(portRangeSize),
357363
EnableStandbyLeaderSelector: enableStandbyLeaderSelector,
358364
EnableLegacyStandbySelector: enableLegacyStandbySelector,
365+
StandbyClustersSourceRanges: standbyClusterSourceRanges,
366+
EnableLBSourceRanges: enableLBSourceRanges,
359367
}
360368
if err = (&controllers.PostgresReconciler{
361369
CtrlClient: ctrlPlaneClusterMgr.GetClient(),

pkg/lbmanager/lbmanager.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ type Options struct {
1717
PortRangeSize int32
1818
EnableStandbyLeaderSelector bool
1919
EnableLegacyStandbySelector bool
20+
StandbyClustersSourceRanges []string
21+
EnableLBSourceRanges bool
2022
}
2123

2224
// LBManager Responsible for the creation and deletion of externally accessible Services to access the Postgresql clusters managed by the Postgreslet.
@@ -60,14 +62,28 @@ func (m *LBManager) CreateSvcLBIfNone(ctx context.Context, in *api.Postgres) err
6062
lbIPToUse = ""
6163
}
6264

63-
if err := m.Create(ctx, in.ToSvcLB(lbIPToUse, nextFreePort, m.options.EnableStandbyLeaderSelector, m.options.EnableLegacyStandbySelector)); err != nil {
65+
svc := in.ToSvcLB(lbIPToUse, nextFreePort, m.options.EnableStandbyLeaderSelector, m.options.EnableLegacyStandbySelector, m.options.StandbyClustersSourceRanges)
66+
if !m.options.EnableLBSourceRanges {
67+
// leave empty / disable source ranges
68+
svc.Spec.LoadBalancerSourceRanges = []string{}
69+
}
70+
if err := m.Create(ctx, svc); err != nil {
6471
return fmt.Errorf("failed to create Service of type LoadBalancer: %w", err)
6572
}
6673
return nil
6774
}
6875

76+
updated := in.ToSvcLB("", 0, m.options.EnableStandbyLeaderSelector, m.options.EnableLegacyStandbySelector, m.options.StandbyClustersSourceRanges)
6977
// update the selector, and only the selector (we do NOT want the change the ip or port here!!!)
70-
svc.Spec.Selector = in.ToSvcLB("", 0, m.options.EnableStandbyLeaderSelector, m.options.EnableLegacyStandbySelector).Spec.Selector
78+
svc.Spec.Selector = updated.Spec.Selector
79+
// also update the source ranges
80+
if m.options.EnableLBSourceRanges {
81+
// use the given source ranges
82+
svc.Spec.LoadBalancerSourceRanges = updated.Spec.LoadBalancerSourceRanges
83+
} else {
84+
// leave empty / disable source ranges
85+
svc.Spec.LoadBalancerSourceRanges = []string{}
86+
}
7187

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

0 commit comments

Comments
 (0)