@@ -5,20 +5,21 @@ import (
5
5
"crypto/sha256"
6
6
"encoding/hex"
7
7
"fmt"
8
+ "regexp"
9
+ "sort"
10
+ "strconv"
11
+ "strings"
12
+
8
13
"github.com/aws/aws-sdk-go/aws"
9
14
"github.com/pkg/errors"
10
15
corev1 "k8s.io/api/core/v1"
11
16
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
17
"k8s.io/apimachinery/pkg/types"
13
18
"k8s.io/apimachinery/pkg/util/intstr"
14
- "regexp"
15
19
elbv2api "sigs.k8s.io/aws-load-balancer-controller/apis/elbv2/v1beta1"
16
20
"sigs.k8s.io/aws-load-balancer-controller/pkg/annotations"
17
21
"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
18
22
elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
19
- "sort"
20
- "strconv"
21
- "strings"
22
23
)
23
24
24
25
const (
@@ -395,9 +396,10 @@ func (t *defaultModelBuildTask) buildTargetGroupBindingSpec(ctx context.Context,
395
396
}, nil
396
397
}
397
398
398
- func (t * defaultModelBuildTask ) buildPeersFromSourceRanges (_ context.Context , defaultSourceRanges []string ) []elbv2model.NetworkingPeer {
399
+ func (t * defaultModelBuildTask ) buildPeersFromSourceRanges (_ context.Context , defaultSourceRanges []string ) ( []elbv2model.NetworkingPeer , bool ) {
399
400
var sourceRanges []string
400
401
var peers []elbv2model.NetworkingPeer
402
+ customSourceRangesConfigured := true
401
403
for _ , cidr := range t .service .Spec .LoadBalancerSourceRanges {
402
404
sourceRanges = append (sourceRanges , cidr )
403
405
}
@@ -406,6 +408,7 @@ func (t *defaultModelBuildTask) buildPeersFromSourceRanges(_ context.Context, de
406
408
}
407
409
if len (sourceRanges ) == 0 {
408
410
sourceRanges = defaultSourceRanges
411
+ customSourceRangesConfigured = false
409
412
}
410
413
for _ , cidr := range sourceRanges {
411
414
peers = append (peers , elbv2model.NetworkingPeer {
@@ -414,7 +417,7 @@ func (t *defaultModelBuildTask) buildPeersFromSourceRanges(_ context.Context, de
414
417
},
415
418
})
416
419
}
417
- return peers
420
+ return peers , customSourceRangesConfigured
418
421
}
419
422
420
423
func (t * defaultModelBuildTask ) buildTargetGroupBindingNetworking (ctx context.Context , tgPort intstr.IntOrString , preserveClientIP bool ,
@@ -438,8 +441,9 @@ func (t *defaultModelBuildTask) buildTargetGroupBindingNetworking(ctx context.Co
438
441
},
439
442
}
440
443
trafficSource := fromVPC
444
+ customSourceRangesConfigured := false
441
445
if networkingProtocol == elbv2api .NetworkingProtocolUDP || preserveClientIP {
442
- trafficSource = t .buildPeersFromSourceRanges (ctx , defaultSourceRanges )
446
+ trafficSource , customSourceRangesConfigured = t .buildPeersFromSourceRanges (ctx , defaultSourceRanges )
443
447
}
444
448
tgbNetworking := & elbv2model.TargetGroupBindingNetworking {
445
449
Ingress : []elbv2model.NetworkingIngressRule {
@@ -449,21 +453,9 @@ func (t *defaultModelBuildTask) buildTargetGroupBindingNetworking(ctx context.Co
449
453
},
450
454
},
451
455
}
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 ... )
467
459
}
468
460
return tgbNetworking
469
461
}
@@ -483,3 +475,35 @@ func (t *defaultModelBuildTask) buildTargetGroupBindingNodeSelector(_ context.Co
483
475
MatchLabels : targetNodeLabels ,
484
476
}, nil
485
477
}
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