-
Notifications
You must be signed in to change notification settings - Fork 27
/
controller.go
256 lines (231 loc) · 6.63 KB
/
controller.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
// Package controller provides REST API to configure balancer
//
// controller API
//
// - Authentication
// Basic HTTP Auth
//
// - Stats
// GET http://{controller_address}/stats
//
// - List All LB instance
// GET http://{controller_address}/vs
//
// - Add LB instance
// POST http://{controller_address}/vs
// Body {"name":"redis","address":"127.0.0.1:6379"}
// Example: curl -XPOST -u admin:admin -H 'content-type: application/json' -d '{"name":"redis","address":"127.0.0.1:6379"}' http://127.0.0.1:6587/vs
//
// - Enable LB instance
// POST http://{controller_address}/vs/{name}
// Body {"action":"enable"}
//
// - Disable LB instance
// POST http://{controller_address}/vs/{name}
// Body {"action":"disable"}
//
// - List pool member of LB instance
// GET http://{controller_address}/vs/{name}
//
// - Add pool member to LB instance
// POST http://{controller_address}/vs/{name}/pool
// Body: {"address":"127.0.0.1:10003","weight":2}
// Example: curl -XPOST -u admin:admin -H 'content-type: application/json' -d '{"address":"127.0.0.1:10003"}' http://127.0.0.1:6587/vs/web/pool
//
// - Remove pool member from LB instance
// DELETE http://{controller_address}/vs/{name}/pool
// Body: {"address":"127.0.0.1:10002"}
// Example: curl -XDELETE -u admin:admin -H 'content-type: application/json' -d '{"address":"127.0.0.1:10002"}' http://127.0.0.1:6587/vs/web/pool
//
package controller
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/onestraw/golb/balancer"
"github.com/onestraw/golb/config"
)
// Controller provides interface to operate balancer.
type Controller struct {
Address string
Auth *authentication
}
// New returns a Controller object.
func New(ctlCfg *config.Controller) *Controller {
return &Controller{
Address: ctlCfg.Address,
Auth: &authentication{ctlCfg.Auth.Username, ctlCfg.Auth.Password},
}
}
// Run starts the controller.
func (c *Controller) Run(balancer *balancer.Balancer) {
r := mux.NewRouter()
r.Handle("/stats", statsHandler(balancer)).Methods("GET")
r.Handle("/vs", addVirtualServer(balancer)).Methods("POST")
r.Handle("/vs", listAllVirtualServer(balancer)).Methods("GET")
r.Handle("/vs/{name}", modifyVirtualServerStatus(balancer)).Methods("POST")
r.Handle("/vs/{name}", listVirtualServer(balancer)).Methods("GET")
r.Handle("/vs/{name}/pool", addPoolMember(balancer)).Methods("POST")
r.Handle("/vs/{name}/pool", deletePoolMember(balancer)).Methods("DELETE")
go func() {
if err := http.ListenAndServe(c.Address, BasicAuth(c.Auth)(r)); err != nil {
panic(err)
}
}()
}
// writeBadRequest writes the error with 400 to http.ResponseWriter.
func writeBadRequest(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
}
func statsHandler(b *balancer.Balancer) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
result := []string{}
for _, vs := range b.VServers {
s := vs.Stats()
log.Infof(s)
result = append(result, s)
}
io.WriteString(w, strings.Join(result, "\n"))
})
}
func listAllVirtualServer(b *balancer.Balancer) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, vs := range b.VServers {
data := fmt.Sprintf("Name:%s, Address:%s, Status:%s, Pool:\n%s\n\n",
vs.Name, vs.Address, vs.Status(), vs.Pool)
io.WriteString(w, data)
}
})
}
func listVirtualServer(b *balancer.Balancer) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
vs, err := b.FindVirtualServer(name)
if err != nil {
log.Errorf("FindVirtualServer err=%v", err)
writeBadRequest(w, err)
return
}
msg := vs.Pool.String()
io.WriteString(w, msg)
})
}
type modifyVirtualServer struct {
Action string `json:"action"`
}
var errUnknownAction = errors.New("unknown action")
func modifyVirtualServerStatus(b *balancer.Balancer) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
var req modifyVirtualServer
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&req); err != nil {
log.Errorf("Decode request err=%v", err)
writeBadRequest(w, err)
return
}
action := req.Action
log.Infof("virtual server name %s, action %s", name, action)
msg := "success"
vs, err := b.FindVirtualServer(name)
if err != nil {
log.Errorf("FindVirtualServer err=%v", err)
writeBadRequest(w, err)
return
}
if action == "enable" {
if err := vs.Run(); err != nil {
msg = err.Error()
}
} else if action == "disable" {
if err := vs.Stop(); err != nil {
msg = err.Error()
}
} else {
log.Errorf("%v", errUnknownAction)
writeBadRequest(w, errUnknownAction)
return
}
io.WriteString(w, msg)
})
}
func addVirtualServer(b *balancer.Balancer) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var vs config.VirtualServer
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&vs)
if err != nil {
log.Errorf("Decode request err=%v", err)
writeBadRequest(w, err)
return
}
log.Infof("VirtualServer %v", vs)
err = b.AddVirtualServer(&vs)
if err != nil {
log.Errorf("AddVirtualServer err=%v", err)
writeBadRequest(w, err)
return
}
io.WriteString(w, "Add success")
})
}
func decodeServer(r *http.Request) (*config.Server, error) {
var server config.Server
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&server)
if err != nil {
log.Errorf("Decode request err=%v", err)
return nil, err
}
return &server, nil
}
func addPoolMember(b *balancer.Balancer) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
vs, err := b.FindVirtualServer(name)
if err != nil {
log.Errorf("FindVirtualServer err=%v", err)
writeBadRequest(w, err)
return
}
server, err := decodeServer(r)
if err != nil {
writeBadRequest(w, err)
return
}
weight := server.Weight
if weight <= 0 {
weight = 1
}
vs.AddPeer(server.Address, weight)
io.WriteString(w, "Add peer success")
})
}
func deletePoolMember(b *balancer.Balancer) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
vs, err := b.FindVirtualServer(name)
if err != nil {
log.Errorf("FindVirtualServer err=%v", err)
writeBadRequest(w, err)
return
}
server, err := decodeServer(r)
if err != nil {
writeBadRequest(w, err)
return
}
vs.RemovePeer(server.Address)
io.WriteString(w, "Remove peer success")
})
}