diff --git a/docs/data-sources/hcp_machine_pool.md b/docs/data-sources/hcp_machine_pool.md index 9ba4274c..d50d355b 100644 --- a/docs/data-sources/hcp_machine_pool.md +++ b/docs/data-sources/hcp_machine_pool.md @@ -39,6 +39,7 @@ data "rhcs_hcp_machine_pool" "machine_pool" { - `aws_node_pool` (Attributes) AWS settings for node pool (see [below for nested schema](#nestedatt--aws_node_pool)) - `current_version` (String) The currently running version of OpenShift on the machine pool, for example '4.11.0'. - `id` (String) Unique identifier of the machine pool. +- `ignore_deletion_error` (Boolean) Indicates to the provider to disregard API errors when deleting the machine pool. This will remove the resource from the management file, but not necessirely delete the underlying pool in case it errors. Setting this to true can bypass issues when destroying the cluster resource alongside the pool resource in the same management file. This is not recommended to be set in other use cases - `kubelet_configs` (String) Name of the kubelet config applied to the machine pool. - `labels` (Map of String) Labels for the machine pool. Format should be a comma-separated list of 'key = value'. This list will overwrite any modifications made to node labels on an ongoing basis. - `replicas` (Number) The number of machines of the pool diff --git a/docs/data-sources/machine_pool.md b/docs/data-sources/machine_pool.md index 954a5cbf..60c897a5 100644 --- a/docs/data-sources/machine_pool.md +++ b/docs/data-sources/machine_pool.md @@ -34,6 +34,7 @@ data "rhcs_machine_pool" "machine_pool" { - `availability_zones` (List of String) A list of Availability Zones. Relevant only for multiple availability zones machine pool. For single availability zone check "availability_zone" attribute. - `aws_additional_security_group_ids` (List of String) AWS additional security group ids. - `disk_size` (Number) The root disk size, in GiB. +- `ignore_deletion_error` (Boolean) Indicates to the provider to disregard API errors when deleting the machine pool. This will remove the resource from the management file, but not necessirely delete the underlying pool in case it errors. Setting this to true can bypass issues when destroying the cluster resource alongside the pool resource in the same management file. This is not recommended to be set in other use cases - `labels` (Map of String) The list of the Labels of this machine pool. - `machine_type` (String) Identifier of the machine type used by the nodes, for example `m5.xlarge`. - `max_replicas` (Number) The maximum number of replicas for auto-scaling functionality. relevant only in case of 'autoscaling_enabled = true' diff --git a/docs/resources/hcp_machine_pool.md b/docs/resources/hcp_machine_pool.md index c78b46d7..73e72eb5 100644 --- a/docs/resources/hcp_machine_pool.md +++ b/docs/resources/hcp_machine_pool.md @@ -42,6 +42,7 @@ resource "rhcs_hcp_machine_pool" "machine_pool" { ### Optional +- `ignore_deletion_error` (Boolean) Indicates to the provider to disregard API errors when deleting the machine pool. This will remove the resource from the management file, but not necessirely delete the underlying pool in case it errors. Setting this to true can bypass issues when destroying the cluster resource alongside the pool resource in the same management file. This is not recommended to be set in other use cases - `kubelet_configs` (String) Name of the kubelet config applied to the machine pool. A single kubelet config is allowed. Kubelet config must already exist. - `labels` (Map of String) Labels for the machine pool. Format should be a comma-separated list of 'key = value'. This list will overwrite any modifications made to node labels on an ongoing basis. - `replicas` (Number) The number of machines of the pool diff --git a/docs/resources/machine_pool.md b/docs/resources/machine_pool.md index 7e731167..329477c7 100644 --- a/docs/resources/machine_pool.md +++ b/docs/resources/machine_pool.md @@ -38,6 +38,7 @@ resource "rhcs_machine_pool" "machine_pool" { - `aws_additional_security_group_ids` (List of String) AWS additional security group ids. After the creation of the resource, it is not possible to update the attribute value. - `aws_tags` (Map of String) Apply user defined tags to all machine pool resources created in AWS. After the creation of the resource, it is not possible to update the attribute value. - `disk_size` (Number) Root disk size, in GiB. After the creation of the resource, it is not possible to update the attribute value. +- `ignore_deletion_error` (Boolean) Indicates to the provider to disregard API errors when deleting the machine pool. This will remove the resource from the management file, but not necessirely delete the underlying pool in case it errors. Setting this to true can bypass issues when destroying the cluster resource alongside the pool resource in the same management file. This is not recommended to be set in other use cases - `labels` (Map of String) Labels for the machine pool. Format should be a comma-separated list of 'key = value'. This list will overwrite any modifications made to node labels on an ongoing basis. - `max_replicas` (Number) The maximum number of replicas for autoscaling functionality. - `max_spot_price` (Number) Max Spot price. After the creation of the resource, it is not possible to update the attribute value. diff --git a/provider/machinepool/classic/machine_pool_datasource.go b/provider/machinepool/classic/machine_pool_datasource.go index 45a03a0c..4c002eaa 100644 --- a/provider/machinepool/classic/machine_pool_datasource.go +++ b/provider/machinepool/classic/machine_pool_datasource.go @@ -165,6 +165,13 @@ func (r *MachinePoolDatasource) Schema(ctx context.Context, req datasource.Schem ElementType: types.StringType, Computed: true, }, + "ignore_deletion_error": schema.BoolAttribute{ + Description: "Indicates to the provider to disregard API errors when deleting the machine pool." + + " This will remove the resource from the management file, but not necessirely delete the underlying pool in case it errors." + + " Setting this to true can bypass issues when destroying the cluster resource alongside the pool resource in the same management file." + + " This is not recommended to be set in other use cases", + Computed: true, + }, }, } } @@ -195,5 +202,7 @@ func (r *MachinePoolDatasource) Read(ctx context.Context, req datasource.ReadReq return } + state.IgnoreDeletionError = types.BoolNull() + resp.Diagnostics.Append(resp.State.Set(ctx, state)...) } diff --git a/provider/machinepool/classic/machine_pool_resource.go b/provider/machinepool/classic/machine_pool_resource.go index 27bc6d5f..51978afe 100644 --- a/provider/machinepool/classic/machine_pool_resource.go +++ b/provider/machinepool/classic/machine_pool_resource.go @@ -34,6 +34,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" @@ -215,6 +216,15 @@ func (r *MachinePoolResource) Schema(ctx context.Context, req resource.SchemaReq ElementType: types.StringType, Optional: true, }, + "ignore_deletion_error": schema.BoolAttribute{ + Description: "Indicates to the provider to disregard API errors when deleting the machine pool." + + " This will remove the resource from the management file, but not necessirely delete the underlying pool in case it errors." + + " Setting this to true can bypass issues when destroying the cluster resource alongside the pool resource in the same management file." + + " This is not recommended to be set in other use cases", + Optional: true, + Computed: true, + Default: booldefault.StaticBool(false), + }, }, } } @@ -803,6 +813,8 @@ func adjustInitialStateToPlan(state, plan *MachinePoolState) { // update the Replicas with the plan value (important for nil and zero value cases) state.Replicas = plan.Replicas + state.IgnoreDeletionError = plan.IgnoreDeletionError + if common.HasValue(plan.AwsTags) { state.AwsTags = plan.AwsTags } @@ -926,6 +938,19 @@ func (r *MachinePoolResource) Delete(ctx context.Context, req resource.DeleteReq MachinePool(state.ID.ValueString()) _, err := resource.Delete().SendContext(ctx) if err != nil { + if common.BoolWithFalseDefault(state.IgnoreDeletionError) { + resp.Diagnostics.AddWarning( + "Cannot delete machine pool", + fmt.Sprintf( + "An error occurred deleting the pool,"+ + " because ignore deletion error is set it will still be removed from the terraform state. Reason: %v", + err, + ), + ) + // Remove the state: + resp.State.RemoveResource(ctx) + return + } // We can't delete the pool, see if it's the last one: numPools, err2 := r.countPools(ctx, state.Cluster.ValueString()) if numPools == 1 && err2 == nil { @@ -941,7 +966,9 @@ func (r *MachinePoolResource) Delete(ctx context.Context, req resource.DeleteReq state.Cluster.ValueString(), ), ) - // No return, we want to remove the state + // Remove the state: + resp.State.RemoveResource(ctx) + return } else { // Wasn't the last one, return error resp.Diagnostics.AddError( @@ -956,6 +983,7 @@ func (r *MachinePoolResource) Delete(ctx context.Context, req resource.DeleteReq } } + // Everything went fine deleting the resource in OCM // Remove the state: resp.State.RemoveResource(ctx) } diff --git a/provider/machinepool/classic/machine_pool_state.go b/provider/machinepool/classic/machine_pool_state.go index ca8e7034..57c6c5b6 100644 --- a/provider/machinepool/classic/machine_pool_state.go +++ b/provider/machinepool/classic/machine_pool_state.go @@ -41,6 +41,7 @@ type MachinePoolState struct { DiskSize types.Int64 `tfsdk:"disk_size"` AdditionalSecurityGroupIds types.List `tfsdk:"aws_additional_security_group_ids"` AwsTags types.Map `tfsdk:"aws_tags"` + IgnoreDeletionError types.Bool `tfsdk:"ignore_deletion_error"` } type Taints struct { diff --git a/provider/machinepool/hcp/machine_pool_datasource.go b/provider/machinepool/hcp/machine_pool_datasource.go index 3482fad2..e89b31c5 100644 --- a/provider/machinepool/hcp/machine_pool_datasource.go +++ b/provider/machinepool/hcp/machine_pool_datasource.go @@ -172,6 +172,13 @@ func (r *HcpMachinePoolDatasource) Schema(ctx context.Context, req datasource.Sc "upgrade to OpenShift 4.12.z from 4.11 or before).", Computed: true, }, + "ignore_deletion_error": schema.BoolAttribute{ + Description: "Indicates to the provider to disregard API errors when deleting the machine pool." + + " This will remove the resource from the management file, but not necessirely delete the underlying pool in case it errors." + + " Setting this to true can bypass issues when destroying the cluster resource alongside the pool resource in the same management file." + + " This is not recommended to be set in other use cases", + Computed: true, + }, }, } } @@ -204,6 +211,7 @@ func (r *HcpMachinePoolDatasource) Read(ctx context.Context, req datasource.Read state.UpgradeAcksFor = types.StringNull() state.Version = types.StringNull() + state.IgnoreDeletionError = types.BoolNull() resp.Diagnostics.Append(resp.State.Set(ctx, state)...) } diff --git a/provider/machinepool/hcp/machine_pool_resource.go b/provider/machinepool/hcp/machine_pool_resource.go index 34c9ef45..290fa382 100644 --- a/provider/machinepool/hcp/machine_pool_resource.go +++ b/provider/machinepool/hcp/machine_pool_resource.go @@ -35,6 +35,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -206,6 +207,15 @@ func (r *HcpMachinePoolResource) Schema(ctx context.Context, req resource.Schema "upgrade to OpenShift 4.12.z from 4.11 or before).", Optional: true, }, + "ignore_deletion_error": schema.BoolAttribute{ + Description: "Indicates to the provider to disregard API errors when deleting the machine pool." + + " This will remove the resource from the management file, but not necessirely delete the underlying pool in case it errors." + + " Setting this to true can bypass issues when destroying the cluster resource alongside the pool resource in the same management file." + + " This is not recommended to be set in other use cases", + Optional: true, + Computed: true, + Default: booldefault.StaticBool(false), + }, }, } } @@ -826,6 +836,7 @@ func adjustInitialStateToPlan(state, plan *HcpMachinePoolState) { state.Replicas = plan.Replicas state.NodePoolStatus = plan.NodePoolStatus state.Version = plan.Version + state.IgnoreDeletionError = plan.IgnoreDeletionError if state.AWSNodePool == nil { state.AWSNodePool = new(AWSNodePool) @@ -1068,6 +1079,19 @@ func (r *HcpMachinePoolResource) Delete(ctx context.Context, req resource.Delete NodePool(state.ID.ValueString()) _, err := resource.Delete().SendContext(ctx) if err != nil { + if common.BoolWithFalseDefault(state.IgnoreDeletionError) { + resp.Diagnostics.AddWarning( + "Cannot delete machine pool", + fmt.Sprintf( + "An error occurred deleting the pool,"+ + " because ignore deletion error is set it will still be removed from the terraform state. Reason: %v", + err, + ), + ) + // Remove the state: + resp.State.RemoveResource(ctx) + return + } // We can't delete the pool, see if it's the last one: numPools, err2 := r.countPools(ctx, state.Cluster.ValueString()) if numPools == 1 && err2 == nil { @@ -1083,7 +1107,9 @@ func (r *HcpMachinePoolResource) Delete(ctx context.Context, req resource.Delete state.Cluster.ValueString(), ), ) - // No return, we want to remove the state + // Remove the state: + resp.State.RemoveResource(ctx) + return } else { // Wasn't the last one, return error resp.Diagnostics.AddError( @@ -1098,6 +1124,7 @@ func (r *HcpMachinePoolResource) Delete(ctx context.Context, req resource.Delete } } + // Everything went fine deleting the resource in OCM // Remove the state: resp.State.RemoveResource(ctx) } diff --git a/provider/machinepool/hcp/machine_pool_state.go b/provider/machinepool/hcp/machine_pool_state.go index d1e56aad..088b48f9 100644 --- a/provider/machinepool/hcp/machine_pool_state.go +++ b/provider/machinepool/hcp/machine_pool_state.go @@ -42,6 +42,8 @@ type HcpMachinePoolState struct { TuningConfigs types.List `tfsdk:"tuning_configs"` KubeletConfigs types.String `tfsdk:"kubelet_configs"` AutoRepair types.Bool `tfsdk:"auto_repair"` + + IgnoreDeletionError types.Bool `tfsdk:"ignore_deletion_error"` } type Taints struct {