-
Notifications
You must be signed in to change notification settings - Fork 4
/
kubectl.go
57 lines (50 loc) · 1.26 KB
/
kubectl.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
package schema
import (
"bytes"
"crypto/tls"
"errors"
"io/ioutil"
"net/http"
"strings"
"time"
)
type K8sRequestOption struct {
Endpoint string
Token string
Api string
Method string
PostData string
}
func ServerAccountRequest(opts K8sRequestOption) (string, error) {
// http client
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: 20 * time.Second,
}
opts.Method = strings.ToUpper(opts.Method)
request, err := http.NewRequest(opts.Method, opts.Endpoint+opts.Api, bytes.NewBuffer([]byte(opts.PostData)))
if err != nil {
return "", errors.New("err found while generate post request in net.http: " + err.Error())
}
// set request header
if opts.Method == "POST" {
request.Header.Set("Content-Type", "application/json")
}
// auth token
if opts.Token != "" {
token := strings.TrimSpace(opts.Token)
request.Header.Set("Authorization", "Bearer "+token)
}
resp, err := client.Do(request)
if err != nil {
return "", errors.New("err found in post request: " + err.Error())
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.New("err found in post request: " + err.Error())
}
return string(content), nil
}