Skip to content

Commit

Permalink
Merge pull request #10307 from terraform-providers/f-eks-tagging
Browse files Browse the repository at this point in the history
service/eks: Implement tagging support
  • Loading branch information
bflad authored Oct 3, 2019
2 parents ce20280 + 052093c commit 85be253
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 0 deletions.
7 changes: 7 additions & 0 deletions aws/data_source_aws_eks_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go/service/eks"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsEksCluster() *schema.Resource {
Expand Down Expand Up @@ -84,6 +85,7 @@ func dataSourceAwsEksCluster() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
"vpc_config": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -163,6 +165,11 @@ func dataSourceAwsEksClusterRead(d *schema.ResourceData, meta interface{}) error
d.Set("platform_version", cluster.PlatformVersion)
d.Set("role_arn", cluster.RoleArn)
d.Set("status", cluster.Status)

if err := d.Set("tags", keyvaluetags.EksKeyValueTags(cluster.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

d.Set("version", cluster.Version)

if err := d.Set("vpc_config", flattenEksVpcConfigResponse(cluster.ResourcesVpcConfig)); err != nil {
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 @@ -36,6 +36,7 @@ func TestAccAWSEksClusterDataSource_basic(t *testing.T) {
resource.TestMatchResourceAttr(dataSourceResourceName, "platform_version", regexp.MustCompile(`^eks\.\d+$`)),
resource.TestCheckResourceAttrPair(resourceName, "role_arn", dataSourceResourceName, "role_arn"),
resource.TestCheckResourceAttrPair(resourceName, "status", dataSourceResourceName, "status"),
resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceResourceName, "tags.%"),
resource.TestCheckResourceAttrPair(resourceName, "version", dataSourceResourceName, "version"),
resource.TestCheckResourceAttr(dataSourceResourceName, "vpc_config.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "vpc_config.0.endpoint_private_access", dataSourceResourceName, "vpc_config.0.endpoint_private_access"),
Expand Down
1 change: 1 addition & 0 deletions aws/internal/keyvaluetags/generators/listtags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var serviceNames = []string{
"ecr",
"ecs",
"efs",
"eks",
"elasticache",
"elasticbeanstalk",
"elasticsearchservice",
Expand Down
1 change: 1 addition & 0 deletions aws/internal/keyvaluetags/generators/servicetags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ var mapServiceNames = []string{
"codecommit",
"cognitoidentity",
"cognitoidentityprovider",
"eks",
"glacier",
"glue",
"guardduty",
Expand Down
1 change: 1 addition & 0 deletions aws/internal/keyvaluetags/generators/updatetags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var serviceNames = []string{
"ecr",
"ecs",
"efs",
"eks",
"elasticache",
"elasticsearchservice",
"emr",
Expand Down
18 changes: 18 additions & 0 deletions aws/internal/keyvaluetags/list_tags_gen.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 @@ -37,6 +37,7 @@ import (
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/efs"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
"github.com/aws/aws-sdk-go/service/elasticsearchservice"
Expand Down Expand Up @@ -152,6 +153,8 @@ func ServiceClientType(serviceName string) string {
funcType = reflect.TypeOf(ecs.New)
case "efs":
funcType = reflect.TypeOf(efs.New)
case "eks":
funcType = reflect.TypeOf(eks.New)
case "elasticache":
funcType = reflect.TypeOf(elasticache.New)
case "elasticbeanstalk":
Expand Down
10 changes: 10 additions & 0 deletions aws/internal/keyvaluetags/service_tags_gen.go

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

37 changes: 37 additions & 0 deletions aws/internal/keyvaluetags/update_tags_gen.go

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

18 changes: 18 additions & 0 deletions aws/resource_aws_eks_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

var eksLogTypes = []string{
Expand Down Expand Up @@ -104,6 +105,7 @@ func resourceAwsEksCluster() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
"version": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -170,6 +172,10 @@ func resourceAwsEksClusterCreate(d *schema.ResourceData, meta interface{}) error
Logging: expandEksLoggingTypes(d.Get("enabled_cluster_log_types").(*schema.Set)),
}

if v := d.Get("tags").(map[string]interface{}); len(v) > 0 {
input.Tags = keyvaluetags.New(v).IgnoreAws().EksTags()
}

if v, ok := d.GetOk("version"); ok && v.(string) != "" {
input.Version = aws.String(v.(string))
}
Expand Down Expand Up @@ -262,6 +268,11 @@ func resourceAwsEksClusterRead(d *schema.ResourceData, meta interface{}) error {
d.Set("platform_version", cluster.PlatformVersion)
d.Set("role_arn", cluster.RoleArn)
d.Set("status", cluster.Status)

if err := d.Set("tags", keyvaluetags.EksKeyValueTags(cluster.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

d.Set("version", cluster.Version)
if err := d.Set("enabled_cluster_log_types", flattenEksEnabledLogTypes(cluster.Logging)); err != nil {
return fmt.Errorf("error setting enabled_cluster_log_types: %s", err)
Expand All @@ -277,6 +288,13 @@ func resourceAwsEksClusterRead(d *schema.ResourceData, meta interface{}) error {
func resourceAwsEksClusterUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).eksconn

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if err := keyvaluetags.EksUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}

if d.HasChange("version") {
input := &eks.UpdateClusterVersionInput{
Name: aws.String(d.Id()),
Expand Down
84 changes: 84 additions & 0 deletions aws/resource_aws_eks_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func TestAccAWSEksCluster_basic(t *testing.T) {
resource.TestMatchResourceAttr(resourceName, "platform_version", regexp.MustCompile(`^eks\.\d+$`)),
resource.TestMatchResourceAttr(resourceName, "role_arn", regexp.MustCompile(fmt.Sprintf("%s$", rName))),
resource.TestCheckResourceAttr(resourceName, "status", eks.ClusterStatusActive),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestMatchResourceAttr(resourceName, "version", regexp.MustCompile(`^\d+\.\d+$`)),
resource.TestCheckResourceAttr(resourceName, "vpc_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "vpc_config.0.endpoint_private_access", "false"),
Expand Down Expand Up @@ -197,6 +198,50 @@ func TestAccAWSEksCluster_Logging(t *testing.T) {
})
}

func TestAccAWSEksCluster_Tags(t *testing.T) {
var cluster1, cluster2, cluster3 eks.Cluster
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_eks_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEks(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEksClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEksClusterConfigTags1(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEksClusterExists(resourceName, &cluster1),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSEksClusterConfigTags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEksClusterExists(resourceName, &cluster2),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccAWSEksClusterConfigTags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEksClusterExists(resourceName, &cluster3),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
})
}

func TestAccAWSEksCluster_VpcConfig_SecurityGroupIds(t *testing.T) {
var cluster eks.Cluster

Expand Down Expand Up @@ -520,6 +565,45 @@ resource "aws_eks_cluster" "test" {
`, testAccAWSEksClusterConfig_Base(rName), rName, strings.Join(logTypes, "\", \""))
}

func testAccAWSEksClusterConfigTags1(rName, tagKey1, tagValue1 string) string {
return testAccAWSEksClusterConfig_Base(rName) + fmt.Sprintf(`
resource "aws_eks_cluster" "test" {
name = %[1]q
role_arn = "${aws_iam_role.test.arn}"
tags = {
%[2]q = %[3]q
}
vpc_config {
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"]
}
`, rName, tagKey1, tagValue1)
}

func testAccAWSEksClusterConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return testAccAWSEksClusterConfig_Base(rName) + fmt.Sprintf(`
resource "aws_eks_cluster" "test" {
name = %[1]q
role_arn = "${aws_iam_role.test.arn}"
tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
vpc_config {
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"]
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}

func testAccAWSEksClusterConfig_VpcConfig_SecurityGroupIds(rName string) string {
return fmt.Sprintf(`
%s
Expand Down
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 @@ -49,6 +49,7 @@ output "identity-oidc-issuer" {
* `platform_version` - The platform version for the cluster.
* `role_arn` - The Amazon Resource Name (ARN) of the IAM role that provides permissions for the Kubernetes control plane to make calls to AWS API operations on your behalf.
* `status` - The status of the EKS cluster. One of `CREATING`, `ACTIVE`, `DELETING`, `FAILED`.
* `tags` - Key-value mapping of resource tags.
* `version` - The Kubernetes server version for the cluster.
* `vpc_config` - Nested attribute containing VPC configuration for the cluster.
* `endpoint_private_access` - Indicates whether or not the Amazon EKS private API server endpoint is enabled.
Expand Down
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 @@ -112,6 +112,7 @@ The following arguments are supported:
* `role_arn` - (Required) The Amazon Resource Name (ARN) of the IAM role that provides permissions for the Kubernetes control plane to make calls to AWS API operations on your behalf.
* `vpc_config` - (Required) Nested argument for the VPC associated with your cluster. Amazon EKS VPC resources have specific requirements to work properly with Kubernetes. For more information, see [Cluster VPC Considerations](https://docs.aws.amazon.com/eks/latest/userguide/network_reqs.html) and [Cluster Security Group Considerations](https://docs.aws.amazon.com/eks/latest/userguide/sec-group-reqs.html) in the Amazon EKS User Guide. Configuration detailed below.
* `enabled_cluster_log_types` - (Optional) A list of the desired control plane logging to enable. For more information, see [Amazon EKS Control Plane Logging](https://docs.aws.amazon.com/eks/latest/userguide/control-plane-logs.html)
* `tags` - (Optional) Key-value mapping of resource tags.
* `version` – (Optional) Desired Kubernetes master version. If you do not specify a value, the latest available version at resource creation is used and no upgrades will occur except those automatically triggered by EKS. The value must be configured and increased to upgrade the version when desired. Downgrades are not supported by EKS.

### vpc_config
Expand Down

0 comments on commit 85be253

Please sign in to comment.