Skip to content

Commit 8050e01

Browse files
committed
Add Control Plane Private Subnet Filter
1 parent c383c6e commit 8050e01

File tree

7 files changed

+53
-2
lines changed

7 files changed

+53
-2
lines changed

config/crd/bases/controlplane.cluster.x-k8s.io_awsmanagedcontrolplanes.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,6 +2832,11 @@ spec:
28322832
region:
28332833
description: The AWS Region the cluster lives in.
28342834
type: string
2835+
restrictPrivateSubnets:
2836+
default: false
2837+
description: RestrictPrivateSubnets indicates that the EKS control
2838+
plane should only use private subnets.
2839+
type: boolean
28352840
roleAdditionalPolicies:
28362841
description: |-
28372842
RoleAdditionalPolicies allows you to attach additional polices to

controlplane/eks/api/v1beta1/conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func (r *AWSManagedControlPlane) ConvertTo(dstRaw conversion.Hub) error {
4040
}
4141
dst.Spec.VpcCni.Disable = r.Spec.DisableVPCCNI
4242
dst.Spec.Partition = restored.Spec.Partition
43+
dst.Spec.RestrictPrivateSubnets = restored.Spec.RestrictPrivateSubnets
4344

4445
return nil
4546
}

controlplane/eks/api/v1beta1/zz_generated.conversion.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controlplane/eks/api/v1beta2/awsmanagedcontrolplane_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ type AWSManagedControlPlaneSpec struct { //nolint: maligned
173173
// +optional
174174
VpcCni VpcCni `json:"vpcCni,omitempty"`
175175

176+
// RestrictPrivateSubnets indicates that the EKS control plane should only use private subnets.
177+
// +kubebuilder:default=false
178+
RestrictPrivateSubnets bool `json:"restrictPrivateSubnets,omitempty"`
179+
176180
// KubeProxy defines managed attributes of the kube-proxy daemonset
177181
KubeProxy KubeProxy `json:"kubeProxy,omitempty"`
178182
}

controlplane/eks/api/v1beta2/awsmanagedcontrolplane_webhook.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func (r *AWSManagedControlPlane) ValidateCreate() (admission.Warnings, error) {
9191
allErrs = append(allErrs, r.validateSecondaryCIDR()...)
9292
allErrs = append(allErrs, r.validateEKSAddons()...)
9393
allErrs = append(allErrs, r.validateDisableVPCCNI()...)
94+
allErrs = append(allErrs, r.validateRestrictPrivateSubnets()...)
9495
allErrs = append(allErrs, r.validateKubeProxy()...)
9596
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
9697
allErrs = append(allErrs, r.validateNetwork()...)
@@ -126,6 +127,7 @@ func (r *AWSManagedControlPlane) ValidateUpdate(old runtime.Object) (admission.W
126127
allErrs = append(allErrs, r.validateSecondaryCIDR()...)
127128
allErrs = append(allErrs, r.validateEKSAddons()...)
128129
allErrs = append(allErrs, r.validateDisableVPCCNI()...)
130+
allErrs = append(allErrs, r.validateRestrictPrivateSubnets()...)
129131
allErrs = append(allErrs, r.validateKubeProxy()...)
130132
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
131133
allErrs = append(allErrs, r.validatePrivateDNSHostnameTypeOnLaunch()...)
@@ -392,6 +394,22 @@ func (r *AWSManagedControlPlane) validateDisableVPCCNI() field.ErrorList {
392394
return allErrs
393395
}
394396

397+
func (r *AWSManagedControlPlane) validateRestrictPrivateSubnets() field.ErrorList {
398+
var allErrs field.ErrorList
399+
400+
if r.Spec.RestrictPrivateSubnets && r.Spec.NetworkSpec.VPC.IsUnmanaged(r.Spec.EKSClusterName) {
401+
boolField := field.NewPath("spec", "restrictPrivateSubnets")
402+
if len(r.Spec.NetworkSpec.Subnets.FilterPrivate()) == 0 {
403+
allErrs = append(allErrs, field.Invalid(boolField, r.Spec.RestrictPrivateSubnets, "cannot enable private subnets restriction when no private subnets are specified"))
404+
}
405+
}
406+
407+
if len(allErrs) == 0 {
408+
return nil
409+
}
410+
return allErrs
411+
}
412+
395413
func (r *AWSManagedControlPlane) validatePrivateDNSHostnameTypeOnLaunch() field.ErrorList {
396414
var allErrs field.ErrorList
397415

pkg/cloud/scope/managedcontrolplane.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,11 @@ func (s *ManagedControlPlaneScope) VpcCni() ekscontrolplanev1.VpcCni {
407407
return s.ControlPlane.Spec.VpcCni
408408
}
409409

410+
// RestrictPrivateSubnets returns whether Control Plane should be restricted to Private subnets.
411+
func (s *ManagedControlPlaneScope) RestrictPrivateSubnets() bool {
412+
return s.ControlPlane.Spec.RestrictPrivateSubnets
413+
}
414+
410415
// OIDCIdentityProviderConfig returns the OIDC identity provider config.
411416
func (s *ManagedControlPlaneScope) OIDCIdentityProviderConfig() *ekscontrolplanev1.OIDCIdentityProviderConfig {
412417
return s.ControlPlane.Spec.OIDCIdentityProviderConfig

pkg/cloud/services/eks/cluster.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,18 @@ func makeEksLogging(loggingSpec *ekscontrolplanev1.ControlPlaneLoggingSpec) *eks
359359
}
360360

361361
func (s *Service) createCluster(eksClusterName string) (*eks.Cluster, error) {
362+
var (
363+
vpcConfig *eks.VpcConfigRequest
364+
err error
365+
)
362366
logging := makeEksLogging(s.scope.ControlPlane.Spec.Logging)
363367
encryptionConfigs := makeEksEncryptionConfigs(s.scope.ControlPlane.Spec.EncryptionConfig)
364-
vpcConfig, err := makeVpcConfig(s.scope.Subnets(), s.scope.ControlPlane.Spec.EndpointAccess, s.scope.SecurityGroups())
368+
if s.scope.ControlPlane.Spec.RestrictPrivateSubnets {
369+
s.scope.Info("Filtering private subnets")
370+
vpcConfig, err = makeVpcConfig(s.scope.Subnets().FilterPrivate(), s.scope.ControlPlane.Spec.EndpointAccess, s.scope.SecurityGroups())
371+
} else {
372+
vpcConfig, err = makeVpcConfig(s.scope.Subnets(), s.scope.ControlPlane.Spec.EndpointAccess, s.scope.SecurityGroups())
373+
}
365374
if err != nil {
366375
return nil, errors.Wrap(err, "couldn't create vpc config for cluster")
367376
}
@@ -542,8 +551,16 @@ func publicAccessCIDRsEqual(as []*string, bs []*string) bool {
542551
}
543552

544553
func (s *Service) reconcileVpcConfig(vpcConfig *eks.VpcConfigResponse) (*eks.VpcConfigRequest, error) {
554+
var (
555+
updatedVpcConfig *eks.VpcConfigRequest
556+
err error
557+
)
545558
endpointAccess := s.scope.ControlPlane.Spec.EndpointAccess
546-
updatedVpcConfig, err := makeVpcConfig(s.scope.Subnets(), endpointAccess, s.scope.SecurityGroups())
559+
if s.scope.ControlPlane.Spec.RestrictPrivateSubnets {
560+
updatedVpcConfig, err = makeVpcConfig(s.scope.Subnets().FilterPrivate(), endpointAccess, s.scope.SecurityGroups())
561+
} else {
562+
updatedVpcConfig, err = makeVpcConfig(s.scope.Subnets(), endpointAccess, s.scope.SecurityGroups())
563+
}
547564
if err != nil {
548565
return nil, err
549566
}

0 commit comments

Comments
 (0)