Skip to content

Commit

Permalink
feat(k8s): wait for pools to be ready
Browse files Browse the repository at this point in the history
  • Loading branch information
debovema committed Feb 18, 2020
1 parent 4e5abda commit 428db88
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 27 deletions.
18 changes: 9 additions & 9 deletions scaleway/helpers_k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type KubeconfigStruct struct {
const (
K8SClusterWaitForReadyTimeout = 10 * time.Minute
K8SClusterWaitForDeletedTimeout = 10 * time.Minute
K8SPoolNodesWaitForReadyTimeout = 10 * time.Minute
K8SPoolWaitForReadyTimeout = 10 * time.Minute
)

func k8sAPIWithRegion(d *schema.ResourceData, m interface{}) (*k8s.API, scw.Region, error) {
Expand Down Expand Up @@ -73,14 +73,6 @@ func waitK8SClusterReady(k8sAPI *k8s.API, region scw.Region, clusterID string) e
return fmt.Errorf("Cluster %s has state %s, wants %s", clusterID, cluster.Status.String(), k8s.ClusterStatusReady.String())
}

func waitK8SClusterNodesReady(k8sAPI *k8s.API, region scw.Region, clusterID string) error {
return k8sAPI.WaitForClusterPools(&k8s.WaitForClusterPoolsRequest{
ClusterID: clusterID,
Region: region,
Timeout: scw.DurationPtr(K8SPoolNodesWaitForReadyTimeout),
})
}

func waitK8SClusterDeleted(k8sAPI *k8s.API, region scw.Region, clusterID string) error {
cluster, err := k8sAPI.WaitForCluster(&k8s.WaitForClusterRequest{
ClusterID: clusterID,
Expand All @@ -97,6 +89,14 @@ func waitK8SClusterDeleted(k8sAPI *k8s.API, region scw.Region, clusterID string)
return fmt.Errorf("Cluster %s has state %s, wants %s", clusterID, cluster.Status.String(), k8s.ClusterStatusDeleted.String())
}

func waitK8SPoolReady(k8sAPI *k8s.API, region scw.Region, poolID string) error {
return k8sAPI.WaitForPool(&k8s.WaitForPoolRequest{
PoolID: poolID,
Region: region,
Timeout: scw.DurationPtr(K8SPoolWaitForReadyTimeout),
})
}

// convert a list of nodes to a list of map
func convertNodes(res *k8s.ListNodesResponse) []map[string]interface{} {
var result []map[string]interface{}
Expand Down
36 changes: 20 additions & 16 deletions scaleway/resource_k8s_cluster_beta.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ func resourceScalewayK8SClusterBeta() *schema.Resource {
k8s.RuntimeCrio.String(),
}, false),
},
"wait_for_pool_ready": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether to wait for the pool to be ready",
},
// Computed elements
"pool_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -251,12 +257,6 @@ func resourceScalewayK8SClusterBeta() *schema.Resource {
},
},
},
"wait_for_pools": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether to wait for all pools of the cluster to be ready",
},
"region": regionSchema(),
"organization_id": organizationIDSchema(),
// Computed elements
Expand Down Expand Up @@ -452,14 +452,18 @@ func resourceScalewayK8SClusterBetaCreate(d *schema.ResourceData, m interface{})
return err
}

if d.Get("wait_for_pools").(bool) { // wait for nodes to be ready if specified
err = waitK8SClusterNodesReady(k8sAPI, region, res.ID)
clusterRead := resourceScalewayK8SClusterBetaRead(d, m) // ensure that 'default_pool.0.pool_id' is set

if d.Get("default_pool.0.wait_for_pool_ready").(bool) { // wait for the pool to be ready if specified
defaultPoolID := d.Get("default_pool.0.pool_id").(string)

err = waitK8SPoolReady(k8sAPI, region, defaultPoolID)
if err != nil {
return err
}
}

return resourceScalewayK8SClusterBetaRead(d, m)
return clusterRead
}

// resourceScalewayK8SClusterBetaDefaultPoolRead is only called after a resourceScalewayK8SClusterBetaCreate
Expand Down Expand Up @@ -714,6 +718,13 @@ func resourceScalewayK8SClusterBetaDefaultPoolUpdate(d *schema.ResourceData, m i
}
}
}

if d.Get("default_pool.0.wait_for_pool_ready").(bool) { // wait for the pool to be ready if specified
err = waitK8SPoolReady(k8sAPI, region, defaultPoolID)
if err != nil {
return err
}
}
}

return resourceScalewayK8SClusterBetaDefaultPoolRead(d, m)
Expand Down Expand Up @@ -836,13 +847,6 @@ func resourceScalewayK8SClusterBetaUpdate(d *schema.ResourceData, m interface{})
return err
}

if d.Get("wait_for_pools").(bool) { // wait for nodes to be ready if specified
err = waitK8SClusterNodesReady(k8sAPI, region, clusterID)
if err != nil {
return err
}
}

return resourceScalewayK8SClusterBetaRead(d, m)
}

Expand Down
22 changes: 21 additions & 1 deletion scaleway/resource_k8s_pool_beta.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ func resourceScalewayK8SPoolBeta() *schema.Resource {
k8s.RuntimeCrio.String(),
}, false),
},
"wait_for_pool_ready": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether to wait for the pool to be ready",
},
"placement_group_id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -183,6 +189,13 @@ func resourceScalewayK8SPoolBetaCreate(d *schema.ResourceData, m interface{}) er

d.SetId(newRegionalId(region, res.ID))

if d.Get("wait_for_pool_ready").(bool) { // wait for nodes to be ready if specified
err = waitK8SPoolReady(k8sAPI, region, res.ID)
if err != nil {
return err
}
}

return resourceScalewayK8SPoolBetaRead(d, m)
}

Expand Down Expand Up @@ -267,11 +280,18 @@ func resourceScalewayK8SPoolBetaUpdate(d *schema.ResourceData, m interface{}) er
updateRequest.Size = scw.Uint32Ptr(uint32(d.Get("size").(int)))
}

_, err = k8sAPI.UpdatePool(updateRequest)
res, err := k8sAPI.UpdatePool(updateRequest)
if err != nil {
return err
}

if d.Get("wait_for_pool_ready").(bool) { // wait for nodes to be ready if specified
err = waitK8SPoolReady(k8sAPI, region, res.ID)
if err != nil {
return err
}
}

return resourceScalewayK8SPoolBetaRead(d, m)
}

Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/k8s_cluster_beta.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ The following arguments are supported:
- `container_runtime` - (Defaults to `docker`) The container runtime of the default pool.
~> **Important:** Updates to this field will recreate a new default pool.

- `wait_for_pools` - (Default to `false`) Whether to wait for all pools of the cluster to be ready
- `wait_for_pool_ready` - (Default to `false`) Whether to wait for the pool to be ready

- `region` - (Defaults to [provider](../index.html#region) `region`) The [region](../guides/regions_and_zones.html#regions) in which the cluster should be created.

Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/k8s_pool_beta.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ The following arguments are supported:

- `region` - (Defaults to [provider](../index.html#region) `region`) The [region](../guides/regions_and_zones.html#regions) in which the pool should be created.

- `wait_for_pool_ready` - (Default to `false`) Whether to wait for the pool to be ready

## Attributes Reference

In addition to all above arguments, the following attributes are exported:
Expand Down

0 comments on commit 428db88

Please sign in to comment.