Skip to content

Commit 03cb802

Browse files
committed
Fix statefulset cross-namespace
1 parent d9aaf1c commit 03cb802

4 files changed

Lines changed: 202 additions & 3 deletions

File tree

pkg/qdr/qdr.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,18 +1056,27 @@ func GetRouterConfigForHeadlessProxy(definition types.ServiceInterface, siteId s
10561056
})
10571057
svcPorts := definition.Ports
10581058
ports := map[int]int{}
1059+
targetNs := namespace
1060+
10591061
if len(definition.Targets) > 0 {
1060-
ports = definition.Targets[0].TargetPorts
1061-
} else {
1062+
target := definition.Targets[0]
1063+
targetNs = target.Namespace
1064+
if len(target.TargetPorts) > 0 {
1065+
ports = target.TargetPorts
1066+
}
1067+
}
1068+
1069+
if len(ports) == 0 {
10621070
for _, sp := range svcPorts {
10631071
ports[sp] = sp
10641072
}
10651073
}
1074+
10661075
for iPort, ePort := range ports {
10671076
address := fmt.Sprintf("%s-%s:%d", definition.Address, "${POD_ID}", iPort)
10681077
if definition.IsOfLocalOrigin() {
10691078
name := fmt.Sprintf("egress:%d", ePort)
1070-
host := definition.Headless.Name + "-${POD_ID}." + definition.Address + "." + namespace
1079+
host := definition.Headless.Name + "-${POD_ID}." + definition.Address + "." + targetNs
10711080
// in the originating site, just have egress bindings
10721081
switch definition.Protocol {
10731082
case "tcp":

pkg/qdr/qdr_test.go

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,180 @@ func TestGetSslProfilesDifference(t *testing.T) {
559559
assert.Assert(t, utils.StringSlicesEqual(deletedSslProfiles, expectedDeletedSslProfiles), "Expected %v but got %v", expectedDeletedSslProfiles, deletedSslProfiles)
560560

561561
}
562+
563+
func TestGetRouterConfigForHeadlessProxy(t *testing.T) {
564+
type test struct {
565+
name string
566+
definition types.ServiceInterface
567+
site string
568+
version string
569+
namespace string
570+
expectedResult BridgeConfig
571+
}
572+
573+
testTable := []test{
574+
{
575+
name: "definition-with-targets",
576+
definition: types.ServiceInterface{
577+
Origin: "",
578+
Address: "address",
579+
Protocol: "tcp",
580+
Ports: []int{9000},
581+
Headless: &types.Headless{
582+
Name: "dummy",
583+
},
584+
Targets: []types.ServiceInterfaceTarget{{
585+
Name: "target-1",
586+
Selector: "app=dummy",
587+
Namespace: "default",
588+
TargetPorts: map[int]int{
589+
3000: 3000,
590+
},
591+
}},
592+
},
593+
site: "site-1",
594+
version: "version-1",
595+
namespace: "namespace-1",
596+
expectedResult: BridgeConfig{
597+
TcpConnectors: TcpEndpointMap{
598+
"egress:3000": TcpEndpoint{
599+
Name: "egress:3000",
600+
Host: "dummy-${POD_ID}.address.default",
601+
Port: "3000",
602+
Address: "address-${POD_ID}:3000",
603+
SiteId: "site-1",
604+
},
605+
},
606+
TcpListeners: TcpEndpointMap{},
607+
HttpConnectors: HttpEndpointMap{},
608+
HttpListeners: HttpEndpointMap{},
609+
},
610+
},
611+
{
612+
name: "definition-without-targets",
613+
definition: types.ServiceInterface{
614+
Origin: "",
615+
Address: "address",
616+
Protocol: "tcp",
617+
Ports: []int{9000},
618+
Headless: &types.Headless{
619+
Name: "dummy",
620+
},
621+
},
622+
site: "site-1",
623+
version: "version-1",
624+
namespace: "namespace-1",
625+
expectedResult: BridgeConfig{
626+
TcpConnectors: TcpEndpointMap{
627+
"egress:9000": TcpEndpoint{
628+
Name: "egress:9000",
629+
Host: "dummy-${POD_ID}.address.namespace-1",
630+
Port: "9000",
631+
Address: "address-${POD_ID}:9000",
632+
SiteId: "site-1",
633+
},
634+
},
635+
TcpListeners: TcpEndpointMap{},
636+
HttpConnectors: HttpEndpointMap{},
637+
HttpListeners: HttpEndpointMap{},
638+
},
639+
},
640+
{
641+
name: "definition-without-targets-http",
642+
definition: types.ServiceInterface{
643+
Origin: "",
644+
Address: "address",
645+
Protocol: "http",
646+
Ports: []int{9000},
647+
Headless: &types.Headless{
648+
Name: "dummy",
649+
},
650+
},
651+
site: "site-1",
652+
version: "version-1",
653+
namespace: "namespace-1",
654+
expectedResult: BridgeConfig{
655+
TcpConnectors: TcpEndpointMap{},
656+
TcpListeners: TcpEndpointMap{},
657+
HttpConnectors: HttpEndpointMap{
658+
"egress:9000": HttpEndpoint{
659+
Name: "egress:9000",
660+
Host: "dummy-${POD_ID}.address.namespace-1",
661+
Port: "9000",
662+
Address: "address-${POD_ID}:9000",
663+
SiteId: "site-1",
664+
},
665+
},
666+
HttpListeners: HttpEndpointMap{},
667+
},
668+
},
669+
{
670+
name: "definition-external-origin",
671+
definition: types.ServiceInterface{
672+
Origin: "external",
673+
Address: "address",
674+
Protocol: "tcp",
675+
Ports: []int{9000},
676+
Headless: &types.Headless{
677+
Name: "dummy",
678+
},
679+
},
680+
site: "site-1",
681+
version: "version-1",
682+
namespace: "namespace-1",
683+
expectedResult: BridgeConfig{
684+
TcpConnectors: TcpEndpointMap{},
685+
TcpListeners: TcpEndpointMap{
686+
"ingress:9000": TcpEndpoint{
687+
Name: "ingress:9000",
688+
Host: "",
689+
Port: "9000",
690+
Address: "address-${POD_ID}:9000",
691+
SiteId: "site-1",
692+
},
693+
},
694+
HttpConnectors: HttpEndpointMap{},
695+
HttpListeners: HttpEndpointMap{},
696+
},
697+
},
698+
{
699+
name: "definition-external-http",
700+
definition: types.ServiceInterface{
701+
Origin: "external",
702+
Address: "address",
703+
Protocol: "http",
704+
Ports: []int{9000},
705+
Headless: &types.Headless{
706+
Name: "dummy",
707+
},
708+
},
709+
site: "site-1",
710+
version: "version-1",
711+
namespace: "namespace-1",
712+
expectedResult: BridgeConfig{
713+
TcpConnectors: TcpEndpointMap{},
714+
TcpListeners: TcpEndpointMap{},
715+
HttpConnectors: HttpEndpointMap{},
716+
HttpListeners: HttpEndpointMap{
717+
"ingress:9000": HttpEndpoint{
718+
Name: "ingress:9000",
719+
Host: "",
720+
Port: "9000",
721+
Address: "address-${POD_ID}:9000",
722+
SiteId: "site-1",
723+
},
724+
},
725+
},
726+
},
727+
}
728+
729+
for _, test := range testTable {
730+
t.Run(test.name, func(t *testing.T) {
731+
expectedResult := test.expectedResult
732+
actualResult, _ := GetRouterConfigForHeadlessProxy(test.definition, test.site, test.version, test.namespace)
733+
routerConfig, _ := UnmarshalRouterConfig(actualResult)
734+
735+
assert.Assert(t, reflect.DeepEqual(expectedResult, routerConfig.Bridges))
736+
})
737+
}
738+
}

pkg/service/bindings.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ func (bindings *ServiceBindings) AsServiceInterface() types.ServiceInterface {
118118
if bindings.ingressBinding != nil {
119119
mode = bindings.ingressBinding.Mode()
120120
}
121+
122+
targets := []types.ServiceInterfaceTarget{}
123+
for key, egress := range bindings.targets {
124+
targets = append(targets, types.ServiceInterfaceTarget{
125+
Name: egress.name,
126+
Selector: egress.Selector,
127+
Service: egress.service,
128+
Namespace: key.namespace,
129+
})
130+
}
131+
121132
return types.ServiceInterface{
122133
Address: bindings.Address,
123134
Protocol: bindings.protocol,
@@ -132,6 +143,7 @@ func (bindings *ServiceBindings) AsServiceInterface() types.ServiceInterface {
132143
TlsCredentials: bindings.TlsCredentials,
133144
TlsCertAuthority: bindings.TlsCertAuthority,
134145
PublishNotReadyAddresses: bindings.PublishNotReadyAddresses,
146+
Targets: targets,
135147
}
136148
}
137149

pkg/service/bindings_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ func TestNewServiceBindings(t *testing.T) {
530530
}
531531
}
532532
si := b.AsServiceInterface()
533+
si.Targets = nil
533534
copy := s.service
534535
copy.Targets = nil
535536
assert.DeepEqual(t, si, copy)

0 commit comments

Comments
 (0)