37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.go 4.00 KB
一键复制 编辑 原始数据 按行查看 历史
Murali Paluru 提交于 2018-07-06 14:17 . adding logserver to agent
package main
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"time"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/rancher/rancher/pkg/agent/cluster"
"github.com/rancher/rancher/pkg/agent/node"
"github.com/rancher/rancher/pkg/logserver"
"github.com/rancher/rancher/pkg/remotedialer"
"github.com/rancher/rancher/pkg/rkenodeconfigclient"
"github.com/sirupsen/logrus"
)
var (
VERSION = "dev"
)
const (
Token = "X-API-Tunnel-Token"
Params = "X-API-Tunnel-Params"
)
func main() {
logserver.StartServerWithDefaults()
if os.Getenv("CATTLE_DEBUG") == "true" || os.Getenv("RANCHER_DEBUG") == "true" {
logrus.SetLevel(logrus.DebugLevel)
}
if err := run(); err != nil {
log.Fatal(err)
}
}
func isCluster() bool {
return os.Getenv("CATTLE_CLUSTER") == "true"
}
func getParams() (map[string]interface{}, error) {
if isCluster() {
return cluster.Params()
}
return node.Params(), nil
}
func getTokenAndURL() (string, string, error) {
token, url, err := node.TokenAndURL()
if err != nil {
return "", "", err
}
if token == "" {
return cluster.TokenAndURL()
}
return token, url, nil
}
func isConnect() bool {
if os.Getenv("CATTLE_AGENT_CONNECT") == "true" {
return true
}
_, err := os.Stat("connected")
return err == nil
}
func connected() {
f, err := os.Create("connected")
if err != nil {
f.Close()
}
}
func cleanup(ctx context.Context) error {
if os.Getenv("CATTLE_K8S_MANAGED") != "true" {
return nil
}
c, err := client.NewEnvClient()
if err != nil {
return err
}
defer c.Close()
args := filters.NewArgs()
args.Add("label", "io.cattle.agent=true")
containers, err := c.ContainerList(ctx, types.ContainerListOptions{
All: true,
Filters: args,
})
if err != nil {
return err
}
for _, container := range containers {
if _, ok := container.Labels["io.kubernetes.pod.namespace"]; ok {
continue
}
if strings.Contains(container.Names[0], "share-mnt") {
continue
}
container := container
go func() {
time.Sleep(15 * time.Second)
logrus.Infof("Removing unmanaged agent %s(%s)", container.Names[0], container.ID)
c.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{
Force: true,
})
}()
}
return nil
}
func run() error {
logrus.Infof("Rancher agent version %s is starting", VERSION)
params, err := getParams()
if err != nil {
return err
}
writeCertsOnly := os.Getenv("CATTLE_WRITE_CERT_ONLY") == "true"
bytes, err := json.Marshal(params)
if err != nil {
return err
}
token, server, err := getTokenAndURL()
if err != nil {
return err
}
headers := map[string][]string{
Token: {token},
Params: {base64.StdEncoding.EncodeToString(bytes)},
}
serverURL, err := url.Parse(server)
if err != nil {
return err
}
onConnect := func(ctx context.Context) error {
connected()
connectConfig := fmt.Sprintf("https://%s/v3/connect/config", serverURL.Host)
if err := rkenodeconfigclient.ConfigClient(ctx, connectConfig, headers, writeCertsOnly); err != nil {
return err
}
if isCluster() {
return nil
}
if err := cleanup(context.Background()); err != nil {
return err
}
go func() {
logrus.Infof("Starting plan monitor")
for {
select {
case <-time.After(2 * time.Minute):
err := rkenodeconfigclient.ConfigClient(ctx, connectConfig, headers, writeCertsOnly)
if err != nil {
logrus.Errorf("failed to check plan: %v", err)
}
case <-ctx.Done():
return
}
}
}()
return nil
}
for {
wsURL := fmt.Sprintf("wss://%s/v3/connect", serverURL.Host)
if !isConnect() {
wsURL += "/register"
}
logrus.Infof("Connecting to %s with token %s", wsURL, token)
remotedialer.ClientConnect(wsURL, http.Header(headers), nil, func(proto, address string) bool {
switch proto {
case "tcp":
return true
case "unix":
return address == "/var/run/docker.sock"
}
return false
}, onConnect)
time.Sleep(5 * time.Second)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.7-rc2

搜索帮助