|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: corymhall < [email protected]> |
| 3 | +Date: Fri, 1 Aug 2025 14:29:38 -0400 |
| 4 | +Subject: [PATCH] aws_eks_cluster: implement default_addons_to_remove |
| 5 | + |
| 6 | +add back this feature and set it to deprecated. Users should instead |
| 7 | +set `bootstrapSelfManagedAddons: false` and selectively add the addons |
| 8 | +they want with the `aws.eks.Addon` resource. |
| 9 | + |
| 10 | +diff --git a/internal/service/eks/cluster.go b/internal/service/eks/cluster.go |
| 11 | +index 8a7d28be91..a62c86208f 100644 |
| 12 | +--- a/internal/service/eks/cluster.go |
| 13 | ++++ b/internal/service/eks/cluster.go |
| 14 | +@@ -168,6 +168,14 @@ func resourceCluster() *schema.Resource { |
| 15 | + Type: schema.TypeString, |
| 16 | + Computed: true, |
| 17 | + }, |
| 18 | ++ "default_addons_to_remove": { |
| 19 | ++ Type: schema.TypeList, |
| 20 | ++ Deprecated: "Configure bootstrap_self_managed_addons instead. This attribute will be removed in the next major version of the provider", |
| 21 | ++ Optional: true, |
| 22 | ++ Elem: &schema.Schema{ |
| 23 | ++ Type: schema.TypeString, |
| 24 | ++ }, |
| 25 | ++ }, |
| 26 | + "enabled_cluster_log_types": { |
| 27 | + Type: schema.TypeSet, |
| 28 | + Optional: true, |
| 29 | +@@ -600,6 +608,10 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, meta any |
| 30 | + return sdkdiag.AppendErrorf(diags, "waiting for EKS Cluster (%s) create: %s", d.Id(), err) |
| 31 | + } |
| 32 | + |
| 33 | ++ if err := removeAddons(d, conn); err != nil { |
| 34 | ++ return sdkdiag.AppendErrorf(diags, "removing addons (%s): %s", d.Id(), err) |
| 35 | ++ } |
| 36 | ++ |
| 37 | + return append(diags, resourceClusterRead(ctx, d, meta)...) |
| 38 | + } |
| 39 | + |
| 40 | +diff --git a/internal/service/eks/cluster_addon_removal.go b/internal/service/eks/cluster_addon_removal.go |
| 41 | +new file mode 100644 |
| 42 | +index 0000000000..bd53bad5c1 |
| 43 | +--- /dev/null |
| 44 | ++++ b/internal/service/eks/cluster_addon_removal.go |
| 45 | +@@ -0,0 +1,139 @@ |
| 46 | ++package eks |
| 47 | ++ |
| 48 | ++import ( |
| 49 | ++ "context" |
| 50 | ++ "fmt" |
| 51 | ++ "log" |
| 52 | ++ "sync" |
| 53 | ++ "time" |
| 54 | ++ |
| 55 | ++ "github.com/aws/aws-sdk-go-v2/aws" |
| 56 | ++ "github.com/aws/aws-sdk-go-v2/service/eks" |
| 57 | ++ "github.com/aws/aws-sdk-go-v2/service/eks/types" |
| 58 | ++ |
| 59 | ++ "github.com/hashicorp/go-multierror" |
| 60 | ++ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" |
| 61 | ++ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" |
| 62 | ++ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 63 | ++ "github.com/hashicorp/terraform-provider-aws/internal/errs" |
| 64 | ++ |
| 65 | ++ "github.com/hashicorp/terraform-provider-aws/internal/flex" |
| 66 | ++ "github.com/hashicorp/terraform-provider-aws/internal/tfresource" |
| 67 | ++) |
| 68 | ++ |
| 69 | ++const ( |
| 70 | ++ addonCreatedTimeout = 20 * time.Minute |
| 71 | ++ addonUpdatedTimeout = 20 * time.Minute |
| 72 | ++ addonDeletedTimeout = 40 * time.Minute |
| 73 | ++) |
| 74 | ++ |
| 75 | ++func removeAddons(d *schema.ResourceData, conn *eks.Client) error { |
| 76 | ++ if v, ok := d.GetOk("default_addons_to_remove"); ok && len(v.([]interface{})) > 0 { |
| 77 | ++ ctx := context.Background() |
| 78 | ++ var wg sync.WaitGroup |
| 79 | ++ var removalErrors *multierror.Error |
| 80 | ++ |
| 81 | ++ for _, addon := range flex.ExpandStringList(v.([]interface{})) { |
| 82 | ++ if addon == nil { |
| 83 | ++ return fmt.Errorf("addonName cannot be dereferenced") |
| 84 | ++ } |
| 85 | ++ addonName := *addon |
| 86 | ++ wg.Add(1) |
| 87 | ++ |
| 88 | ++ go func() { |
| 89 | ++ defer wg.Done() |
| 90 | ++ removalErrors = multierror.Append(removalErrors, removeAddon(d, conn, addonName, ctx)) |
| 91 | ++ }() |
| 92 | ++ } |
| 93 | ++ wg.Wait() |
| 94 | ++ return removalErrors.ErrorOrNil() |
| 95 | ++ } |
| 96 | ++ return nil |
| 97 | ++} |
| 98 | ++ |
| 99 | ++func removeAddon(d *schema.ResourceData, conn *eks.Client, addonName string, ctx context.Context) error { |
| 100 | ++ log.Printf("[DEBUG] Creating EKS Add-On: %s", addonName) |
| 101 | ++ createAddonInput := &eks.CreateAddonInput{ |
| 102 | ++ AddonName: aws.String(addonName), |
| 103 | ++ ClientRequestToken: aws.String(id.UniqueId()), |
| 104 | ++ ClusterName: aws.String(d.Id()), |
| 105 | ++ ResolveConflicts: types.ResolveConflictsOverwrite, |
| 106 | ++ } |
| 107 | ++ |
| 108 | ++ err := retry.RetryContext(ctx, propagationTimeout, func() *retry.RetryError { |
| 109 | ++ _, err := conn.CreateAddon(ctx, createAddonInput) |
| 110 | ++ |
| 111 | ++ if errs.IsAErrorMessageContains[*types.InvalidParameterException](err, "CREATE_FAILED") { |
| 112 | ++ return retry.RetryableError(err) |
| 113 | ++ } |
| 114 | ++ |
| 115 | ++ if errs.IsAErrorMessageContains[*types.InvalidParameterException](err, "does not exist") { |
| 116 | ++ return retry.RetryableError(err) |
| 117 | ++ } |
| 118 | ++ |
| 119 | ++ if err != nil { |
| 120 | ++ return retry.NonRetryableError(err) |
| 121 | ++ } |
| 122 | ++ |
| 123 | ++ return nil |
| 124 | ++ }) |
| 125 | ++ |
| 126 | ++ if tfresource.TimedOut(err) { |
| 127 | ++ _, err = conn.CreateAddon(ctx, createAddonInput) |
| 128 | ++ } |
| 129 | ++ |
| 130 | ++ if err != nil { |
| 131 | ++ return fmt.Errorf("error creating EKS Add-On (%s): %w", addonName, err) |
| 132 | ++ } |
| 133 | ++ |
| 134 | ++ _, err = waitAddonCreatedAllowDegraded(ctx, conn, d.Id(), addonName) |
| 135 | ++ |
| 136 | ++ if err != nil { |
| 137 | ++ return fmt.Errorf("unexpected EKS Add-On (%s) state returned during creation: %w", addonName, err) |
| 138 | ++ } |
| 139 | ++ log.Printf("[DEBUG] Created EKS Add-On: %s", addonName) |
| 140 | ++ |
| 141 | ++ deleteAddonInput := &eks.DeleteAddonInput{ |
| 142 | ++ AddonName: aws.String(addonName), |
| 143 | ++ ClusterName: aws.String(d.Id()), |
| 144 | ++ } |
| 145 | ++ |
| 146 | ++ log.Printf("[DEBUG] Deleting EKS Add-On: %s", addonName) |
| 147 | ++ _, err = conn.DeleteAddon(ctx, deleteAddonInput) |
| 148 | ++ |
| 149 | ++ if err != nil { |
| 150 | ++ return fmt.Errorf("error deleting EKS Add-On (%s): %w", addonName, err) |
| 151 | ++ } |
| 152 | ++ |
| 153 | ++ _, err = waitAddonDeleted(ctx, conn, d.Id(), addonName, addonDeletedTimeout) |
| 154 | ++ |
| 155 | ++ if err != nil { |
| 156 | ++ return fmt.Errorf("error waiting for EKS Add-On (%s) to delete: %w", addonName, err) |
| 157 | ++ } |
| 158 | ++ log.Printf("[DEBUG] Deleted EKS Add-On: %s", addonName) |
| 159 | ++ return nil |
| 160 | ++} |
| 161 | ++ |
| 162 | ++func waitAddonCreatedAllowDegraded(ctx context.Context, conn *eks.Client, clusterName, addonName string) (*types.Addon, error) { |
| 163 | ++ // We do not care about the addons actually being created successfully here. We only want them to be adopted by |
| 164 | ++ // Terraform to be able to fully remove them afterwards again. |
| 165 | ++ |
| 166 | ++ stateConf := retry.StateChangeConf{ |
| 167 | ++ Pending: []string{string(types.AddonStatusCreating)}, |
| 168 | ++ Target: []string{string(types.AddonStatusActive), string(types.AddonStatusDegraded)}, |
| 169 | ++ Refresh: statusAddon(ctx, conn, clusterName, addonName), |
| 170 | ++ Timeout: addonCreatedTimeout, |
| 171 | ++ } |
| 172 | ++ |
| 173 | ++ outputRaw, err := stateConf.WaitForStateContext(ctx) |
| 174 | ++ |
| 175 | ++ if output, ok := outputRaw.(*types.Addon); ok { |
| 176 | ++ if status, health := output.Status, output.Health; status == types.AddonStatusCreateFailed && health != nil { |
| 177 | ++ tfresource.SetLastError(err, addonIssuesError(health.Issues)) |
| 178 | ++ } |
| 179 | ++ |
| 180 | ++ return output, err |
| 181 | ++ } |
| 182 | ++ |
| 183 | ++ return nil, err |
| 184 | ++} |
0 commit comments