Fixed asyncs tasks parsing; Applied k8s API changes; Updated docs

This commit is contained in:
kjubybot
2022-03-23 11:40:51 +03:00
parent d9c10c0d9c
commit bf1afc66dd
15 changed files with 715 additions and 36 deletions

View File

@@ -25,7 +25,8 @@ Visit https://github.com/rudecs/terraform-provider-decort for full source code p
package decort
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"time"
)
@@ -35,6 +36,7 @@ import (
var Timeout30s = time.Second * 30
var Timeout60s = time.Second * 60
var Timeout180s = time.Second * 180
var Timeout10m = time.Minute * 10
//
// structures related to /cloudapi/rg/list API
@@ -581,11 +583,12 @@ const VinsDeleteAPI = "/restmachine/cloudapi/vins/delete"
//K8sNodeRecord represents a worker/master group
type K8sNodeRecord struct {
ID int `json:"id"`
Disk int `json:"disk"`
Cpu int `json:"cpu"`
Num int `json:"num"`
Ram int `json:"ram"`
ID int `json:"id"`
Name string `json:"name"`
Disk int `json:"disk"`
Cpu int `json:"cpu"`
Num int `json:"num"`
Ram int `json:"ram"`
}
//K8sRecord represents k8s instance
@@ -611,22 +614,35 @@ const K8sDeleteAPI = "/restmachine/cloudapi/k8s/delete"
const K8sWgCreateAPI = "/restmachine/cloudapi/k8s/workersGroupAdd"
const K8sWgDeleteAPI = "/restmachine/cloudapi/k8s/workersGroupDelete"
const K8sGetConfigAPI = "/restmachine/cloudapi/k8s/getConfig"
//Blasphemous workaround for parsing Result value
type TaskResult int
func (r *TaskResult) UnmarshalJSON(b []byte) error {
b = bytes.Trim(b, `"`)
if len(b) == 0 {
*r = 0
return nil
if b[0] == '"' {
b := b[1 : len(b)-1]
if len(b) == 0 {
*r = 0
return nil
}
n, err := strconv.Atoi(string(b))
if err != nil {
return err
}
*r = TaskResult(n)
} else if b[0] == '[' {
res := []interface{}{}
if err := json.Unmarshal(b, &res); err != nil {
return err
}
if n, ok := res[0].(float64); ok {
*r = TaskResult(n)
} else {
return fmt.Errorf("could not unmarshal %v into int", res[0])
}
}
n, err := strconv.Atoi(string(b))
if err != nil {
return err
}
*r = TaskResult(n)
return nil
}

View File

@@ -44,6 +44,7 @@ func resourceK8sCreate(d *schema.ResourceData, m interface{}) error {
urlValues.Add("name", d.Get("name").(string))
urlValues.Add("rgId", strconv.Itoa(d.Get("rg_id").(int)))
urlValues.Add("k8ciId", strconv.Itoa(d.Get("k8sci_id").(int)))
urlValues.Add("workerGroupName", d.Get("wg_name").(string))
var masterNode K8sNodeRecord
if masters, ok := d.GetOk("masters"); ok {
@@ -67,7 +68,6 @@ func resourceK8sCreate(d *schema.ResourceData, m interface{}) error {
urlValues.Add("workerRam", strconv.Itoa(workerNode.Ram))
urlValues.Add("workerDisk", strconv.Itoa(workerNode.Disk))
//TODO find a way to avoid hardcoding these values
//if withLB, ok := d.GetOk("with_lb"); ok {
//urlValues.Add("withLB", strconv.FormatBool(withLB.(bool)))
//}
@@ -121,6 +121,11 @@ func resourceK8sCreate(d *schema.ResourceData, m interface{}) error {
d.Set("default_wg_id", k8s.Groups.Workers[0].ID)
urlValues = &url.Values{}
urlValues.Add("k8sId", d.Id())
kubeconfig, err := controller.decortAPICall("POST", K8sGetConfigAPI, urlValues)
d.Set("kubeconfig", kubeconfig)
return nil
}
@@ -136,6 +141,7 @@ func resourceK8sRead(d *schema.ResourceData, m interface{}) error {
d.Set("name", k8s.Name)
d.Set("rg_id", k8s.RgID)
d.Set("k8sci_id", k8s.CI)
d.Set("wg_name", k8s.Groups.Workers[0].Name)
d.Set("masters", nodeToResource(k8s.Groups.Masters))
d.Set("workers", nodeToResource(k8s.Groups.Workers[0]))
d.Set("default_wg_id", k8s.Groups.Workers[0].ID)
@@ -216,6 +222,13 @@ func resourceK8sSchemaMake() map[string]*schema.Schema {
Description: "ID of the k8s catalog item to base this instance on.",
},
"wg_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name for first worker group created with cluster.",
},
"masters": {
Type: schema.TypeList,
Optional: true,
@@ -265,6 +278,12 @@ func resourceK8sSchemaMake() map[string]*schema.Schema {
Computed: true,
Description: "ID of default workers group for this instace.",
},
"kubeconfig": {
Type: schema.TypeString,
Computed: true,
Description: "Kubeconfig for cluster access.",
},
}
}
@@ -282,7 +301,13 @@ func resourceK8s() *schema.Resource {
State: schema.ImportStatePassthrough,
},
//TODO timeouts
Timeouts: &schema.ResourceTimeout{
Create: &Timeout10m,
Read: &Timeout30s,
Update: &Timeout60s,
Delete: &Timeout60s,
Default: &Timeout60s,
},
Schema: resourceK8sSchemaMake(),
}

View File

@@ -28,7 +28,6 @@ import (
"net/url"
"strconv"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
log "github.com/sirupsen/logrus"
)
@@ -39,7 +38,7 @@ func resourceK8sWgCreate(d *schema.ResourceData, m interface{}) error {
controller := m.(*ControllerCfg)
urlValues := &url.Values{}
urlValues.Add("k8sId", strconv.Itoa(d.Get("k8s_id").(int)))
urlValues.Add("name", uuid.New().String())
urlValues.Add("name", d.Get("name").(string))
urlValues.Add("workerNum", strconv.Itoa(d.Get("num").(int)))
urlValues.Add("workerCpu", strconv.Itoa(d.Get("cpu").(int)))
urlValues.Add("workerRam", strconv.Itoa(d.Get("ram").(int)))
@@ -51,6 +50,9 @@ func resourceK8sWgCreate(d *schema.ResourceData, m interface{}) error {
}
d.SetId(resp)
// This code is the supposed flow, but at the time of writing it's not yet implemented by the platfom
//urlValues = &url.Values{}
//urlValues.Add("auditId", strings.Trim(resp, `"`))
@@ -90,6 +92,7 @@ func resourceK8sWgRead(d *schema.ResourceData, m interface{}) error {
return err
}
d.Set("name", wg.Name)
d.Set("num", wg.Num)
d.Set("cpu", wg.Cpu)
d.Set("ram", wg.Ram)
@@ -145,13 +148,12 @@ func resourceK8sWgSchemaMake() map[string]*schema.Schema {
Description: "ID of k8s instance.",
},
//Unused but required by creation API. Sending generated UUID each time
//"name": {
//Type: schema.TypeString,
//Required: true,
//ForceNew: true,
//Description: "Name of the worker group.",
//},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the worker group.",
},
"num": {
Type: schema.TypeInt,
@@ -200,7 +202,12 @@ func resourceK8sWg() *schema.Resource {
State: schema.ImportStatePassthrough,
},
//TODO timeouts
Timeouts: &schema.ResourceTimeout{
Create: &Timeout10m,
Read: &Timeout30s,
Delete: &Timeout60s,
Default: &Timeout60s,
},
Schema: resourceK8sWgSchemaMake(),
}