-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkamatera_krequests.go
149 lines (139 loc) · 3.92 KB
/
kamatera_krequests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"io"
"net/http"
"net/url"
"strings"
"time"
)
var cloudcliBaseUrl = "https://cloudcli.cloudwm.com"
var cloudcliDebug = ""
type KTaskIdResponse struct {
TaskId string `json:"task_id"`
}
type KTaskResponse struct {
TaskName *string `json:"task_name"`
State string `json:"state"`
Result *interface{} `json:"result"`
Error *string `json:"error"`
Meta *interface{} `json:"meta"`
}
type KubeConfig struct {
ApiVersion string `json:"apiVersion"`
Kind string `json:"kind"`
CurrentContext string `json:"current-context"`
Preferences interface{}
Clusters []struct {
Name string `json:"name"`
Cluster struct {
CertificateAuthorityData string `json:"certificate-authority-data"`
Server string `json:"server"`
} `json:"cluster"`
}
Contexts []struct {
Name string `json:"name"`
Context struct {
Cluster string `json:"cluster"`
User string `json:"user"`
} `json:"context"`
}
Users []struct {
Name string `json:"name"`
User struct {
ClientCertificateData string `json:"client-certificate-data"`
ClientKeyData string `json:"client-key-data"`
}
}
}
func KPost(apiClientid string, apiSecret string, path string, data url.Values) ([]byte, error) {
if cloudcliDebug == "true" {
logrus.Infof("POST %s/k8s/%s", cloudcliBaseUrl, path)
logrus.Infof("data: %s", data)
}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/k8s/%s", cloudcliBaseUrl, path), strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("AuthClientId", apiClientid)
req.Header.Set("AuthSecret", apiSecret)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if cloudcliDebug == "true" {
logrus.Infof("response: %s", body)
}
return body, nil
}
func KCreateCluster(apiClientid string, apiSecret string, config KConfig) (*KTaskResponse, error) {
return KClusterTask(apiClientid, apiSecret, config, "create_cluster")
}
func KGetKubeconfig(apiClientid string, apiSecret string, config KConfig) (*KubeConfig, error) {
taskResponse, err := KClusterTask(apiClientid, apiSecret, config, "kubeconfig?json_format=true")
if err != nil {
return nil, err
}
if taskResponse.State != "SUCCESS" {
return nil, fmt.Errorf("failed to get kubeconfig: %s", *taskResponse.Error)
}
if taskResponse.Result == nil {
return nil, fmt.Errorf("failed to get kubeconfig: result is nil")
}
kubeconfigJson := (*taskResponse.Result).(string)
var kubeconfig KubeConfig
err = json.Unmarshal([]byte(kubeconfigJson), &kubeconfig)
if err != nil {
return nil, err
}
return &kubeconfig, nil
}
func KClusterTask(apiClientid string, apiSecret string, config KConfig, taskName string) (*KTaskResponse, error) {
jsonBytes, err := json.Marshal(config)
if err != nil {
return nil, err
}
data := url.Values{}
data.Set("kconfig", string(jsonBytes))
body, err := KPost(apiClientid, apiSecret, taskName, data)
if err != nil {
return nil, err
}
var response KTaskIdResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
return KWaitTask(apiClientid, apiSecret, response.TaskId)
}
func KWaitTask(apiClientId string, apiSecret string, taskId string) (*KTaskResponse, error) {
for {
data := url.Values{}
data.Set("task_id", taskId)
body, err := KPost(apiClientId, apiSecret, "task_status", data)
if err != nil {
return nil, err
}
var response KTaskResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
if response.State != "PENDING" {
return &response, nil
}
time.Sleep(5 * time.Second)
}
}