/
configurator.go
630 lines (510 loc) · 21.3 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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
package configs
import (
"bytes"
"fmt"
"strings"
"github.com/nginxinc/kubernetes-ingress/internal/configs/version2"
"github.com/golang/glog"
"github.com/nginxinc/kubernetes-ingress/internal/configs/version1"
"github.com/nginxinc/kubernetes-ingress/internal/nginx"
conf_v1alpha1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1alpha1"
api_v1 "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const pemFileNameForMissingTLSSecret = "/etc/nginx/secrets/default"
const pemFileNameForWildcardTLSSecret = "/etc/nginx/secrets/wildcard"
// DefaultServerSecretName is the filename of the Secret with a TLS cert and a key for the default server.
const DefaultServerSecretName = "default"
// WildcardSecretName is the filename of the Secret with a TLS cert and a key for the ingress resources with TLS termination enabled but not secret defined.
const WildcardSecretName = "wildcard"
// JWTKeyKey is the key of the data field of a Secret where the JWK must be stored.
const JWTKeyKey = "jwk"
// Configurator configures NGINX.
type Configurator struct {
nginxManager nginx.Manager
staticCfgParams *StaticConfigParams
cfgParams *ConfigParams
templateExecutor *version1.TemplateExecutor
templateExecutorV2 *version2.TemplateExecutor
ingresses map[string]*IngressEx
minions map[string]map[string]bool
isWildcardEnabled bool
isPlus bool
}
// NewConfigurator creates a new Configurator.
func NewConfigurator(nginxManager nginx.Manager, staticCfgParams *StaticConfigParams, config *ConfigParams, templateExecutor *version1.TemplateExecutor,
templateExecutorV2 *version2.TemplateExecutor, isPlus bool, isWildcardEnabled bool) *Configurator {
cnf := Configurator{
nginxManager: nginxManager,
staticCfgParams: staticCfgParams,
cfgParams: config,
ingresses: make(map[string]*IngressEx),
templateExecutor: templateExecutor,
templateExecutorV2: templateExecutorV2,
minions: make(map[string]map[string]bool),
isPlus: isPlus,
isWildcardEnabled: isWildcardEnabled,
}
return &cnf
}
// AddOrUpdateDHParam creates a dhparam file with the content of the string.
func (cnf *Configurator) AddOrUpdateDHParam(content string) (string, error) {
return cnf.nginxManager.CreateDHParam(content)
}
// AddOrUpdateIngress adds or updates NGINX configuration for the Ingress resource.
func (cnf *Configurator) AddOrUpdateIngress(ingEx *IngressEx) error {
if err := cnf.addOrUpdateIngress(ingEx); err != nil {
return fmt.Errorf("Error adding or updating ingress %v/%v: %v", ingEx.Ingress.Namespace, ingEx.Ingress.Name, err)
}
if err := cnf.nginxManager.Reload(); err != nil {
return fmt.Errorf("Error reloading NGINX for %v/%v: %v", ingEx.Ingress.Namespace, ingEx.Ingress.Name, err)
}
return nil
}
func (cnf *Configurator) addOrUpdateIngress(ingEx *IngressEx) error {
pems := cnf.updateTLSSecrets(ingEx)
jwtKeyFileName := cnf.updateJWKSecret(ingEx)
isMinion := false
nginxCfg := generateNginxCfg(ingEx, pems, isMinion, cnf.cfgParams, cnf.isPlus, cnf.IsResolverConfigured(), jwtKeyFileName)
name := objectMetaToFileName(&ingEx.Ingress.ObjectMeta)
content, err := cnf.templateExecutor.ExecuteIngressConfigTemplate(&nginxCfg)
if err != nil {
return fmt.Errorf("Error generating Ingress Config %v: %v", name, err)
}
cnf.nginxManager.CreateConfig(name, content)
cnf.ingresses[name] = ingEx
return nil
}
// AddOrUpdateMergeableIngress adds or updates NGINX configuration for the Ingress resources with Mergeable Types.
func (cnf *Configurator) AddOrUpdateMergeableIngress(mergeableIngs *MergeableIngresses) error {
if err := cnf.addOrUpdateMergeableIngress(mergeableIngs); err != nil {
return fmt.Errorf("Error when adding or updating ingress %v/%v: %v", mergeableIngs.Master.Ingress.Namespace, mergeableIngs.Master.Ingress.Name, err)
}
if err := cnf.nginxManager.Reload(); err != nil {
return fmt.Errorf("Error reloading NGINX for %v/%v: %v", mergeableIngs.Master.Ingress.Namespace, mergeableIngs.Master.Ingress.Name, err)
}
return nil
}
func (cnf *Configurator) addOrUpdateMergeableIngress(mergeableIngs *MergeableIngresses) error {
masterPems := cnf.updateTLSSecrets(mergeableIngs.Master)
masterJwtKeyFileName := cnf.updateJWKSecret(mergeableIngs.Master)
minionJwtKeyFileNames := make(map[string]string)
for _, minion := range mergeableIngs.Minions {
minionName := objectMetaToFileName(&minion.Ingress.ObjectMeta)
minionJwtKeyFileNames[minionName] = cnf.updateJWKSecret(minion)
}
nginxCfg := generateNginxCfgForMergeableIngresses(mergeableIngs, masterPems, masterJwtKeyFileName, minionJwtKeyFileNames, cnf.cfgParams, cnf.isPlus, cnf.IsResolverConfigured())
name := objectMetaToFileName(&mergeableIngs.Master.Ingress.ObjectMeta)
content, err := cnf.templateExecutor.ExecuteIngressConfigTemplate(&nginxCfg)
if err != nil {
return fmt.Errorf("Error generating Ingress Config %v: %v", name, err)
}
cnf.nginxManager.CreateConfig(name, content)
cnf.ingresses[name] = mergeableIngs.Master
cnf.minions[name] = make(map[string]bool)
for _, minion := range mergeableIngs.Minions {
minionName := objectMetaToFileName(&minion.Ingress.ObjectMeta)
cnf.minions[name][minionName] = true
}
return nil
}
// AddOrUpdateVirtualServer adds or updates NGINX configuration for the VirtualServer resource.
func (cnf *Configurator) AddOrUpdateVirtualServer(virtualServerEx *VirtualServerEx) error {
if err := cnf.addOrUpdateVirtualServer(virtualServerEx); err != nil {
return fmt.Errorf("Error adding or updating VirtualServer %v/%v: %v", virtualServerEx.VirtualServer.Namespace, virtualServerEx.VirtualServer.Name, err)
}
if err := cnf.nginxManager.Reload(); err != nil {
return fmt.Errorf("Error reloading NGINX for VirtualServer %v/%v: %v", virtualServerEx.VirtualServer.Namespace, virtualServerEx.VirtualServer.Name, err)
}
return nil
}
func (cnf *Configurator) addOrUpdateOpenTracingTracerConfig(content string) error {
err := cnf.nginxManager.CreateOpenTracingTracerConfig(content)
return err
}
func (cnf *Configurator) addOrUpdateVirtualServer(virtualServerEx *VirtualServerEx) error {
tlsPemFileName := ""
if virtualServerEx.TLSSecret != nil {
tlsPemFileName = cnf.addOrUpdateTLSSecret(virtualServerEx.TLSSecret)
}
vsCfg := generateVirtualServerConfig(virtualServerEx, tlsPemFileName, cnf.cfgParams, cnf.isPlus, cnf.IsResolverConfigured())
name := getFileNameForVirtualServer(virtualServerEx.VirtualServer)
content, err := cnf.templateExecutorV2.ExecuteVirtualServerTemplate(&vsCfg)
if err != nil {
return fmt.Errorf("Error generating VirtualServer config: %v: %v", name, err)
}
cnf.nginxManager.CreateConfig(name, content)
return nil
}
func (cnf *Configurator) updateTLSSecrets(ingEx *IngressEx) map[string]string {
pems := make(map[string]string)
for _, tls := range ingEx.Ingress.Spec.TLS {
secretName := tls.SecretName
pemFileName := pemFileNameForMissingTLSSecret
if secretName == "" && cnf.isWildcardEnabled {
pemFileName = pemFileNameForWildcardTLSSecret
} else if secret, exists := ingEx.TLSSecrets[secretName]; exists {
pemFileName = cnf.addOrUpdateTLSSecret(secret)
}
for _, host := range tls.Hosts {
pems[host] = pemFileName
}
if len(tls.Hosts) == 0 {
pems[emptyHost] = pemFileName
}
}
return pems
}
func (cnf *Configurator) updateJWKSecret(ingEx *IngressEx) string {
if !cnf.isPlus || ingEx.JWTKey.Name == "" {
return ""
}
if ingEx.JWTKey.Secret != nil {
cnf.addOrUpdateJWKSecret(ingEx.JWTKey.Secret)
}
return cnf.nginxManager.GetFilenameForSecret(ingEx.Ingress.Namespace + "-" + ingEx.JWTKey.Name)
}
func (cnf *Configurator) addOrUpdateJWKSecret(secret *api_v1.Secret) string {
name := objectMetaToFileName(&secret.ObjectMeta)
data := secret.Data[JWTKeyKey]
return cnf.nginxManager.CreateSecret(name, data, nginx.JWKSecretFileMode)
}
func (cnf *Configurator) AddOrUpdateJWKSecret(secret *api_v1.Secret) {
cnf.addOrUpdateJWKSecret(secret)
}
// AddOrUpdateTLSSecret adds or updates a file with the content of the TLS secret.
func (cnf *Configurator) AddOrUpdateTLSSecret(secret *api_v1.Secret, ingExes []IngressEx, mergeableIngresses []MergeableIngresses, virtualServerExes []*VirtualServerEx) error {
cnf.addOrUpdateTLSSecret(secret)
for i := range ingExes {
err := cnf.addOrUpdateIngress(&ingExes[i])
if err != nil {
return fmt.Errorf("Error adding or updating ingress %v/%v: %v", ingExes[i].Ingress.Namespace, ingExes[i].Ingress.Name, err)
}
}
for i := range mergeableIngresses {
err := cnf.addOrUpdateMergeableIngress(&mergeableIngresses[i])
if err != nil {
return fmt.Errorf("Error adding or updating mergeableIngress %v/%v: %v", mergeableIngresses[i].Master.Ingress.Namespace, mergeableIngresses[i].Master.Ingress.Name, err)
}
}
for _, vsEx := range virtualServerExes {
err := cnf.addOrUpdateVirtualServer(vsEx)
if err != nil {
return fmt.Errorf("Error adding or updating VirtualServer %v/%v: %v", vsEx.VirtualServer.Namespace, vsEx.VirtualServer.Name, err)
}
}
if err := cnf.nginxManager.Reload(); err != nil {
return fmt.Errorf("Error when reloading NGINX when updating Secret: %v", err)
}
return nil
}
func (cnf *Configurator) addOrUpdateTLSSecret(secret *api_v1.Secret) string {
name := objectMetaToFileName(&secret.ObjectMeta)
data := GenerateCertAndKeyFileContent(secret)
return cnf.nginxManager.CreateSecret(name, data, nginx.TLSSecretFileMode)
}
// AddOrUpdateSpecialTLSSecrets adds or updates a file with a TLS cert and a key from a Special TLS Secret (eg. DefaultServerSecret, WildcardTLSSecret).
func (cnf *Configurator) AddOrUpdateSpecialTLSSecrets(secret *api_v1.Secret, secretNames []string) error {
data := GenerateCertAndKeyFileContent(secret)
for _, secretName := range secretNames {
cnf.nginxManager.CreateSecret(secretName, data, nginx.TLSSecretFileMode)
}
if err := cnf.nginxManager.Reload(); err != nil {
return fmt.Errorf("Error when reloading NGINX when updating the special Secrets: %v", err)
}
return nil
}
// GenerateCertAndKeyFileContent generates a pem file content from the TLS secret.
func GenerateCertAndKeyFileContent(secret *api_v1.Secret) []byte {
var res bytes.Buffer
res.Write(secret.Data[api_v1.TLSCertKey])
res.WriteString("\n")
res.Write(secret.Data[api_v1.TLSPrivateKeyKey])
return res.Bytes()
}
// DeleteSecret deletes the file associated with the secret and the configuration files for Ingress and VirtualServer resources.
// NGINX is reloaded only when the total number of the resources > 0.
func (cnf *Configurator) DeleteSecret(key string, ingExes []IngressEx, mergeableIngresses []MergeableIngresses, virtualServerExes []*VirtualServerEx) error {
cnf.nginxManager.DeleteSecret(keyToFileName(key))
for i := range ingExes {
err := cnf.addOrUpdateIngress(&ingExes[i])
if err != nil {
return fmt.Errorf("Error adding or updating ingress %v/%v: %v", ingExes[i].Ingress.Namespace, ingExes[i].Ingress.Name, err)
}
}
for i := range mergeableIngresses {
err := cnf.addOrUpdateMergeableIngress(&mergeableIngresses[i])
if err != nil {
return fmt.Errorf("Error adding or updating mergeableIngress %v/%v: %v", mergeableIngresses[i].Master.Ingress.Namespace, mergeableIngresses[i].Master.Ingress.Name, err)
}
}
for _, vsEx := range virtualServerExes {
err := cnf.addOrUpdateVirtualServer(vsEx)
if err != nil {
return fmt.Errorf("Error adding or updating VirtualServer %v/%v: %v", vsEx.VirtualServer.Namespace, vsEx.VirtualServer.Name, err)
}
}
if len(ingExes)+len(mergeableIngresses)+len(virtualServerExes) > 0 {
if err := cnf.nginxManager.Reload(); err != nil {
return fmt.Errorf("Error when reloading NGINX when deleting Secret %v: %v", key, err)
}
}
return nil
}
// DeleteIngress deletes NGINX configuration for the Ingress resource.
func (cnf *Configurator) DeleteIngress(key string) error {
name := keyToFileName(key)
cnf.nginxManager.DeleteConfig(name)
delete(cnf.ingresses, name)
delete(cnf.minions, name)
if err := cnf.nginxManager.Reload(); err != nil {
return fmt.Errorf("Error when removing ingress %v: %v", key, err)
}
return nil
}
// DeleteVirtualServer deletes NGINX configuration for the VirtualServer resource.
func (cnf *Configurator) DeleteVirtualServer(key string) error {
name := getFileNameForVirtualServerFromKey(key)
cnf.nginxManager.DeleteConfig(name)
if err := cnf.nginxManager.Reload(); err != nil {
return fmt.Errorf("Error when removing VirtualServer %v: %v", key, err)
}
return nil
}
// UpdateEndpoints updates endpoints in NGINX configuration for the Ingress resources.
func (cnf *Configurator) UpdateEndpoints(ingExes []*IngressEx) error {
reloadPlus := false
for _, ingEx := range ingExes {
err := cnf.addOrUpdateIngress(ingEx)
if err != nil {
return fmt.Errorf("Error adding or updating ingress %v/%v: %v", ingEx.Ingress.Namespace, ingEx.Ingress.Name, err)
}
if cnf.isPlus {
err := cnf.updatePlusEndpoints(ingEx)
if err != nil {
glog.Warningf("Couldn't update the endpoints via the API: %v; reloading configuration instead", err)
reloadPlus = true
}
}
}
if cnf.isPlus && !reloadPlus {
glog.V(3).Info("No need to reload nginx")
return nil
}
if err := cnf.nginxManager.Reload(); err != nil {
return fmt.Errorf("Error reloading NGINX when updating endpoints: %v", err)
}
return nil
}
// UpdateEndpointsMergeableIngress updates endpoints in NGINX configuration for a mergeable Ingress resource.
func (cnf *Configurator) UpdateEndpointsMergeableIngress(mergeableIngresses []*MergeableIngresses) error {
reloadPlus := false
for i := range mergeableIngresses {
err := cnf.addOrUpdateMergeableIngress(mergeableIngresses[i])
if err != nil {
return fmt.Errorf("Error adding or updating mergeableIngress %v/%v: %v", mergeableIngresses[i].Master.Ingress.Namespace, mergeableIngresses[i].Master.Ingress.Name, err)
}
if cnf.isPlus {
for _, ing := range mergeableIngresses[i].Minions {
err = cnf.updatePlusEndpoints(ing)
if err != nil {
glog.Warningf("Couldn't update the endpoints via the API: %v; reloading configuration instead", err)
reloadPlus = true
}
}
}
}
if cnf.isPlus && !reloadPlus {
glog.V(3).Info("No need to reload nginx")
return nil
}
if err := cnf.nginxManager.Reload(); err != nil {
return fmt.Errorf("Error reloading NGINX when updating endpoints for %v: %v", mergeableIngresses, err)
}
return nil
}
// UpdateEndpointsForVirtualServers updates endpoints in NGINX configuration for the s resources.
func (cnf *Configurator) UpdateEndpointsForVirtualServers(virtualServerExes []*VirtualServerEx) error {
reloadPlus := false
for _, vs := range virtualServerExes {
err := cnf.addOrUpdateVirtualServer(vs)
if err != nil {
return fmt.Errorf("Error adding or updating VirtualServer %v/%v: %v", vs.VirtualServer.Namespace, vs.VirtualServer.Name, err)
}
if cnf.isPlus {
err := cnf.updatePlusEndpointsForVirtualServer(vs)
if err != nil {
glog.Warningf("Couldn't update the endpoints via the API: %v; reloading configuration instead", err)
reloadPlus = true
}
}
}
if cnf.isPlus && !reloadPlus {
glog.V(3).Info("No need to reload nginx")
return nil
}
if err := cnf.nginxManager.Reload(); err != nil {
return fmt.Errorf("Error reloading NGINX when updating endpoints: %v", err)
}
return nil
}
func (cnf *Configurator) updatePlusEndpointsForVirtualServer(virtualServerEx *VirtualServerEx) error {
serverCfg := createUpstreamServersConfig(cnf.cfgParams)
upstreamServers := createUpstreamServersForPlus(virtualServerEx)
for upstream, servers := range upstreamServers {
err := cnf.nginxManager.UpdateServersInPlus(upstream, servers, serverCfg)
if err != nil {
return fmt.Errorf("Couldn't update the endpoints for %v: %v", upstream, err)
}
}
return nil
}
func (cnf *Configurator) updatePlusEndpoints(ingEx *IngressEx) error {
ingCfg := parseAnnotations(ingEx, cnf.cfgParams, cnf.isPlus)
cfg := nginx.ServerConfig{
MaxFails: ingCfg.MaxFails,
MaxConns: ingCfg.MaxConns,
FailTimeout: ingCfg.FailTimeout,
SlowStart: ingCfg.SlowStart,
}
if ingEx.Ingress.Spec.Backend != nil {
endps, exists := ingEx.Endpoints[ingEx.Ingress.Spec.Backend.ServiceName+ingEx.Ingress.Spec.Backend.ServicePort.String()]
if exists {
if _, isExternalName := ingEx.ExternalNameSvcs[ingEx.Ingress.Spec.Backend.ServiceName]; isExternalName {
glog.V(3).Infof("Service %s is Type ExternalName, skipping NGINX Plus endpoints update via API", ingEx.Ingress.Spec.Backend.ServiceName)
} else {
name := getNameForUpstream(ingEx.Ingress, emptyHost, ingEx.Ingress.Spec.Backend)
err := cnf.nginxManager.UpdateServersInPlus(name, endps, cfg)
if err != nil {
return fmt.Errorf("Couldn't update the endpoints for %v: %v", name, err)
}
}
}
}
for _, rule := range ingEx.Ingress.Spec.Rules {
if rule.IngressRuleValue.HTTP == nil {
continue
}
for _, path := range rule.HTTP.Paths {
endps, exists := ingEx.Endpoints[path.Backend.ServiceName+path.Backend.ServicePort.String()]
if exists {
if _, isExternalName := ingEx.ExternalNameSvcs[path.Backend.ServiceName]; isExternalName {
glog.V(3).Infof("Service %s is Type ExternalName, skipping NGINX Plus endpoints update via API", path.Backend.ServiceName)
continue
}
name := getNameForUpstream(ingEx.Ingress, rule.Host, &path.Backend)
err := cnf.nginxManager.UpdateServersInPlus(name, endps, cfg)
if err != nil {
return fmt.Errorf("Couldn't update the endpoints for %v: %v", name, err)
}
}
}
}
return nil
}
// UpdateConfig updates NGINX configuration parameters.
func (cnf *Configurator) UpdateConfig(cfgParams *ConfigParams, ingExes []*IngressEx, mergeableIngs map[string]*MergeableIngresses, virtualServerExes []*VirtualServerEx) error {
cnf.cfgParams = cfgParams
if cnf.cfgParams.MainServerSSLDHParamFileContent != nil {
fileName, err := cnf.nginxManager.CreateDHParam(*cnf.cfgParams.MainServerSSLDHParamFileContent)
if err != nil {
return fmt.Errorf("Error when updating dhparams: %v", err)
}
cfgParams.MainServerSSLDHParam = fileName
}
if cfgParams.MainTemplate != nil {
err := cnf.templateExecutor.UpdateMainTemplate(cfgParams.MainTemplate)
if err != nil {
return fmt.Errorf("Error when parsing the main template: %v", err)
}
}
if cfgParams.IngressTemplate != nil {
err := cnf.templateExecutor.UpdateIngressTemplate(cfgParams.IngressTemplate)
if err != nil {
return fmt.Errorf("Error when parsing the ingress template: %v", err)
}
}
mainCfg := GenerateNginxMainConfig(cnf.staticCfgParams, cfgParams)
mainCfgContent, err := cnf.templateExecutor.ExecuteMainConfigTemplate(mainCfg)
if err != nil {
return fmt.Errorf("Error when writing main Config")
}
cnf.nginxManager.CreateMainConfig(mainCfgContent)
for _, ingEx := range ingExes {
if err := cnf.addOrUpdateIngress(ingEx); err != nil {
return err
}
}
for _, mergeableIng := range mergeableIngs {
if err := cnf.addOrUpdateMergeableIngress(mergeableIng); err != nil {
return err
}
}
for _, vsEx := range virtualServerExes {
if err := cnf.addOrUpdateVirtualServer(vsEx); err != nil {
return err
}
}
if mainCfg.OpenTracingLoadModule {
if err := cnf.addOrUpdateOpenTracingTracerConfig(mainCfg.OpenTracingTracerConfig); err != nil {
return fmt.Errorf("Error when updating OpenTracing tracer config: %v", err)
}
}
cnf.nginxManager.SetOpenTracing(mainCfg.OpenTracingLoadModule)
if err := cnf.nginxManager.Reload(); err != nil {
return fmt.Errorf("Error when updating config from ConfigMap: %v", err)
}
return nil
}
func keyToFileName(key string) string {
return strings.Replace(key, "/", "-", -1)
}
func objectMetaToFileName(meta *meta_v1.ObjectMeta) string {
return meta.Namespace + "-" + meta.Name
}
func getFileNameForVirtualServer(virtualServer *conf_v1alpha1.VirtualServer) string {
return fmt.Sprintf("vs_%s_%s", virtualServer.Namespace, virtualServer.Name)
}
func getFileNameForVirtualServerFromKey(key string) string {
replaced := strings.Replace(key, "/", "_", -1)
return fmt.Sprintf("vs_%s", replaced)
}
// HasIngress checks if the Ingress resource is present in NGINX configuration.
func (cnf *Configurator) HasIngress(ing *extensions.Ingress) bool {
name := objectMetaToFileName(&ing.ObjectMeta)
_, exists := cnf.ingresses[name]
return exists
}
// HasMinion checks if the minion Ingress resource of the master is present in NGINX configuration.
func (cnf *Configurator) HasMinion(master *extensions.Ingress, minion *extensions.Ingress) bool {
masterName := objectMetaToFileName(&master.ObjectMeta)
if _, exists := cnf.minions[masterName]; !exists {
return false
}
return cnf.minions[masterName][objectMetaToFileName(&minion.ObjectMeta)]
}
// IsResolverConfigured checks if a DNS resolver is present in NGINX configuration.
func (cnf *Configurator) IsResolverConfigured() bool {
return len(cnf.cfgParams.ResolverAddresses) != 0
}
// GetIngressCounts returns the total count of Ingress resources that are handled by the Ingress Controller grouped by their type
func (cnf *Configurator) GetIngressCounts() map[string]int {
counters := map[string]int{
"master": 0,
"regular": 0,
"minion": 0,
}
// cnf.ingresses contains only master and regular Ingress Resources
for _, ing := range cnf.ingresses {
if ing.Ingress.Annotations["nginx.org/mergeable-ingress-type"] == "master" {
counters["master"]++
} else {
counters["regular"]++
}
}
for _, min := range cnf.minions {
counters["minion"] += len(min)
}
return counters
}