Skip to content

Commit

Permalink
feat(k8s): add 'nodes' computed value in pools
Browse files Browse the repository at this point in the history
  • Loading branch information
debovema committed Feb 14, 2020
1 parent 1c38c2a commit 4e49d4e
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
45 changes: 45 additions & 0 deletions scaleway/helpers_k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type KubeconfigStruct struct {
const (
K8SClusterWaitForReadyTimeout = 10 * time.Minute
K8SClusterWaitForDeletedTimeout = 10 * time.Minute
K8SPoolNodesWaitForReadyTimeout = 10 * time.Minute
)

func k8sAPIWithRegion(d *schema.ResourceData, m interface{}) (*k8s.API, scw.Region, error) {
Expand Down Expand Up @@ -72,6 +73,15 @@ 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 {
timeout := K8SPoolNodesWaitForReadyTimeout
return k8sAPI.WaitForClusterNodesReady(&k8s.WaitForClusterNodesReadyRequest{
ClusterID: clusterID,
Region: region,
Timeout: &timeout,
})
}

func waitK8SClusterDeleted(k8sAPI *k8s.API, region scw.Region, clusterID string) error {
cluster, err := k8sAPI.WaitForCluster(&k8s.WaitForClusterRequest{
ClusterID: clusterID,
Expand All @@ -88,6 +98,41 @@ 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())
}

// convert a list of nodes to a list of map
func convertNodes(res *k8s.ListNodesResponse) []map[string]interface{} {
var result []map[string]interface{}
for _, node := range res.Nodes {
n := make(map[string]interface{})
n["name"] = node.Name
n["pool_id"] = node.PoolID
n["status"] = node.Status.String()
if node.PublicIPV4 != nil && node.PublicIPV4.String() != "<nil>" {
n["public_ip"] = node.PublicIPV4.String()
}
if node.PublicIPV6 != nil && node.PublicIPV6.String() != "<nil>" {
n["public_ip_v6"] = node.PublicIPV6.String()
}
result = append(result, n)
}
return result
}

func getNodes(k8sAPI *k8s.API, pool *k8s.Pool) (interface{}, error) {
req := &k8s.ListNodesRequest{
Region: pool.Region,
ClusterID: pool.ClusterID,
PoolID: &pool.ID,
}

nodes, err := k8sAPI.ListNodes(req, scw.WithAllPages())

if err != nil {
return nil, err
}

return convertNodes(nodes), nil
}

func clusterAutoscalerConfigFlatten(cluster *k8s.Cluster) []map[string]interface{} {
autoscalerConfig := map[string]interface{}{}
autoscalerConfig["disable_scale_down"] = cluster.AutoscalerConfig.ScaleDownDisabled
Expand Down
59 changes: 59 additions & 0 deletions scaleway/resource_k8s_cluster_beta.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,39 @@ func resourceScalewayK8SClusterBeta() *schema.Resource {
Computed: true,
Description: "The date and time of the last update of the default pool",
},
"nodes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the node",
},
"pool_id": {
Type: schema.TypeString,
Computed: true,
Description: "The pool ID whose the node belongs to",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The status of the node",
},
"public_ip": {
Type: schema.TypeString,
Computed: true,
Description: "The public IPv4 address of the node",
},
"public_ip_v6": {
Type: schema.TypeString,
Computed: true,
Description: "The public IPv6 address of the node",
},
},
},
},
"status": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -220,6 +253,12 @@ func resourceScalewayK8SClusterBeta() *schema.Resource {
},
"region": regionSchema(),
"organization_id": organizationIDSchema(),
"wait_for_nodes": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether to wait for all nodes in all pools of the cluster to be ready",
},
// Computed elements
"created_at": {
Type: schema.TypeString,
Expand Down Expand Up @@ -413,6 +452,13 @@ func resourceScalewayK8SClusterBetaCreate(d *schema.ResourceData, m interface{})
return err
}

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

return resourceScalewayK8SClusterBetaRead(d, m)
}

Expand Down Expand Up @@ -455,6 +501,11 @@ func resourceScalewayK8SClusterBetaDefaultPoolRead(d *schema.ResourceData, m int
pool = response.Pools[0]
}

nodes, err := getNodes(k8sAPI, pool)
if err != nil {
return err
}

defaultPool := map[string]interface{}{}
defaultPool["pool_id"] = newRegionalId(region, pool.ID)
defaultPool["node_type"] = pool.NodeType
Expand All @@ -466,6 +517,7 @@ func resourceScalewayK8SClusterBetaDefaultPoolRead(d *schema.ResourceData, m int
defaultPool["container_runtime"] = pool.ContainerRuntime
defaultPool["created_at"] = pool.CreatedAt.String()
defaultPool["updated_at"] = pool.UpdatedAt.String()
defaultPool["nodes"] = nodes
defaultPool["status"] = pool.Status.String()

if pool.PlacementGroupID != nil {
Expand Down Expand Up @@ -784,6 +836,13 @@ func resourceScalewayK8SClusterBetaUpdate(d *schema.ResourceData, m interface{})
return err
}

if d.Get("wait_for_nodes").(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
39 changes: 39 additions & 0 deletions scaleway/resource_k8s_pool_beta.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,39 @@ func resourceScalewayK8SPoolBeta() *schema.Resource {
Computed: true,
Description: "The Kubernetes version of the pool",
},
"nodes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the node",
},
"pool_id": {
Type: schema.TypeString,
Computed: true,
Description: "The pool ID whose the node belongs to",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The status of the node",
},
"public_ip": {
Type: schema.TypeString,
Computed: true,
Description: "The public IPv4 address of the node",
},
"public_ip_v6": {
Type: schema.TypeString,
Computed: true,
Description: "The public IPv6 address of the node",
},
},
},
},
},
}
}
Expand Down Expand Up @@ -174,6 +207,11 @@ func resourceScalewayK8SPoolBetaRead(d *schema.ResourceData, m interface{}) erro
return err
}

nodes, err := getNodes(k8sAPI, pool)
if err != nil {
return err
}

_ = d.Set("cluster_id", newRegionalId(region, pool.ClusterID))
_ = d.Set("name", pool.Name)
_ = d.Set("node_type", pool.NodeType)
Expand All @@ -186,6 +224,7 @@ func resourceScalewayK8SPoolBetaRead(d *schema.ResourceData, m interface{}) erro
_ = d.Set("container_runtime", pool.ContainerRuntime)
_ = d.Set("created_at", pool.CreatedAt)
_ = d.Set("updated_at", pool.UpdatedAt)
_ = d.Set("nodes", nodes)

if pool.PlacementGroupID != nil {
_ = d.Set("placement_group_id", newZonedIdFromRegion(region, *pool.PlacementGroupID)) // TODO fix this ZonedIdFromRegion
Expand Down

0 comments on commit 4e49d4e

Please sign in to comment.