Skip to content

Commit 4bf9d24

Browse files
committed
sg: allow both ipv4 and ipv6 cidrs to API LB if vpc ipv6 block is defined
When AWSCluster.spec.network.vpc.ipv6 is non-nil, most handlers in CAPA treats it as "adding" IPv6 capabilities on top of IPv4 infrastructure. Except security group ingress rules for API LB. This commit aligns the API LB SG handler with the rest of the code base. These rules can be overriden in the AWSCluster LB spec to allow only IPv6 CIDRs if needed.
1 parent 0afbb24 commit 4bf9d24

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

pkg/cloud/services/securitygroup/securitygroups.go

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -994,19 +994,7 @@ func (s *Service) getControlPlaneLBIngressRules() infrav1.IngressRules {
994994
}
995995

996996
func (s *Service) getIngressRuleToAllowAnyIPInTheAPIServer() infrav1.IngressRules {
997-
if s.scope.VPC().IsIPv6Enabled() {
998-
return infrav1.IngressRules{
999-
{
1000-
Description: "Kubernetes API IPv6",
1001-
Protocol: infrav1.SecurityGroupProtocolTCP,
1002-
FromPort: int64(s.scope.APIServerPort()),
1003-
ToPort: int64(s.scope.APIServerPort()),
1004-
IPv6CidrBlocks: []string{services.AnyIPv6CidrBlock},
1005-
},
1006-
}
1007-
}
1008-
1009-
return infrav1.IngressRules{
997+
rules := infrav1.IngressRules{
1010998
{
1011999
Description: "Kubernetes API",
10121000
Protocol: infrav1.SecurityGroupProtocolTCP,
@@ -1015,22 +1003,20 @@ func (s *Service) getIngressRuleToAllowAnyIPInTheAPIServer() infrav1.IngressRule
10151003
CidrBlocks: []string{services.AnyIPv4CidrBlock},
10161004
},
10171005
}
1006+
if s.scope.VPC().IsIPv6Enabled() {
1007+
rules = append(rules, infrav1.IngressRule{
1008+
Description: "Kubernetes API IPv6",
1009+
Protocol: infrav1.SecurityGroupProtocolTCP,
1010+
FromPort: int64(s.scope.APIServerPort()),
1011+
ToPort: int64(s.scope.APIServerPort()),
1012+
IPv6CidrBlocks: []string{services.AnyIPv6CidrBlock},
1013+
})
1014+
}
1015+
return rules
10181016
}
10191017

10201018
func (s *Service) getIngressRuleToAllowVPCCidrInTheAPIServer() infrav1.IngressRules {
1021-
if s.scope.VPC().IsIPv6Enabled() {
1022-
return infrav1.IngressRules{
1023-
{
1024-
Description: "Kubernetes API IPv6",
1025-
Protocol: infrav1.SecurityGroupProtocolTCP,
1026-
FromPort: int64(s.scope.APIServerPort()),
1027-
ToPort: int64(s.scope.APIServerPort()),
1028-
IPv6CidrBlocks: []string{s.scope.VPC().IPv6.CidrBlock},
1029-
},
1030-
}
1031-
}
1032-
1033-
return infrav1.IngressRules{
1019+
rules := infrav1.IngressRules{
10341020
{
10351021
Description: "Kubernetes API",
10361022
Protocol: infrav1.SecurityGroupProtocolTCP,
@@ -1039,6 +1025,16 @@ func (s *Service) getIngressRuleToAllowVPCCidrInTheAPIServer() infrav1.IngressRu
10391025
CidrBlocks: []string{s.scope.VPC().CidrBlock},
10401026
},
10411027
}
1028+
if s.scope.VPC().IsIPv6Enabled() {
1029+
rules = append(rules, infrav1.IngressRule{
1030+
Description: "Kubernetes API IPv6",
1031+
Protocol: infrav1.SecurityGroupProtocolTCP,
1032+
FromPort: int64(s.scope.APIServerPort()),
1033+
ToPort: int64(s.scope.APIServerPort()),
1034+
IPv6CidrBlocks: []string{s.scope.VPC().IPv6.CidrBlock},
1035+
})
1036+
}
1037+
return rules
10421038
}
10431039

10441040
func (s *Service) processIngressRulesSGs(ingressRules []infrav1.IngressRule) (infrav1.IngressRules, error) {

pkg/cloud/services/securitygroup/securitygroups_test.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,7 @@ func TestControlPlaneLoadBalancerIngressRules(t *testing.T) {
16071607
},
16081608
},
16091609
{
1610-
name: "when no ingress rules are passed and nat gateway IPs are not available, the default for IPv6 is set",
1610+
name: "when no ingress rules are passed and nat gateway IPs are not available with vpc ipv6 block is defined, the default for IPv4 and IPv6 are set",
16111611
awsCluster: &infrav1.AWSCluster{
16121612
Spec: infrav1.AWSClusterSpec{
16131613
ControlPlaneLoadBalancer: &infrav1.AWSLoadBalancerSpec{},
@@ -1621,6 +1621,13 @@ func TestControlPlaneLoadBalancerIngressRules(t *testing.T) {
16211621
Status: infrav1.AWSClusterStatus{},
16221622
},
16231623
expectedIngresRules: infrav1.IngressRules{
1624+
infrav1.IngressRule{
1625+
Description: "Kubernetes API",
1626+
Protocol: infrav1.SecurityGroupProtocolTCP,
1627+
FromPort: 6443,
1628+
ToPort: 6443,
1629+
CidrBlocks: []string{services.AnyIPv4CidrBlock},
1630+
},
16241631
infrav1.IngressRule{
16251632
Description: "Kubernetes API IPv6",
16261633
Protocol: infrav1.SecurityGroupProtocolTCP,
@@ -1748,20 +1755,35 @@ func TestControlPlaneLoadBalancerIngressRules(t *testing.T) {
17481755
},
17491756
NetworkSpec: infrav1.NetworkSpec{
17501757
VPC: infrav1.VPCSpec{
1758+
CidrBlock: "10.0.0.0/16",
17511759
IPv6: &infrav1.IPv6{
1752-
CidrBlock: "10.0.0.0/16",
1760+
CidrBlock: "2001:1234:5678:9a40::/56",
17531761
},
17541762
},
17551763
},
17561764
},
17571765
},
17581766
expectedIngresRules: infrav1.IngressRules{
1767+
infrav1.IngressRule{
1768+
Description: "Kubernetes API",
1769+
Protocol: infrav1.SecurityGroupProtocolTCP,
1770+
FromPort: 6443,
1771+
ToPort: 6443,
1772+
CidrBlocks: []string{"10.0.0.0/16"},
1773+
},
17591774
infrav1.IngressRule{
17601775
Description: "Kubernetes API IPv6",
17611776
Protocol: infrav1.SecurityGroupProtocolTCP,
17621777
FromPort: 6443,
17631778
ToPort: 6443,
1764-
IPv6CidrBlocks: []string{"10.0.0.0/16"},
1779+
IPv6CidrBlocks: []string{"2001:1234:5678:9a40::/56"},
1780+
},
1781+
infrav1.IngressRule{
1782+
Description: "Kubernetes API",
1783+
Protocol: infrav1.SecurityGroupProtocolTCP,
1784+
FromPort: 6443,
1785+
ToPort: 6443,
1786+
CidrBlocks: []string{services.AnyIPv4CidrBlock},
17651787
},
17661788
infrav1.IngressRule{
17671789
Description: "Kubernetes API IPv6",

0 commit comments

Comments
 (0)