diff --git a/hcloud/load_balancers.go b/hcloud/load_balancers.go index e0d82d4f..3219ae73 100644 --- a/hcloud/load_balancers.go +++ b/hcloud/load_balancers.go @@ -36,6 +36,7 @@ type loadBalancers struct { lbOps LoadBalancerOps disablePrivateIngressDefault bool disableIPv6Default bool + useProxyProtocolDefault bool } func newLoadBalancers(lbOps LoadBalancerOps, disablePrivateIngressDefault bool, disableIPv6Default bool) *loadBalancers { @@ -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() @@ -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, + }) } } @@ -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, + }) } } @@ -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 { diff --git a/hcloud/load_balancers_test.go b/hcloud/load_balancers_test.go index 196e0cdd..24982df2 100644 --- a/hcloud/load_balancers_test.go +++ b/hcloud/load_balancers_test.go @@ -242,7 +242,6 @@ func TestLoadBalancers_EnsureLoadBalancer_CreateLoadBalancer(t *testing.T) { } ipModeVIP := corev1.LoadBalancerIPModeVIP - ipModeProxy := corev1.LoadBalancerIPModeProxy tests := []LoadBalancerTestCase{ { @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)