-
Notifications
You must be signed in to change notification settings - Fork 870
/
webkubectl.go
44 lines (39 loc) · 959 Bytes
/
webkubectl.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
package webkubectl
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/spf13/viper"
)
type response struct {
Success bool `json:"success"`
Token string `json:"token"`
Message string `json:"message"`
}
func GetConnectToken(name string, apiServer string, token string) (string, error) {
req := map[string]string{
"name": name,
"apiServer": apiServer,
"token": token,
}
j, _ := json.Marshal(&req)
url := fmt.Sprintf("http://%s:%d/api/kube-token", viper.GetString("webkubectl.host"), viper.GetInt("webkubectl.port"))
resp, err := http.Post(url, "application/json", bytes.NewReader(j))
if err != nil {
return "", err
}
var r response
if resp.StatusCode == http.StatusOK {
buf, _ := ioutil.ReadAll(resp.Body)
_ = json.Unmarshal(buf, &r)
if r.Success {
return r.Token, nil
} else {
return "", errors.New(r.Message)
}
}
return "", errors.New(fmt.Sprint(resp.StatusCode))
}