Skip to content

Commit

Permalink
feat(load-balancer): Set IPMode to "Proxy" if load balancer is config…
Browse files Browse the repository at this point in the history
…ured to use proxy protocol (#727)

Fixes #727
  • Loading branch information
simonhammes authored and lukasmetzner committed Nov 6, 2024
1 parent 265279d commit 22e64dd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 33 deletions.
55 changes: 34 additions & 21 deletions hcloud/load_balancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type loadBalancers struct {
lbOps LoadBalancerOps
disablePrivateIngressDefault bool
disableIPv6Default bool
useProxyProtocolDefault bool
}

func newLoadBalancers(lbOps LoadBalancerOps, disablePrivateIngressDefault bool, disableIPv6Default bool) *loadBalancers {
Expand Down Expand Up @@ -223,6 +224,16 @@ func (l *loadBalancers) buildLoadBalancerStatusIngress(lb *hcloud.LoadBalancer,
metrics.OperationCalled.WithLabelValues(op).Inc()

var ingress []corev1.LoadBalancerIngress
ipMode := corev1.LoadBalancerIPModeVIP

useProxyProtocol, err := l.getUseProxyProtocol(svc)
if err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
}

if useProxyProtocol {
ipMode = corev1.LoadBalancerIPModeProxy
}

// "IPMode" was introduced in 1.29
supportsIPModeField, err := checkIPModeSupport()
Expand All @@ -236,26 +247,20 @@ func (l *loadBalancers) buildLoadBalancerStatusIngress(lb *hcloud.LoadBalancer,
}

if !disablePubNet {
i := corev1.LoadBalancerIngress{IP: lb.PublicNet.IPv4.IP.String()}

if supportsIPModeField {
i.IPMode = &ipMode
}

ingress = append(ingress, i)
ingress = append(ingress, corev1.LoadBalancerIngress{
IP: lb.PublicNet.IPv4.IP.String(),
IPMode: &ipMode,
})

disableIPV6, err := l.getDisableIPv6(svc)
if err != nil {
return nil, fmt.Errorf("%s: %w", op, err)
}
if !disableIPV6 {
i := corev1.LoadBalancerIngress{IP: lb.PublicNet.IPv6.IP.String()}

if supportsIPModeField {
i.IPMode = &ipMode
}

ingress = append(ingress, i)
ingress = append(ingress, corev1.LoadBalancerIngress{
IP: lb.PublicNet.IPv6.IP.String(),
IPMode: &ipMode,
})
}
}

Expand All @@ -266,13 +271,10 @@ func (l *loadBalancers) buildLoadBalancerStatusIngress(lb *hcloud.LoadBalancer,

if !disablePrivIngress {
for _, privateNet := range lb.PrivateNet {
i := corev1.LoadBalancerIngress{IP: privateNet.IP.String()}

if supportsIPModeField {
i.IPMode = &ipMode
}

ingress = append(ingress, i)
ingress = append(ingress, corev1.LoadBalancerIngress{
IP: privateNet.IP.String(),
IPMode: &ipMode,
})
}
}

Expand All @@ -290,6 +292,17 @@ func (l *loadBalancers) getDisablePrivateIngress(svc *corev1.Service) (bool, err
return false, err
}

func (l *loadBalancers) getUseProxyProtocol(svc *corev1.Service) (bool, error) {
disable, err := annotation.LBSvcProxyProtocol.BoolFromService(svc)
if err == nil {
return disable, nil
}
if errors.Is(err, annotation.ErrNotSet) {
return l.useProxyProtocolDefault, nil
}
return false, err
}

func (l *loadBalancers) getDisableIPv6(svc *corev1.Service) (bool, error) {
disable, err := annotation.LBIPv6Disabled.BoolFromService(svc)
if err == nil {
Expand Down
23 changes: 11 additions & 12 deletions hcloud/load_balancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ func TestLoadBalancers_EnsureLoadBalancer_CreateLoadBalancer(t *testing.T) {
}

ipModeVIP := corev1.LoadBalancerIPModeVIP
ipModeProxy := corev1.LoadBalancerIPModeProxy

tests := []LoadBalancerTestCase{
{
Expand Down Expand Up @@ -281,7 +280,7 @@ func TestLoadBalancers_EnsureLoadBalancer_CreateLoadBalancer(t *testing.T) {
Perform: func(t *testing.T, tt *LoadBalancerTestCase) {
expected := &corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{IP: tt.LB.PublicNet.IPv4.IP.String()},
{IP: tt.LB.PublicNet.IPv4.IP.String(), IPMode: &ipModeVIP},
},
}
lbStat, err := tt.LoadBalancers.EnsureLoadBalancer(tt.Ctx, tt.ClusterName, tt.Service, tt.Nodes)
Expand Down Expand Up @@ -312,8 +311,8 @@ func TestLoadBalancers_EnsureLoadBalancer_CreateLoadBalancer(t *testing.T) {
Perform: func(t *testing.T, tt *LoadBalancerTestCase) {
expected := &corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{IP: tt.LB.PublicNet.IPv4.IP.String()},
{IP: tt.LB.PublicNet.IPv6.IP.String()},
{IP: tt.LB.PublicNet.IPv4.IP.String(), IPMode: &ipModeVIP},
{IP: tt.LB.PublicNet.IPv6.IP.String(), IPMode: &ipModeVIP},
},
}
lbStat, err := tt.LoadBalancers.EnsureLoadBalancer(tt.Ctx, tt.ClusterName, tt.Service, tt.Nodes)
Expand Down Expand Up @@ -354,9 +353,9 @@ func TestLoadBalancers_EnsureLoadBalancer_CreateLoadBalancer(t *testing.T) {
Perform: func(t *testing.T, tt *LoadBalancerTestCase) {
expected := &corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{IP: tt.LB.PublicNet.IPv4.IP.String()},
{IP: tt.LB.PublicNet.IPv6.IP.String()},
{IP: tt.LB.PrivateNet[0].IP.String()},
{IP: tt.LB.PublicNet.IPv4.IP.String(), IPMode: &ipModeVIP},
{IP: tt.LB.PublicNet.IPv6.IP.String(), IPMode: &ipModeVIP},
{IP: tt.LB.PrivateNet[0].IP.String(), IPMode: &ipModeVIP},
},
}
lbStat, err := tt.LoadBalancers.EnsureLoadBalancer(tt.Ctx, tt.ClusterName, tt.Service, tt.Nodes)
Expand Down Expand Up @@ -398,8 +397,8 @@ func TestLoadBalancers_EnsureLoadBalancer_CreateLoadBalancer(t *testing.T) {
Perform: func(t *testing.T, tt *LoadBalancerTestCase) {
expected := &corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{IP: tt.LB.PublicNet.IPv4.IP.String()},
{IP: tt.LB.PublicNet.IPv6.IP.String()},
{IP: tt.LB.PublicNet.IPv4.IP.String(), IPMode: &ipModeVIP},
{IP: tt.LB.PublicNet.IPv6.IP.String(), IPMode: &ipModeVIP},
},
}
lbStat, err := tt.LoadBalancers.EnsureLoadBalancer(tt.Ctx, tt.ClusterName, tt.Service, tt.Nodes)
Expand Down Expand Up @@ -441,8 +440,8 @@ func TestLoadBalancers_EnsureLoadBalancer_CreateLoadBalancer(t *testing.T) {
Perform: func(t *testing.T, tt *LoadBalancerTestCase) {
expected := &corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{IP: tt.LB.PublicNet.IPv4.IP.String()},
{IP: tt.LB.PublicNet.IPv6.IP.String()},
{IP: tt.LB.PublicNet.IPv4.IP.String(), IPMode: &ipModeVIP},
{IP: tt.LB.PublicNet.IPv6.IP.String(), IPMode: &ipModeVIP},
},
}
lbStat, err := tt.LoadBalancers.EnsureLoadBalancer(tt.Ctx, tt.ClusterName, tt.Service, tt.Nodes)
Expand Down Expand Up @@ -496,7 +495,7 @@ func TestLoadBalancers_EnsureLoadBalancer_CreateLoadBalancer(t *testing.T) {
Perform: func(t *testing.T, tt *LoadBalancerTestCase) {
expected := &corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{IP: tt.LB.PrivateNet[0].IP.String()},
{IP: tt.LB.PrivateNet[0].IP.String(), IPMode: &ipModeVIP},
},
}
lbStat, err := tt.LoadBalancers.EnsureLoadBalancer(tt.Ctx, tt.ClusterName, tt.Service, tt.Nodes)
Expand Down

0 comments on commit 22e64dd

Please sign in to comment.