Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #69 from bardielle/machinpool_manipulation
Browse files Browse the repository at this point in the history
Supporting default machine pool manipulation on update cluster
  • Loading branch information
bardielle authored Jan 9, 2023
2 parents 9302b1e + 4db18a8 commit 6374a30
Show file tree
Hide file tree
Showing 6 changed files with 687 additions and 46 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/terraform-plugin-go v0.5.0
github.com/onsi/ginkgo/v2 v2.1.4
github.com/onsi/gomega v1.19.0
github.com/openshift-online/ocm-sdk-go v0.1.275
github.com/openshift-online/ocm-sdk-go v0.1.303
)

require (
Expand Down
90 changes: 68 additions & 22 deletions provider/cluster_rosa_classic_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,16 +509,45 @@ func (r *ClusterRosaClassicResource) Update(ctx context.Context, request tfsdk.U
}

// Send request to update the cluster:
builder := cmv1.NewCluster()
var nodes *cmv1.ClusterNodesBuilder
updateNodes := false
clusterBuilder := cmv1.NewCluster()
clusterNodesBuilder := cmv1.NewClusterNodes()
compute, ok := shouldPatchInt(state.ComputeNodes, plan.ComputeNodes)
if ok {
nodes.Compute(int(compute))
clusterNodesBuilder = clusterNodesBuilder.Compute(int(compute))
updateNodes = true
}
if !nodes.Empty() {
builder.Nodes(nodes)

if !plan.AutoScalingEnabled.Unknown && !plan.AutoScalingEnabled.Null && plan.AutoScalingEnabled.Value {
// autoscaling enabled
autoscaling := cmv1.NewMachinePoolAutoscaling()

if !plan.MaxReplicas.Unknown && !plan.MaxReplicas.Null {
autoscaling = autoscaling.MaxReplicas(int(plan.MaxReplicas.Value))
}
if !plan.MinReplicas.Unknown && !plan.MinReplicas.Null {
autoscaling = autoscaling.MinReplicas(int(plan.MinReplicas.Value))
}

clusterNodesBuilder = clusterNodesBuilder.AutoscaleCompute(autoscaling)
updateNodes = true

} else {
if (!plan.MaxReplicas.Unknown && !plan.MaxReplicas.Null) || (!plan.MinReplicas.Unknown && !plan.MinReplicas.Null) {
response.Diagnostics.AddError(
"Can't update cluster",
fmt.Sprintf(
"Can't update MaxReplica and/or MinReplica of cluster when autoscaling is not enabled",
),
)
return
}
}

if updateNodes {
clusterBuilder = clusterBuilder.Nodes(clusterNodesBuilder)
}
patch, err := builder.Build()
clusterSpec, err := clusterBuilder.Build()
if err != nil {
response.Diagnostics.AddError(
"Can't build cluster patch",
Expand All @@ -530,7 +559,7 @@ func (r *ClusterRosaClassicResource) Update(ctx context.Context, request tfsdk.U
return
}
update, err := r.collection.Cluster(state.ID.Value).Update().
Body(patch).
Body(clusterSpec).
SendContext(ctx)
if err != nil {
response.Diagnostics.AddError(
Expand All @@ -542,6 +571,12 @@ func (r *ClusterRosaClassicResource) Update(ctx context.Context, request tfsdk.U
)
return
}

// update the autoscaling enabled with the plan value (important for nil and false cases)
state.AutoScalingEnabled = plan.AutoScalingEnabled
// update the ComputeNodes with the plan value (important for nil and zero value cases)
state.ComputeNodes = plan.ComputeNodes

object := update.Body()

// Update the state:
Expand Down Expand Up @@ -651,6 +686,32 @@ func populateRosaClassicClusterState(ctx context.Context, object *cmv1.Cluster,
Value: object.Nodes().ComputeMachineType().ID(),
}

autoScaleCompute, ok := object.Nodes().GetAutoscaleCompute()
if ok {
var maxReplicas, minReplicas int
state.AutoScalingEnabled = types.Bool{
Value: true,
}

maxReplicas, ok = autoScaleCompute.GetMaxReplicas()
if ok {
state.MaxReplicas = types.Int64{
Value: int64(maxReplicas),
}
}

minReplicas, ok = autoScaleCompute.GetMinReplicas()
if ok {
state.MinReplicas = types.Int64{
Value: int64(minReplicas),
}
}
} else {
// autoscaling not enabled - initialize the MaxReplica and MinReplica
state.MaxReplicas.Null = true
state.MinReplicas.Null = true
}

azs, ok := object.Nodes().GetAvailabilityZones()
if ok {
state.AvailabilityZones.Elems = make([]attr.Value, 0)
Expand All @@ -669,21 +730,6 @@ func populateRosaClassicClusterState(ctx context.Context, object *cmv1.Cluster,
Value: object.EtcdEncryption(),
}

autoscaleCompute := object.Nodes().AutoscaleCompute()
if autoscaleCompute != nil {
state.AutoScalingEnabled = types.Bool{
Value: true,
}

state.MaxReplicas = types.Int64{
Value: int64(autoscaleCompute.MaxReplicas()),
}

state.MinReplicas = types.Int64{
Value: int64(autoscaleCompute.MinReplicas()),
}
}

//The API does not return account id
awsAccountID, ok := object.AWS().GetAccountID()
if ok {
Expand Down
Loading

0 comments on commit 6374a30

Please sign in to comment.