forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sidecar.go
231 lines (216 loc) · 6.24 KB
/
sidecar.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// WUTONG, Application Management Platform
// Copyright (C) 2014-2017 Wutong Co., Ltd.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Wutong,
// one or multiple Commercial Licenses authorized by Wutong Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/sirupsen/logrus"
api_model "github.com/wutong-paas/wutong/api/model"
"github.com/wutong-paas/wutong/cmd"
envoyv2 "github.com/wutong-paas/wutong/node/core/envoy/v2"
"github.com/wutong-paas/wutong/util"
)
func main() {
if os.Getenv("LOG_LEVEL") != "" {
level, _ := logrus.ParseLevel(os.Getenv("LOG_LEVEL"))
logrus.SetLevel(level)
}
if len(os.Args) > 1 && os.Args[1] == "version" {
cmd.ShowVersion("sidecar")
}
if len(os.Args) > 1 && os.Args[1] == "wait" {
var timeoutSeconds = 60
var envoyReadyURL = "http://127.0.0.1:65533/ready"
var envoyListennerReadyURL = "http://127.0.0.1:65533/listeners"
var periodMillis = 500
var requestTimeoutMillis = 500
client := &http.Client{
Timeout: time.Duration(requestTimeoutMillis) * time.Millisecond,
}
logrus.Debugf("Waiting for Envoy proxy to be ready (timeout: %d seconds)...", timeoutSeconds)
var err error
timeoutAt := time.Now().Add(time.Duration(timeoutSeconds) * time.Second)
for time.Now().Before(timeoutAt) {
err = checkEnvoyIfReady(client, envoyReadyURL)
if err == nil {
logrus.Infof("Sidecar server is ready!")
break
}
logrus.Debugf("Not ready yet: %v", err)
time.Sleep(time.Duration(periodMillis) * time.Millisecond)
}
if len(os.Args) > 2 && os.Args[2] != "0" {
for time.Now().Before(timeoutAt) {
err = checkEnvoyListenerIfReady(client, envoyListennerReadyURL, os.Args[2])
if err == nil {
logrus.Infof("Sidecar is ready!")
os.Exit(0)
}
logrus.Debugf("Not ready yet: %v", err)
time.Sleep(time.Duration(periodMillis) * time.Millisecond)
}
} else {
logrus.Infof("Sidecar is ready!")
os.Exit(0)
}
logrus.Errorf("timeout waiting for Mesh Sidecar to become ready. Last error: %v", err)
os.Exit(1)
}
if len(os.Args) > 1 && os.Args[1] == "run" {
if err := run(); err != nil {
os.Exit(1)
}
os.Exit(0)
}
loggerFile, _ := os.Create("/var/log/sidecar.log")
if loggerFile != nil {
defer loggerFile.Close()
logrus.SetOutput(loggerFile)
}
if os.Getenv("DEBUG") == "true" {
logrus.SetLevel(logrus.DebugLevel)
}
if err := Run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
// Run run
func Run() error {
// start run first
run()
ticker := time.NewTicker(time.Second * 5)
defer ticker.Stop()
//step finally: listen Signal
term := make(chan os.Signal, 1)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
for {
select {
case <-term:
logrus.Warn("Received SIGTERM, exiting gracefully...")
return nil
case <-ticker.C:
run()
}
}
}
func run() error {
configs := discoverConfig()
if configs != nil {
if hosts := getHosts(configs); hosts != nil {
if err := writeHosts(hosts); err != nil {
logrus.Errorf("write hosts failure %s", err.Error())
return err
}
logrus.Debugf("rewrite hosts file success, %+v", hosts)
}
}
return nil
}
func discoverConfig() *api_model.ResourceSpec {
discoverURL := fmt.Sprintf("http://%s:6100/v1/resources/%s/%s/%s", os.Getenv("XDS_HOST_IP"), os.Getenv("POD_NAMESPACE"), os.Getenv("WT_SERVICE_ALIAS"), os.Getenv("WT_PLUGIN_ID"))
http.DefaultClient.Timeout = time.Second * 5
res, err := http.Get(discoverURL)
if err != nil {
logrus.Errorf("get config failure %s", err.Error())
}
if res != nil && res.Body != nil {
defer res.Body.Close()
var rs api_model.ResourceSpec
if err := json.NewDecoder(res.Body).Decode(&rs); err != nil {
logrus.Errorf("parse config body failure %s", err.Error())
} else {
return &rs
}
}
return nil
}
func getHosts(configs *api_model.ResourceSpec) map[string]string {
hosts := make(map[string]string)
for _, service := range configs.BaseServices {
options := envoyv2.GetOptionValues(service.Options)
for _, domain := range options.Domains {
if domain != "" && domain != "*" {
if strings.Contains(domain, ":") {
domain = strings.Split(domain, ":")[0]
}
hosts[domain] = "127.0.0.1"
}
}
}
if len(hosts) == 0 {
return nil
}
return hosts
}
func writeHosts(ipnames map[string]string) error {
hostFilePath := os.Getenv("HOST_FILE_PATH")
if hostFilePath == "" {
hostFilePath = "/etc/hosts"
}
hosts, err := util.NewHosts(hostFilePath)
if err != nil {
return err
}
for name, ip := range ipnames {
hosts.Add(ip, name)
}
return hosts.Flush()
}
func checkEnvoyIfReady(client *http.Client, envoyReadyURL string) error {
req, err := http.NewRequest(http.MethodGet, envoyReadyURL, nil)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
reBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != 200 || !strings.Contains(string(reBody), "LIVE") {
return fmt.Errorf("HTTP status code %d, body: %s", resp.StatusCode, string(reBody))
}
return nil
}
func checkEnvoyListenerIfReady(client *http.Client, url string, port string) error {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
reBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != 200 || !strings.Contains(string(reBody), fmt.Sprintf(":%s", port)) {
return fmt.Errorf("check Listeners HTTP status code %v, body is %s", resp.StatusCode, string(reBody))
}
return nil
}