36 Star 396 Fork 71

GVPrancher / rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
client.go 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
Daishan Peng 提交于 2018-07-10 14:04 . fix certs
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, writeCertOnly bool) 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, writeCertOnly)
}
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)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.16-rc1

搜索帮助

344bd9b3 5694891 D2dac590 5694891