forked from nginx/kubernetes-ingress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigurator.go
401 lines (329 loc) · 11.9 KB
/
configurator.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
package nginx
import (
"fmt"
"strings"
"sync"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/extensions"
)
const emptyHost = ""
// Configurator transforms an Ingress resource into NGINX Configuration
type Configurator struct {
nginx *NginxController
config *Config
lock sync.Mutex
}
// NewConfigurator creates a new Configurator
func NewConfigurator(nginx *NginxController, config *Config) *Configurator {
cnf := Configurator{
nginx: nginx,
config: config,
}
return &cnf
}
func (cnf *Configurator) AddOrUpdateDHParam(content string) (string, error) {
return cnf.nginx.AddOrUpdateDHParam(content)
}
// AddOrUpdateIngress adds or updates NGINX configuration for an Ingress resource
func (cnf *Configurator) AddOrUpdateIngress(name string, ingEx *IngressEx) {
cnf.lock.Lock()
defer cnf.lock.Unlock()
pems := cnf.updateCertificates(ingEx)
nginxCfg := cnf.generateNginxCfg(ingEx, pems)
cnf.nginx.AddOrUpdateIngress(name, nginxCfg)
if err := cnf.nginx.Reload(); err != nil {
glog.Errorf("Error when adding or updating ingress %q: %q", name, err)
}
}
func (cnf *Configurator) updateCertificates(ingEx *IngressEx) map[string]string {
pems := make(map[string]string)
for _, tls := range ingEx.Ingress.Spec.TLS {
secretName := tls.SecretName
secret, exist := ingEx.Secrets[secretName]
if !exist {
continue
}
cert, ok := secret.Data[api.TLSCertKey]
if !ok {
glog.Warningf("Secret %v has no private key", secretName)
continue
}
key, ok := secret.Data[api.TLSPrivateKeyKey]
if !ok {
glog.Warningf("Secret %v has no cert", secretName)
continue
}
pemFileName := cnf.nginx.AddOrUpdateCertAndKey(secretName, string(cert), string(key))
for _, host := range tls.Hosts {
pems[host] = pemFileName
}
if len(tls.Hosts) == 0 {
pems[emptyHost] = pemFileName
}
}
return pems
}
func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]string) IngressNginxConfig {
ingCfg := cnf.createConfig(ingEx)
upstreams := make(map[string]Upstream)
wsServices := getWebsocketServices(ingEx)
rewrites := getRewrites(ingEx)
sslServices := getSSLServices(ingEx)
if ingEx.Ingress.Spec.Backend != nil {
name := getNameForUpstream(ingEx.Ingress, emptyHost, ingEx.Ingress.Spec.Backend.ServiceName)
upstream := cnf.createUpstream(ingEx, name, ingEx.Ingress.Spec.Backend, ingEx.Ingress.Namespace)
upstreams[name] = upstream
}
var servers []Server
for _, rule := range ingEx.Ingress.Spec.Rules {
if rule.IngressRuleValue.HTTP == nil {
continue
}
serverName := rule.Host
if rule.Host == emptyHost {
glog.Warningf("Host field of ingress rule in %v/%v is empty", ingEx.Ingress.Namespace, ingEx.Ingress.Name)
}
server := Server{
Name: serverName,
HTTP2: ingCfg.HTTP2,
HSTS: ingCfg.HSTS,
HSTSMaxAge: ingCfg.HSTSMaxAge,
HSTSIncludeSubdomains: ingCfg.HSTSIncludeSubdomains,
}
if pemFile, ok := pems[serverName]; ok {
server.SSL = true
server.SSLCertificate = pemFile
server.SSLCertificateKey = pemFile
}
var locations []Location
rootLocation := false
for _, path := range rule.HTTP.Paths {
upsName := getNameForUpstream(ingEx.Ingress, rule.Host, path.Backend.ServiceName)
if _, exists := upstreams[upsName]; !exists {
upstream := cnf.createUpstream(ingEx, upsName, &path.Backend, ingEx.Ingress.Namespace)
upstreams[upsName] = upstream
}
loc := createLocation(pathOrDefault(path.Path), upstreams[upsName], &ingCfg, wsServices[path.Backend.ServiceName], rewrites[path.Backend.ServiceName], sslServices[path.Backend.ServiceName])
locations = append(locations, loc)
if loc.Path == "/" {
rootLocation = true
}
}
if rootLocation == false && ingEx.Ingress.Spec.Backend != nil {
upsName := getNameForUpstream(ingEx.Ingress, emptyHost, ingEx.Ingress.Spec.Backend.ServiceName)
loc := createLocation(pathOrDefault("/"), upstreams[upsName], &ingCfg, wsServices[ingEx.Ingress.Spec.Backend.ServiceName], rewrites[ingEx.Ingress.Spec.Backend.ServiceName], sslServices[ingEx.Ingress.Spec.Backend.ServiceName])
locations = append(locations, loc)
}
server.Locations = locations
servers = append(servers, server)
}
if len(ingEx.Ingress.Spec.Rules) == 0 && ingEx.Ingress.Spec.Backend != nil {
server := Server{
Name: emptyHost,
HTTP2: ingCfg.HTTP2,
HSTS: ingCfg.HSTS,
HSTSMaxAge: ingCfg.HSTSMaxAge,
HSTSIncludeSubdomains: ingCfg.HSTSIncludeSubdomains,
}
if pemFile, ok := pems[emptyHost]; ok {
server.SSL = true
server.SSLCertificate = pemFile
server.SSLCertificateKey = pemFile
}
var locations []Location
upsName := getNameForUpstream(ingEx.Ingress, emptyHost, ingEx.Ingress.Spec.Backend.ServiceName)
loc := createLocation(pathOrDefault("/"), upstreams[upsName], &ingCfg, wsServices[ingEx.Ingress.Spec.Backend.ServiceName], rewrites[ingEx.Ingress.Spec.Backend.ServiceName], sslServices[ingEx.Ingress.Spec.Backend.ServiceName])
locations = append(locations, loc)
server.Locations = locations
servers = append(servers, server)
}
return IngressNginxConfig{Upstreams: upstreamMapToSlice(upstreams), Servers: servers}
}
func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
ingCfg := *cnf.config
if proxyConnectTimeout, exists := ingEx.Ingress.Annotations["nginx.org/proxy-connect-timeout"]; exists {
ingCfg.ProxyConnectTimeout = proxyConnectTimeout
}
if proxyReadTimeout, exists := ingEx.Ingress.Annotations["nginx.org/proxy-read-timeout"]; exists {
ingCfg.ProxyReadTimeout = proxyReadTimeout
}
if clientMaxBodySize, exists := ingEx.Ingress.Annotations["nginx.org/client-max-body-size"]; exists {
ingCfg.ClientMaxBodySize = clientMaxBodySize
}
if HTTP2, exists, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/http2", ingEx.Ingress); exists {
if err != nil {
glog.Error(err)
} else {
ingCfg.HTTP2 = HTTP2
}
}
if proxyBuffering, exists, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/proxy-buffering", ingEx.Ingress); exists {
if err != nil {
glog.Error(err)
} else {
ingCfg.ProxyBuffering = proxyBuffering
}
}
if hsts, exists, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/hsts", ingEx.Ingress); exists {
if err != nil {
glog.Error(err)
} else {
parsingErrors := false
hstsMaxAge, existsMA, err := GetMapKeyAsInt(ingEx.Ingress.Annotations, "nginx.org/hsts-max-age", ingEx.Ingress)
if existsMA && err != nil {
glog.Error(err)
parsingErrors = true
}
hstsIncludeSubdomains, existsIS, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/hsts-include-subdomains", ingEx.Ingress)
if existsIS && err != nil {
glog.Error(err)
parsingErrors = true
}
if parsingErrors {
glog.Errorf("Ingress %s/%s: There are configuration issues with hsts annotations, skipping annotions for all hsts settings", ingEx.Ingress.GetNamespace(), ingEx.Ingress.GetName())
} else {
ingCfg.HSTS = hsts
if existsMA {
ingCfg.HSTSMaxAge = hstsMaxAge
}
if existsIS {
ingCfg.HSTSIncludeSubdomains = hstsIncludeSubdomains
}
}
}
}
if proxyBuffers, exists := ingEx.Ingress.Annotations["nginx.org/proxy-buffers"]; exists {
ingCfg.ProxyBuffers = proxyBuffers
}
if proxyBufferSize, exists := ingEx.Ingress.Annotations["nginx.org/proxy-buffer-size"]; exists {
ingCfg.ProxyBufferSize = proxyBufferSize
}
if proxyMaxTempFileSize, exists := ingEx.Ingress.Annotations["nginx.org/proxy-max-temp-file-size"]; exists {
ingCfg.ProxyMaxTempFileSize = proxyMaxTempFileSize
}
return ingCfg
}
func getWebsocketServices(ingEx *IngressEx) map[string]bool {
wsServices := make(map[string]bool)
if services, exists := ingEx.Ingress.Annotations["nginx.org/websocket-services"]; exists {
for _, svc := range strings.Split(services, ",") {
wsServices[svc] = true
}
}
return wsServices
}
func getRewrites(ingEx *IngressEx) map[string]string {
rewrites := make(map[string]string)
if services, exists := ingEx.Ingress.Annotations["nginx.org/rewrites"]; exists {
for _, svc := range strings.Split(services, ";") {
if serviceName, rewrite, err := parseRewrites(svc); err != nil {
glog.Errorf("In %v nginx.org/rewrites contains invalid declaration: %v, ignoring", ingEx.Ingress.Name, err)
} else {
rewrites[serviceName] = rewrite
}
}
}
return rewrites
}
func parseRewrites(service string) (serviceName string, rewrite string, err error) {
parts := strings.SplitN(service, " ", 2)
if len(parts) != 2 {
return "", "", fmt.Errorf("Invalid rewrite format: %s\n", service)
}
svcNameParts := strings.Split(parts[0], "=")
if len(svcNameParts) != 2 {
return "", "", fmt.Errorf("Invalid rewrite format: %s\n", svcNameParts)
}
rwPathParts := strings.Split(parts[1], "=")
if len(rwPathParts) != 2 {
return "", "", fmt.Errorf("Invalid rewrite format: %s\n", rwPathParts)
}
return svcNameParts[1], rwPathParts[1], nil
}
func getSSLServices(ingEx *IngressEx) map[string]bool {
sslServices := make(map[string]bool)
if services, exists := ingEx.Ingress.Annotations["nginx.org/ssl-services"]; exists {
for _, svc := range strings.Split(services, ",") {
sslServices[svc] = true
}
}
return sslServices
}
func createLocation(path string, upstream Upstream, cfg *Config, websocket bool, rewrite string, ssl bool) Location {
loc := Location{
Path: path,
Upstream: upstream,
ProxyConnectTimeout: cfg.ProxyConnectTimeout,
ProxyReadTimeout: cfg.ProxyReadTimeout,
ClientMaxBodySize: cfg.ClientMaxBodySize,
Websocket: websocket,
Rewrite: rewrite,
SSL: ssl,
ProxyBuffering: cfg.ProxyBuffering,
ProxyBuffers: cfg.ProxyBuffers,
ProxyBufferSize: cfg.ProxyBufferSize,
ProxyMaxTempFileSize: cfg.ProxyMaxTempFileSize,
}
return loc
}
func (cnf *Configurator) createUpstream(ingEx *IngressEx, name string, backend *extensions.IngressBackend, namespace string) Upstream {
ups := NewUpstreamWithDefaultServer(name)
endps, exists := ingEx.Endpoints[backend.ServiceName+backend.ServicePort.String()]
if exists {
var upsServers []UpstreamServer
for _, endp := range endps {
addressport := strings.Split(endp, ":")
upsServers = append(upsServers, UpstreamServer{addressport[0], addressport[1]})
}
if len(upsServers) > 0 {
ups.UpstreamServers = upsServers
}
}
return ups
}
func pathOrDefault(path string) string {
if path == "" {
return "/"
}
return path
}
func getNameForUpstream(ing *extensions.Ingress, host string, service string) string {
return fmt.Sprintf("%v-%v-%v-%v", ing.Namespace, ing.Name, host, service)
}
func upstreamMapToSlice(upstreams map[string]Upstream) []Upstream {
result := make([]Upstream, 0, len(upstreams))
for _, ups := range upstreams {
result = append(result, ups)
}
return result
}
// DeleteIngress deletes NGINX configuration for an Ingress resource
func (cnf *Configurator) DeleteIngress(name string) {
cnf.lock.Lock()
defer cnf.lock.Unlock()
cnf.nginx.DeleteIngress(name)
if err := cnf.nginx.Reload(); err != nil {
glog.Errorf("Error when removing ingress %q: %q", name, err)
}
}
// UpdateEndpoints updates endpoints in NGINX configuration for an Ingress resource
func (cnf *Configurator) UpdateEndpoints(name string, ingEx *IngressEx) {
cnf.AddOrUpdateIngress(name, ingEx)
}
// UpdateConfig updates NGINX Configuration parameters
func (cnf *Configurator) UpdateConfig(config *Config) {
cnf.lock.Lock()
defer cnf.lock.Unlock()
cnf.config = config
mainCfg := &NginxMainConfig{
ServerNamesHashBucketSize: config.MainServerNamesHashBucketSize,
ServerNamesHashMaxSize: config.MainServerNamesHashMaxSize,
LogFormat: config.MainLogFormat,
SSLProtocols: config.MainServerSSLProtocols,
SSLCiphers: config.MainServerSSLCiphers,
SSLDHParam: config.MainServerSSLDHParam,
SSLPreferServerCiphers: config.MainServerSSLPreferServerCiphers,
}
cnf.nginx.UpdateMainConfigFile(mainCfg)
}