Skip to content

Commit df74b67

Browse files
committed
Process TSes and GC in Controller via Configuration
1 parent 7838177 commit df74b67

File tree

6 files changed

+240
-454
lines changed

6 files changed

+240
-454
lines changed

internal/configs/configurator.go

+37-54
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"encoding/pem"
99
"fmt"
1010
"os"
11-
"sort"
1211
"strings"
1312

1413
"github.com/nginxinc/kubernetes-ingress/internal/k8s/secrets"
@@ -65,6 +64,14 @@ const (
6564
spiffeKeyFileMode = os.FileMode(0600)
6665
)
6766

67+
// ExtendedResources holds all extended configuration resources, for which Configurator configures NGINX.
68+
type ExtendedResources struct {
69+
IngressExes []*IngressEx
70+
MergeableIngresses []*MergeableIngresses
71+
VirtualServerExes []*VirtualServerEx
72+
TransportServerExes []*TransportServerEx
73+
}
74+
6875
type tlsPassthroughPair struct {
6976
Host string
7077
UnixSocket string
@@ -550,8 +557,7 @@ func (cnf *Configurator) AddOrUpdateTransportServer(transportServerEx *Transport
550557
func (cnf *Configurator) addOrUpdateTransportServer(transportServerEx *TransportServerEx) error {
551558
name := getFileNameForTransportServer(transportServerEx.TransportServer)
552559

553-
listener := cnf.globalCfgParams.Listeners[transportServerEx.TransportServer.Spec.Listener.Name]
554-
tsCfg := generateTransportServerConfig(transportServerEx, listener.Port, cnf.isPlus)
560+
tsCfg := generateTransportServerConfig(transportServerEx, transportServerEx.ListenerPort, cnf.isPlus)
555561

556562
content, err := cnf.templateExecutorV2.ExecuteTransportServerTemplate(&tsCfg)
557563
if err != nil {
@@ -590,11 +596,7 @@ func (cnf *Configurator) GetVirtualServerRoutesForVirtualServer(key string) []*c
590596
}
591597

592598
func (cnf *Configurator) updateTLSPassthroughHostsConfig() error {
593-
cfg, duplicatedHosts := generateTLSPassthroughHostsConfig(cnf.tlsPassthroughPairs)
594-
595-
for _, host := range duplicatedHosts {
596-
glog.Warningf("host %s is used by more than one TransportServers", host)
597-
}
599+
cfg := generateTLSPassthroughHostsConfig(cnf.tlsPassthroughPairs)
598600

599601
content, err := cnf.templateExecutorV2.ExecuteTLSPassthroughHostsTemplate(cfg)
600602
if err != nil {
@@ -606,30 +608,14 @@ func (cnf *Configurator) updateTLSPassthroughHostsConfig() error {
606608
return nil
607609
}
608610

609-
func generateTLSPassthroughHostsConfig(tlsPassthroughPairs map[string]tlsPassthroughPair) (*version2.TLSPassthroughHostsConfig, []string) {
610-
var keys []string
611-
612-
for key := range tlsPassthroughPairs {
613-
keys = append(keys, key)
614-
}
615-
616-
// we sort the keys of tlsPassthroughPairs so that we get the same result for the same input
617-
sort.Strings(keys)
618-
611+
func generateTLSPassthroughHostsConfig(tlsPassthroughPairs map[string]tlsPassthroughPair) *version2.TLSPassthroughHostsConfig {
619612
cfg := version2.TLSPassthroughHostsConfig{}
620-
var duplicatedHosts []string
621-
622-
for _, key := range keys {
623-
pair := tlsPassthroughPairs[key]
624-
625-
if _, exists := cfg[pair.Host]; exists {
626-
duplicatedHosts = append(duplicatedHosts, pair.Host)
627-
}
628613

614+
for _, pair := range tlsPassthroughPairs {
629615
cfg[pair.Host] = pair.UnixSocket
630616
}
631617

632-
return &cfg, duplicatedHosts
618+
return &cfg
633619
}
634620

635621
func (cnf *Configurator) addOrUpdateCASecret(secret *api_v1.Secret) string {
@@ -645,33 +631,40 @@ func (cnf *Configurator) addOrUpdateJWKSecret(secret *api_v1.Secret) string {
645631
}
646632

647633
// AddOrUpdateResources adds or updates configuration for resources.
648-
func (cnf *Configurator) AddOrUpdateResources(ingExes []*IngressEx, mergeableIngresses []*MergeableIngresses, virtualServerExes []*VirtualServerEx) (Warnings, error) {
634+
func (cnf *Configurator) AddOrUpdateResources(resources ExtendedResources) (Warnings, error) {
649635
allWarnings := newWarnings()
650636

651-
for _, ingEx := range ingExes {
637+
for _, ingEx := range resources.IngressExes {
652638
warnings, err := cnf.addOrUpdateIngress(ingEx)
653639
if err != nil {
654640
return allWarnings, fmt.Errorf("Error adding or updating ingress %v/%v: %v", ingEx.Ingress.Namespace, ingEx.Ingress.Name, err)
655641
}
656642
allWarnings.Add(warnings)
657643
}
658644

659-
for _, m := range mergeableIngresses {
645+
for _, m := range resources.MergeableIngresses {
660646
warnings, err := cnf.addOrUpdateMergeableIngress(m)
661647
if err != nil {
662648
return allWarnings, fmt.Errorf("Error adding or updating mergeableIngress %v/%v: %v", m.Master.Ingress.Namespace, m.Master.Ingress.Name, err)
663649
}
664650
allWarnings.Add(warnings)
665651
}
666652

667-
for _, vsEx := range virtualServerExes {
653+
for _, vsEx := range resources.VirtualServerExes {
668654
warnings, err := cnf.addOrUpdateVirtualServer(vsEx)
669655
if err != nil {
670656
return allWarnings, fmt.Errorf("Error adding or updating VirtualServer %v/%v: %v", vsEx.VirtualServer.Namespace, vsEx.VirtualServer.Name, err)
671657
}
672658
allWarnings.Add(warnings)
673659
}
674660

661+
for _, tsEx := range resources.TransportServerExes {
662+
err := cnf.addOrUpdateTransportServer(tsEx)
663+
if err != nil {
664+
return allWarnings, fmt.Errorf("Error adding or updating TransportServer %v/%v: %v", tsEx.TransportServer.Namespace, tsEx.TransportServer.Name, err)
665+
}
666+
}
667+
675668
if err := cnf.nginxManager.Reload(nginx.ReloadForOtherUpdate); err != nil {
676669
return allWarnings, fmt.Errorf("Error when reloading NGINX when updating resources: %v", err)
677670
}
@@ -1080,36 +1073,26 @@ func (cnf *Configurator) UpdateConfig(cfgParams *ConfigParams, ingExes []*Ingres
10801073
return allWarnings, nil
10811074
}
10821075

1083-
// UpdateGlobalConfiguration updates NGINX config based on the changes to the GlobalConfiguration resource.
1084-
// Currently, changes to the GlobalConfiguration only affect TransportServer resources.
1085-
// As a result of the changes, the configuration for TransportServers is updated and some TransportServers
1086-
// might be removed from NGINX.
1087-
func (cnf *Configurator) UpdateGlobalConfiguration(globalConfiguration *conf_v1alpha1.GlobalConfiguration,
1088-
transportServerExes []*TransportServerEx) (updatedTransportServerExes []*TransportServerEx, deletedTransportServerExes []*TransportServerEx, err error) {
1089-
cnf.globalCfgParams = ParseGlobalConfiguration(globalConfiguration, cnf.staticCfgParams.TLSPassthrough)
1090-
1091-
for _, tsEx := range transportServerExes {
1092-
if cnf.CheckIfListenerExists(&tsEx.TransportServer.Spec.Listener) {
1093-
updatedTransportServerExes = append(updatedTransportServerExes, tsEx)
1094-
1095-
err := cnf.addOrUpdateTransportServer(tsEx)
1096-
if err != nil {
1097-
return updatedTransportServerExes, deletedTransportServerExes, fmt.Errorf("Error when updating global configuration: %v", err)
1098-
}
1076+
func (cnf *Configurator) UpdateTransportServers(updatedTSExes []*TransportServerEx, deletedKeys []string) error {
1077+
for _, tsEx := range updatedTSExes {
1078+
err := cnf.addOrUpdateTransportServer(tsEx)
1079+
if err != nil {
1080+
return fmt.Errorf("Error adding or updating TransportServer %v/%v: %v", tsEx.TransportServer.Namespace, tsEx.TransportServer.Name, err)
1081+
}
1082+
}
10991083

1100-
} else {
1101-
deletedTransportServerExes = append(deletedTransportServerExes, tsEx)
1102-
if err != nil {
1103-
return updatedTransportServerExes, deletedTransportServerExes, fmt.Errorf("Error when updating global configuration: %v", err)
1104-
}
1084+
for _, key := range deletedKeys {
1085+
err := cnf.deleteTransportServer(key)
1086+
if err != nil {
1087+
return fmt.Errorf("Error when removing TransportServer %v: %v", key, err)
11051088
}
11061089
}
11071090

11081091
if err := cnf.nginxManager.Reload(nginx.ReloadForOtherUpdate); err != nil {
1109-
return updatedTransportServerExes, deletedTransportServerExes, fmt.Errorf("Error when updating global configuration: %v", err)
1092+
return fmt.Errorf("Error when updating TransportServers: %v", err)
11101093
}
11111094

1112-
return updatedTransportServerExes, deletedTransportServerExes, nil
1095+
return nil
11131096
}
11141097

11151098
func keyToFileName(key string) string {

internal/configs/configurator_test.go

+5-100
Original file line numberDiff line numberDiff line change
@@ -327,122 +327,27 @@ func TestGenerateNamespaceNameKey(t *testing.T) {
327327
}
328328
}
329329

330-
func TestUpdateGlobalConfiguration(t *testing.T) {
331-
globalConfiguration := &conf_v1alpha1.GlobalConfiguration{
332-
Spec: conf_v1alpha1.GlobalConfigurationSpec{
333-
Listeners: []conf_v1alpha1.Listener{
334-
{
335-
Name: "tcp-listener",
336-
Port: 53,
337-
Protocol: "TCP",
338-
},
339-
},
340-
},
341-
}
342-
343-
tsExTCP := &TransportServerEx{
344-
TransportServer: &conf_v1alpha1.TransportServer{
345-
ObjectMeta: meta_v1.ObjectMeta{
346-
Name: "tcp-server",
347-
Namespace: "default",
348-
},
349-
Spec: conf_v1alpha1.TransportServerSpec{
350-
Listener: conf_v1alpha1.TransportServerListener{
351-
Name: "tcp-listener",
352-
Protocol: "TCP",
353-
},
354-
Upstreams: []conf_v1alpha1.Upstream{
355-
{
356-
Name: "tcp-app",
357-
Service: "tcp-app-svc",
358-
Port: 5001,
359-
},
360-
},
361-
Action: &conf_v1alpha1.Action{
362-
Pass: "tcp-app",
363-
},
364-
},
365-
},
366-
}
367-
368-
tsExUDP := &TransportServerEx{
369-
TransportServer: &conf_v1alpha1.TransportServer{
370-
ObjectMeta: meta_v1.ObjectMeta{
371-
Name: "udp-server",
372-
Namespace: "default",
373-
},
374-
Spec: conf_v1alpha1.TransportServerSpec{
375-
Listener: conf_v1alpha1.TransportServerListener{
376-
Name: "udp-listener",
377-
Protocol: "UDP",
378-
},
379-
Upstreams: []conf_v1alpha1.Upstream{
380-
{
381-
Name: "udp-app",
382-
Service: "udp-app-svc",
383-
Port: 5001,
384-
},
385-
},
386-
Action: &conf_v1alpha1.Action{
387-
Pass: "udp-app",
388-
},
389-
},
390-
},
391-
}
392-
393-
cnf, err := createTestConfigurator()
394-
if err != nil {
395-
t.Fatalf("Failed to create a test configurator: %v", err)
396-
}
397-
398-
transportServerExes := []*TransportServerEx{tsExTCP, tsExUDP}
399-
400-
expectedUpdatedTransportServerExes := []*TransportServerEx{tsExTCP}
401-
expectedDeletedTransportServerExes := []*TransportServerEx{tsExUDP}
402-
403-
updatedTransportServerExes, deletedTransportServerExes, err := cnf.UpdateGlobalConfiguration(globalConfiguration, transportServerExes)
404-
405-
if !reflect.DeepEqual(updatedTransportServerExes, expectedUpdatedTransportServerExes) {
406-
t.Errorf("UpdateGlobalConfiguration() returned %v but expected %v", updatedTransportServerExes, expectedUpdatedTransportServerExes)
407-
}
408-
if !reflect.DeepEqual(deletedTransportServerExes, expectedDeletedTransportServerExes) {
409-
t.Errorf("UpdateGlobalConfiguration() returned %v but expected %v", deletedTransportServerExes, expectedDeletedTransportServerExes)
410-
}
411-
if err != nil {
412-
t.Errorf("UpdateGlobalConfiguration() returned an unexpected error %v", err)
413-
}
414-
}
415-
416330
func TestGenerateTLSPassthroughHostsConfig(t *testing.T) {
417331
tlsPassthroughPairs := map[string]tlsPassthroughPair{
418332
"default/ts-1": {
419-
Host: "app.example.com",
333+
Host: "one.example.com",
420334
UnixSocket: "socket1.sock",
421335
},
422336
"default/ts-2": {
423-
Host: "app.example.com",
337+
Host: "two.example.com",
424338
UnixSocket: "socket2.sock",
425339
},
426-
"default/ts-3": {
427-
Host: "some.example.com",
428-
UnixSocket: "socket3.sock",
429-
},
430340
}
431341

432342
expectedCfg := &version2.TLSPassthroughHostsConfig{
433-
"app.example.com": "socket2.sock",
434-
"some.example.com": "socket3.sock",
343+
"one.example.com": "socket1.sock",
344+
"two.example.com": "socket2.sock",
435345
}
436-
expectedDuplicatedHosts := []string{"app.example.com"}
437346

438-
resultCfg, resultDuplicatedHosts := generateTLSPassthroughHostsConfig(tlsPassthroughPairs)
347+
resultCfg := generateTLSPassthroughHostsConfig(tlsPassthroughPairs)
439348
if !reflect.DeepEqual(resultCfg, expectedCfg) {
440349
t.Errorf("generateTLSPassthroughHostsConfig() returned %v but expected %v", resultCfg, expectedCfg)
441350
}
442-
443-
if !reflect.DeepEqual(resultDuplicatedHosts, expectedDuplicatedHosts) {
444-
t.Errorf("generateTLSPassthroughHostsConfig() returned %v but expected %v", resultDuplicatedHosts, expectedDuplicatedHosts)
445-
}
446351
}
447352

448353
func TestAddInternalRouteConfig(t *testing.T) {

internal/configs/transportserver.go

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const nginxNonExistingUnixSocket = "unix:/var/lib/nginx/non-existing-unix-socket
1111

1212
// TransportServerEx holds a TransportServer along with the resources referenced by it.
1313
type TransportServerEx struct {
14+
ListenerPort int
1415
TransportServer *conf_v1alpha1.TransportServer
1516
Endpoints map[string][]string
1617
PodsByIP map[string]string

0 commit comments

Comments
 (0)