@@ -39,23 +39,12 @@ func (m *LBManager) CreateSvcLBIfNone(ctx context.Context, in *api.Postgres) err
39
39
return fmt .Errorf ("failed to fetch Service of type LoadBalancer: %w" , err )
40
40
}
41
41
42
- existingLBIP , nextFreePort , err := m .nextFreeSocket (ctx )
42
+ nextFreePort , err := m .nextFreePort (ctx )
43
43
if err != nil {
44
- return fmt .Errorf ("failed to get a free port for creating Service of type LoadBalancer: %w" , err )
45
- }
46
- var lbIPToUse string
47
- if m .LBIP != "" {
48
- // a specific IP was configured in the config, so use that one
49
- lbIPToUse = m .LBIP
50
- } else if existingLBIP != "" {
51
- // no ip was configured, but one is already in use, so use the existing one
52
- lbIPToUse = existingLBIP
53
- } else {
54
- // nothing was configured, nothing exists yet, so use an empty address so a new loadbalancer will be created and assigned
55
- lbIPToUse = ""
44
+ return fmt .Errorf ("failed to get the next free port: %w" , err )
56
45
}
57
46
58
- if err := m .Create (ctx , in .ToSvcLB (lbIPToUse , nextFreePort )); err != nil {
47
+ if err := m .Create (ctx , in .ToSvcLB (m . LBIP , nextFreePort )); err != nil {
59
48
return fmt .Errorf ("failed to create Service of type LoadBalancer: %w" , err )
60
49
}
61
50
return nil
@@ -75,35 +64,27 @@ func (m *LBManager) DeleteSvcLB(ctx context.Context, in *api.Postgres) error {
75
64
}
76
65
77
66
// nextFreeSocket finds any existing LoadBalancerIP and the next free port out of the configure port range.
78
- func (m * LBManager ) nextFreeSocket (ctx context.Context ) (string , int32 , error ) {
67
+ func (m * LBManager ) nextFreePort (ctx context.Context ) (int32 , error ) {
79
68
// TODO prevent concurrency issues when calculating port / ip.
80
69
81
- anyExistingLBIP := ""
82
-
83
70
// Fetch all services managed by this postgreslet
84
71
lbs := & corev1.ServiceList {}
85
72
if err := m .List (ctx , lbs , client .MatchingLabels (api .SvcLoadBalancerLabel )); err != nil {
86
- return anyExistingLBIP , 0 , fmt .Errorf ("failed to fetch the list of services of type LoadBalancer: %w" , err )
73
+ return 0 , fmt .Errorf ("failed to fetch the list of services of type LoadBalancer: %w" , err )
87
74
}
88
75
89
76
// If there are none, this will be the first (managed) service we create, so start with PortRangeStart and return
90
77
if len (lbs .Items ) == 0 {
91
- return anyExistingLBIP , m .PortRangeStart , nil
78
+ return m .PortRangeStart , nil
92
79
}
93
80
94
81
// If there are already any managed services, store all the used ports in a slice.
95
- // Also store the LoadBalancerIP.
96
- portsInUse := make ([]int32 , len (lbs .Items ))
82
+ portsInUse := []int32 {}
97
83
for i := range lbs .Items {
98
84
svc := lbs .Items [i ]
99
85
if len (svc .Spec .Ports ) > 0 {
100
86
portsInUse = append (portsInUse , svc .Spec .Ports [0 ].Port )
101
87
}
102
- if svc .Spec .LoadBalancerIP != "" {
103
- // Technically, we only store the IP of the last Service in this list.
104
- // As there should only be one IP per postgreslet and one postgreslet per cluster, this is good enough.
105
- anyExistingLBIP = svc .Spec .LoadBalancerIP
106
- }
107
88
}
108
89
109
90
// Now try all ports in the configured port range to find a free one.
@@ -115,11 +96,11 @@ func (m *LBManager) nextFreeSocket(ctx context.Context) (string, int32, error) {
115
96
continue
116
97
}
117
98
// The postgreslet hasn't assigned this port yet, so use it.
118
- return anyExistingLBIP , port , nil
99
+ return port , nil
119
100
}
120
101
121
102
// If we made it this far, no free port could be found.
122
- return anyExistingLBIP , 0 , errors .New ("no free port in the configured port range found" )
103
+ return 0 , errors .New ("no free port in the configured port range found" )
123
104
}
124
105
125
106
func containsElem (s []int32 , v int32 ) bool {
0 commit comments