Skip to content

Commit

Permalink
Merge branch 'eks-public-access-cidrs' of ssh://github.com/seddarj/te…
Browse files Browse the repository at this point in the history
…rraform-provider-aws into seddarj-eks-public-access-cidrs
  • Loading branch information
bflad committed Jan 9, 2020
2 parents 179ee0d + 1b11a03 commit 32384f8
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
5 changes: 5 additions & 0 deletions aws/data_source_aws_eks_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ func dataSourceAwsEksCluster() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"public_access_cidrs": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
Expand Down
1 change: 1 addition & 0 deletions aws/data_source_aws_eks_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestAccAWSEksClusterDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, "vpc_config.0.endpoint_public_access", dataSourceResourceName, "vpc_config.0.endpoint_public_access"),
resource.TestCheckResourceAttrPair(resourceName, "vpc_config.0.security_group_ids.#", dataSourceResourceName, "vpc_config.0.security_group_ids.#"),
resource.TestCheckResourceAttrPair(resourceName, "vpc_config.0.subnet_ids.#", dataSourceResourceName, "vpc_config.0.subnet_ids.#"),
resource.TestCheckResourceAttrPair(resourceName, "vpc_config.0.public_access_cidrs.#", dataSourceResourceName, "vpc_config.0.public_access_cidrs.#"),
resource.TestCheckResourceAttrPair(resourceName, "vpc_config.0.vpc_id", dataSourceResourceName, "vpc_config.0.vpc_id"),
),
},
Expand Down
27 changes: 24 additions & 3 deletions aws/resource_aws_eks_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ func resourceAwsEksCluster() *schema.Resource {
MinItems: 1,
Elem: &schema.Schema{Type: schema.TypeString},
},
"public_access_cidrs": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateCIDRNetworkAddress,
},
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -354,7 +362,7 @@ func resourceAwsEksClusterUpdate(d *schema.ResourceData, meta interface{}) error
}
}

if d.HasChange("vpc_config.0.endpoint_private_access") || d.HasChange("vpc_config.0.endpoint_public_access") {
if d.HasChange("vpc_config.0.endpoint_private_access") || d.HasChange("vpc_config.0.endpoint_public_access") || d.HasChange("vpc_config.0.public_access_cidrs") {
input := &eks.UpdateClusterConfigInput{
Name: aws.String(d.Id()),
ResourcesVpcConfig: expandEksVpcConfigUpdateRequest(d.Get("vpc_config").([]interface{})),
Expand Down Expand Up @@ -427,12 +435,18 @@ func expandEksVpcConfigRequest(l []interface{}) *eks.VpcConfigRequest {

m := l[0].(map[string]interface{})

return &eks.VpcConfigRequest{
vpcConfigRequest := &eks.VpcConfigRequest{
EndpointPrivateAccess: aws.Bool(m["endpoint_private_access"].(bool)),
EndpointPublicAccess: aws.Bool(m["endpoint_public_access"].(bool)),
SecurityGroupIds: expandStringSet(m["security_group_ids"].(*schema.Set)),
SubnetIds: expandStringSet(m["subnet_ids"].(*schema.Set)),
}

if v, ok := m["public_access_cidrs"].(*schema.Set); ok && v.Len() > 0 {
vpcConfigRequest.PublicAccessCidrs = expandStringSet(v)
}

return vpcConfigRequest
}

func expandEksVpcConfigUpdateRequest(l []interface{}) *eks.VpcConfigRequest {
Expand All @@ -442,10 +456,16 @@ func expandEksVpcConfigUpdateRequest(l []interface{}) *eks.VpcConfigRequest {

m := l[0].(map[string]interface{})

return &eks.VpcConfigRequest{
vpcConfigRequest := &eks.VpcConfigRequest{
EndpointPrivateAccess: aws.Bool(m["endpoint_private_access"].(bool)),
EndpointPublicAccess: aws.Bool(m["endpoint_public_access"].(bool)),
}

if v, ok := m["public_access_cidrs"].(*schema.Set); ok && v.Len() > 0 {
vpcConfigRequest.PublicAccessCidrs = expandStringSet(v)
}

return vpcConfigRequest
}

func expandEksLoggingTypes(vEnabledLogTypes *schema.Set) *eks.Logging {
Expand Down Expand Up @@ -516,6 +536,7 @@ func flattenEksVpcConfigResponse(vpcConfig *eks.VpcConfigResponse) []map[string]
"endpoint_public_access": aws.BoolValue(vpcConfig.EndpointPublicAccess),
"security_group_ids": schema.NewSet(schema.HashString, flattenStringList(vpcConfig.SecurityGroupIds)),
"subnet_ids": schema.NewSet(schema.HashString, flattenStringList(vpcConfig.SubnetIds)),
"public_access_cidrs": schema.NewSet(schema.HashString, flattenStringList(vpcConfig.PublicAccessCidrs)),
"vpc_id": aws.StringValue(vpcConfig.VpcId),
}

Expand Down
56 changes: 56 additions & 0 deletions aws/resource_aws_eks_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,42 @@ func TestAccAWSEksCluster_VpcConfig_EndpointPublicAccess(t *testing.T) {
})
}

func TestAccAWSEksCluster_VpcConfig_PublicAccessCidrs(t *testing.T) {
var cluster1 eks.Cluster

rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5))
resourceName := "aws_eks_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEks(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEksClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEksClusterConfig_VpcConfig_PublicAccessCidrs(rName, `["1.2.3.4/32", "5.6.7.8/32"]`),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEksClusterExists(resourceName, &cluster1),
resource.TestCheckResourceAttr(resourceName, "vpc_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "vpc_config.0.public_access_cidrs.#", "2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSEksClusterConfig_VpcConfig_PublicAccessCidrs(rName, `["4.3.2.1/32", "8.7.6.5/32"]`),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEksClusterExists(resourceName, &cluster1),
resource.TestCheckResourceAttr(resourceName, "vpc_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "vpc_config.0.public_access_cidrs.#", "2"),
),
},
},
})
}

func testAccCheckAWSEksClusterExists(resourceName string, cluster *eks.Cluster) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
Expand Down Expand Up @@ -667,3 +703,23 @@ resource "aws_eks_cluster" "test" {
}
`, testAccAWSEksClusterConfig_Base(rName), rName, endpointPublicAccess)
}

func testAccAWSEksClusterConfig_VpcConfig_PublicAccessCidrs(rName string, publicAccessCidr string) string {
return fmt.Sprintf(`
%[1]s
resource "aws_eks_cluster" "test" {
name = %[2]q
role_arn = "${aws_iam_role.test.arn}"
vpc_config {
endpoint_private_access = true
endpoint_public_access = true
public_access_cidrs = %s
subnet_ids = ["${aws_subnet.test.*.id[0]}", "${aws_subnet.test.*.id[1]}"]
}
depends_on = ["aws_iam_role_policy_attachment.test-AmazonEKSClusterPolicy", "aws_iam_role_policy_attachment.test-AmazonEKSServicePolicy"]
}
`, testAccAWSEksClusterConfig_Base(rName), rName, publicAccessCidr)
}
1 change: 1 addition & 0 deletions website/docs/d/eks_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ output "identity-oidc-issuer" {
* `cluster_security_group_id` - The cluster security group that was created by Amazon EKS for the cluster.
* `endpoint_private_access` - Indicates whether or not the Amazon EKS private API server endpoint is enabled.
* `endpoint_public_access` - Indicates whether or not the Amazon EKS public API server endpoint is enabled.
* `public_access_cidrs` - List of CIDR blocks. Indicates which CIDR blocks can access the Amazon EKS public API server endpoint.
* `security_group_ids` – List of security group IDs
* `subnet_ids` – List of subnet IDs
* `vpc_id` – The VPC associated with your cluster.
1 change: 1 addition & 0 deletions website/docs/r/eks_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ The following arguments are supported:

* `endpoint_private_access` - (Optional) Indicates whether or not the Amazon EKS private API server endpoint is enabled. Default is `false`.
* `endpoint_public_access` - (Optional) Indicates whether or not the Amazon EKS public API server endpoint is enabled. Default is `true`.
* `public_access_cidrs` - (Optional) List of CIDR blocks. Indicates which CIDR blocks can access the Amazon EKS public API server endpoint.
* `security_group_ids` – (Optional) List of security group IDs for the cross-account elastic network interfaces that Amazon EKS creates to use to allow communication between your worker nodes and the Kubernetes control plane.
* `subnet_ids` – (Required) List of subnet IDs. Must be in at least two different availability zones. Amazon EKS creates cross-account elastic network interfaces in these subnets to allow communication between your worker nodes and the Kubernetes control plane.

Expand Down

0 comments on commit 32384f8

Please sign in to comment.