@@ -5,20 +5,21 @@ import (
55"crypto/sha256"
66"encoding/hex"
77"fmt"
8+ "regexp"
9+ "sort"
10+ "strconv"
11+ "strings"
12+
813"github.com/aws/aws-sdk-go/aws"
914"github.com/pkg/errors"
1015corev1 "k8s.io/api/core/v1"
1116metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1217"k8s.io/apimachinery/pkg/types"
1318"k8s.io/apimachinery/pkg/util/intstr"
14- "regexp"
1519elbv2api "sigs.k8s.io/aws-load-balancer-controller/apis/elbv2/v1beta1"
1620"sigs.k8s.io/aws-load-balancer-controller/pkg/annotations"
1721"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
1822elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
19- "sort"
20- "strconv"
21- "strings"
2223)
2324
2425const (
@@ -395,9 +396,10 @@ func (t *defaultModelBuildTask) buildTargetGroupBindingSpec(ctx context.Context,
395396}, nil
396397}
397398
398- func (t * defaultModelBuildTask ) buildPeersFromSourceRanges (_ context.Context , defaultSourceRanges []string ) []elbv2model.NetworkingPeer {
399+ func (t * defaultModelBuildTask ) buildPeersFromSourceRanges (_ context.Context , defaultSourceRanges []string ) ( []elbv2model.NetworkingPeer , bool ) {
399400var sourceRanges []string
400401var peers []elbv2model.NetworkingPeer
402+ customSourceRangesConfigured := true
401403for _ , cidr := range t .service .Spec .LoadBalancerSourceRanges {
402404sourceRanges = append (sourceRanges , cidr )
403405}
@@ -406,6 +408,7 @@ func (t *defaultModelBuildTask) buildPeersFromSourceRanges(_ context.Context, de
406408}
407409if len (sourceRanges ) == 0 {
408410sourceRanges = defaultSourceRanges
411+ customSourceRangesConfigured = false
409412}
410413for _ , cidr := range sourceRanges {
411414peers = append (peers , elbv2model.NetworkingPeer {
@@ -414,7 +417,7 @@ func (t *defaultModelBuildTask) buildPeersFromSourceRanges(_ context.Context, de
414417},
415418})
416419}
417- return peers
420+ return peers , customSourceRangesConfigured
418421}
419422
420423func (t * defaultModelBuildTask ) buildTargetGroupBindingNetworking (ctx context.Context , tgPort intstr.IntOrString , preserveClientIP bool ,
@@ -438,8 +441,9 @@ func (t *defaultModelBuildTask) buildTargetGroupBindingNetworking(ctx context.Co
438441},
439442}
440443trafficSource := fromVPC
444+ customSourceRangesConfigured := false
441445if networkingProtocol == elbv2api .NetworkingProtocolUDP || preserveClientIP {
442- trafficSource = t .buildPeersFromSourceRanges (ctx , defaultSourceRanges )
446+ trafficSource , customSourceRangesConfigured = t .buildPeersFromSourceRanges (ctx , defaultSourceRanges )
443447}
444448tgbNetworking := & elbv2model.TargetGroupBindingNetworking {
445449Ingress : []elbv2model.NetworkingIngressRule {
@@ -449,21 +453,9 @@ func (t *defaultModelBuildTask) buildTargetGroupBindingNetworking(ctx context.Co
449453},
450454},
451455}
452- if preserveClientIP || tgProtocol == corev1 .ProtocolUDP || (hcPort .String () != healthCheckPortTrafficPort && hcPort .IntValue () != tgPort .IntValue ()) {
453- var healthCheckPorts []elbv2api.NetworkingPort
454- networkingProtocolTCP := elbv2api .NetworkingProtocolTCP
455- networkingHealthCheckPort := hcPort
456- if hcPort .String () == healthCheckPortTrafficPort {
457- networkingHealthCheckPort = tgPort
458- }
459- healthCheckPorts = append (healthCheckPorts , elbv2api.NetworkingPort {
460- Port : & networkingHealthCheckPort ,
461- Protocol : & networkingProtocolTCP ,
462- })
463- tgbNetworking .Ingress = append (tgbNetworking .Ingress , elbv2model.NetworkingIngressRule {
464- From : fromVPC ,
465- Ports : healthCheckPorts ,
466- })
456+ if hcIngressRules := t .buildHealthCheckNetworkingIngressRules (trafficSource , fromVPC , tgPort , hcPort , tgProtocol ,
457+ preserveClientIP , customSourceRangesConfigured ); len (hcIngressRules ) > 0 {
458+ tgbNetworking .Ingress = append (tgbNetworking .Ingress , hcIngressRules ... )
467459}
468460return tgbNetworking
469461}
@@ -483,3 +475,35 @@ func (t *defaultModelBuildTask) buildTargetGroupBindingNodeSelector(_ context.Co
483475MatchLabels : targetNodeLabels ,
484476}, nil
485477}
478+
479+ func (t * defaultModelBuildTask ) buildHealthCheckNetworkingIngressRules (trafficSource , hcSource []elbv2model.NetworkingPeer , tgPort , hcPort intstr.IntOrString ,
480+ tgProtocol corev1.Protocol , preserveClientIP , customSoureRanges bool ) []elbv2model.NetworkingIngressRule {
481+ if tgProtocol != corev1 .ProtocolUDP &&
482+ (hcPort .String () == healthCheckPortTrafficPort || hcPort .IntValue () == tgPort .IntValue ()) {
483+ if ! preserveClientIP {
484+ return []elbv2model.NetworkingIngressRule {}
485+ }
486+ if ! customSoureRanges {
487+ return []elbv2model.NetworkingIngressRule {}
488+ }
489+ for _ , src := range trafficSource {
490+ if src .IPBlock .CIDR == "0.0.0.0/0" {
491+ return []elbv2model.NetworkingIngressRule {}
492+ }
493+ }
494+ }
495+ var healthCheckPorts []elbv2api.NetworkingPort
496+ networkingProtocolTCP := elbv2api .NetworkingProtocolTCP
497+ networkingHealthCheckPort := hcPort
498+ if hcPort .String () == healthCheckPortTrafficPort {
499+ networkingHealthCheckPort = tgPort
500+ }
501+ healthCheckPorts = append (healthCheckPorts , elbv2api.NetworkingPort {
502+ Port : & networkingHealthCheckPort ,
503+ Protocol : & networkingProtocolTCP ,
504+ })
505+ return []elbv2model.NetworkingIngressRule {{
506+ From : hcSource ,
507+ Ports : healthCheckPorts ,
508+ }}
509+ }
0 commit comments