Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_eks_cluster: Add ip_family argument #22485

Merged
merged 5 commits into from
Jan 10, 2022
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
7 changes: 7 additions & 0 deletions .changelog/22485.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_eks_cluster: Add `ip_family` to the `kubernetes_network_config` configuration block
```

```release-note:enhancement
data-source/aws_eks_cluster: Add `ip_family` to the `kubernetes_network_config` configuration block
```
12 changes: 12 additions & 0 deletions internal/service/eks/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ func ResourceCluster() *schema.Resource {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip_family": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(eks.IpFamily_Values(), false),
},
"service_ipv4_cidr": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -615,6 +622,10 @@ func expandEksNetworkConfigRequest(tfList []interface{}) *eks.KubernetesNetworkC
apiObject.ServiceIpv4Cidr = aws.String(v)
}

if v, ok := tfMap["ip_family"].(string); ok && v != "" {
apiObject.IpFamily = aws.String(v)
}

return apiObject
}

Expand Down Expand Up @@ -748,6 +759,7 @@ func flattenEksNetworkConfig(apiObject *eks.KubernetesNetworkConfigResponse) []i

tfMap := map[string]interface{}{
"service_ipv4_cidr": aws.StringValue(apiObject.ServiceIpv4Cidr),
"ip_family": aws.StringValue(apiObject.IpFamily),
}

return []interface{}{tfMap}
Expand Down
4 changes: 4 additions & 0 deletions internal/service/eks/cluster_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func DataSourceCluster() *schema.Resource {
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip_family": {
Type: schema.TypeString,
Computed: true,
},
"service_ipv4_cidr": {
Type: schema.TypeString,
Computed: true,
Expand Down
1 change: 1 addition & 0 deletions internal/service/eks/cluster_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestAccEKSClusterDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, "identity.0.oidc.#", dataSourceResourceName, "identity.0.oidc.#"),
resource.TestCheckResourceAttrPair(resourceName, "identity.0.oidc.0.issuer", dataSourceResourceName, "identity.0.oidc.0.issuer"),
resource.TestCheckResourceAttrPair(resourceName, "kubernetes_network_config.#", dataSourceResourceName, "kubernetes_network_config.#"),
resource.TestCheckResourceAttrPair(resourceName, "kubernetes_network_config.0.ip_family", dataSourceResourceName, "kubernetes_network_config.0.ip_family"),
resource.TestCheckResourceAttrPair(resourceName, "kubernetes_network_config.0.service_ipv4_cidr", dataSourceResourceName, "kubernetes_network_config.0.service_ipv4_cidr"),
resource.TestMatchResourceAttr(dataSourceResourceName, "platform_version", regexp.MustCompile(`^eks\.\d+$`)),
resource.TestCheckResourceAttrPair(resourceName, "role_arn", dataSourceResourceName, "role_arn"),
Expand Down
75 changes: 75 additions & 0 deletions internal/service/eks/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestAccEKSCluster_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "kubernetes_network_config.#", "1"),
resource.TestCheckResourceAttrSet(resourceName, "kubernetes_network_config.0.service_ipv4_cidr"),
resource.TestCheckResourceAttr(resourceName, "kubernetes_network_config.0.ip_family", "ipv4"),
resource.TestMatchResourceAttr(resourceName, "platform_version", regexp.MustCompile(`^eks\.\d+$`)),
resource.TestCheckResourceAttrPair(resourceName, "role_arn", "aws_iam_role.test", "arn"),
resource.TestCheckResourceAttr(resourceName, "status", eks.ClusterStatusActive),
Expand Down Expand Up @@ -548,6 +549,56 @@ func TestAccEKSCluster_Network_serviceIPv4CIDR(t *testing.T) {
})
}

func TestAccEKSCluster_Network_ipFamily(t *testing.T) {
var cluster1, cluster2 eks.Cluster
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_eks_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, eks.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccClusterConfig_NetworkConfig_IPFamily(rName, `"v6"`),
ExpectError: regexp.MustCompile(`expected .* to be one of \[ipv4 ipv6]`),
},
{
Config: testAccClusterConfig_NetworkConfig_IPFamily(rName, `"IPv4"`),
ExpectError: regexp.MustCompile(`expected .* to be one of \[ipv4 ipv6]`),
},
{
Config: testAccClusterConfig_NetworkConfig_IPFamily(rName, `"ipv6"`),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(resourceName, &cluster1),
resource.TestCheckResourceAttr(resourceName, "kubernetes_network_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "kubernetes_network_config.0.ip_family", "ipv6"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccClusterConfig_NetworkConfig_IPFamily(rName, `"ipv6"`),
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
{
Config: testAccClusterConfig_NetworkConfig_IPFamily(rName, `"ipv4"`),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(resourceName, &cluster2),
testAccCheckClusterRecreated(&cluster1, &cluster2),
resource.TestCheckResourceAttr(resourceName, "kubernetes_network_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "kubernetes_network_config.0.ip_family", "ipv4"),
),
},
},
})
}

func testAccCheckClusterExists(resourceName string, cluster *eks.Cluster) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
Expand Down Expand Up @@ -673,6 +724,8 @@ resource "aws_iam_role_policy_attachment" "test-AmazonEKSClusterPolicy" {
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"

assign_generated_ipv6_cidr_block = true

tags = {
Name = %[1]q
"kubernetes.io/cluster/%[1]s" = "shared"
Expand All @@ -686,6 +739,9 @@ resource "aws_subnet" "test" {
cidr_block = "10.0.${count.index}.0/24"
vpc_id = aws_vpc.test.id

ipv6_cidr_block = cidrsubnet(aws_vpc.test.ipv6_cidr_block, 8, count.index)
assign_ipv6_address_on_creation = true

tags = {
Name = %[1]q
"kubernetes.io/cluster/%[1]s" = "shared"
Expand Down Expand Up @@ -931,3 +987,22 @@ resource "aws_eks_cluster" "test" {
}
`, rName, serviceIpv4Cidr))
}

func testAccClusterConfig_NetworkConfig_IPFamily(rName string, ipFamily string) string {
return acctest.ConfigCompose(testAccClusterConfig_Base(rName), fmt.Sprintf(`
resource "aws_eks_cluster" "test" {
name = %[1]q
role_arn = aws_iam_role.test.arn

vpc_config {
subnet_ids = aws_subnet.test[*].id
}

kubernetes_network_config {
ip_family = %[2]s
}

depends_on = [aws_iam_role_policy_attachment.test-AmazonEKSClusterPolicy]
}
`, rName, ipFamily))
}
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 @@ -201,6 +201,7 @@ The following arguments are supported in the `kubernetes_network_config` configu
* Doesn't overlap with any CIDR block assigned to the VPC that you selected for VPC.

* Between /24 and /12.
* `ip_family` - (Optional) The IP family used to assign Kubernetes pod and service addresses. Valid values are `ipv4` (default) and `ipv6`. You can only specify an IP family when you create a cluster, changing this value will force a new cluster to be created.

## Attributes Reference

Expand Down