Skip to content

Commit 8d29153

Browse files
authored
fix: populate ingress private ip when disable-private-ingress is false (#715)
Closes #553 Refactor getting the ingresses for a load-balancer for both get load-balancer and ensure load-balancer, into using a shared function. This ensures we are consistent between the 2 methods. ### Upgrading The load balancer ingress IPs are now populated with the private IPs, unless the `load-balancer.hetzner.cloud/disable-private-ingress` annotation is set to `true`. Please make sure that you configured the annotation according to your needs, for example if you are using `external-dns`.
1 parent 57af221 commit 8d29153

2 files changed

Lines changed: 100 additions & 16 deletions

File tree

hcloud/load_balancers.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,12 @@ func (l *loadBalancers) GetLoadBalancer(
8686
}, true, nil
8787
}
8888

89-
ingresses := []corev1.LoadBalancerIngress{
90-
{
91-
IP: lb.PublicNet.IPv4.IP.String(),
92-
},
93-
}
94-
95-
disableIPV6, err := l.getDisableIPv6(service)
89+
ingress, err := l.buildLoadBalancerStatusIngress(lb, service)
9690
if err != nil {
9791
return nil, false, fmt.Errorf("%s: %v", op, err)
9892
}
99-
if !disableIPV6 {
100-
ingresses = append(ingresses, corev1.LoadBalancerIngress{
101-
IP: lb.PublicNet.IPv6.IP.String(),
102-
})
103-
}
10493

105-
return &corev1.LoadBalancerStatus{Ingress: ingresses}, true, nil
94+
return &corev1.LoadBalancerStatus{Ingress: ingress}, true, nil
10695
}
10796

10897
func (l *loadBalancers) GetLoadBalancerName(_ context.Context, _ string, service *corev1.Service) string {
@@ -203,6 +192,18 @@ func (l *loadBalancers) EnsureLoadBalancer(
203192
}, nil
204193
}
205194

195+
ingress, err := l.buildLoadBalancerStatusIngress(lb, svc)
196+
if err != nil {
197+
return nil, fmt.Errorf("%s: %w", op, err)
198+
}
199+
200+
return &corev1.LoadBalancerStatus{Ingress: ingress}, nil
201+
}
202+
203+
func (l *loadBalancers) buildLoadBalancerStatusIngress(lb *hcloud.LoadBalancer, svc *corev1.Service) ([]corev1.LoadBalancerIngress, error) {
204+
const op = "hcloud/loadBalancers.getLoadBalancerStatusIngress"
205+
metrics.OperationCalled.WithLabelValues(op).Inc()
206+
206207
var ingress []corev1.LoadBalancerIngress
207208

208209
disablePubNet, err := annotation.LBDisablePublicNetwork.BoolFromService(svc)
@@ -226,13 +227,14 @@ func (l *loadBalancers) EnsureLoadBalancer(
226227
if err != nil {
227228
return nil, fmt.Errorf("%s: %w", op, err)
228229
}
230+
229231
if !disablePrivIngress {
230-
for _, nw := range lb.PrivateNet {
231-
ingress = append(ingress, corev1.LoadBalancerIngress{IP: nw.IP.String()})
232+
for _, privateNet := range lb.PrivateNet {
233+
ingress = append(ingress, corev1.LoadBalancerIngress{IP: privateNet.IP.String()})
232234
}
233235
}
234236

235-
return &corev1.LoadBalancerStatus{Ingress: ingress}, nil
237+
return ingress, nil
236238
}
237239

238240
func (l *loadBalancers) getDisablePrivateIngress(svc *corev1.Service) (bool, error) {

hcloud/load_balancers_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,88 @@ func TestLoadBalancers_GetLoadBalancer(t *testing.T) {
105105
assert.Equal(t, "hostname", status.Ingress[0].Hostname)
106106
},
107107
},
108+
{
109+
Name: "get load balancer with private network",
110+
ServiceUID: "1",
111+
LB: &hcloud.LoadBalancer{
112+
ID: 1,
113+
Name: "with-priv-net",
114+
LoadBalancerType: &hcloud.LoadBalancerType{Name: "lb11"},
115+
Location: &hcloud.Location{Name: "nbg1", NetworkZone: hcloud.NetworkZoneEUCentral},
116+
PublicNet: hcloud.LoadBalancerPublicNet{
117+
Enabled: true,
118+
IPv4: hcloud.LoadBalancerPublicNetIPv4{IP: net.ParseIP("1.2.3.4")},
119+
IPv6: hcloud.LoadBalancerPublicNetIPv6{IP: net.ParseIP("fe80::1")},
120+
},
121+
PrivateNet: []hcloud.LoadBalancerPrivateNet{
122+
{
123+
Network: &hcloud.Network{
124+
ID: 4711,
125+
Name: "priv-net",
126+
},
127+
IP: net.ParseIP("10.10.10.2"),
128+
},
129+
},
130+
},
131+
Mock: func(_ *testing.T, tt *LoadBalancerTestCase) {
132+
tt.LBOps.
133+
On("GetByK8SServiceUID", tt.Ctx, tt.Service).
134+
Return(tt.LB, nil)
135+
},
136+
Perform: func(t *testing.T, tt *LoadBalancerTestCase) {
137+
status, exists, err := tt.LoadBalancers.GetLoadBalancer(tt.Ctx, tt.ClusterName, tt.Service)
138+
assert.NoError(t, err)
139+
assert.True(t, exists)
140+
141+
if !assert.Len(t, status.Ingress, 3) {
142+
return
143+
}
144+
assert.Equal(t, tt.LB.PublicNet.IPv4.IP.String(), status.Ingress[0].IP)
145+
assert.Equal(t, tt.LB.PublicNet.IPv6.IP.String(), status.Ingress[1].IP)
146+
assert.Equal(t, tt.LB.PrivateNet[0].IP.String(), status.Ingress[2].IP)
147+
},
148+
},
149+
{
150+
Name: "get load balancer with private network and without private ingress",
151+
ServiceUID: "1",
152+
DisablePrivateIngressDefault: true,
153+
LB: &hcloud.LoadBalancer{
154+
ID: 1,
155+
Name: "with-priv-net-without-private-ingress",
156+
LoadBalancerType: &hcloud.LoadBalancerType{Name: "lb11"},
157+
Location: &hcloud.Location{Name: "nbg1", NetworkZone: hcloud.NetworkZoneEUCentral},
158+
PublicNet: hcloud.LoadBalancerPublicNet{
159+
Enabled: true,
160+
IPv4: hcloud.LoadBalancerPublicNetIPv4{IP: net.ParseIP("1.2.3.4")},
161+
IPv6: hcloud.LoadBalancerPublicNetIPv6{IP: net.ParseIP("fe80::1")},
162+
},
163+
PrivateNet: []hcloud.LoadBalancerPrivateNet{
164+
{
165+
Network: &hcloud.Network{
166+
ID: 4711,
167+
Name: "priv-net",
168+
},
169+
IP: net.ParseIP("10.10.10.2"),
170+
},
171+
},
172+
},
173+
Mock: func(_ *testing.T, tt *LoadBalancerTestCase) {
174+
tt.LBOps.
175+
On("GetByK8SServiceUID", tt.Ctx, tt.Service).
176+
Return(tt.LB, nil)
177+
},
178+
Perform: func(t *testing.T, tt *LoadBalancerTestCase) {
179+
status, exists, err := tt.LoadBalancers.GetLoadBalancer(tt.Ctx, tt.ClusterName, tt.Service)
180+
assert.NoError(t, err)
181+
assert.True(t, exists)
182+
183+
if !assert.Len(t, status.Ingress, 2) {
184+
return
185+
}
186+
assert.Equal(t, tt.LB.PublicNet.IPv4.IP.String(), status.Ingress[0].IP)
187+
assert.Equal(t, tt.LB.PublicNet.IPv6.IP.String(), status.Ingress[1].IP)
188+
},
189+
},
108190
{
109191
Name: "load balancer not found",
110192
ServiceUID: "3",

0 commit comments

Comments
 (0)