forked from openshift/geard
/
routes.go
220 lines (196 loc) · 4.43 KB
/
routes.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
package router
import (
"encoding/json"
"fmt"
"io/ioutil"
"os/exec"
"strconv"
"time"
)
const (
ProtocolHttp = "http"
ProtocolHttps = "https"
ProtocolTls = "tls"
)
const (
TERM_EDGE = "TERM_EDGE"
TERM_GEAR = "TERM_GEAR"
TERM_RESSL = "TERM_RESSL"
)
const (
RouteFile = "/var/lib/containers/router/routes.json"
)
type Frontend struct {
Name string
HostAliases []string
BeTable map[string]Backend
EndpointTable map[string]Endpoint
}
type Backend struct {
Id string
FePath string
BePath string
Protocols []string
EndpointIds []string
SslTerm string
Certificates []Certificate
}
type Certificate struct {
Id string
Contents []byte
PrivateKey []byte
PrivateKeyPassword string
}
type Endpoint struct {
Id string
IP string
Port string
}
var GlobalRoutes map[string]Frontend
func makeId() string {
var s string
s = strconv.FormatInt(time.Now().UnixNano(), 16)
return s
}
func PrintRoutes() string {
dat, err := ioutil.ReadFile(RouteFile)
var s string
if err != nil {
s = fmt.Sprintf("Error reading routes file : %s", err.Error())
} else {
s = fmt.Sprintf("%s", string(dat))
}
return s
}
func PrintFrontendRoutes(frontendname string) string {
dat, err := json.MarshalIndent(GlobalRoutes[frontendname], "", " ")
if err != nil {
fmt.Println("Failed to marshal routes - %s", err.Error())
}
return string(dat)
}
func ReadRoutes() {
//fmt.Printf("Reading routes file (%s)\n", RouteFile)
dat, err := ioutil.ReadFile(RouteFile)
if err != nil {
GlobalRoutes = make(map[string]Frontend)
return
}
json.Unmarshal(dat, &GlobalRoutes)
}
func WriteRoutes() {
dat, err := json.MarshalIndent(GlobalRoutes, "", " ")
if err != nil {
fmt.Println("Failed to marshal routes - %s", err.Error())
}
err = ioutil.WriteFile(RouteFile, dat, 0644)
if err != nil {
fmt.Println("Failed to write to routes file - %s", err.Error())
}
}
func (a *Frontend) Init() {
// a.HostAliases = make([]string)
// a.Certificates = make([]Certificate)
if a.BeTable == nil {
a.BeTable = make(map[string]Backend)
}
if a.EndpointTable == nil {
a.EndpointTable = make(map[string]Endpoint)
}
}
func CreateFrontend(name string, url string) {
a := Frontend{}
a.Init()
a.Name = name
a.HostAliases = make([]string, 1)
if url != "" {
a.HostAliases[0] = url
}
GlobalRoutes[a.Name] = a
WriteRoutes()
}
func DeleteFrontend(frontendname string) {
delete(GlobalRoutes, frontendname)
WriteRoutes()
BumpRouter()
}
func AddAlias(alias string, frontendname string) {
a := GlobalRoutes[frontendname]
a.HostAliases = append(a.HostAliases, alias)
GlobalRoutes[frontendname] = a
WriteRoutes()
}
func AddRoute(frontendname string, fe_path string, be_path string, protocols []string, endpoints []Endpoint) {
var id string
a := GlobalRoutes[frontendname]
a.Init()
ep_ids := make([]string, 1)
for new_ep_id := range endpoints {
new_endpoint := endpoints[new_ep_id]
found := false
for _, ep := range a.EndpointTable {
if ep.IP == new_endpoint.IP && ep.Port == new_endpoint.Port {
ep_ids = append(ep_ids, ep.Id)
found = true
break
}
}
if !found {
id = makeId()
ep := Endpoint{id, new_endpoint.IP, new_endpoint.Port}
a.EndpointTable[id] = ep
ep_ids = append(ep_ids, ep.Id)
}
}
// locate a backend that may already exist with this protocol and fe/be path
found := false
for _, be := range a.BeTable {
if be.FePath == fe_path && be.BePath == be_path && cmpStrSlices(protocols, be.Protocols) {
for _, ep_id := range ep_ids {
be.EndpointIds = append(be.EndpointIds, ep_id)
}
a.BeTable[be.Id] = be
found = true
break
}
}
if !found {
id = makeId()
a.BeTable[id] = Backend{id, fe_path, be_path, protocols, ep_ids, TERM_EDGE, nil}
}
GlobalRoutes[a.Name] = a
WriteRoutes()
BumpRouter()
}
func cmpStrSlices(first []string, second []string) bool {
if len(first) != len(second) {
return false
}
for _, fi := range first {
found := false
for _, si := range second {
if fi == si {
found = true
break
}
}
if !found {
return false
}
}
return true
}
func (a Frontend) PrintOut() {
fmt.Println(GlobalRoutes)
}
func BumpRouter() {
out, err := exec.Command("docker", "kill", "-s", "SIGUSR2", "geard-router").CombinedOutput()
if err != nil {
fmt.Println(string(out))
fmt.Printf("Failed to reload the router - %s\n", err.Error())
fmt.Println("Router plugin not installed?")
}
}
func init() {
ReadRoutes()
}