Skip to content

Commit

Permalink
Merge pull request #828 from gdbranco/fix/ocm-12048
Browse files Browse the repository at this point in the history
OCM-12048 | feat: include indicator for bypassing machine pool deletion errors
  • Loading branch information
gdbranco authored Oct 24, 2024
2 parents 817d29a + ef9b278 commit ee00601
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/hcp_machine_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/machine_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions docs/resources/hcp_machine_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/resources/machine_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions provider/machinepool/classic/machine_pool_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
}
Expand Down Expand Up @@ -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)...)
}
30 changes: 29 additions & 1 deletion provider/machinepool/classic/machine_pool_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
},
},
}
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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(
Expand All @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions provider/machinepool/classic/machine_pool_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions provider/machinepool/hcp/machine_pool_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
}
Expand Down Expand Up @@ -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)...)
}
29 changes: 28 additions & 1 deletion provider/machinepool/hcp/machine_pool_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
},
},
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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(
Expand All @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions provider/machinepool/hcp/machine_pool_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ee00601

Please sign in to comment.