Skip to content

Commit b315177

Browse files
committed
Update ZoneSync main config,add validation, add dynamic domain
1 parent 3407b65 commit b315177

File tree

4 files changed

+90
-27
lines changed

4 files changed

+90
-27
lines changed

internal/configs/config_params.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ type ConfigParams struct {
9090
UseClusterIP bool
9191
VariablesHashBucketSize uint64
9292
VariablesHashMaxSize uint64
93-
94-
ZoneSync bool
95-
ZoneSyncPort int
93+
ZoneSync ZoneSync
9694

9795
RealIPHeader string
9896
RealIPRecursive bool
@@ -178,6 +176,13 @@ type Listener struct {
178176
Protocol string
179177
}
180178

179+
// ZoneSync holds zone sync values for state sharing.
180+
type ZoneSync struct {
181+
EnableZoneSync bool
182+
Port int
183+
Domain string
184+
}
185+
181186
// MGMTSecrets holds mgmt block secret names
182187
type MGMTSecrets struct {
183188
License string

internal/configs/configmaps.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"os"
78
"strings"
89
"time"
910

@@ -406,7 +407,14 @@ func ParseConfigMap(ctx context.Context, cfgm *v1.ConfigMap, nginxPlus bool, has
406407
eventLog.Event(cfgm, v1.EventTypeWarning, invalidValueReason, err.Error())
407408
configOk = false
408409
} else {
409-
cfgParams.ZoneSync = zoneSync
410+
if nginxPlus {
411+
cfgParams.ZoneSync.EnableZoneSync = zoneSync
412+
} else {
413+
errorText := fmt.Sprintf("ConfigMap %s/%s key %s requires NGINX Plus", cfgm.Namespace, cfgm.Name, "zone-sync")
414+
nl.Warn(l, errorText)
415+
eventLog.Event(cfgm, v1.EventTypeWarning, invalidValueReason, errorText)
416+
configOk = false
417+
}
410418
}
411419
}
412420

@@ -416,13 +424,20 @@ func ParseConfigMap(ctx context.Context, cfgm *v1.ConfigMap, nginxPlus bool, has
416424
eventLog.Event(cfgm, v1.EventTypeWarning, invalidValueReason, err.Error())
417425
configOk = false
418426
} else {
419-
statusPortValidationError := validation.ValidatePort(zoneSyncPort)
420-
if statusPortValidationError != nil {
421-
nl.Error(l, statusPortValidationError)
422-
eventLog.Event(cfgm, v1.EventTypeWarning, invalidValueReason, statusPortValidationError.Error())
423-
configOk = false
427+
if cfgParams.ZoneSync.EnableZoneSync {
428+
portValidationError := validation.ValidatePort(zoneSyncPort)
429+
if portValidationError != nil {
430+
nl.Error(l, portValidationError)
431+
eventLog.Event(cfgm, v1.EventTypeWarning, invalidValueReason, portValidationError.Error())
432+
configOk = false
433+
} else {
434+
cfgParams.ZoneSync.Port = zoneSyncPort
435+
}
424436
} else {
425-
cfgParams.ZoneSyncPort = zoneSyncPort
437+
errorText := fmt.Sprintf("ConfigMap %s/%s key %s requires 'zone-sync' to be enabled", cfgm.Namespace, cfgm.Name, "zone-sync-port")
438+
nl.Warn(l, errorText)
439+
eventLog.Event(cfgm, v1.EventTypeWarning, invalidValueReason, errorText)
440+
configOk = false
426441
}
427442
}
428443
}
@@ -805,9 +820,11 @@ func GenerateNginxMainConfig(staticCfgParams *StaticConfigParams, config *Config
805820
}
806821
}
807822

808-
var zoneSyncConfig version1.ZoneSyncConfig = version1.ZoneSyncConfig{
809-
ZoneSync: config.ZoneSync,
810-
ZoneSyncPort: config.ZoneSyncPort,
823+
podNamespace := os.Getenv("POD_NAMESPACE")
824+
zoneSyncConfig := version1.ZoneSyncConfig{
825+
EnableZoneSync: config.ZoneSync.EnableZoneSync,
826+
Port: config.ZoneSync.Port,
827+
Domain: fmt.Sprintf("%s-headless.%s.svc.cluster.local", podNamespace, podNamespace),
811828
}
812829

813830
nginxCfg := &version1.MainConfig{

internal/configs/configmaps_test.go

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package configs
22

33
import (
44
"context"
5+
"fmt"
6+
"os"
57
"testing"
68

9+
"github.com/nginx/kubernetes-ingress/internal/configs/version1"
10+
711
v1 "k8s.io/api/core/v1"
812
"k8s.io/client-go/tools/record"
913
)
@@ -792,7 +796,7 @@ func TestParseZoneSync(t *testing.T) {
792796
t.Parallel()
793797
tests := []struct {
794798
configMap *v1.ConfigMap
795-
want *ConfigParams
799+
want *ZoneSync
796800
msg string
797801
}{
798802
{
@@ -801,8 +805,8 @@ func TestParseZoneSync(t *testing.T) {
801805
"zone-sync": "true",
802806
},
803807
},
804-
want: &ConfigParams{
805-
ZoneSync: true,
808+
want: &ZoneSync{
809+
EnableZoneSync: true,
806810
},
807811
msg: "zone-sync set to true",
808812
},
@@ -812,8 +816,8 @@ func TestParseZoneSync(t *testing.T) {
812816
"zone-sync": "false",
813817
},
814818
},
815-
want: &ConfigParams{
816-
ZoneSync: false,
819+
want: &ZoneSync{
820+
EnableZoneSync: false,
817821
},
818822
msg: "zone-sync set to false",
819823
},
@@ -822,8 +826,8 @@ func TestParseZoneSync(t *testing.T) {
822826
for _, test := range tests {
823827
t.Run(test.msg, func(t *testing.T) {
824828
result, _ := ParseConfigMap(context.Background(), test.configMap, true, false, false, false, makeEventLogger())
825-
if result.ZoneSync != test.want.ZoneSync {
826-
t.Errorf("ZoneSync: want %v, got %v", test.want.ZoneSync, result.ZoneSync)
829+
if result.ZoneSync.EnableZoneSync != test.want.EnableZoneSync {
830+
t.Errorf("EnableZoneSync: want %v, got %v", test.want.EnableZoneSync, result.ZoneSync)
827831
}
828832
})
829833
}
@@ -833,7 +837,7 @@ func TestParseZoneSyncPort(t *testing.T) {
833837
t.Parallel()
834838
tests := []struct {
835839
configMap *v1.ConfigMap
836-
want *ConfigParams
840+
want *ZoneSync
837841
msg string
838842
}{
839843
{
@@ -842,8 +846,8 @@ func TestParseZoneSyncPort(t *testing.T) {
842846
"zone-sync-port": "1234",
843847
},
844848
},
845-
want: &ConfigParams{
846-
ZoneSyncPort: 1234,
849+
want: &ZoneSync{
850+
Port: 1234,
847851
},
848852
msg: "zone-sync-port set to 1234",
849853
},
@@ -857,8 +861,8 @@ func TestParseZoneSyncPort(t *testing.T) {
857861
for _, test := range tests {
858862
t.Run(test.msg, func(t *testing.T) {
859863
result, _ := ParseConfigMap(context.Background(), test.configMap, nginxPlus, hasAppProtect, hasAppProtectDos, hasTLSPassthrough, makeEventLogger())
860-
if result.ZoneSyncPort != test.want.ZoneSyncPort {
861-
t.Errorf("ZoneSyncPort: want %v, got %v", test.want.ZoneSyncPort, result.ZoneSyncPort)
864+
if result.ZoneSync.Port != test.want.Port {
865+
t.Errorf("Port: want %v, got %v", test.want.Port, result.ZoneSync.EnableZoneSync)
862866
}
863867
})
864868
}
@@ -933,6 +937,42 @@ func TestParseZoneSyncPortErrors(t *testing.T) {
933937
}
934938
}
935939

940+
func TestMultipleNamespaces(t *testing.T) {
941+
testCases := []struct {
942+
name string
943+
namespace string
944+
want string
945+
}{
946+
{
947+
name: "nginx-ingress",
948+
namespace: "nginx-ingress",
949+
want: "nginx-ingress-headless.nginx-ingress.svc.cluster.local",
950+
},
951+
{
952+
name: "my-release-nginx-ingress",
953+
namespace: "my-release-nginx-ingress",
954+
want: "my-release-nginx-ingress-headless.my-release-nginx-ingress.svc.cluster.local",
955+
},
956+
}
957+
958+
for _, tc := range testCases {
959+
t.Run(tc.name, func(t *testing.T) {
960+
_ = os.Setenv("POD_NAMESPACE", tc.namespace)
961+
962+
zoneSyncConfig := version1.ZoneSyncConfig{
963+
EnableZoneSync: true,
964+
Domain: fmt.Sprintf("%s-headless.%s.svc.cluster.local",
965+
os.Getenv("POD_NAMESPACE"),
966+
os.Getenv("POD_NAMESPACE")),
967+
}
968+
969+
if zoneSyncConfig.Domain != tc.want {
970+
t.Errorf("want %q, got %q", tc.want, zoneSyncConfig.Domain)
971+
}
972+
})
973+
}
974+
}
975+
936976
func makeEventLogger() record.EventRecorder {
937977
return record.NewFakeRecorder(1024)
938978
}

internal/configs/version1/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ type Location struct {
191191

192192
// ZoneSyncConfig is tbe configuration for the zone_sync directives for state sharing.
193193
type ZoneSyncConfig struct {
194-
ZoneSync bool
195-
ZoneSyncPort int
194+
EnableZoneSync bool
195+
Port int
196+
Domain string
196197
}
197198

198199
// MGMTConfig is tbe configuration for the MGMT block.

0 commit comments

Comments
 (0)