Skip to content

Commit

Permalink
Merge pull request #5058 from jas-nik/filter-pvt-sub
Browse files Browse the repository at this point in the history
✨  Add EKS Control Plane private subnet restriction flag
  • Loading branch information
k8s-ci-robot authored Jul 29, 2024
2 parents 6ad02ef + 8050e01 commit 8d68d03
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2860,6 +2860,11 @@ spec:
region:
description: The AWS Region the cluster lives in.
type: string
restrictPrivateSubnets:
default: false
description: RestrictPrivateSubnets indicates that the EKS control
plane should only use private subnets.
type: boolean
roleAdditionalPolicies:
description: |-
RoleAdditionalPolicies allows you to attach additional polices to
Expand Down
1 change: 1 addition & 0 deletions controlplane/eks/api/v1beta1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (r *AWSManagedControlPlane) ConvertTo(dstRaw conversion.Hub) error {
}
dst.Spec.VpcCni.Disable = r.Spec.DisableVPCCNI
dst.Spec.Partition = restored.Spec.Partition
dst.Spec.RestrictPrivateSubnets = restored.Spec.RestrictPrivateSubnets

return nil
}
Expand Down
1 change: 1 addition & 0 deletions controlplane/eks/api/v1beta1/zz_generated.conversion.go

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

4 changes: 4 additions & 0 deletions controlplane/eks/api/v1beta2/awsmanagedcontrolplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ type AWSManagedControlPlaneSpec struct { //nolint: maligned
// +optional
VpcCni VpcCni `json:"vpcCni,omitempty"`

// RestrictPrivateSubnets indicates that the EKS control plane should only use private subnets.
// +kubebuilder:default=false
RestrictPrivateSubnets bool `json:"restrictPrivateSubnets,omitempty"`

// KubeProxy defines managed attributes of the kube-proxy daemonset
KubeProxy KubeProxy `json:"kubeProxy,omitempty"`
}
Expand Down
18 changes: 18 additions & 0 deletions controlplane/eks/api/v1beta2/awsmanagedcontrolplane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (r *AWSManagedControlPlane) ValidateCreate() (admission.Warnings, error) {
allErrs = append(allErrs, r.validateSecondaryCIDR()...)
allErrs = append(allErrs, r.validateEKSAddons()...)
allErrs = append(allErrs, r.validateDisableVPCCNI()...)
allErrs = append(allErrs, r.validateRestrictPrivateSubnets()...)
allErrs = append(allErrs, r.validateKubeProxy()...)
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
allErrs = append(allErrs, r.validateNetwork()...)
Expand Down Expand Up @@ -126,6 +127,7 @@ func (r *AWSManagedControlPlane) ValidateUpdate(old runtime.Object) (admission.W
allErrs = append(allErrs, r.validateSecondaryCIDR()...)
allErrs = append(allErrs, r.validateEKSAddons()...)
allErrs = append(allErrs, r.validateDisableVPCCNI()...)
allErrs = append(allErrs, r.validateRestrictPrivateSubnets()...)
allErrs = append(allErrs, r.validateKubeProxy()...)
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
allErrs = append(allErrs, r.validatePrivateDNSHostnameTypeOnLaunch()...)
Expand Down Expand Up @@ -392,6 +394,22 @@ func (r *AWSManagedControlPlane) validateDisableVPCCNI() field.ErrorList {
return allErrs
}

func (r *AWSManagedControlPlane) validateRestrictPrivateSubnets() field.ErrorList {
var allErrs field.ErrorList

if r.Spec.RestrictPrivateSubnets && r.Spec.NetworkSpec.VPC.IsUnmanaged(r.Spec.EKSClusterName) {
boolField := field.NewPath("spec", "restrictPrivateSubnets")
if len(r.Spec.NetworkSpec.Subnets.FilterPrivate()) == 0 {
allErrs = append(allErrs, field.Invalid(boolField, r.Spec.RestrictPrivateSubnets, "cannot enable private subnets restriction when no private subnets are specified"))
}
}

if len(allErrs) == 0 {
return nil
}
return allErrs
}

func (r *AWSManagedControlPlane) validatePrivateDNSHostnameTypeOnLaunch() field.ErrorList {
var allErrs field.ErrorList

Expand Down
5 changes: 5 additions & 0 deletions pkg/cloud/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ func (s *ManagedControlPlaneScope) VpcCni() ekscontrolplanev1.VpcCni {
return s.ControlPlane.Spec.VpcCni
}

// RestrictPrivateSubnets returns whether Control Plane should be restricted to Private subnets.
func (s *ManagedControlPlaneScope) RestrictPrivateSubnets() bool {
return s.ControlPlane.Spec.RestrictPrivateSubnets
}

// OIDCIdentityProviderConfig returns the OIDC identity provider config.
func (s *ManagedControlPlaneScope) OIDCIdentityProviderConfig() *ekscontrolplanev1.OIDCIdentityProviderConfig {
return s.ControlPlane.Spec.OIDCIdentityProviderConfig
Expand Down
21 changes: 19 additions & 2 deletions pkg/cloud/services/eks/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,18 @@ func makeEksLogging(loggingSpec *ekscontrolplanev1.ControlPlaneLoggingSpec) *eks
}

func (s *Service) createCluster(eksClusterName string) (*eks.Cluster, error) {
var (
vpcConfig *eks.VpcConfigRequest
err error
)
logging := makeEksLogging(s.scope.ControlPlane.Spec.Logging)
encryptionConfigs := makeEksEncryptionConfigs(s.scope.ControlPlane.Spec.EncryptionConfig)
vpcConfig, err := makeVpcConfig(s.scope.Subnets(), s.scope.ControlPlane.Spec.EndpointAccess, s.scope.SecurityGroups())
if s.scope.ControlPlane.Spec.RestrictPrivateSubnets {
s.scope.Info("Filtering private subnets")
vpcConfig, err = makeVpcConfig(s.scope.Subnets().FilterPrivate(), s.scope.ControlPlane.Spec.EndpointAccess, s.scope.SecurityGroups())
} else {
vpcConfig, err = makeVpcConfig(s.scope.Subnets(), s.scope.ControlPlane.Spec.EndpointAccess, s.scope.SecurityGroups())
}
if err != nil {
return nil, errors.Wrap(err, "couldn't create vpc config for cluster")
}
Expand Down Expand Up @@ -542,8 +551,16 @@ func publicAccessCIDRsEqual(as []*string, bs []*string) bool {
}

func (s *Service) reconcileVpcConfig(vpcConfig *eks.VpcConfigResponse) (*eks.VpcConfigRequest, error) {
var (
updatedVpcConfig *eks.VpcConfigRequest
err error
)
endpointAccess := s.scope.ControlPlane.Spec.EndpointAccess
updatedVpcConfig, err := makeVpcConfig(s.scope.Subnets(), endpointAccess, s.scope.SecurityGroups())
if s.scope.ControlPlane.Spec.RestrictPrivateSubnets {
updatedVpcConfig, err = makeVpcConfig(s.scope.Subnets().FilterPrivate(), endpointAccess, s.scope.SecurityGroups())
} else {
updatedVpcConfig, err = makeVpcConfig(s.scope.Subnets(), endpointAccess, s.scope.SecurityGroups())
}
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8d68d03

Please sign in to comment.