forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
70 lines (57 loc) · 1.38 KB
/
client.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
package rkenodeconfigclient
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/rancher/rancher/pkg/rkeworker"
"github.com/sirupsen/logrus"
)
var (
client = &http.Client{
Timeout: 300 * time.Second,
}
)
func ConfigClient(ctx context.Context, url string, header http.Header) error {
for {
nc, err := getConfig(client, url, header)
if err != nil {
logrus.Infof("Error while getting agent config: %v", err)
time.Sleep(5 * time.Second)
continue
}
if nc != nil {
return rkeworker.ExecutePlan(ctx, nc)
}
logrus.Infof("waiting for node to register")
time.Sleep(2 * time.Second)
}
}
func getConfig(client *http.Client, url string, header http.Header) (*rkeworker.NodeConfig, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
for k, v := range header {
req.Header[k] = v
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusServiceUnavailable {
return nil, nil
}
if resp.StatusCode == http.StatusNotFound {
return &rkeworker.NodeConfig{}, nil
}
if resp.StatusCode != http.StatusOK {
content, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("invalid response %d: %s", resp.StatusCode, string(content))
}
nc := &rkeworker.NodeConfig{}
return nc, json.NewDecoder(resp.Body).Decode(nc)
}