This repository has been archived by the owner on May 26, 2021. It is now read-only.
forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
haproxy.go
283 lines (244 loc) · 6.23 KB
/
haproxy.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
/*
Copyright 2015 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package haproxy
import (
"crypto/md5"
"fmt"
"io"
"sort"
"strings"
"k8s.io/contrib/ingress/controllers/haproxy/pkg/balancer"
)
const (
// Global
maxconn = 4096
maxpipes = 2048
spreadChecks = 5
debug = false
// Defaults
mode = "http"
balance = "roundrobin"
maxconnUpstream = 2048
tcpLog = true
httpLog = false
abortOnClose = true
httpServerClose = true
forwardFor = true
retries = 3
redispatch = true
timeoutConnect = "5s"
timeoutClient = "30s"
timeoutServer = "30s"
timeoutCheck = "5s"
dontLogNull = true
// Stats
statsEnable = false
statsAuth = "admin:youshouldntbeusingth1$.passwrd"
serverCheckInter = 10000
)
// HAProxy balancer instance
type HAProxy struct {
Global Global
Defaults Defaults
Frontends map[int]FrontEnd
Backends map[string]Backend
CertsDir string
}
// FrontEnd listening and request parsing
type FrontEnd struct {
Name string
Bind Bind
ACLs map[string]ACL
DefaultBackend UseBackend
UseBackends []UseBackend
Opts []string
}
// UseBackendsByPrio returns ordered use_backend by priority
func (fe FrontEnd) UseBackendsByPrio() []UseBackend {
sort.Sort(UseBackends(fe.UseBackends))
return fe.UseBackends
}
// Bind ip port and certificates
type Bind struct {
IP string
Port int
// TODO part away from balancer.Certs and create a struct that also holds the filename
Certs []balancer.Certificate
}
// IsTLS retuns true if some certificates need to be configured for the binded port
func (b Bind) IsTLS() bool {
return len(b.Certs) != 0
}
// func (b *Bind) CertFiles() []string {
// certs := []string{}
// for _, c := range b.Certs{
// certs =
// }
// }
// ACL from request
type ACL struct {
Name string
Content string
}
// NewHostNameACL returns a host check ACL
func NewHostNameACL(h string) *ACL {
return &ACL{
Content: fmt.Sprintf("hdr(host) -i %s", h),
Name: convertToValidName("ishost_" + h),
}
}
// NewPathACL returns a path check ACL
func NewPathACL(p string) *ACL {
return &ACL{
Content: fmt.Sprintf("path_beg %s", p),
Name: convertToValidName("ispath_" + p),
}
}
// Backend servers group
type Backend struct {
Servers map[string]Server
}
// TODO delete!
// Name accourding to backend contents
func (b *Backend) Name() string {
h := md5.New()
io.WriteString(h, fmt.Sprintf("%#v", b.Servers))
return fmt.Sprintf("%x", h.Sum(nil))
}
// Server for backend
type Server struct {
Address string
Port int
CheckInter int
Opts []string
}
// Name according to Server contents
func (s *Server) Name() string {
return convertToValidName(fmt.Sprintf("%s_port_%d", s.Address, s.Port))
}
// UseBackend associates backends and ACLs
type UseBackend struct {
Priority int
Backend string
ACLs []ACL
}
// UseBackends holds a sortable collection of UseBackend
type UseBackends []UseBackend
// Len of use_backends
func (u UseBackends) Len() int {
return len(u)
}
// Swap use backends
func (u UseBackends) Swap(i, j int) {
u[i], u[j] = u[j], u[i]
}
// Less compares use_backend priotities
func (u UseBackends) Less(i, j int) bool {
return u[i].Priority < u[j].Priority
}
// Certs certificates
type Certs struct {
Name string
Content string
}
// Global HAProxy section
type Global struct {
// maximum concurrent connections
Maxconn int
// maximum number of pipes for kernel splicing
Maxpipes int
// Randomness for health checks intervals
SpreadChecks int
// Activate verbose logging
Debug bool
// placeholder for other global parameters
Others map[string]string
}
// Defaults HAProxy section
type Defaults struct {
// running mode for HAProxy
Mode string
// default load balancing strategy
Balance string
// maximum number of connections for an upstream server
Maxconn int
// enables rich TCP log
TCPLog bool
// enables rich HTTP log
HTTPLog bool
// early drop of aborted requests
AbortOnClose bool
// enables connection close, allowing also keep-alive
HTTPServerClose bool
// adds X-Forwarded-For header
ForwardFor bool
// number of retries to servers
Retries int
// break affinity when the upstream server is down
Redispatch bool
// timeout for connections to an upstream server
TimeoutConnect string
// timeout for client inactivity
TimeoutClient string
// timeout for server inactivity
TimeoutServer string
// avoid logging empty data connections
DontLogNull bool
// timeout for health checks
TimeoutCheck string
// placeholder for other global parameters
Others map[string]string
}
// NewDefaultBalancer returns an HAProxy balancer
func NewDefaultBalancer() *HAProxy {
ha := &HAProxy{
Global: Global{
Maxconn: maxconn,
Maxpipes: maxpipes,
SpreadChecks: spreadChecks,
Debug: debug,
},
Defaults: Defaults{
Mode: mode,
Balance: balance,
Maxconn: maxconnUpstream,
TCPLog: tcpLog,
HTTPLog: httpLog,
AbortOnClose: abortOnClose,
HTTPServerClose: httpServerClose,
ForwardFor: forwardFor,
Retries: retries,
Redispatch: redispatch,
TimeoutConnect: timeoutConnect,
TimeoutClient: timeoutClient,
TimeoutServer: timeoutServer,
DontLogNull: dontLogNull,
TimeoutCheck: timeoutCheck,
},
}
ha.Backends = make(map[string]Backend)
ha.Frontends = make(map[int]FrontEnd)
return ha
}
// NewDefaultBackendServer returns a backend server
func NewDefaultBackendServer() *Server {
return &Server{
CheckInter: serverCheckInter,
}
}
// convertToValidName replace non valid characters in config names
func convertToValidName(s string) string {
s = strings.TrimSpace(s)
r := strings.NewReplacer("/", "_", ".", "_")
return r.Replace(s)
}