-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathingress.go
564 lines (470 loc) · 19.3 KB
/
ingress.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package configs
import (
"fmt"
"sort"
"strings"
"github.com/golang/glog"
"github.com/nginxinc/kubernetes-ingress/internal/k8s/secrets"
api_v1 "k8s.io/api/core/v1"
networking "k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/nginxinc/kubernetes-ingress/internal/configs/version1"
)
const emptyHost = ""
const appProtectPolicyKey = "policy"
const appProtectLogConfKey = "logconf"
// IngressEx holds an Ingress along with the resources that are referenced in this Ingress.
type IngressEx struct {
Ingress *networking.Ingress
Endpoints map[string][]string
HealthChecks map[string]*api_v1.Probe
ExternalNameSvcs map[string]bool
PodsByIP map[string]PodInfo
ValidHosts map[string]bool
ValidMinionPaths map[string]bool
AppProtectPolicy *unstructured.Unstructured
AppProtectLogConf *unstructured.Unstructured
AppProtectLogDst string
SecretRefs map[string]*secrets.SecretReference
}
// JWTKey represents a secret that holds JSON Web Key.
type JWTKey struct {
Name string
Secret *api_v1.Secret
}
func (ingEx *IngressEx) String() string {
if ingEx.Ingress == nil {
return "IngressEx has no Ingress"
}
return fmt.Sprintf("%v/%v", ingEx.Ingress.Namespace, ingEx.Ingress.Name)
}
// MergeableIngresses is a mergeable ingress of a master and minions.
type MergeableIngresses struct {
Master *IngressEx
Minions []*IngressEx
}
func generateNginxCfg(ingEx *IngressEx, apResources map[string]string, isMinion bool, baseCfgParams *ConfigParams, isPlus bool,
isResolverConfigured bool, staticParams *StaticConfigParams, isWildcardEnabled bool) version1.IngressNginxConfig {
hasAppProtect := staticParams.MainAppProtectLoadModule
cfgParams := parseAnnotations(ingEx, baseCfgParams, isPlus, hasAppProtect, staticParams.EnableInternalRoutes)
wsServices := getWebsocketServices(ingEx)
spServices := getSessionPersistenceServices(ingEx)
rewrites := getRewrites(ingEx)
sslServices := getSSLServices(ingEx)
grpcServices := getGrpcServices(ingEx)
upstreams := make(map[string]version1.Upstream)
healthChecks := make(map[string]version1.HealthCheck)
// HTTP2 is required for gRPC to function
if len(grpcServices) > 0 && !cfgParams.HTTP2 {
glog.Errorf("Ingress %s/%s: annotation nginx.org/grpc-services requires HTTP2, ignoring", ingEx.Ingress.Namespace, ingEx.Ingress.Name)
grpcServices = make(map[string]bool)
}
if ingEx.Ingress.Spec.Backend != nil {
name := getNameForUpstream(ingEx.Ingress, emptyHost, ingEx.Ingress.Spec.Backend)
upstream := createUpstream(ingEx, name, ingEx.Ingress.Spec.Backend, spServices[ingEx.Ingress.Spec.Backend.ServiceName], &cfgParams,
isPlus, isResolverConfigured, staticParams.EnableLatencyMetrics)
upstreams[name] = upstream
if cfgParams.HealthCheckEnabled {
if hc, exists := ingEx.HealthChecks[ingEx.Ingress.Spec.Backend.ServiceName+ingEx.Ingress.Spec.Backend.ServicePort.String()]; exists {
healthChecks[name] = createHealthCheck(hc, name, &cfgParams)
}
}
}
var servers []version1.Server
for _, rule := range ingEx.Ingress.Spec.Rules {
// skipping invalid hosts
if !ingEx.ValidHosts[rule.Host] {
continue
}
httpIngressRuleValue := rule.HTTP
if httpIngressRuleValue == nil {
// the code in this loop expects non-nil
httpIngressRuleValue = &networking.HTTPIngressRuleValue{}
}
serverName := rule.Host
statusZone := rule.Host
server := version1.Server{
Name: serverName,
ServerTokens: cfgParams.ServerTokens,
HTTP2: cfgParams.HTTP2,
RedirectToHTTPS: cfgParams.RedirectToHTTPS,
SSLRedirect: cfgParams.SSLRedirect,
ProxyProtocol: cfgParams.ProxyProtocol,
HSTS: cfgParams.HSTS,
HSTSMaxAge: cfgParams.HSTSMaxAge,
HSTSIncludeSubdomains: cfgParams.HSTSIncludeSubdomains,
HSTSBehindProxy: cfgParams.HSTSBehindProxy,
StatusZone: statusZone,
RealIPHeader: cfgParams.RealIPHeader,
SetRealIPFrom: cfgParams.SetRealIPFrom,
RealIPRecursive: cfgParams.RealIPRecursive,
ProxyHideHeaders: cfgParams.ProxyHideHeaders,
ProxyPassHeaders: cfgParams.ProxyPassHeaders,
ServerSnippets: cfgParams.ServerSnippets,
Ports: cfgParams.Ports,
SSLPorts: cfgParams.SSLPorts,
TLSPassthrough: staticParams.TLSPassthrough,
AppProtectEnable: cfgParams.AppProtectEnable,
AppProtectLogEnable: cfgParams.AppProtectLogEnable,
SpiffeCerts: cfgParams.SpiffeServerCerts,
}
addSSLConfig(&server, rule.Host, ingEx.Ingress.Spec.TLS, ingEx.SecretRefs, isWildcardEnabled)
if hasAppProtect {
server.AppProtectPolicy = apResources[appProtectPolicyKey]
server.AppProtectLogConf = apResources[appProtectLogConfKey]
}
if !isMinion && cfgParams.JWTKey != "" {
jwtAuth, redirectLoc := generateJWTConfig(ingEx.SecretRefs, &cfgParams, getNameForRedirectLocation(ingEx.Ingress))
server.JWTAuth = jwtAuth
if redirectLoc != nil {
server.JWTRedirectLocations = append(server.JWTRedirectLocations, *redirectLoc)
}
}
var locations []version1.Location
healthChecks := make(map[string]version1.HealthCheck)
rootLocation := false
grpcOnly := true
if len(grpcServices) > 0 {
for _, path := range httpIngressRuleValue.Paths {
if _, exists := grpcServices[path.Backend.ServiceName]; !exists {
grpcOnly = false
break
}
}
} else {
grpcOnly = false
}
for _, path := range httpIngressRuleValue.Paths {
// skip invalid paths for minions
if isMinion && !ingEx.ValidMinionPaths[path.Path] {
continue
}
upsName := getNameForUpstream(ingEx.Ingress, rule.Host, &path.Backend)
if cfgParams.HealthCheckEnabled {
if hc, exists := ingEx.HealthChecks[path.Backend.ServiceName+path.Backend.ServicePort.String()]; exists {
healthChecks[upsName] = createHealthCheck(hc, upsName, &cfgParams)
}
}
if _, exists := upstreams[upsName]; !exists {
upstream := createUpstream(ingEx, upsName, &path.Backend, spServices[path.Backend.ServiceName], &cfgParams, isPlus, isResolverConfigured, staticParams.EnableLatencyMetrics)
upstreams[upsName] = upstream
}
ssl := isSSLEnabled(sslServices[path.Backend.ServiceName], cfgParams, staticParams)
proxySSLName := generateProxySSLName(path.Backend.ServiceName, ingEx.Ingress.Namespace)
loc := createLocation(pathOrDefault(path.Path), upstreams[upsName], &cfgParams, wsServices[path.Backend.ServiceName], rewrites[path.Backend.ServiceName],
ssl, grpcServices[path.Backend.ServiceName], proxySSLName, path.PathType, path.Backend.ServiceName)
if isMinion && cfgParams.JWTKey != "" {
jwtAuth, redirectLoc := generateJWTConfig(ingEx.SecretRefs, &cfgParams, getNameForRedirectLocation(ingEx.Ingress))
loc.JWTAuth = jwtAuth
if redirectLoc != nil {
server.JWTRedirectLocations = append(server.JWTRedirectLocations, *redirectLoc)
}
}
locations = append(locations, loc)
if loc.Path == "/" {
rootLocation = true
}
}
if !rootLocation && ingEx.Ingress.Spec.Backend != nil {
upsName := getNameForUpstream(ingEx.Ingress, emptyHost, ingEx.Ingress.Spec.Backend)
ssl := isSSLEnabled(sslServices[ingEx.Ingress.Spec.Backend.ServiceName], cfgParams, staticParams)
proxySSLName := generateProxySSLName(ingEx.Ingress.Spec.Backend.ServiceName, ingEx.Ingress.Namespace)
pathtype := networking.PathTypePrefix
loc := createLocation(pathOrDefault("/"), upstreams[upsName], &cfgParams, wsServices[ingEx.Ingress.Spec.Backend.ServiceName], rewrites[ingEx.Ingress.Spec.Backend.ServiceName],
ssl, grpcServices[ingEx.Ingress.Spec.Backend.ServiceName], proxySSLName, &pathtype, ingEx.Ingress.Spec.Backend.ServiceName)
locations = append(locations, loc)
if cfgParams.HealthCheckEnabled {
if hc, exists := ingEx.HealthChecks[ingEx.Ingress.Spec.Backend.ServiceName+ingEx.Ingress.Spec.Backend.ServicePort.String()]; exists {
healthChecks[upsName] = createHealthCheck(hc, upsName, &cfgParams)
}
}
if _, exists := grpcServices[ingEx.Ingress.Spec.Backend.ServiceName]; !exists {
grpcOnly = false
}
}
server.Locations = locations
server.HealthChecks = healthChecks
server.GRPCOnly = grpcOnly
servers = append(servers, server)
}
var keepalive string
if cfgParams.Keepalive > 0 {
keepalive = fmt.Sprint(cfgParams.Keepalive)
}
return version1.IngressNginxConfig{
Upstreams: upstreamMapToSlice(upstreams),
Servers: servers,
Keepalive: keepalive,
Ingress: version1.Ingress{
Name: ingEx.Ingress.Name,
Namespace: ingEx.Ingress.Namespace,
Annotations: ingEx.Ingress.Annotations,
},
SpiffeClientCerts: staticParams.NginxServiceMesh && !cfgParams.SpiffeServerCerts,
}
}
func generateJWTConfig(secretRefs map[string]*secrets.SecretReference, cfgParams *ConfigParams, redirectLocationName string) (*version1.JWTAuth, *version1.JWTRedirectLocation) {
secret := secretRefs[cfgParams.JWTKey]
if secret.Error != nil {
// TO-DO: add a warning
} else if secret.Type != secrets.SecretTypeJWK {
// TO-DO: add a warning
}
// Key is configured for all cases, including when the secret is (1) invalid or (2) of a wrong type.
// For (1) and (2), NGINX Plus will reject such a key at runtime and return 500 to clients.
jwtAuth := &version1.JWTAuth{
Key: secret.Path,
Realm: cfgParams.JWTRealm,
Token: cfgParams.JWTToken,
}
var redirectLocation *version1.JWTRedirectLocation
if cfgParams.JWTLoginURL != "" {
jwtAuth.RedirectLocationName = redirectLocationName
redirectLocation = &version1.JWTRedirectLocation{
Name: redirectLocationName,
LoginURL: cfgParams.JWTLoginURL,
}
}
return jwtAuth, redirectLocation
}
func addSSLConfig(server *version1.Server, host string, ingressTLS []networking.IngressTLS, secretRefs map[string]*secrets.SecretReference, isWildcardEnabled bool) {
var tlsEnabled bool
var tlsSecret string
for _, tls := range ingressTLS {
for _, h := range tls.Hosts {
if h == host {
tlsEnabled = true
tlsSecret = tls.SecretName
break
}
}
}
if !tlsEnabled {
return
}
var pemFile string
if tlsSecret != "" {
secret := secretRefs[tlsSecret]
if secret.Error != nil {
pemFile = pemFileNameForMissingTLSSecret
// TO-DO: add a warning
} else if secret.Type != api_v1.SecretTypeTLS {
pemFile = pemFileNameForMissingTLSSecret
// TO-DO: add a warning
} else {
pemFile = secret.Path
}
} else if isWildcardEnabled {
pemFile = pemFileNameForWildcardTLSSecret
} else {
pemFile = pemFileNameForMissingTLSSecret
// TO-DO: add a warning
}
server.SSL = true
server.SSLCertificate = pemFile
server.SSLCertificateKey = pemFile
if pemFile == pemFileNameForMissingTLSSecret {
server.SSLCiphers = "NULL"
}
}
func generateIngressPath(path string, pathType *networking.PathType) string {
if pathType == nil {
return path
}
if *pathType == networking.PathTypeExact {
path = "= " + path
}
return path
}
func createLocation(path string, upstream version1.Upstream, cfg *ConfigParams, websocket bool, rewrite string, ssl bool, grpc bool, proxySSLName string, pathType *networking.PathType, serviceName string) version1.Location {
loc := version1.Location{
Path: generateIngressPath(path, pathType),
Upstream: upstream,
ProxyConnectTimeout: cfg.ProxyConnectTimeout,
ProxyReadTimeout: cfg.ProxyReadTimeout,
ProxySendTimeout: cfg.ProxySendTimeout,
ClientMaxBodySize: cfg.ClientMaxBodySize,
Websocket: websocket,
Rewrite: rewrite,
SSL: ssl,
GRPC: grpc,
ProxyBuffering: cfg.ProxyBuffering,
ProxyBuffers: cfg.ProxyBuffers,
ProxyBufferSize: cfg.ProxyBufferSize,
ProxyMaxTempFileSize: cfg.ProxyMaxTempFileSize,
ProxySSLName: proxySSLName,
LocationSnippets: cfg.LocationSnippets,
ServiceName: serviceName,
}
return loc
}
// upstreamRequiresQueue checks if the upstream requires a queue.
// Mandatory Health Checks can cause nginx to return errors on reload, since all Upstreams start
// Unhealthy. By adding a queue to the Upstream we can avoid returning errors, at the cost of a short delay.
func upstreamRequiresQueue(name string, ingEx *IngressEx, cfg *ConfigParams) (n int64, timeout int64) {
if cfg.HealthCheckEnabled && cfg.HealthCheckMandatory && cfg.HealthCheckMandatoryQueue > 0 {
if hc, exists := ingEx.HealthChecks[name]; exists {
return cfg.HealthCheckMandatoryQueue, int64(hc.TimeoutSeconds)
}
}
return 0, 0
}
func createUpstream(ingEx *IngressEx, name string, backend *networking.IngressBackend, stickyCookie string, cfg *ConfigParams,
isPlus bool, isResolverConfigured bool, isLatencyMetricsEnabled bool) version1.Upstream {
var ups version1.Upstream
labels := version1.UpstreamLabels{
Service: backend.ServiceName,
ResourceType: "ingress",
ResourceName: ingEx.Ingress.Name,
ResourceNamespace: ingEx.Ingress.Namespace,
}
if isPlus {
queue, timeout := upstreamRequiresQueue(backend.ServiceName+backend.ServicePort.String(), ingEx, cfg)
ups = version1.Upstream{Name: name, StickyCookie: stickyCookie, Queue: queue, QueueTimeout: timeout, UpstreamLabels: labels}
} else {
ups = version1.NewUpstreamWithDefaultServer(name)
if isLatencyMetricsEnabled {
ups.UpstreamLabels = labels
}
}
endps, exists := ingEx.Endpoints[backend.ServiceName+backend.ServicePort.String()]
if exists {
var upsServers []version1.UpstreamServer
// Always false for NGINX OSS
_, isExternalNameSvc := ingEx.ExternalNameSvcs[backend.ServiceName]
if isExternalNameSvc && !isResolverConfigured {
glog.Warningf("A resolver must be configured for Type ExternalName service %s, no upstream servers will be created", backend.ServiceName)
endps = []string{}
}
for _, endp := range endps {
addressport := strings.Split(endp, ":")
upsServers = append(upsServers, version1.UpstreamServer{
Address: addressport[0],
Port: addressport[1],
MaxFails: cfg.MaxFails,
MaxConns: cfg.MaxConns,
FailTimeout: cfg.FailTimeout,
SlowStart: cfg.SlowStart,
Resolve: isExternalNameSvc,
})
}
if len(upsServers) > 0 {
ups.UpstreamServers = upsServers
}
}
ups.LBMethod = cfg.LBMethod
ups.UpstreamZoneSize = cfg.UpstreamZoneSize
return ups
}
func createHealthCheck(hc *api_v1.Probe, upstreamName string, cfg *ConfigParams) version1.HealthCheck {
return version1.HealthCheck{
UpstreamName: upstreamName,
Fails: hc.FailureThreshold,
Interval: hc.PeriodSeconds,
Passes: hc.SuccessThreshold,
URI: hc.HTTPGet.Path,
Scheme: strings.ToLower(string(hc.HTTPGet.Scheme)),
Mandatory: cfg.HealthCheckMandatory,
Headers: headersToString(hc.HTTPGet.HTTPHeaders),
TimeoutSeconds: int64(hc.TimeoutSeconds),
}
}
func headersToString(headers []api_v1.HTTPHeader) map[string]string {
m := make(map[string]string)
for _, header := range headers {
m[header.Name] = header.Value
}
return m
}
func pathOrDefault(path string) string {
if path == "" {
return "/"
}
return path
}
func getNameForUpstream(ing *networking.Ingress, host string, backend *networking.IngressBackend) string {
return fmt.Sprintf("%v-%v-%v-%v-%v", ing.Namespace, ing.Name, host, backend.ServiceName, backend.ServicePort.String())
}
func getNameForRedirectLocation(ing *networking.Ingress) string {
return fmt.Sprintf("@login_url_%v-%v", ing.Namespace, ing.Name)
}
func upstreamMapToSlice(upstreams map[string]version1.Upstream) []version1.Upstream {
keys := make([]string, 0, len(upstreams))
for k := range upstreams {
keys = append(keys, k)
}
// this ensures that the slice 'result' is sorted, which preserves the order of upstream servers
// in the generated configuration file from one version to another and is also required for repeatable
// Unit test results
sort.Strings(keys)
result := make([]version1.Upstream, 0, len(upstreams))
for _, k := range keys {
result = append(result, upstreams[k])
}
return result
}
func generateNginxCfgForMergeableIngresses(mergeableIngs *MergeableIngresses, masterApResources map[string]string,
baseCfgParams *ConfigParams, isPlus bool, isResolverConfigured bool, staticParams *StaticConfigParams,
isWildcardEnabled bool) version1.IngressNginxConfig {
var masterServer version1.Server
var locations []version1.Location
var upstreams []version1.Upstream
healthChecks := make(map[string]version1.HealthCheck)
var keepalive string
// replace master with a deepcopy because we will modify it
mergeableIngs.Master.Ingress = mergeableIngs.Master.Ingress.DeepCopy()
removedAnnotations := filterMasterAnnotations(mergeableIngs.Master.Ingress.Annotations)
if len(removedAnnotations) != 0 {
glog.Errorf("Ingress Resource %v/%v with the annotation 'nginx.org/mergeable-ingress-type' set to 'master' cannot contain the '%v' annotation(s). They will be ignored",
mergeableIngs.Master.Ingress.Namespace, mergeableIngs.Master.Ingress.Name, strings.Join(removedAnnotations, ","))
}
isMinion := false
masterNginxCfg := generateNginxCfg(mergeableIngs.Master, masterApResources, isMinion, baseCfgParams, isPlus, isResolverConfigured, staticParams, isWildcardEnabled)
masterServer = masterNginxCfg.Servers[0]
masterServer.Locations = []version1.Location{}
upstreams = append(upstreams, masterNginxCfg.Upstreams...)
if masterNginxCfg.Keepalive != "" {
keepalive = masterNginxCfg.Keepalive
}
minions := mergeableIngs.Minions
for _, minion := range minions {
// replace minion with a deepcopy because we will modify it
minion.Ingress = minion.Ingress.DeepCopy()
// Remove the default backend so that "/" will not be generated
minion.Ingress.Spec.Backend = nil
// Add acceptable master annotations to minion
mergeMasterAnnotationsIntoMinion(minion.Ingress.Annotations, mergeableIngs.Master.Ingress.Annotations)
removedAnnotations = filterMinionAnnotations(minion.Ingress.Annotations)
if len(removedAnnotations) != 0 {
glog.Errorf("Ingress Resource %v/%v with the annotation 'nginx.org/mergeable-ingress-type' set to 'minion' cannot contain the %v annotation(s). They will be ignored",
minion.Ingress.Namespace, minion.Ingress.Name, strings.Join(removedAnnotations, ","))
}
isMinion := true
// App Protect Resources not allowed in minions - pass empty map
dummyApResources := make(map[string]string)
nginxCfg := generateNginxCfg(minion, dummyApResources, isMinion, baseCfgParams, isPlus, isResolverConfigured, staticParams, isWildcardEnabled)
for _, server := range nginxCfg.Servers {
for _, loc := range server.Locations {
loc.MinionIngress = &nginxCfg.Ingress
locations = append(locations, loc)
}
for hcName, healthCheck := range server.HealthChecks {
healthChecks[hcName] = healthCheck
}
masterServer.JWTRedirectLocations = append(masterServer.JWTRedirectLocations, server.JWTRedirectLocations...)
}
upstreams = append(upstreams, nginxCfg.Upstreams...)
}
masterServer.HealthChecks = healthChecks
masterServer.Locations = locations
return version1.IngressNginxConfig{
Servers: []version1.Server{masterServer},
Upstreams: upstreams,
Keepalive: keepalive,
Ingress: masterNginxCfg.Ingress,
SpiffeClientCerts: staticParams.NginxServiceMesh && !baseCfgParams.SpiffeServerCerts,
}
}
func isSSLEnabled(isSSLService bool, cfgParams ConfigParams, staticCfgParams *StaticConfigParams) bool {
return isSSLService || staticCfgParams.NginxServiceMesh && !cfgParams.SpiffeServerCerts
}