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

feat: Add upgrade_policy config block for aws_eks_cluster #38573

Merged
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/38573.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_eks_cluster: Add `upgrade_policy` configuration block
```

```release-note:enhancement
data-source/aws_eks_cluster: Add `upgrade_policy` attribute
```
73 changes: 73 additions & 0 deletions internal/service/eks/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,22 @@ func resourceCluster() *schema.Resource {
},
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"upgrade_policy": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"support_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateDiagFunc: enum.Validate[types.SupportType](),
},
},
},
},
names.AttrVersion: {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -357,6 +373,10 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, meta int
input.OutpostConfig = expandOutpostConfigRequest(v.([]interface{}))
}

if v, ok := d.GetOk("upgrade_policy"); ok {
input.UpgradePolicy = expandUpgradePolicy(v.([]interface{}))
}

if v, ok := d.GetOk(names.AttrVersion); ok {
input.Version = aws.String(v.(string))
}
Expand Down Expand Up @@ -464,6 +484,9 @@ func resourceClusterRead(ctx context.Context, d *schema.ResourceData, meta inter
d.Set("platform_version", cluster.PlatformVersion)
d.Set(names.AttrRoleARN, cluster.RoleArn)
d.Set(names.AttrStatus, cluster.Status)
if err := d.Set("upgrade_policy", flattenUpgradePolicy(cluster.UpgradePolicy)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting upgrade_policy: %s", err)
}
d.Set(names.AttrVersion, cluster.Version)
if err := d.Set(names.AttrVPCConfig, flattenVPCConfigResponse(cluster.ResourcesVpcConfig)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting vpc_config: %s", err)
Expand Down Expand Up @@ -564,6 +587,25 @@ func resourceClusterUpdate(ctx context.Context, d *schema.ResourceData, meta int
}
}

if d.HasChange("upgrade_policy") {
input := &eks.UpdateClusterConfigInput{
Name: aws.String(d.Id()),
UpgradePolicy: expandUpgradePolicy(d.Get("upgrade_policy").([]interface{})),
}

output, err := conn.UpdateClusterConfig(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "updating EKS Cluster (%s) upgrade policy: %s", d.Id(), err)
}

updateID := aws.ToString(output.Update.Id)

if _, err := waitClusterUpdateSuccessful(ctx, conn, d.Id(), updateID, d.Timeout(schema.TimeoutUpdate)); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for EKS Cluster (%s) upgrade policy update (%s): %s", d.Id(), updateID, err)
}
}

if d.HasChanges("vpc_config.0.endpoint_private_access", "vpc_config.0.endpoint_public_access", "vpc_config.0.public_access_cidrs") {
config := &types.VpcConfigRequest{
EndpointPrivateAccess: aws.Bool(d.Get("vpc_config.0.endpoint_private_access").(bool)),
Expand Down Expand Up @@ -1022,6 +1064,25 @@ func expandLogging(vEnabledLogTypes *schema.Set) *types.Logging {
}
}

func expandUpgradePolicy(tfList []interface{}) *types.UpgradePolicyRequest {
if len(tfList) == 0 {
return nil
}

tfMap, ok := tfList[0].(map[string]interface{})
if !ok {
return nil
}

upgradePolicyRequest := &types.UpgradePolicyRequest{}

if v, ok := tfMap["support_type"].(string); ok && v != "" {
upgradePolicyRequest.SupportType = types.SupportType(v)
}

return upgradePolicyRequest
}

func flattenCertificate(certificate *types.Certificate) []map[string]interface{} {
if certificate == nil {
return []map[string]interface{}{}
Expand Down Expand Up @@ -1182,3 +1243,15 @@ func flattenControlPlanePlacementResponse(apiObject *types.ControlPlanePlacement

return []interface{}{tfMap}
}

func flattenUpgradePolicy(apiObject *types.UpgradePolicyResponse) []interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{
"support_type": apiObject.SupportType,
}

return []interface{}{tfMap}
}
15 changes: 15 additions & 0 deletions internal/service/eks/cluster_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ func dataSourceCluster() *schema.Resource {
Computed: true,
},
names.AttrTags: tftags.TagsSchemaComputed(),
"upgrade_policy": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"support_type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
names.AttrVersion: {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -249,6 +261,9 @@ func dataSourceClusterRead(ctx context.Context, d *schema.ResourceData, meta int
d.Set("platform_version", cluster.PlatformVersion)
d.Set(names.AttrRoleARN, cluster.RoleArn)
d.Set(names.AttrStatus, cluster.Status)
if err := d.Set("upgrade_policy", flattenUpgradePolicy(cluster.UpgradePolicy)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting upgrade_policy: %s", err)
}
d.Set(names.AttrVersion, cluster.Version)
if err := d.Set(names.AttrVPCConfig, flattenVPCConfigResponse(cluster.ResourcesVpcConfig)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting vpc_config: %s", err)
Expand Down
4 changes: 4 additions & 0 deletions internal/service/eks/cluster_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func TestAccEKSClusterDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, names.AttrRoleARN, dataSourceResourceName, names.AttrRoleARN),
resource.TestCheckResourceAttrPair(resourceName, names.AttrStatus, dataSourceResourceName, names.AttrStatus),
resource.TestCheckResourceAttrPair(resourceName, acctest.CtTagsPercent, dataSourceResourceName, acctest.CtTagsPercent),
resource.TestCheckResourceAttr(resourceName, "upgrade_policy.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "upgrade_policy.0.support_type", "EXTENDED"),
resource.TestCheckResourceAttrPair(resourceName, names.AttrVersion, dataSourceResourceName, names.AttrVersion),
resource.TestCheckResourceAttr(dataSourceResourceName, "vpc_config.#", acctest.Ct1),
resource.TestCheckResourceAttrPair(resourceName, "vpc_config.0.cluster_security_group_id", dataSourceResourceName, "vpc_config.0.cluster_security_group_id"),
Expand Down Expand Up @@ -95,6 +97,8 @@ func TestAccEKSClusterDataSource_outpost(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, names.AttrRoleARN, dataSourceResourceName, names.AttrRoleARN),
resource.TestCheckResourceAttrPair(resourceName, names.AttrStatus, dataSourceResourceName, names.AttrStatus),
resource.TestCheckResourceAttrPair(resourceName, acctest.CtTagsPercent, dataSourceResourceName, acctest.CtTagsPercent),
resource.TestCheckResourceAttr(resourceName, "upgrade_policy.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "upgrade_policy.0.support_type", "EXTENDED"),
resource.TestCheckResourceAttrPair(resourceName, names.AttrVersion, dataSourceResourceName, names.AttrVersion),
resource.TestCheckResourceAttr(dataSourceResourceName, "vpc_config.#", acctest.Ct1),
resource.TestCheckResourceAttrPair(resourceName, "vpc_config.0.cluster_security_group_id", dataSourceResourceName, "vpc_config.0.cluster_security_group_id"),
Expand Down
65 changes: 65 additions & 0 deletions internal/service/eks/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func TestAccEKSCluster_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, names.AttrStatus, string(types.ClusterStatusActive)),
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct0),
resource.TestMatchResourceAttr(resourceName, names.AttrVersion, regexache.MustCompile(`^\d+\.\d+$`)),
resource.TestCheckResourceAttr(resourceName, "upgrade_policy.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "upgrade_policy.0.support_type", "EXTENDED"),
resource.TestCheckResourceAttr(resourceName, "vpc_config.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "vpc_config.0.endpoint_private_access", acctest.CtFalse),
resource.TestCheckResourceAttr(resourceName, "vpc_config.0.endpoint_public_access", acctest.CtTrue),
Expand Down Expand Up @@ -914,6 +916,50 @@ func TestAccEKSCluster_Outpost_placement(t *testing.T) {
})
}

func TestAccEKSCluster_upgradePolicy(t *testing.T) {
ctx := acctest.Context(t)
var cluster types.Cluster
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_eks_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.EKSServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckClusterDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccClusterConfig_upgradePolicy(rName, "STANDARD"),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &cluster),
resource.TestCheckResourceAttr(resourceName, "upgrade_policy.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "upgrade_policy.0.support_type", "STANDARD"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"bootstrap_self_managed_addons"},
},
{
Config: testAccClusterConfig_upgradePolicy(rName, "EXTENDED"),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &cluster),
resource.TestCheckResourceAttr(resourceName, "upgrade_policy.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "upgrade_policy.0.support_type", "EXTENDED"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"bootstrap_self_managed_addons"},
},
},
})
}

func testAccCheckClusterExists(ctx context.Context, n string, v *types.Cluster) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -1474,3 +1520,22 @@ resource "aws_eks_cluster" "test" {
}
`, rName))
}

func testAccClusterConfig_upgradePolicy(rName, supportType 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
}

upgrade_policy {
support_type = %[2]q
}

depends_on = [aws_iam_role_policy_attachment.test-AmazonEKSClusterPolicy]
}
`, rName, supportType))
}
2 changes: 2 additions & 0 deletions website/docs/d/eks_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ This data source exports the following attributes in addition to the arguments a
* `role_arn` - ARN of the IAM role that provides permissions for the Kubernetes control plane to make calls to AWS API operations on your behalf.
* `status` - Status of the EKS cluster. One of `CREATING`, `ACTIVE`, `DELETING`, `FAILED`.
* `tags` - Key-value map of resource tags.
* `upgrade_policy` - (Optional) Configuration block for the support policy to use for the cluster.
* `support_type` - (Optional) Support type to use for the cluster.
* `version` - Kubernetes server version for the cluster.
* `vpc_config` - Nested list containing VPC configuration for the cluster.
* `cluster_security_group_id` - The cluster security group that was created by Amazon EKS for the cluster.
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/eks_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ The following arguments are optional:
* `kubernetes_network_config` - (Optional) Configuration block with kubernetes network configuration for the cluster. Detailed below. If removed, Terraform will only perform drift detection if a configuration value is provided.
* `outpost_config` - (Optional) Configuration block representing the configuration of your local Amazon EKS cluster on an AWS Outpost. This block isn't available for creating Amazon EKS clusters on the AWS cloud.
* `tags` - (Optional) Key-value map of resource tags. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.
* `upgrade_policy` - (Optional) Configuration block for the support policy to use for the cluster. See [upgrade_policy](#upgrade_policy) for details.
* `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.

### access_config
Expand Down Expand Up @@ -285,6 +286,12 @@ The `control_plane_placement` configuration block supports the following argumen

* `outpost_arns` - (Required) The ARN of the Outpost that you want to use for your local Amazon EKS cluster on Outposts. This argument is a list of arns, but only a single Outpost ARN is supported currently.

### upgrade_policy

The `upgrade_policy` configuration block supports the following arguments:

* `support_type` - (Optional) Support type to use for the cluster. If the cluster is set to `EXTENDED`, it will enter extended support at the end of standard support. If the cluster is set to `STANDARD`, it will be automatically upgraded at the end of standard support. Valid values are `EXTENDED`, `STANDARD`

## Attribute Reference

This resource exports the following attributes in addition to the arguments above:
Expand Down
Loading