Skip to content

Commit b60ecb1

Browse files
committed
securitygroup: allow setting allowed IPv6 CIDR for node NodePort services
For IPv4, we have field NodePortIngressRuleCidrBlocks that specifies the allowed source IPv4 CIDR for node NodePort services on port 30000-32767. This extends that field to also accept IPv6 source CIDRs.
1 parent b11e1fc commit b60ecb1

File tree

7 files changed

+169
-34
lines changed

7 files changed

+169
-34
lines changed

api/v1beta2/network_types.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/aws/aws-sdk-go-v2/aws"
2525
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
26+
"k8s.io/utils/net"
2627
"k8s.io/utils/ptr"
2728
)
2829

@@ -367,7 +368,32 @@ type NetworkSpec struct {
367368
// NodePortIngressRuleCidrBlocks is an optional set of CIDR blocks to allow traffic to nodes' NodePort services.
368369
// If none are specified here, all IPs are allowed to connect.
369370
// +optional
370-
NodePortIngressRuleCidrBlocks []string `json:"nodePortIngressRuleCidrBlocks,omitempty"`
371+
NodePortIngressRuleCidrBlocks CidrBlocks `json:"nodePortIngressRuleCidrBlocks,omitempty"`
372+
}
373+
374+
// CidrBlocks defines a set of CIDR blocks.
375+
type CidrBlocks []string
376+
377+
// IPv4CidrBlocks returns only IPv4 CIDR blocks.
378+
func (c CidrBlocks) IPv4CidrBlocks() CidrBlocks {
379+
var cidrs CidrBlocks
380+
for _, cidr := range c {
381+
if net.IsIPv4CIDRString(cidr) {
382+
cidrs = append(cidrs, cidr)
383+
}
384+
}
385+
return cidrs
386+
}
387+
388+
// IPv6CidrBlocks returns only IPv6 CIDR blocks.
389+
func (c CidrBlocks) IPv6CidrBlocks() CidrBlocks {
390+
var cidrs CidrBlocks
391+
for _, cidr := range c {
392+
if net.IsIPv6CIDRString(cidr) {
393+
cidrs = append(cidrs, cidr)
394+
}
395+
}
396+
return cidrs
371397
}
372398

373399
// IPv6 contains ipv6 specific settings for the network.

api/v1beta2/zz_generated.deepcopy.go

Lines changed: 21 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cloud/scope/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,6 @@ func (s *ClusterScope) UnstructuredControlPlane() (*unstructured.Unstructured, e
433433
}
434434

435435
// NodePortIngressRuleCidrBlocks returns the CIDR blocks for the node NodePort ingress rules.
436-
func (s *ClusterScope) NodePortIngressRuleCidrBlocks() []string {
436+
func (s *ClusterScope) NodePortIngressRuleCidrBlocks() infrav1.CidrBlocks {
437437
return s.AWSCluster.Spec.NetworkSpec.DeepCopy().NodePortIngressRuleCidrBlocks
438438
}

pkg/cloud/scope/managedcontrolplane.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ func (s *ManagedControlPlaneScope) UnstructuredControlPlane() (*unstructured.Uns
493493
}
494494

495495
// NodePortIngressRuleCidrBlocks returns the CIDR blocks for the node NodePort ingress rules.
496-
func (s *ManagedControlPlaneScope) NodePortIngressRuleCidrBlocks() []string {
496+
func (s *ManagedControlPlaneScope) NodePortIngressRuleCidrBlocks() infrav1.CidrBlocks {
497497
return nil
498498
}
499499

pkg/cloud/scope/sg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@ type SGScope interface {
6464
ControlPlaneLoadBalancers() []*infrav1.AWSLoadBalancerSpec
6565

6666
// NodePortIngressRuleCidrBlocks returns the CIDR blocks for the node NodePort ingress rules.
67-
NodePortIngressRuleCidrBlocks() []string
67+
NodePortIngressRuleCidrBlocks() infrav1.CidrBlocks
6868
}

pkg/cloud/services/securitygroup/securitygroups.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -647,17 +647,27 @@ func (s *Service) getSecurityGroupIngressRules(role infrav1.SecurityGroupRole) (
647647
return append(cniRules, rules...), nil
648648

649649
case infrav1.SecurityGroupNode:
650-
cidrBlocks := []string{services.AnyIPv4CidrBlock}
651-
if scopeCidrBlocks := s.scope.NodePortIngressRuleCidrBlocks(); len(scopeCidrBlocks) > 0 {
652-
cidrBlocks = scopeCidrBlocks
650+
ipv4CidrBlocks := []string{services.AnyIPv4CidrBlock}
651+
if scopeCidrBlocks := s.scope.NodePortIngressRuleCidrBlocks().IPv4CidrBlocks(); len(scopeCidrBlocks) > 0 {
652+
ipv4CidrBlocks = scopeCidrBlocks
653653
}
654+
655+
var ipv6CidrBlocks []string
656+
if s.scope.VPC().IsIPv6Enabled() {
657+
ipv6CidrBlocks = []string{services.AnyIPv6CidrBlock}
658+
if scopeCidrBlocks := s.scope.NodePortIngressRuleCidrBlocks().IPv6CidrBlocks(); len(scopeCidrBlocks) > 0 {
659+
ipv6CidrBlocks = scopeCidrBlocks
660+
}
661+
}
662+
654663
rules := infrav1.IngressRules{
655664
{
656-
Description: "Node Port Services",
657-
Protocol: infrav1.SecurityGroupProtocolTCP,
658-
FromPort: 30000,
659-
ToPort: 32767,
660-
CidrBlocks: cidrBlocks,
665+
Description: "Node Port Services",
666+
Protocol: infrav1.SecurityGroupProtocolTCP,
667+
FromPort: 30000,
668+
ToPort: 32767,
669+
CidrBlocks: ipv4CidrBlocks,
670+
IPv6CidrBlocks: ipv6CidrBlocks,
661671
},
662672
{
663673
Description: "Kubelet API",
@@ -671,18 +681,10 @@ func (s *Service) getSecurityGroupIngressRules(role infrav1.SecurityGroupRole) (
671681
},
672682
},
673683
}
684+
674685
if s.scope.Bastion().Enabled {
675686
rules = append(rules, s.defaultSSHIngressRule(s.scope.SecurityGroups()[infrav1.SecurityGroupBastion].ID))
676687
}
677-
if s.scope.VPC().IsIPv6Enabled() {
678-
rules = append(rules, infrav1.IngressRule{
679-
Description: "Node Port Services IPv6",
680-
Protocol: infrav1.SecurityGroupProtocolTCP,
681-
FromPort: 30000,
682-
ToPort: 32767,
683-
IPv6CidrBlocks: []string{services.AnyIPv6CidrBlock},
684-
})
685-
}
686688

687689
additionalIngressRules, err := s.processIngressRulesSGs(s.scope.AdditionalNodeIngressRules())
688690
if err != nil {

pkg/cloud/services/securitygroup/securitygroups_test.go

Lines changed: 99 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,12 +2344,16 @@ func TestNodePortServicesIngressRules(t *testing.T) {
23442344

23452345
testCases := []struct {
23462346
name string
2347-
cidrBlocks []string
2347+
networkSpec infrav1.NetworkSpec
23482348
expectedIngresRules infrav1.IngressRules
23492349
}{
23502350
{
2351-
name: "default node ports services ingress rules, no node port cidr block provided",
2352-
cidrBlocks: nil,
2351+
name: "default node ports services ingress rules, no node port cidr block provided",
2352+
networkSpec: infrav1.NetworkSpec{
2353+
VPC: infrav1.VPCSpec{
2354+
CidrBlock: "10.0.0.0/16",
2355+
},
2356+
},
23532357
expectedIngresRules: infrav1.IngressRules{
23542358
{
23552359
Description: "Node Port Services",
@@ -2368,8 +2372,39 @@ func TestNodePortServicesIngressRules(t *testing.T) {
23682372
},
23692373
},
23702374
{
2371-
name: "node port cidr block provided, no default cidr block used for node port services ingress rule",
2372-
cidrBlocks: []string{"10.0.0.0/16"},
2375+
name: "default node ports services ingress rules for IPv6, no node port cidr block provided",
2376+
networkSpec: infrav1.NetworkSpec{
2377+
VPC: infrav1.VPCSpec{
2378+
CidrBlock: "10.0.0.0/16",
2379+
IPv6: &infrav1.IPv6{},
2380+
},
2381+
},
2382+
expectedIngresRules: infrav1.IngressRules{
2383+
{
2384+
Description: "Node Port Services",
2385+
Protocol: infrav1.SecurityGroupProtocolTCP,
2386+
FromPort: 30000,
2387+
ToPort: 32767,
2388+
CidrBlocks: []string{services.AnyIPv4CidrBlock},
2389+
IPv6CidrBlocks: []string{services.AnyIPv6CidrBlock},
2390+
},
2391+
{
2392+
Description: "Kubelet API",
2393+
Protocol: infrav1.SecurityGroupProtocolTCP,
2394+
FromPort: 10250,
2395+
ToPort: 10250,
2396+
SourceSecurityGroupIDs: []string{"Id1", "Id2"},
2397+
},
2398+
},
2399+
},
2400+
{
2401+
name: "node port cidr block provided, no default cidr block used for node port services ingress rule",
2402+
networkSpec: infrav1.NetworkSpec{
2403+
VPC: infrav1.VPCSpec{
2404+
CidrBlock: "10.0.0.0/16",
2405+
},
2406+
NodePortIngressRuleCidrBlocks: []string{"10.0.0.0/16"},
2407+
},
23732408
expectedIngresRules: infrav1.IngressRules{
23742409
{
23752410
Description: "Node Port Services",
@@ -2387,6 +2422,64 @@ func TestNodePortServicesIngressRules(t *testing.T) {
23872422
},
23882423
},
23892424
},
2425+
{
2426+
name: "node port cidr block provided for only IPv6, no default cidr block used for node port services ingress rule",
2427+
networkSpec: infrav1.NetworkSpec{
2428+
VPC: infrav1.VPCSpec{
2429+
CidrBlock: "10.0.0.0/16",
2430+
IPv6: &infrav1.IPv6{
2431+
CidrBlock: "2001:1234:5678:9a40::/56",
2432+
},
2433+
},
2434+
NodePortIngressRuleCidrBlocks: []string{"2001:1234:5678:9a40::/56"},
2435+
},
2436+
expectedIngresRules: infrav1.IngressRules{
2437+
{
2438+
Description: "Node Port Services",
2439+
Protocol: infrav1.SecurityGroupProtocolTCP,
2440+
FromPort: 30000,
2441+
ToPort: 32767,
2442+
CidrBlocks: []string{services.AnyIPv4CidrBlock},
2443+
IPv6CidrBlocks: []string{"2001:1234:5678:9a40::/56"},
2444+
},
2445+
{
2446+
Description: "Kubelet API",
2447+
Protocol: infrav1.SecurityGroupProtocolTCP,
2448+
FromPort: 10250,
2449+
ToPort: 10250,
2450+
SourceSecurityGroupIDs: []string{"Id1", "Id2"},
2451+
},
2452+
},
2453+
},
2454+
{
2455+
name: "node port cidr block provided for both IPv4 and IPv6, no default cidr block used for node port services ingress rule",
2456+
networkSpec: infrav1.NetworkSpec{
2457+
VPC: infrav1.VPCSpec{
2458+
CidrBlock: "10.0.0.0/16",
2459+
IPv6: &infrav1.IPv6{
2460+
CidrBlock: "2001:1234:5678:9a40::/56",
2461+
},
2462+
},
2463+
NodePortIngressRuleCidrBlocks: []string{"10.0.0.0/16", "2001:1234:5678:9a40::/56"},
2464+
},
2465+
expectedIngresRules: infrav1.IngressRules{
2466+
{
2467+
Description: "Node Port Services",
2468+
Protocol: infrav1.SecurityGroupProtocolTCP,
2469+
FromPort: 30000,
2470+
ToPort: 32767,
2471+
CidrBlocks: []string{"10.0.0.0/16"},
2472+
IPv6CidrBlocks: []string{"2001:1234:5678:9a40::/56"},
2473+
},
2474+
{
2475+
Description: "Kubelet API",
2476+
Protocol: infrav1.SecurityGroupProtocolTCP,
2477+
FromPort: 10250,
2478+
ToPort: 10250,
2479+
SourceSecurityGroupIDs: []string{"Id1", "Id2"},
2480+
},
2481+
},
2482+
},
23902483
}
23912484

23922485
for _, tc := range testCases {
@@ -2399,12 +2492,7 @@ func TestNodePortServicesIngressRules(t *testing.T) {
23992492
AWSCluster: &infrav1.AWSCluster{
24002493
Spec: infrav1.AWSClusterSpec{
24012494
ControlPlaneLoadBalancer: &infrav1.AWSLoadBalancerSpec{},
2402-
NetworkSpec: infrav1.NetworkSpec{
2403-
VPC: infrav1.VPCSpec{
2404-
CidrBlock: "10.0.0.0/16",
2405-
},
2406-
NodePortIngressRuleCidrBlocks: tc.cidrBlocks,
2407-
},
2495+
NetworkSpec: tc.networkSpec,
24082496
},
24092497
Status: infrav1.AWSClusterStatus{
24102498
Network: infrav1.NetworkStatus{

0 commit comments

Comments
 (0)