This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
/
main.go
493 lines (430 loc) · 11.7 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"sort"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/codegangsta/cli"
"github.com/golang/gddo/httputil"
"github.com/gorilla/mux"
"github.com/rancher/log"
logserver "github.com/rancher/log/server"
"github.com/rancher/rancher-metadata/config"
"github.com/rancher/rancher-metadata/server"
"gopkg.in/yaml.v2"
)
const (
ContentText = 1
ContentJSON = 2
ContentYAML = 3
)
var (
VERSION string
// A key to check for magic traversing of arrays by a string field in them
// For example, given: { things: [ {name: 'asdf', stuff: 42}, {name: 'zxcv', stuff: 43} ] }
// Both ../things/0/stuff and ../things/asdf/stuff will return 42 because 'asdf' matched the 'anme' field of one of the 'things'.
)
// ServerConfig specifies the configuration for the metadata server
type ServerConfig struct {
sync.Mutex
metadataController *server.MetadataController
listen string
listenReload string
enableXff bool
router *mux.Router
reloadRouter *mux.Router
reloadChan chan chan error
}
func main() {
logserver.StartServerWithDefaults()
app := getCliApp()
app.Action = appMain
app.Run(os.Args)
}
func getCliApp() *cli.App {
app := cli.NewApp()
app.Version = VERSION
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "Debug",
},
cli.BoolFlag{
Name: "xff",
Usage: "X-Forwarded-For header support",
},
cli.StringFlag{
Name: "listen",
Value: ":80",
Usage: "Address to listen to (TCP)",
},
cli.StringFlag{
Name: "listenReload",
Value: "127.0.0.1:8112",
Usage: "Address to listen to for reload requests (TCP)",
},
cli.StringFlag{
Name: "answers",
Value: "./answers.json",
Usage: "File containing the answers to respond with",
},
cli.StringFlag{
Name: "log",
Value: "",
Usage: "Log file",
},
cli.StringFlag{
Name: "pid-file",
Value: "",
Usage: "PID to write to",
},
cli.BoolFlag{
Name: "subscribe",
Usage: "Subscribe to Rancher events",
},
cli.Int64Flag{
Name: "reload-interval-limit",
Usage: "Limits reload to 1 per interval (milliseconds)",
Value: 1000,
},
}
return app
}
func appMain(ctx *cli.Context) error {
if ctx.GlobalBool("debug") {
log.SetLevelString("debug")
}
logFile := ctx.GlobalString("log")
if logFile != "" {
if output, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err != nil {
log.Fatalf("Failed to log to file %s: %v", logFile, err)
} else {
log.SetOutput(output)
}
}
pidFile := ctx.GlobalString("pid-file")
if pidFile != "" {
log.Infof("Writing pid %d to %s", os.Getpid(), pidFile)
if err := ioutil.WriteFile(pidFile, []byte(strconv.Itoa(os.Getpid())), 0644); err != nil {
log.Fatalf("Failed to write pid file %s: %v", pidFile, err)
}
}
sc := NewServerConfig(
ctx.GlobalString("listen"),
ctx.GlobalString("listenReload"),
ctx.GlobalBool("xff"),
ctx.GlobalBool("subscribe"),
ctx.GlobalString("answers"),
ctx.Int64("reload-interval-limit"),
)
if err := sc.StartServer(); err != nil {
return err
}
// Run the server
sc.RunServer()
return nil
}
func (sc *ServerConfig) StartServer() error {
if err := sc.metadataController.Start(); err != nil {
return err
}
go func() {
log.Info(http.ListenAndServe(":6060", nil))
}()
return nil
}
func NewServerConfig(listen, listenReload string, enableXff bool, subscribe bool, answers string, reloadInterval int64) *ServerConfig {
router := mux.NewRouter()
reloadRouter := mux.NewRouter()
reloadChan := make(chan chan error)
return &ServerConfig{
listen: listen,
listenReload: listenReload,
enableXff: enableXff,
router: router,
reloadRouter: reloadRouter,
reloadChan: reloadChan,
metadataController: server.NewMetadataController(subscribe, answers, reloadInterval),
}
}
func (sc *ServerConfig) watchSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
go func() {
for _ = range c {
log.Info("Received HUP signal")
sc.reloadChan <- nil
}
}()
go func() {
for resp := range sc.reloadChan {
err := sc.metadataController.LoadVersionsFromFile()
if resp != nil {
resp <- err
}
}
}()
}
func (sc *ServerConfig) watchHttp() {
sc.reloadRouter.HandleFunc("/favicon.ico", http.NotFound)
sc.reloadRouter.HandleFunc("/v1/reload", sc.httpReload).Methods("POST")
log.Info("Listening for Reload on ", sc.listenReload)
go http.ListenAndServe(sc.listenReload, sc.reloadRouter)
}
func (sc *ServerConfig) RunServer() {
sc.watchSignals()
sc.watchHttp()
sc.router.HandleFunc("/favicon.ico", http.NotFound)
sc.router.HandleFunc("/", sc.root).
Methods("GET", "HEAD").
Name("Root")
sc.router.HandleFunc("/{version}", sc.metadata).
Methods("GET", "HEAD").
Name("Version")
sc.router.HandleFunc("/{version}/{key:.*}", sc.metadata).
Queries("wait", "true", "value", "{oldValue}").
Methods("GET", "HEAD").
Name("Wait")
sc.router.HandleFunc("/{version}/{key:.*}", sc.metadata).
Methods("GET", "HEAD").
Name("Metadata")
log.Info("Listening on ", sc.listen)
log.Fatal(http.ListenAndServe(sc.listen, sc.router))
}
func (sc *ServerConfig) httpReload(w http.ResponseWriter, req *http.Request) {
log.Debugf("Received HTTP reload request")
respChan := make(chan error)
sc.reloadChan <- respChan
err := <-respChan
if err == nil {
io.WriteString(w, "OK")
} else {
w.WriteHeader(500)
io.WriteString(w, err.Error())
}
}
func contentType(req *http.Request) int {
str := httputil.NegotiateContentType(req, []string{
"text/plain",
"application/json",
"application/yaml",
"application/x-yaml",
"text/yaml",
"text/x-yaml",
}, "text/plain")
if strings.Contains(str, "json") {
return ContentJSON
} else if strings.Contains(str, "yaml") {
return ContentYAML
} else {
return ContentText
}
}
func (sc *ServerConfig) root(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
log.Debugf("OK: %s client=%v version=%v", "/", sc.requestIp(req), "root")
answers := sc.metadataController.GetVersions()
m := make(map[string]interface{})
for _, k := range answers.Versions() {
url, err := sc.router.Get("Version").URL("version", k)
if err == nil {
m[k] = (*url).String()
} else {
log.Warn("Error: ", err.Error())
}
}
// If latest isn't in the list, pretend it is
_, ok := m["latest"]
if !ok {
url, err := sc.router.Get("Version").URL("version", "latest")
if err == nil {
m["latest"] = (*url).String()
} else {
log.Warn("Error: ", err.Error())
}
}
respondSuccess(w, req, m)
}
func (sc *ServerConfig) metadata(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
vars := mux.Vars(req)
clientIp := sc.requestIp(req)
version := vars["version"]
wait := mux.CurrentRoute(req).GetName() == "Wait"
oldValue := vars["oldValue"]
maxWait, _ := strconv.Atoi(req.URL.Query().Get("maxWait"))
answers := sc.metadataController.GetVersions()
_, ok := answers[version]
if !ok {
// If a `latest` key is not provided, pick the ASCII-betically highest version and call it that.
if version == "latest" {
version = ""
for _, k := range answers.Versions() {
if k > version {
version = k
}
}
log.Debugf("Picked %s for latest version because none provided", version)
} else {
respondError(w, req, "Invalid version", http.StatusNotFound)
return
}
}
path := strings.TrimRight(req.URL.EscapedPath()[1:], "/")
pathSegments := strings.Split(path, "/")[1:]
displayKey := ""
var err error
for i := 0; err == nil && i < len(pathSegments); i++ {
displayKey += "/" + pathSegments[i]
pathSegments[i], err = url.QueryUnescape(pathSegments[i])
}
if err != nil {
respondError(w, req, err.Error(), http.StatusBadRequest)
return
}
log.Debugf("Searching for: %s version=%v client=%v wait=%v oldValue=%v maxWait=%v", displayKey, version, clientIp, wait, oldValue, maxWait)
val, ok := sc.metadataController.LookupAnswer(wait, oldValue, version, clientIp, pathSegments, time.Duration(maxWait)*time.Second)
if ok {
log.Debugf("OK: %s version=%v client=%v", displayKey, version, clientIp)
respondSuccess(w, req, val)
} else {
log.Infof("Error: %s version=%v client=%v", displayKey, version, clientIp)
respondError(w, req, "Not found", http.StatusNotFound)
}
}
func respondError(w http.ResponseWriter, req *http.Request, msg string, statusCode int) {
obj := make(map[string]interface{})
obj["message"] = msg
obj["type"] = "error"
obj["code"] = statusCode
switch contentType(req) {
case ContentText:
http.Error(w, msg, statusCode)
case ContentJSON:
bytes, err := json.Marshal(obj)
if err == nil {
http.Error(w, string(bytes), statusCode)
} else {
http.Error(w, "{\"type\": \"error\", \"message\": \"JSON marshal error\"}", http.StatusInternalServerError)
}
case ContentYAML:
bytes, err := yaml.Marshal(obj)
if err == nil {
http.Error(w, string(bytes), statusCode)
} else {
http.Error(w, "type: \"error\"\nmessage: \"JSON marshal error\"", http.StatusInternalServerError)
}
}
}
func respondSuccess(w http.ResponseWriter, req *http.Request, val interface{}) {
switch contentType(req) {
case ContentText:
respondText(w, req, val)
case ContentJSON:
respondJSON(w, req, val)
case ContentYAML:
respondYAML(w, req, val)
}
}
func respondText(w http.ResponseWriter, req *http.Request, val interface{}) {
if val == nil {
fmt.Fprint(w, "")
return
}
switch v := val.(type) {
case string, json.Number:
fmt.Fprint(w, v)
case uint, uint8, uint16, uint32, uint64, int, int8, int16, int32, int64:
fmt.Fprintf(w, "%d", v)
case float64:
// The default format has extra trailing zeros
str := strings.TrimRight(fmt.Sprintf("%f", v), "0")
str = strings.TrimRight(str, ".")
fmt.Fprint(w, str)
case bool:
if v {
fmt.Fprint(w, "true")
} else {
fmt.Fprint(w, "false")
}
case map[string]interface{}:
out := make([]string, len(v))
i := 0
for k, vv := range v {
_, isMap := vv.(map[string]interface{})
_, isArray := vv.([]interface{})
if isMap || isArray {
out[i] = fmt.Sprintf("%s/\n", url.QueryEscape(k))
} else {
out[i] = fmt.Sprintf("%s\n", url.QueryEscape(k))
}
i++
}
sort.Strings(out)
for _, vv := range out {
fmt.Fprint(w, vv)
}
case []interface{}:
outer:
for k, vv := range v {
vvMap, isMap := vv.(map[string]interface{})
_, isArray := vv.([]interface{})
if isMap {
// If the child is a map and has a "name" property, show index=name ("0=foo")
for _, magicKey := range config.MAGIC_ARRAY_KEYS {
name, ok := vvMap[magicKey]
if ok {
fmt.Fprintf(w, "%d=%s\n", k, url.QueryEscape(name.(string)))
continue outer
}
}
}
if isMap || isArray {
// If the child is a map or array, show index ("0/")
fmt.Fprintf(w, "%d/\n", k)
} else {
// Otherwise, show index ("0" )
fmt.Fprintf(w, "%d\n", k)
}
}
default:
http.Error(w, "Value is of a type I don't know how to handle", http.StatusInternalServerError)
}
}
func respondJSON(w http.ResponseWriter, req *http.Request, val interface{}) {
bytes, err := json.Marshal(val)
if err == nil {
w.Write(bytes)
} else {
respondError(w, req, "Error serializing to JSON: "+err.Error(), http.StatusInternalServerError)
}
}
func respondYAML(w http.ResponseWriter, req *http.Request, val interface{}) {
bytes, err := yaml.Marshal(val)
if err == nil {
w.Write(bytes)
} else {
respondError(w, req, "Error serializing to YAML: "+err.Error(), http.StatusInternalServerError)
}
}
func (sc *ServerConfig) requestIp(req *http.Request) string {
if sc.enableXff {
clientIp := req.Header.Get("X-Forwarded-For")
if len(clientIp) > 0 {
return clientIp
}
}
clientIp, _, _ := net.SplitHostPort(req.RemoteAddr)
return clientIp
}