-
Notifications
You must be signed in to change notification settings - Fork 2k
/
configurator.go
1664 lines (1382 loc) · 64.7 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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package configs
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/nginxinc/kubernetes-ingress/pkg/apis/dos/v1beta1"
"github.com/nginxinc/kubernetes-ingress/internal/k8s/secrets"
"github.com/nginxinc/nginx-prometheus-exporter/collector"
"github.com/spiffe/go-spiffe/v2/workloadapi"
"github.com/nginxinc/kubernetes-ingress/internal/configs/version2"
conf_v1alpha1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1alpha1"
"github.com/golang/glog"
api_v1 "k8s.io/api/core/v1"
networking "k8s.io/api/networking/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/nginxinc/kubernetes-ingress/internal/configs/version1"
"github.com/nginxinc/kubernetes-ingress/internal/nginx"
conf_v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
latCollector "github.com/nginxinc/kubernetes-ingress/internal/metrics/collectors"
)
const (
pemFileNameForWildcardTLSSecret = "/etc/nginx/secrets/wildcard" // #nosec G101
appProtectBundleFolder = "/etc/nginx/waf/bundles/"
appProtectPolicyFolder = "/etc/nginx/waf/nac-policies/"
appProtectLogConfFolder = "/etc/nginx/waf/nac-logconfs/"
appProtectUserSigFolder = "/etc/nginx/waf/nac-usersigs/"
appProtectUserSigIndex = "/etc/nginx/waf/nac-usersigs/index.conf"
appProtectDosPolicyFolder = "/etc/nginx/dos/policies/"
appProtectDosLogConfFolder = "/etc/nginx/dos/logconfs/"
)
// DefaultServerSecretPath is the full path to the Secret with a TLS cert and a key for the default server. #nosec G101
const DefaultServerSecretPath = "/etc/nginx/secrets/default"
// DefaultSecretPath is the full default path to where secrets are stored and accessed.
const DefaultSecretPath = "/etc/nginx/secrets" // #nosec G101
// 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"
// HtpasswdFileKey is the key of the data field of a Secret where the HTTP basic authorization list must be stored
const HtpasswdFileKey = "htpasswd"
// CACrtKey is the key of the data field of a Secret where the cert must be stored.
const CACrtKey = "ca.crt"
// CACrlKey is the key of the data field of a Secret where the cert revocation list must be stored.
const CACrlKey = "ca.crl"
// ClientSecretKey is the key of the data field of a Secret where the OIDC client secret must be stored.
const ClientSecretKey = "client-secret"
// SPIFFE filenames and modes
const (
spiffeCertFileName = "spiffe_cert.pem"
spiffeKeyFileName = "spiffe_key.pem"
spiffeBundleFileName = "spiffe_rootca.pem"
spiffeCertsFileMode = os.FileMode(0o644)
spiffeKeyFileMode = os.FileMode(0o600)
)
// ExtendedResources holds all extended configuration resources, for which Configurator configures NGINX.
type ExtendedResources struct {
IngressExes []*IngressEx
MergeableIngresses []*MergeableIngresses
VirtualServerExes []*VirtualServerEx
TransportServerExes []*TransportServerEx
}
type tlsPassthroughPair struct {
Host string
UnixSocket string
}
// metricLabelsIndex keeps the relations between Ingress Controller resources and NGINX configuration.
// Used to be able to add Prometheus Metrics variable labels grouped by resource key.
type metricLabelsIndex struct {
ingressUpstreams map[string][]string
virtualServerUpstreams map[string][]string
transportServerUpstreams map[string][]string
ingressServerZones map[string][]string
virtualServerServerZones map[string][]string
transportServerServerZones map[string][]string
ingressUpstreamPeers map[string][]string
virtualServerUpstreamPeers map[string][]string
transportServerUpstreamPeers map[string][]string
}
// Configurator configures NGINX.
// Until reloads are enabled via EnableReloads(), the Configurator will not reload NGINX and update NGINX Plus
// upstream servers via NGINX Plus API for configuration changes.
// This allows the Ingress Controller to incrementally build the NGINX configuration during the IC start and
// then apply it at the end of the start.
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
virtualServers map[string]*VirtualServerEx
transportServers map[string]*TransportServerEx
tlsPassthroughPairs map[string]tlsPassthroughPair
isWildcardEnabled bool
isPlus bool
labelUpdater collector.LabelUpdater
metricLabelsIndex *metricLabelsIndex
isPrometheusEnabled bool
latencyCollector latCollector.LatencyCollector
isLatencyMetricsEnabled bool
isReloadsEnabled 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,
labelUpdater collector.LabelUpdater, isPrometheusEnabled bool, latencyCollector latCollector.LatencyCollector, isLatencyMetricsEnabled bool,
) *Configurator {
metricLabelsIndex := &metricLabelsIndex{
ingressUpstreams: make(map[string][]string),
virtualServerUpstreams: make(map[string][]string),
transportServerUpstreams: make(map[string][]string),
ingressServerZones: make(map[string][]string),
virtualServerServerZones: make(map[string][]string),
transportServerServerZones: make(map[string][]string),
ingressUpstreamPeers: make(map[string][]string),
virtualServerUpstreamPeers: make(map[string][]string),
transportServerUpstreamPeers: make(map[string][]string),
}
cnf := Configurator{
nginxManager: nginxManager,
staticCfgParams: staticCfgParams,
cfgParams: config,
ingresses: make(map[string]*IngressEx),
virtualServers: make(map[string]*VirtualServerEx),
transportServers: make(map[string]*TransportServerEx),
templateExecutor: templateExecutor,
templateExecutorV2: templateExecutorV2,
minions: make(map[string]map[string]bool),
tlsPassthroughPairs: make(map[string]tlsPassthroughPair),
isPlus: isPlus,
isWildcardEnabled: isWildcardEnabled,
labelUpdater: labelUpdater,
metricLabelsIndex: metricLabelsIndex,
isPrometheusEnabled: isPrometheusEnabled,
latencyCollector: latencyCollector,
isLatencyMetricsEnabled: isLatencyMetricsEnabled,
isReloadsEnabled: false,
}
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)
}
func findRemovedKeys(currentKeys []string, newKeys map[string]bool) []string {
var removedKeys []string
for _, name := range currentKeys {
if _, exists := newKeys[name]; !exists {
removedKeys = append(removedKeys, name)
}
}
return removedKeys
}
func (cnf *Configurator) updateIngressMetricsLabels(ingEx *IngressEx, upstreams []version1.Upstream) {
upstreamServerLabels := make(map[string][]string)
newUpstreams := make(map[string]bool)
var newUpstreamsNames []string
upstreamServerPeerLabels := make(map[string][]string)
newPeers := make(map[string]bool)
var newPeersIPs []string
for _, u := range upstreams {
upstreamServerLabels[u.Name] = []string{u.UpstreamLabels.Service, u.UpstreamLabels.ResourceType, u.UpstreamLabels.ResourceName, u.UpstreamLabels.ResourceNamespace}
newUpstreams[u.Name] = true
newUpstreamsNames = append(newUpstreamsNames, u.Name)
for _, server := range u.UpstreamServers {
podInfo := ingEx.PodsByIP[server.Address]
labelKey := fmt.Sprintf("%v/%v", u.Name, server.Address)
upstreamServerPeerLabels[labelKey] = []string{podInfo.Name}
if cnf.staticCfgParams.NginxServiceMesh {
ownerLabelVal := fmt.Sprintf("%s/%s", podInfo.OwnerType, podInfo.OwnerName)
upstreamServerPeerLabels[labelKey] = append(upstreamServerPeerLabels[labelKey], ownerLabelVal)
}
newPeers[labelKey] = true
newPeersIPs = append(newPeersIPs, labelKey)
}
}
key := fmt.Sprintf("%v/%v", ingEx.Ingress.Namespace, ingEx.Ingress.Name)
removedUpstreams := findRemovedKeys(cnf.metricLabelsIndex.ingressUpstreams[key], newUpstreams)
cnf.metricLabelsIndex.ingressUpstreams[key] = newUpstreamsNames
cnf.latencyCollector.UpdateUpstreamServerLabels(upstreamServerLabels)
cnf.latencyCollector.DeleteUpstreamServerLabels(removedUpstreams)
removedPeers := findRemovedKeys(cnf.metricLabelsIndex.ingressUpstreamPeers[key], newPeers)
cnf.metricLabelsIndex.ingressUpstreamPeers[key] = newPeersIPs
cnf.latencyCollector.UpdateUpstreamServerPeerLabels(upstreamServerPeerLabels)
cnf.latencyCollector.DeleteUpstreamServerPeerLabels(removedPeers)
cnf.latencyCollector.DeleteMetrics(removedPeers)
if cnf.isPlus {
cnf.labelUpdater.UpdateUpstreamServerLabels(upstreamServerLabels)
cnf.labelUpdater.DeleteUpstreamServerLabels(removedUpstreams)
cnf.labelUpdater.UpdateUpstreamServerPeerLabels(upstreamServerPeerLabels)
cnf.labelUpdater.DeleteUpstreamServerPeerLabels(removedPeers)
serverZoneLabels := make(map[string][]string)
newZones := make(map[string]bool)
var newZonesNames []string
for _, rule := range ingEx.Ingress.Spec.Rules {
serverZoneLabels[rule.Host] = []string{"ingress", ingEx.Ingress.Name, ingEx.Ingress.Namespace}
newZones[rule.Host] = true
newZonesNames = append(newZonesNames, rule.Host)
}
removedZones := findRemovedKeys(cnf.metricLabelsIndex.ingressServerZones[key], newZones)
cnf.metricLabelsIndex.ingressServerZones[key] = newZonesNames
cnf.labelUpdater.UpdateServerZoneLabels(serverZoneLabels)
cnf.labelUpdater.DeleteServerZoneLabels(removedZones)
}
}
func (cnf *Configurator) deleteIngressMetricsLabels(key string) {
cnf.latencyCollector.DeleteUpstreamServerLabels(cnf.metricLabelsIndex.ingressUpstreams[key])
cnf.latencyCollector.DeleteUpstreamServerPeerLabels(cnf.metricLabelsIndex.ingressUpstreamPeers[key])
cnf.latencyCollector.DeleteMetrics(cnf.metricLabelsIndex.ingressUpstreamPeers[key])
if cnf.isPlus {
cnf.labelUpdater.DeleteUpstreamServerLabels(cnf.metricLabelsIndex.ingressUpstreams[key])
cnf.labelUpdater.DeleteServerZoneLabels(cnf.metricLabelsIndex.ingressServerZones[key])
cnf.labelUpdater.DeleteUpstreamServerPeerLabels(cnf.metricLabelsIndex.ingressUpstreamPeers[key])
}
delete(cnf.metricLabelsIndex.ingressUpstreams, key)
delete(cnf.metricLabelsIndex.ingressServerZones, key)
delete(cnf.metricLabelsIndex.ingressUpstreamPeers, key)
}
// AddOrUpdateIngress adds or updates NGINX configuration for the Ingress resource.
func (cnf *Configurator) AddOrUpdateIngress(ingEx *IngressEx) (Warnings, error) {
warnings, err := cnf.addOrUpdateIngress(ingEx)
if err != nil {
return warnings, fmt.Errorf("error adding or updating ingress %v/%v: %w", ingEx.Ingress.Namespace, ingEx.Ingress.Name, err)
}
if err := cnf.reload(nginx.ReloadForOtherUpdate); err != nil {
return warnings, fmt.Errorf("error reloading NGINX for %v/%v: %w", ingEx.Ingress.Namespace, ingEx.Ingress.Name, err)
}
return warnings, nil
}
// virtualServerForHost takes a hostname and returns a VS for the given hostname.
func (cnf *Configurator) virtualServerForHost(hostname string) *conf_v1.VirtualServer {
for _, vsEx := range cnf.virtualServers {
if vsEx.VirtualServer.Spec.Host == hostname {
return vsEx.VirtualServer
}
}
return nil
}
// upstreamsForVirtualServer takes VirtualServer and returns a list of associated upstreams.
func (cnf *Configurator) upstreamsForVirtualServer(vs *conf_v1.VirtualServer) []string {
glog.V(3).Infof("Get upstreamName for vs: %s", vs.Spec.Host)
upstreamNames := make([]string, 0, len(vs.Spec.Upstreams))
virtualServerUpstreamNamer := NewUpstreamNamerForVirtualServer(vs)
for _, u := range vs.Spec.Upstreams {
upstreamName := virtualServerUpstreamNamer.GetNameForUpstream(u.Name)
glog.V(3).Infof("upstream: %s, upstreamName: %s", u.Name, upstreamName)
upstreamNames = append(upstreamNames, upstreamName)
}
return upstreamNames
}
// UpstreamsForHost takes a hostname and returns upstreams for the given hostname.
func (cnf *Configurator) UpstreamsForHost(hostname string) []string {
glog.V(3).Infof("Get upstream for host: %s", hostname)
vs := cnf.virtualServerForHost(hostname)
if vs != nil {
return cnf.upstreamsForVirtualServer(vs)
}
return nil
}
// StreamUpstreamsForName takes a name and returns stream upstreams
// associated with this name. The name represents TS's
// (TransportServer) action name.
func (cnf *Configurator) StreamUpstreamsForName(name string) []string {
glog.V(3).Infof("Get stream upstreams for name: '%s'", name)
ts := cnf.transportServerForActionName(name)
if ts != nil {
return cnf.streamUpstreamsForTransportServer(ts)
}
return nil
}
// transportServerForActionName takes an action name and returns
// Transport Server obj associated with that name.
func (cnf *Configurator) transportServerForActionName(name string) *conf_v1alpha1.TransportServer {
for _, tsEx := range cnf.transportServers {
glog.V(3).Infof("Check ts action '%s' for requested name: '%s'", tsEx.TransportServer.Spec.Action.Pass, name)
if tsEx.TransportServer.Spec.Action.Pass == name {
return tsEx.TransportServer
}
}
return nil
}
// streamUpstreamsForTransportServer takes TransportServer obj and returns
// a list of stream upstreams associated with this TransportServer.
func (cnf *Configurator) streamUpstreamsForTransportServer(ts *conf_v1alpha1.TransportServer) []string {
upstreamNames := make([]string, 0, len(ts.Spec.Upstreams))
n := newUpstreamNamerForTransportServer(ts)
for _, u := range ts.Spec.Upstreams {
un := n.GetNameForUpstream(u.Name)
upstreamNames = append(upstreamNames, un)
}
return upstreamNames
}
func (cnf *Configurator) addOrUpdateIngress(ingEx *IngressEx) (Warnings, error) {
apResources := cnf.updateApResources(ingEx)
cnf.updateDosResource(ingEx.DosEx)
dosResource := getAppProtectDosResource(ingEx.DosEx)
// LocalSecretStore will not set Path if the secret is not on the filesystem.
// However, NGINX configuration for an Ingress resource, to handle the case of a missing secret,
// relies on the path to be always configured.
if jwtKey, exists := ingEx.Ingress.Annotations[JWTKeyAnnotation]; exists {
ingEx.SecretRefs[jwtKey].Path = cnf.nginxManager.GetFilenameForSecret(ingEx.Ingress.Namespace + "-" + jwtKey)
}
if basicAuth, exists := ingEx.Ingress.Annotations[BasicAuthSecretAnnotation]; exists {
ingEx.SecretRefs[basicAuth].Path = cnf.nginxManager.GetFilenameForSecret(ingEx.Ingress.Namespace + "-" + basicAuth)
}
isMinion := false
nginxCfg, warnings := generateNginxCfg(ingEx, apResources, dosResource, isMinion, cnf.cfgParams, cnf.isPlus, cnf.IsResolverConfigured(),
cnf.staticCfgParams, cnf.isWildcardEnabled)
name := objectMetaToFileName(&ingEx.Ingress.ObjectMeta)
content, err := cnf.templateExecutor.ExecuteIngressConfigTemplate(&nginxCfg)
if err != nil {
return warnings, fmt.Errorf("error generating Ingress Config %v: %w", name, err)
}
cnf.nginxManager.CreateConfig(name, content)
cnf.ingresses[name] = ingEx
if (cnf.isPlus && cnf.isPrometheusEnabled) || cnf.isLatencyMetricsEnabled {
cnf.updateIngressMetricsLabels(ingEx, nginxCfg.Upstreams)
}
return warnings, nil
}
// AddOrUpdateMergeableIngress adds or updates NGINX configuration for the Ingress resources with Mergeable Types.
func (cnf *Configurator) AddOrUpdateMergeableIngress(mergeableIngs *MergeableIngresses) (Warnings, error) {
warnings, err := cnf.addOrUpdateMergeableIngress(mergeableIngs)
if err != nil {
return warnings, fmt.Errorf("error when adding or updating ingress %v/%v: %w", mergeableIngs.Master.Ingress.Namespace, mergeableIngs.Master.Ingress.Name, err)
}
if err := cnf.reload(nginx.ReloadForOtherUpdate); err != nil {
return warnings, fmt.Errorf("error reloading NGINX for %v/%v: %w", mergeableIngs.Master.Ingress.Namespace, mergeableIngs.Master.Ingress.Name, err)
}
return warnings, nil
}
func (cnf *Configurator) addOrUpdateMergeableIngress(mergeableIngs *MergeableIngresses) (Warnings, error) {
apResources := cnf.updateApResources(mergeableIngs.Master)
cnf.updateDosResource(mergeableIngs.Master.DosEx)
dosResource := getAppProtectDosResource(mergeableIngs.Master.DosEx)
// LocalSecretStore will not set Path if the secret is not on the filesystem.
// However, NGINX configuration for an Ingress resource, to handle the case of a missing secret,
// relies on the path to be always configured.
if jwtKey, exists := mergeableIngs.Master.Ingress.Annotations[JWTKeyAnnotation]; exists {
mergeableIngs.Master.SecretRefs[jwtKey].Path = cnf.nginxManager.GetFilenameForSecret(mergeableIngs.Master.Ingress.Namespace + "-" + jwtKey)
}
if basicAuth, exists := mergeableIngs.Master.Ingress.Annotations[BasicAuthSecretAnnotation]; exists {
mergeableIngs.Master.SecretRefs[basicAuth].Path = cnf.nginxManager.GetFilenameForSecret(mergeableIngs.Master.Ingress.Namespace + "-" + basicAuth)
}
for _, minion := range mergeableIngs.Minions {
if jwtKey, exists := minion.Ingress.Annotations[JWTKeyAnnotation]; exists {
minion.SecretRefs[jwtKey].Path = cnf.nginxManager.GetFilenameForSecret(minion.Ingress.Namespace + "-" + jwtKey)
}
if basicAuth, exists := minion.Ingress.Annotations[BasicAuthSecretAnnotation]; exists {
minion.SecretRefs[basicAuth].Path = cnf.nginxManager.GetFilenameForSecret(minion.Ingress.Namespace + "-" + basicAuth)
}
}
nginxCfg, warnings := generateNginxCfgForMergeableIngresses(mergeableIngs, apResources, dosResource, cnf.cfgParams, cnf.isPlus,
cnf.IsResolverConfigured(), cnf.staticCfgParams, cnf.isWildcardEnabled)
name := objectMetaToFileName(&mergeableIngs.Master.Ingress.ObjectMeta)
content, err := cnf.templateExecutor.ExecuteIngressConfigTemplate(&nginxCfg)
if err != nil {
return warnings, fmt.Errorf("error generating Ingress Config %v: %w", 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
}
if (cnf.isPlus && cnf.isPrometheusEnabled) || cnf.isLatencyMetricsEnabled {
cnf.updateIngressMetricsLabels(mergeableIngs.Master, nginxCfg.Upstreams)
}
return warnings, nil
}
func (cnf *Configurator) updateVirtualServerMetricsLabels(virtualServerEx *VirtualServerEx, upstreams []version2.Upstream) {
labels := make(map[string][]string)
newUpstreams := make(map[string]bool)
var newUpstreamsNames []string
upstreamServerPeerLabels := make(map[string][]string)
newPeers := make(map[string]bool)
var newPeersIPs []string
for _, u := range upstreams {
labels[u.Name] = []string{u.UpstreamLabels.Service, u.UpstreamLabels.ResourceType, u.UpstreamLabels.ResourceName, u.UpstreamLabels.ResourceNamespace}
newUpstreams[u.Name] = true
newUpstreamsNames = append(newUpstreamsNames, u.Name)
for _, server := range u.Servers {
podInfo := virtualServerEx.PodsByIP[server.Address]
labelKey := fmt.Sprintf("%v/%v", u.Name, server.Address)
upstreamServerPeerLabels[labelKey] = []string{podInfo.Name}
if cnf.staticCfgParams.NginxServiceMesh {
ownerLabelVal := fmt.Sprintf("%s/%s", podInfo.OwnerType, podInfo.OwnerName)
upstreamServerPeerLabels[labelKey] = append(upstreamServerPeerLabels[labelKey], ownerLabelVal)
}
newPeers[labelKey] = true
newPeersIPs = append(newPeersIPs, labelKey)
}
}
key := fmt.Sprintf("%v/%v", virtualServerEx.VirtualServer.Namespace, virtualServerEx.VirtualServer.Name)
removedPeers := findRemovedKeys(cnf.metricLabelsIndex.virtualServerUpstreamPeers[key], newPeers)
cnf.metricLabelsIndex.virtualServerUpstreamPeers[key] = newPeersIPs
cnf.latencyCollector.UpdateUpstreamServerPeerLabels(upstreamServerPeerLabels)
cnf.latencyCollector.DeleteUpstreamServerPeerLabels(removedPeers)
removedUpstreams := findRemovedKeys(cnf.metricLabelsIndex.virtualServerUpstreams[key], newUpstreams)
cnf.latencyCollector.UpdateUpstreamServerLabels(labels)
cnf.metricLabelsIndex.virtualServerUpstreams[key] = newUpstreamsNames
cnf.latencyCollector.DeleteUpstreamServerLabels(removedUpstreams)
cnf.latencyCollector.DeleteMetrics(removedPeers)
if cnf.isPlus {
cnf.labelUpdater.UpdateUpstreamServerPeerLabels(upstreamServerPeerLabels)
cnf.labelUpdater.DeleteUpstreamServerPeerLabels(removedPeers)
cnf.labelUpdater.UpdateUpstreamServerLabels(labels)
cnf.labelUpdater.DeleteUpstreamServerLabels(removedUpstreams)
serverZoneLabels := make(map[string][]string)
newZones := make(map[string]bool)
newZonesNames := []string{virtualServerEx.VirtualServer.Spec.Host}
serverZoneLabels[virtualServerEx.VirtualServer.Spec.Host] = []string{
"virtualserver", virtualServerEx.VirtualServer.Name, virtualServerEx.VirtualServer.Namespace,
}
newZones[virtualServerEx.VirtualServer.Spec.Host] = true
removedZones := findRemovedKeys(cnf.metricLabelsIndex.virtualServerServerZones[key], newZones)
cnf.metricLabelsIndex.virtualServerServerZones[key] = newZonesNames
cnf.labelUpdater.UpdateServerZoneLabels(serverZoneLabels)
cnf.labelUpdater.DeleteServerZoneLabels(removedZones)
}
}
func (cnf *Configurator) deleteVirtualServerMetricsLabels(key string) {
cnf.latencyCollector.DeleteUpstreamServerLabels(cnf.metricLabelsIndex.virtualServerUpstreams[key])
cnf.latencyCollector.DeleteUpstreamServerPeerLabels(cnf.metricLabelsIndex.virtualServerUpstreamPeers[key])
cnf.latencyCollector.DeleteMetrics(cnf.metricLabelsIndex.virtualServerUpstreamPeers[key])
if cnf.isPlus {
cnf.labelUpdater.DeleteUpstreamServerLabels(cnf.metricLabelsIndex.virtualServerUpstreams[key])
cnf.labelUpdater.DeleteServerZoneLabels(cnf.metricLabelsIndex.virtualServerServerZones[key])
cnf.labelUpdater.DeleteUpstreamServerPeerLabels(cnf.metricLabelsIndex.virtualServerUpstreamPeers[key])
}
delete(cnf.metricLabelsIndex.virtualServerUpstreams, key)
delete(cnf.metricLabelsIndex.virtualServerServerZones, key)
delete(cnf.metricLabelsIndex.virtualServerUpstreamPeers, key)
}
// AddOrUpdateVirtualServer adds or updates NGINX configuration for the VirtualServer resource.
func (cnf *Configurator) AddOrUpdateVirtualServer(virtualServerEx *VirtualServerEx) (Warnings, error) {
warnings, err := cnf.addOrUpdateVirtualServer(virtualServerEx)
if err != nil {
return warnings, fmt.Errorf("error adding or updating VirtualServer %v/%v: %w", virtualServerEx.VirtualServer.Namespace, virtualServerEx.VirtualServer.Name, err)
}
if err := cnf.reload(nginx.ReloadForOtherUpdate); err != nil {
return warnings, fmt.Errorf("error reloading NGINX for VirtualServer %v/%v: %w", virtualServerEx.VirtualServer.Namespace, virtualServerEx.VirtualServer.Name, err)
}
return warnings, nil
}
func (cnf *Configurator) addOrUpdateOpenTracingTracerConfig(content string) error {
return cnf.nginxManager.CreateOpenTracingTracerConfig(content)
}
func (cnf *Configurator) addOrUpdateVirtualServer(virtualServerEx *VirtualServerEx) (Warnings, error) {
apResources := cnf.updateApResourcesForVs(virtualServerEx)
dosResources := map[string]*appProtectDosResource{}
for k, v := range virtualServerEx.DosProtectedEx {
cnf.updateDosResource(v)
dosRes := getAppProtectDosResource(v)
if dosRes != nil {
dosResources[k] = dosRes
}
}
name := getFileNameForVirtualServer(virtualServerEx.VirtualServer)
vsc := newVirtualServerConfigurator(cnf.cfgParams, cnf.isPlus, cnf.IsResolverConfigured(), cnf.staticCfgParams, cnf.isWildcardEnabled)
vsCfg, warnings := vsc.GenerateVirtualServerConfig(virtualServerEx, apResources, dosResources)
content, err := cnf.templateExecutorV2.ExecuteVirtualServerTemplate(&vsCfg)
if err != nil {
return warnings, fmt.Errorf("error generating VirtualServer config: %v: %w", name, err)
}
cnf.nginxManager.CreateConfig(name, content)
cnf.virtualServers[name] = virtualServerEx
if (cnf.isPlus && cnf.isPrometheusEnabled) || cnf.isLatencyMetricsEnabled {
cnf.updateVirtualServerMetricsLabels(virtualServerEx, vsCfg.Upstreams)
}
return warnings, nil
}
// AddOrUpdateVirtualServers adds or updates NGINX configuration for multiple VirtualServer resources.
func (cnf *Configurator) AddOrUpdateVirtualServers(virtualServerExes []*VirtualServerEx) (Warnings, error) {
allWarnings := newWarnings()
for _, vsEx := range virtualServerExes {
warnings, err := cnf.addOrUpdateVirtualServer(vsEx)
if err != nil {
return allWarnings, err
}
allWarnings.Add(warnings)
}
if err := cnf.reload(nginx.ReloadForOtherUpdate); err != nil {
return allWarnings, fmt.Errorf("error when reloading NGINX when updating Policy: %w", err)
}
return allWarnings, nil
}
func (cnf *Configurator) updateTransportServerMetricsLabels(transportServerEx *TransportServerEx, upstreams []version2.StreamUpstream) {
labels := make(map[string][]string)
newUpstreams := make(map[string]bool)
var newUpstreamsNames []string
upstreamServerPeerLabels := make(map[string][]string)
newPeers := make(map[string]bool)
var newPeersIPs []string
for _, u := range upstreams {
labels[u.Name] = []string{u.UpstreamLabels.Service, u.UpstreamLabels.ResourceType, u.UpstreamLabels.ResourceName, u.UpstreamLabels.ResourceNamespace}
newUpstreams[u.Name] = true
newUpstreamsNames = append(newUpstreamsNames, u.Name)
for _, server := range u.Servers {
podName := transportServerEx.PodsByIP[server.Address]
labelKey := fmt.Sprintf("%v/%v", u.Name, server.Address)
upstreamServerPeerLabels[labelKey] = []string{podName}
newPeers[labelKey] = true
newPeersIPs = append(newPeersIPs, labelKey)
}
}
key := fmt.Sprintf("%v/%v", transportServerEx.TransportServer.Namespace, transportServerEx.TransportServer.Name)
removedPeers := findRemovedKeys(cnf.metricLabelsIndex.transportServerUpstreamPeers[key], newPeers)
cnf.metricLabelsIndex.transportServerUpstreamPeers[key] = newPeersIPs
removedUpstreams := findRemovedKeys(cnf.metricLabelsIndex.transportServerUpstreams[key], newUpstreams)
cnf.metricLabelsIndex.transportServerUpstreams[key] = newUpstreamsNames
cnf.labelUpdater.UpdateStreamUpstreamServerPeerLabels(upstreamServerPeerLabels)
cnf.labelUpdater.DeleteStreamUpstreamServerPeerLabels(removedPeers)
cnf.labelUpdater.UpdateStreamUpstreamServerLabels(labels)
cnf.labelUpdater.DeleteStreamUpstreamServerLabels(removedUpstreams)
streamServerZoneLabels := make(map[string][]string)
newZones := make(map[string]bool)
zoneName := transportServerEx.TransportServer.Spec.Listener.Name
if transportServerEx.TransportServer.Spec.Host != "" {
zoneName = transportServerEx.TransportServer.Spec.Host
}
newZonesNames := []string{zoneName}
streamServerZoneLabels[zoneName] = []string{
"transportserver", transportServerEx.TransportServer.Name, transportServerEx.TransportServer.Namespace,
}
newZones[zoneName] = true
removedZones := findRemovedKeys(cnf.metricLabelsIndex.transportServerServerZones[key], newZones)
cnf.metricLabelsIndex.transportServerServerZones[key] = newZonesNames
cnf.labelUpdater.UpdateStreamServerZoneLabels(streamServerZoneLabels)
cnf.labelUpdater.DeleteStreamServerZoneLabels(removedZones)
}
func (cnf *Configurator) deleteTransportServerMetricsLabels(key string) {
cnf.labelUpdater.DeleteStreamUpstreamServerLabels(cnf.metricLabelsIndex.transportServerUpstreams[key])
cnf.labelUpdater.DeleteStreamServerZoneLabels(cnf.metricLabelsIndex.transportServerServerZones[key])
cnf.labelUpdater.DeleteStreamUpstreamServerPeerLabels(cnf.metricLabelsIndex.transportServerUpstreamPeers[key])
delete(cnf.metricLabelsIndex.transportServerUpstreams, key)
delete(cnf.metricLabelsIndex.transportServerServerZones, key)
delete(cnf.metricLabelsIndex.transportServerUpstreamPeers, key)
}
// AddOrUpdateTransportServer adds or updates NGINX configuration for the TransportServer resource.
// It is a responsibility of the caller to check that the TransportServer references an existing listener.
func (cnf *Configurator) AddOrUpdateTransportServer(transportServerEx *TransportServerEx) (Warnings, error) {
warnings, err := cnf.addOrUpdateTransportServer(transportServerEx)
if err != nil {
return nil, fmt.Errorf("error adding or updating TransportServer %v/%v: %w", transportServerEx.TransportServer.Namespace, transportServerEx.TransportServer.Name, err)
}
if err := cnf.reload(nginx.ReloadForOtherUpdate); err != nil {
return nil, fmt.Errorf("error reloading NGINX for TransportServer %v/%v: %w", transportServerEx.TransportServer.Namespace, transportServerEx.TransportServer.Name, err)
}
return warnings, nil
}
func (cnf *Configurator) addOrUpdateTransportServer(transportServerEx *TransportServerEx) (Warnings, error) {
name := getFileNameForTransportServer(transportServerEx.TransportServer)
tsCfg, warnings := generateTransportServerConfig(transportServerEx, transportServerEx.ListenerPort, cnf.isPlus, cnf.IsResolverConfigured())
content, err := cnf.templateExecutorV2.ExecuteTransportServerTemplate(tsCfg)
if err != nil {
return nil, fmt.Errorf("error generating TransportServer config %v: %w", name, err)
}
if cnf.isPlus && cnf.isPrometheusEnabled {
cnf.updateTransportServerMetricsLabels(transportServerEx, tsCfg.Upstreams)
}
cnf.nginxManager.CreateStreamConfig(name, content)
cnf.transportServers[name] = transportServerEx
// update TLS Passthrough Hosts config in case we have a TLS Passthrough TransportServer
// only TLS Passthrough TransportServers have non-empty hosts
if transportServerEx.TransportServer.Spec.Host != "" {
key := generateNamespaceNameKey(&transportServerEx.TransportServer.ObjectMeta)
cnf.tlsPassthroughPairs[key] = tlsPassthroughPair{
Host: transportServerEx.TransportServer.Spec.Host,
UnixSocket: generateUnixSocket(transportServerEx),
}
err := cnf.updateTLSPassthroughHostsConfig()
if err != nil {
return nil, err
}
return warnings, nil
}
return warnings, nil
}
// GetVirtualServerRoutesForVirtualServer returns the virtualServerRoutes that a virtualServer
// references, if that virtualServer exists
func (cnf *Configurator) GetVirtualServerRoutesForVirtualServer(key string) []*conf_v1.VirtualServerRoute {
vsFileName := getFileNameForVirtualServerFromKey(key)
if cnf.virtualServers[vsFileName] != nil {
return cnf.virtualServers[vsFileName].VirtualServerRoutes
}
return nil
}
func (cnf *Configurator) updateTLSPassthroughHostsConfig() error {
cfg := generateTLSPassthroughHostsConfig(cnf.tlsPassthroughPairs)
content, err := cnf.templateExecutorV2.ExecuteTLSPassthroughHostsTemplate(cfg)
if err != nil {
return fmt.Errorf("error generating config for TLS Passthrough Unix Sockets map: %w", err)
}
cnf.nginxManager.CreateTLSPassthroughHostsConfig(content)
return nil
}
func generateTLSPassthroughHostsConfig(tlsPassthroughPairs map[string]tlsPassthroughPair) *version2.TLSPassthroughHostsConfig {
cfg := version2.TLSPassthroughHostsConfig{}
for _, pair := range tlsPassthroughPairs {
cfg[pair.Host] = pair.UnixSocket
}
return &cfg
}
func (cnf *Configurator) addOrUpdateCASecret(secret *api_v1.Secret) string {
name := objectMetaToFileName(&secret.ObjectMeta)
crtData, crlData := GenerateCAFileContent(secret)
crtSecretName := fmt.Sprintf("%s-%s", name, CACrtKey)
crlSecretName := fmt.Sprintf("%s-%s", name, CACrlKey)
crtFileName := cnf.nginxManager.CreateSecret(crtSecretName, crtData, nginx.TLSSecretFileMode)
crlFileName := cnf.nginxManager.CreateSecret(crlSecretName, crlData, nginx.TLSSecretFileMode)
return fmt.Sprintf("%s %s", crtFileName, crlFileName)
}
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) addOrUpdateHtpasswdSecret(secret *api_v1.Secret) string {
name := objectMetaToFileName(&secret.ObjectMeta)
data := secret.Data[HtpasswdFileKey]
return cnf.nginxManager.CreateSecret(name, data, nginx.HtpasswdSecretFileMode)
}
// AddOrUpdateResources adds or updates configuration for resources.
func (cnf *Configurator) AddOrUpdateResources(resources ExtendedResources) (Warnings, error) {
allWarnings := newWarnings()
for _, ingEx := range resources.IngressExes {
warnings, err := cnf.addOrUpdateIngress(ingEx)
if err != nil {
return nil, fmt.Errorf("error adding or updating ingress %v/%v: %w", ingEx.Ingress.Namespace, ingEx.Ingress.Name, err)
}
allWarnings.Add(warnings)
}
for _, m := range resources.MergeableIngresses {
warnings, err := cnf.addOrUpdateMergeableIngress(m)
if err != nil {
return nil, fmt.Errorf("error adding or updating mergeableIngress %v/%v: %w", m.Master.Ingress.Namespace, m.Master.Ingress.Name, err)
}
allWarnings.Add(warnings)
}
for _, vsEx := range resources.VirtualServerExes {
warnings, err := cnf.addOrUpdateVirtualServer(vsEx)
if err != nil {
return nil, fmt.Errorf("error adding or updating VirtualServer %v/%v: %w", vsEx.VirtualServer.Namespace, vsEx.VirtualServer.Name, err)
}
allWarnings.Add(warnings)
}
for _, tsEx := range resources.TransportServerExes {
warnings, err := cnf.addOrUpdateTransportServer(tsEx)
if err != nil {
return nil, fmt.Errorf("error adding or updating TransportServer %v/%v: %w", tsEx.TransportServer.Namespace, tsEx.TransportServer.Name, err)
}
allWarnings.Add(warnings)
}
if err := cnf.reload(nginx.ReloadForOtherUpdate); err != nil {
return nil, fmt.Errorf("error when reloading NGINX when updating resources: %w", err)
}
return allWarnings, 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.reload(nginx.ReloadForOtherUpdate); err != nil {
return fmt.Errorf("error when reloading NGINX when updating the special Secrets: %w", 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()
}
// GenerateCAFileContent generates a pem file content from the TLS secret.
func GenerateCAFileContent(secret *api_v1.Secret) ([]byte, []byte) {
var caKey bytes.Buffer
var caCrl bytes.Buffer
caKey.Write(secret.Data[CACrtKey])
caCrl.Write(secret.Data[CACrlKey])
return caKey.Bytes(), caCrl.Bytes()
}
// DeleteIngress deletes NGINX configuration for the Ingress resource.
func (cnf *Configurator) DeleteIngress(key string, skipReload bool) error {
name := keyToFileName(key)
cnf.nginxManager.DeleteConfig(name)
delete(cnf.ingresses, name)
delete(cnf.minions, name)
if (cnf.isPlus && cnf.isPrometheusEnabled) || cnf.isLatencyMetricsEnabled {
cnf.deleteIngressMetricsLabels(key)
}
if !skipReload {
if err := cnf.reload(nginx.ReloadForOtherUpdate); err != nil {
return fmt.Errorf("error when removing ingress %v: %w", key, err)
}
}
return nil
}
// DeleteVirtualServer deletes NGINX configuration for the VirtualServer resource.
func (cnf *Configurator) DeleteVirtualServer(key string, skipReload bool) error {
name := getFileNameForVirtualServerFromKey(key)
cnf.nginxManager.DeleteConfig(name)
delete(cnf.virtualServers, name)
if (cnf.isPlus && cnf.isPrometheusEnabled) || cnf.isLatencyMetricsEnabled {
cnf.deleteVirtualServerMetricsLabels(key)
}
if !skipReload {
if err := cnf.reload(nginx.ReloadForOtherUpdate); err != nil {
return fmt.Errorf("error when removing VirtualServer %v: %w", key, err)
}
}
return nil
}
// DeleteTransportServer deletes NGINX configuration for the TransportServer resource.
func (cnf *Configurator) DeleteTransportServer(key string) error {
if cnf.isPlus && cnf.isPrometheusEnabled {
cnf.deleteTransportServerMetricsLabels(key)
}
err := cnf.deleteTransportServer(key)
if err != nil {
return fmt.Errorf("error when removing TransportServer %v: %w", key, err)
}
err = cnf.reload(nginx.ReloadForOtherUpdate)
if err != nil {
return fmt.Errorf("error when removing TransportServer %v: %w", key, err)
}
return nil
}
func (cnf *Configurator) deleteTransportServer(key string) error {
name := getFileNameForTransportServerFromKey(key)
cnf.nginxManager.DeleteStreamConfig(name)
// update TLS Passthrough Hosts config in case we have a TLS Passthrough TransportServer
if _, exists := cnf.tlsPassthroughPairs[key]; exists {
delete(cnf.tlsPassthroughPairs, key)
return cnf.updateTLSPassthroughHostsConfig()
}
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 {
// It is safe to ignore warnings here as no new warnings should appear when updating Endpoints for Ingresses
_, err := cnf.addOrUpdateIngress(ingEx)
if err != nil {
return fmt.Errorf("error adding or updating ingress %v/%v: %w", 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.reload(nginx.ReloadForEndpointsUpdate); err != nil {
return fmt.Errorf("error reloading NGINX when updating endpoints: %w", 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 {
// It is safe to ignore warnings here as no new warnings should appear when updating Endpoints for Ingresses
_, err := cnf.addOrUpdateMergeableIngress(mergeableIngresses[i])
if err != nil {
return fmt.Errorf("error adding or updating mergeableIngress %v/%v: %w", 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.reload(nginx.ReloadForEndpointsUpdate); err != nil {
return fmt.Errorf("error reloading NGINX when updating endpoints for %v: %w", mergeableIngresses, err)
}
return nil
}
// UpdateEndpointsForVirtualServers updates endpoints in NGINX configuration for the VirtualServer resources.
func (cnf *Configurator) UpdateEndpointsForVirtualServers(virtualServerExes []*VirtualServerEx) error {
reloadPlus := false
for _, vs := range virtualServerExes {
// It is safe to ignore warnings here as no new warnings should appear when updating Endpoints for VirtualServers
_, err := cnf.addOrUpdateVirtualServer(vs)
if err != nil {
return fmt.Errorf("error adding or updating VirtualServer %v/%v: %w", 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
}