forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
203 lines (172 loc) · 3.83 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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/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() {
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
}
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); 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)
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)
}
}