This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marathon-varnish.go
109 lines (93 loc) · 2.12 KB
/
marathon-varnish.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
package main
import (
"encoding/json"
"log"
"os"
"strings"
"text/template"
)
type Task struct {
AppId string `json:"appId"`
Id string `json:"id"`
Host string `json:"host"`
Ports []int `json:"ports"`
StartedAt string `json:"startedAt"`
StagedAt string `json:"stagedAt"`
Version string `json:"version"`
ServicePorts []int
}
func (t Task) Backend() string {
return strings.Replace(strings.Replace(t.Id, ".", "_", -1), "-", "_", -1)
}
func (t Task) Director() string {
return t.AppId
}
func (t Task) FirstPort() int {
return t.Ports[0]
}
func (t Task) DirectorId() string {
return strings.TrimLeft(t.AppId, "/")
}
type TaskList struct {
Tasks []Task `json:"tasks"`
}
type Data struct {
Directors map[string][]Task
Backends []Task
}
func (d *Data) Init(tasks []Task) {
d.Directors = map[string][]Task{}
for _, each := range tasks {
ts, ok := d.Directors[each.DirectorId()]
if !ok {
ts = []Task{}
}
ts = append(ts, each)
d.Directors[each.DirectorId()] = ts
}
d.Backends = tasks
}
var configTemplate = `
{{range .Backends}}
backend {{.Backend}} {
.host = "{{.Host}}";
.port = "{{.FirstPort}}";
.probe = { .url = "/"; .interval = 5s; .timeout = 1s; .window = 5; .threshold = 3; }
}
{{end}}
{{range $k, $v := .Directors}}
director {{$k}} round-robin {
{{range $v}}
{ .backend = {{.Backend}}; }
{{end}}
}
{{end}}
sub vcl_error {
# Restart request flow on status 503
if (obj.status == 503 && req.restarts < 4) {
return (restart);
}
}
sub vcl_recv {
{{range $k, $v := .Directors}}
if (req.http.host == "{{$k}}") {
set req.backend = {{$k}};
return (pass);
}
{{end}}
error 405;
}
`
var config = template.Must(template.New("config").Parse(configTemplate))
func main() {
list := TaskList{}
//strings.NewReader(test)
if err := json.NewDecoder(os.Stdin).Decode(&list); err != nil {
log.Fatal(err)
}
data := Data{}
data.Init(list.Tasks)
if err := config.Execute(os.Stdout, data); err != nil {
log.Fatal(err)
}
}