Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/v1beta1/awscluster_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (src *AWSCluster) ConvertTo(dstRaw conversion.Hub) error {
}

dst.Spec.NetworkSpec.AdditionalControlPlaneIngressRules = restored.Spec.NetworkSpec.AdditionalControlPlaneIngressRules
dst.Spec.NetworkSpec.NodePortIngressRuleCidrBlocks = restored.Spec.NetworkSpec.NodePortIngressRuleCidrBlocks

if restored.Spec.NetworkSpec.VPC.IPAMPool != nil {
if dst.Spec.NetworkSpec.VPC.IPAMPool == nil {
Expand Down
1 change: 1 addition & 0 deletions api/v1beta1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions api/v1beta2/awscluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1beta2

import (
"fmt"
"net"
"strings"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -267,6 +268,12 @@ func (r *AWSCluster) validateNetwork() field.ErrorList {
allErrs = append(allErrs, r.validateIngressRule(rule)...)
}

for cidrBlockIndex, cidrBlock := range r.Spec.NetworkSpec.NodePortIngressRuleCidrBlocks {
if _, _, err := net.ParseCIDR(cidrBlock); err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "network", fmt.Sprintf("nodePortIngressRuleCidrBlocks[%d]", cidrBlockIndex)), r.Spec.NetworkSpec.NodePortIngressRuleCidrBlocks, "CIDR block is invalid"))
}
}

if r.Spec.NetworkSpec.VPC.ElasticIPPool != nil {
eipp := r.Spec.NetworkSpec.VPC.ElasticIPPool
if eipp.PublicIpv4Pool != nil {
Expand Down
22 changes: 22 additions & 0 deletions api/v1beta2/awscluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,28 @@ func TestAWSClusterValidateCreate(t *testing.T) {
},
wantErr: false,
},
{
name: "accepts cidrBlock for default node port ingress rule",
cluster: &AWSCluster{
Spec: AWSClusterSpec{
NetworkSpec: NetworkSpec{
NodePortIngressRuleCidrBlocks: []string{"10.0.0.0/16"},
},
},
},
wantErr: false,
},
{
name: "reject invalid cidrBlock for default node port ingress rule",
cluster: &AWSCluster{
Spec: AWSClusterSpec{
NetworkSpec: NetworkSpec{
NodePortIngressRuleCidrBlocks: []string{"10.0.0.0"},
},
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta2/network_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ type NetworkSpec struct {
// AdditionalControlPlaneIngressRules is an optional set of ingress rules to add to the control plane
// +optional
AdditionalControlPlaneIngressRules []IngressRule `json:"additionalControlPlaneIngressRules,omitempty"`

// NodePortIngressRuleCidrBlocks is an optional set of CIDR blocks to allow traffic to nodes' NodePort services.
// If none are specified here, all IPs are allowed to connect.
// +optional
NodePortIngressRuleCidrBlocks []string `json:"nodePortIngressRuleCidrBlocks,omitempty"`
}

// IPv6 contains ipv6 specific settings for the network.
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,13 @@ spec:
type: object
type: array
type: object
nodePortIngressRuleCidrBlocks:
description: |-
NodePortIngressRuleCidrBlocks is an optional set of CIDR blocks to allow traffic to nodes' NodePort services.
If none are specified here, all IPs are allowed to connect.
items:
type: string
type: array
securityGroupOverrides:
additionalProperties:
type: string
Expand Down Expand Up @@ -2500,6 +2507,13 @@ spec:
type: object
type: array
type: object
nodePortIngressRuleCidrBlocks:
description: |-
NodePortIngressRuleCidrBlocks is an optional set of CIDR blocks to allow traffic to nodes' NodePort services.
If none are specified here, all IPs are allowed to connect.
items:
type: string
type: array
securityGroupOverrides:
additionalProperties:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,13 @@ spec:
type: object
type: array
type: object
nodePortIngressRuleCidrBlocks:
description: |-
NodePortIngressRuleCidrBlocks is an optional set of CIDR blocks to allow traffic to nodes' NodePort services.
If none are specified here, all IPs are allowed to connect.
items:
type: string
type: array
securityGroupOverrides:
additionalProperties:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,13 @@ spec:
type: object
type: array
type: object
nodePortIngressRuleCidrBlocks:
description: |-
NodePortIngressRuleCidrBlocks is an optional set of CIDR blocks to allow traffic to nodes' NodePort services.
If none are specified here, all IPs are allowed to connect.
items:
type: string
type: array
securityGroupOverrides:
additionalProperties:
type: string
Expand Down
5 changes: 5 additions & 0 deletions pkg/cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,8 @@ func (s *ClusterScope) AdditionalControlPlaneIngressRules() []infrav1.IngressRul
func (s *ClusterScope) UnstructuredControlPlane() (*unstructured.Unstructured, error) {
return getUnstructuredControlPlane(context.TODO(), s.client, s.Cluster)
}

// NodePortIngressRuleCidrBlocks returns the CIDR blocks for the node NodePort ingress rules.
func (s *ClusterScope) NodePortIngressRuleCidrBlocks() []string {
return s.AWSCluster.Spec.NetworkSpec.DeepCopy().NodePortIngressRuleCidrBlocks
}
5 changes: 5 additions & 0 deletions pkg/cloud/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,8 @@ func (s *ManagedControlPlaneScope) AdditionalControlPlaneIngressRules() []infrav
func (s *ManagedControlPlaneScope) UnstructuredControlPlane() (*unstructured.Unstructured, error) {
return getUnstructuredControlPlane(context.TODO(), s.Client, s.Cluster)
}

// NodePortIngressRuleCidrBlocks returns the CIDR blocks for the node NodePort ingress rules.
func (s *ManagedControlPlaneScope) NodePortIngressRuleCidrBlocks() []string {
return nil
}
3 changes: 3 additions & 0 deletions pkg/cloud/scope/sg.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ type SGScope interface {
// ControlPlaneLoadBalancers returns both the ControlPlaneLoadBalancer and SecondaryControlPlaneLoadBalancer AWSLoadBalancerSpecs.
// The control plane load balancers should always be returned in the above order.
ControlPlaneLoadBalancers() []*infrav1.AWSLoadBalancerSpec

// NodePortIngressRuleCidrBlocks returns the CIDR blocks for the node NodePort ingress rules.
NodePortIngressRuleCidrBlocks() []string
}
5 changes: 4 additions & 1 deletion pkg/cloud/services/securitygroup/securitygroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,6 @@ func (s *Service) getSecurityGroupIngressRules(role infrav1.SecurityGroupRole) (
},
}
}
cidrBlocks := []string{services.AnyIPv4CidrBlock}
switch role {
case infrav1.SecurityGroupBastion:
return infrav1.IngressRules{
Expand Down Expand Up @@ -645,6 +644,10 @@ func (s *Service) getSecurityGroupIngressRules(role infrav1.SecurityGroupRole) (
return append(cniRules, rules...), nil

case infrav1.SecurityGroupNode:
cidrBlocks := []string{services.AnyIPv4CidrBlock}
if scopeCidrBlocks := s.scope.NodePortIngressRuleCidrBlocks(); len(scopeCidrBlocks) > 0 {
cidrBlocks = scopeCidrBlocks
}
rules := infrav1.IngressRules{
{
Description: "Node Port Services",
Expand Down
94 changes: 94 additions & 0 deletions pkg/cloud/services/securitygroup/securitygroups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2237,3 +2237,97 @@ func TestExpandIngressRules(t *testing.T) {
})
}
}

func TestNodePortServicesIngressRules(t *testing.T) {
scheme := runtime.NewScheme()
_ = infrav1.AddToScheme(scheme)

testCases := []struct {
name string
cidrBlocks []string
expectedIngresRules infrav1.IngressRules
}{
{
name: "default node ports services ingress rules, no node port cidr block provided",
cidrBlocks: nil,
expectedIngresRules: infrav1.IngressRules{
{
Description: "Node Port Services",
Protocol: infrav1.SecurityGroupProtocolTCP,
FromPort: 30000,
ToPort: 32767,
CidrBlocks: []string{services.AnyIPv4CidrBlock},
},
{
Description: "Kubelet API",
Protocol: infrav1.SecurityGroupProtocolTCP,
FromPort: 10250,
ToPort: 10250,
SourceSecurityGroupIDs: []string{"Id1", "Id2"},
},
},
},
{
name: "node port cidr block provided, no default cidr block used for node port services ingress rule",
cidrBlocks: []string{"10.0.0.0/16"},
expectedIngresRules: infrav1.IngressRules{
{
Description: "Node Port Services",
Protocol: infrav1.SecurityGroupProtocolTCP,
FromPort: 30000,
ToPort: 32767,
CidrBlocks: []string{"10.0.0.0/16"},
},
{
Description: "Kubelet API",
Protocol: infrav1.SecurityGroupProtocolTCP,
FromPort: 10250,
ToPort: 10250,
SourceSecurityGroupIDs: []string{"Id1", "Id2"},
},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cs, err := scope.NewClusterScope(scope.ClusterScopeParams{
Client: fake.NewClientBuilder().WithScheme(scheme).Build(),
Cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: "test-cluster"},
},
AWSCluster: &infrav1.AWSCluster{
Spec: infrav1.AWSClusterSpec{
ControlPlaneLoadBalancer: &infrav1.AWSLoadBalancerSpec{},
NetworkSpec: infrav1.NetworkSpec{
VPC: infrav1.VPCSpec{
CidrBlock: "10.0.0.0/16",
},
NodePortIngressRuleCidrBlocks: tc.cidrBlocks,
},
},
Status: infrav1.AWSClusterStatus{
Network: infrav1.NetworkStatus{
SecurityGroups: map[infrav1.SecurityGroupRole]infrav1.SecurityGroup{
infrav1.SecurityGroupControlPlane: {ID: "Id1"},
infrav1.SecurityGroupNode: {ID: "Id2"},
},
},
},
},
})
if err != nil {
t.Fatalf("Failed to create test context: %v", err)
}

s := NewService(cs, testSecurityGroupRoles)
rules, err := s.getSecurityGroupIngressRules(infrav1.SecurityGroupNode)
if err != nil {
t.Fatalf("Failed to lookup node security group ingress rules: %v", err)
}

g := NewGomegaWithT(t)
g.Expect(rules).To(Equal(tc.expectedIngresRules))
})
}
}
Loading