Skip to content

Commit

Permalink
potential fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterp committed Jan 5, 2024
1 parent c868abb commit 66b3652
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion exoscale/resource_exoscale_sks_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"encoding/base64"
"errors"
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

egoscale "github.com/exoscale/egoscale/v2"
exov2 "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/terraform-provider-exoscale/pkg/config"
"github.com/exoscale/terraform-provider-exoscale/pkg/general"
Expand Down Expand Up @@ -404,6 +406,34 @@ func resourceSKSClusterRead(ctx context.Context, d *schema.ResourceData, meta in
return diag.FromErr(resourceSKSClusterApply(ctx, d, sksCluster, certificates))
}

func waitForClusterUpdateToSucceed(ctx context.Context, client *exov2.Client, zone, clusterID string) error {
ticker := time.NewTicker(5 * time.Minute)
defer ticker.Stop()

for {
select {
case <-ticker.C:
cluster, err := client.GetSKSCluster(ctx, zone, clusterID)
if err != nil {
return err
}

if *cluster.State != "updating" {
continue
}

return nil
case <-ctx.Done():
err := ctx.Err()
if err != nil {
return err
}

return nil
}
}
}

func resourceSKSClusterUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
tflog.Debug(ctx, "beginning update", map[string]interface{}{
"id": resourceSKSClusterIDString(d),
Expand Down Expand Up @@ -460,7 +490,30 @@ func resourceSKSClusterUpdate(ctx context.Context, d *schema.ResourceData, meta
}

if updated {
if err = client.UpdateSKSCluster(ctx, zone, sksCluster); err != nil {
// due to a bug it's possible for the update operation
// to remain in pending state forever
// we work around this by checking the cluster state
updateErrChan := make(chan error)
getErrChan := make(chan error)

go func() {
updateErrChan <- client.UpdateSKSCluster(ctx, zone, sksCluster)
}()

go func() {
// wait for the update to start
time.Sleep(10 * time.Second)
getErrChan <- waitForClusterUpdateToSucceed(ctx, client, zone, *sksCluster.ID)
}()

var err error

select {
case err = <-updateErrChan:
case err = <-getErrChan:
}

if err != nil {
return diag.FromErr(err)
}
}
Expand Down

0 comments on commit 66b3652

Please sign in to comment.