Skip to content

Commit 6afad25

Browse files
authored
Merge pull request #4866 from r4f4/sg-second-lb
🐛 fix: create ingress rules from all load balancers
2 parents 8e04d87 + 73a5db6 commit 6afad25

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

pkg/cloud/scope/managedcontrolplane.go

+5
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,11 @@ func (s *ManagedControlPlaneScope) ControlPlaneLoadBalancer() *infrav1.AWSLoadBa
430430
return nil
431431
}
432432

433+
// ControlPlaneLoadBalancers returns the AWSLoadBalancerSpecs.
434+
func (s *ManagedControlPlaneScope) ControlPlaneLoadBalancers() []*infrav1.AWSLoadBalancerSpec {
435+
return nil
436+
}
437+
433438
// Partition returns the cluster partition.
434439
func (s *ManagedControlPlaneScope) Partition() string {
435440
if s.ControlPlane.Spec.Partition == "" {

pkg/cloud/scope/sg.go

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type SGScope interface {
4444
Bastion() *infrav1.Bastion
4545

4646
// ControlPlaneLoadBalancer returns the load balancer settings that are requested.
47+
// Deprecated: Use ControlPlaneLoadBalancers()
4748
ControlPlaneLoadBalancer() *infrav1.AWSLoadBalancerSpec
4849

4950
// SetNatGatewaysIPs sets the Nat Gateways Public IPs.
@@ -54,4 +55,8 @@ type SGScope interface {
5455

5556
// AdditionalControlPlaneIngressRules returns the additional ingress rules for the control plane security group.
5657
AdditionalControlPlaneIngressRules() []infrav1.IngressRule
58+
59+
// ControlPlaneLoadBalancers returns both the ControlPlaneLoadBalancer and SecondaryControlPlaneLoadBalancer AWSLoadBalancerSpecs.
60+
// The control plane load balancers should always be returned in the above order.
61+
ControlPlaneLoadBalancers() []*infrav1.AWSLoadBalancerSpec
5762
}

pkg/cloud/services/securitygroup/securitygroups.go

+21-11
Original file line numberDiff line numberDiff line change
@@ -660,10 +660,15 @@ func (s *Service) getSecurityGroupIngressRules(role infrav1.SecurityGroupRole) (
660660
rulesToApply := customIngressRules.Difference(kubeletRules)
661661
return append(kubeletRules, rulesToApply...), nil
662662
case infrav1.SecurityGroupLB:
663+
rules := infrav1.IngressRules{}
664+
allowedNLBTraffic := false
663665
// We hand this group off to the in-cluster cloud provider, so these rules aren't used
664666
// Except if the load balancer type is NLB, and we have an AWS Cluster in which case we
665667
// need to open port 6443 to the NLB traffic and health check inside the VPC.
666-
if s.scope.ControlPlaneLoadBalancer() != nil && s.scope.ControlPlaneLoadBalancer().LoadBalancerType == infrav1.LoadBalancerTypeNLB {
668+
for _, lb := range s.scope.ControlPlaneLoadBalancers() {
669+
if lb == nil || lb.LoadBalancerType != infrav1.LoadBalancerTypeNLB {
670+
continue
671+
}
667672
var (
668673
ipv4CidrBlocks []string
669674
ipv6CidrBlocks []string
@@ -673,25 +678,26 @@ func (s *Service) getSecurityGroupIngressRules(role infrav1.SecurityGroupRole) (
673678
if s.scope.VPC().IsIPv6Enabled() {
674679
ipv6CidrBlocks = []string{s.scope.VPC().IPv6.CidrBlock}
675680
}
676-
if s.scope.ControlPlaneLoadBalancer().PreserveClientIP {
681+
if lb.PreserveClientIP {
677682
ipv4CidrBlocks = []string{services.AnyIPv4CidrBlock}
678683
if s.scope.VPC().IsIPv6Enabled() {
679684
ipv6CidrBlocks = []string{services.AnyIPv6CidrBlock}
680685
}
681686
}
682687

683-
rules := infrav1.IngressRules{
684-
{
688+
if !allowedNLBTraffic {
689+
rules = append(rules, infrav1.IngressRule{
685690
Description: "Allow NLB traffic to the control plane instances.",
686691
Protocol: infrav1.SecurityGroupProtocolTCP,
687692
FromPort: int64(s.scope.APIServerPort()),
688693
ToPort: int64(s.scope.APIServerPort()),
689694
CidrBlocks: ipv4CidrBlocks,
690695
IPv6CidrBlocks: ipv6CidrBlocks,
691-
},
696+
})
697+
allowedNLBTraffic = true
692698
}
693699

694-
for _, ln := range s.scope.ControlPlaneLoadBalancer().AdditionalListeners {
700+
for _, ln := range lb.AdditionalListeners {
695701
rules = append(rules, infrav1.IngressRule{
696702
Description: fmt.Sprintf("Allow NLB traffic to the control plane instances on port %d.", ln.Port),
697703
Protocol: infrav1.SecurityGroupProtocolTCP,
@@ -701,10 +707,8 @@ func (s *Service) getSecurityGroupIngressRules(role infrav1.SecurityGroupRole) (
701707
IPv6CidrBlocks: ipv6CidrBlocks,
702708
})
703709
}
704-
705-
return rules, nil
706710
}
707-
return infrav1.IngressRules{}, nil
711+
return rules, nil
708712
}
709713

710714
return nil, errors.Errorf("Cannot determine ingress rules for unknown security group role %q", role)
@@ -915,8 +919,14 @@ func (s *Service) getIngressRulesToAllowKubeletToAccessTheControlPlaneLB() infra
915919
// getControlPlaneLBIngressRules returns the ingress rules for the control plane LB.
916920
// We allow all traffic when no other rules are defined.
917921
func (s *Service) getControlPlaneLBIngressRules() infrav1.IngressRules {
918-
if s.scope.ControlPlaneLoadBalancer() != nil && len(s.scope.ControlPlaneLoadBalancer().IngressRules) > 0 {
919-
return s.scope.ControlPlaneLoadBalancer().IngressRules
922+
ingressRules := infrav1.IngressRules{}
923+
for _, lb := range s.scope.ControlPlaneLoadBalancers() {
924+
if lb != nil && len(lb.IngressRules) > 0 {
925+
ingressRules = append(ingressRules, lb.IngressRules...)
926+
}
927+
}
928+
if len(ingressRules) > 0 {
929+
return ingressRules
920930
}
921931

922932
// If no custom ingress rules have been defined we allow all traffic so that the MC can access the WC API

pkg/cloud/services/securitygroup/securitygroups_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,64 @@ func TestControlPlaneLoadBalancerIngressRules(t *testing.T) {
13171317
},
13181318
},
13191319
},
1320+
{
1321+
name: "defined rules are used when using internal and external LB",
1322+
awsCluster: &infrav1.AWSCluster{
1323+
Spec: infrav1.AWSClusterSpec{
1324+
ControlPlaneLoadBalancer: &infrav1.AWSLoadBalancerSpec{
1325+
IngressRules: []infrav1.IngressRule{
1326+
{
1327+
Description: "My custom ingress rule",
1328+
Protocol: infrav1.SecurityGroupProtocolTCP,
1329+
FromPort: 1234,
1330+
ToPort: 1234,
1331+
CidrBlocks: []string{"172.126.1.1/0"},
1332+
},
1333+
},
1334+
Scheme: &infrav1.ELBSchemeInternal,
1335+
},
1336+
SecondaryControlPlaneLoadBalancer: &infrav1.AWSLoadBalancerSpec{
1337+
IngressRules: []infrav1.IngressRule{
1338+
{
1339+
Description: "Another custom ingress rule",
1340+
Protocol: infrav1.SecurityGroupProtocolTCP,
1341+
FromPort: 2345,
1342+
ToPort: 2345,
1343+
CidrBlocks: []string{"0.0.0.0/0"},
1344+
},
1345+
},
1346+
},
1347+
NetworkSpec: infrav1.NetworkSpec{
1348+
VPC: infrav1.VPCSpec{
1349+
CidrBlock: "10.0.0.0/16",
1350+
},
1351+
},
1352+
},
1353+
},
1354+
expectedIngresRules: infrav1.IngressRules{
1355+
infrav1.IngressRule{
1356+
Description: "Kubernetes API",
1357+
Protocol: infrav1.SecurityGroupProtocolTCP,
1358+
FromPort: 6443,
1359+
ToPort: 6443,
1360+
CidrBlocks: []string{"10.0.0.0/16"},
1361+
},
1362+
infrav1.IngressRule{
1363+
Description: "My custom ingress rule",
1364+
Protocol: infrav1.SecurityGroupProtocolTCP,
1365+
FromPort: 1234,
1366+
ToPort: 1234,
1367+
CidrBlocks: []string{"172.126.1.1/0"},
1368+
},
1369+
infrav1.IngressRule{
1370+
Description: "Another custom ingress rule",
1371+
Protocol: infrav1.SecurityGroupProtocolTCP,
1372+
FromPort: 2345,
1373+
ToPort: 2345,
1374+
CidrBlocks: []string{"0.0.0.0/0"},
1375+
},
1376+
},
1377+
},
13201378
}
13211379

13221380
for _, tc := range testCases {

0 commit comments

Comments
 (0)