diff --git a/pkg/qdr/qdr.go b/pkg/qdr/qdr.go index 681338a9a..0e80660df 100644 --- a/pkg/qdr/qdr.go +++ b/pkg/qdr/qdr.go @@ -1056,9 +1056,17 @@ func GetRouterConfigForHeadlessProxy(definition types.ServiceInterface, siteId s }) svcPorts := definition.Ports ports := map[int]int{} + targetNs := namespace + if len(definition.Targets) > 0 { - ports = definition.Targets[0].TargetPorts - } else { + target := definition.Targets[0] + targetNs = target.Namespace + if len(target.TargetPorts) > 0 { + ports = target.TargetPorts + } + } + + if len(ports) == 0 { for _, sp := range svcPorts { ports[sp] = sp } @@ -1067,7 +1075,7 @@ func GetRouterConfigForHeadlessProxy(definition types.ServiceInterface, siteId s address := fmt.Sprintf("%s-%s:%d", definition.Address, "${POD_ID}", iPort) if definition.IsOfLocalOrigin() { name := fmt.Sprintf("egress:%d", ePort) - host := definition.Headless.Name + "-${POD_ID}." + definition.Address + "." + namespace + host := definition.Headless.Name + "-${POD_ID}." + definition.Address + "." + targetNs // in the originating site, just have egress bindings switch definition.Protocol { case "tcp": diff --git a/pkg/qdr/qdr_test.go b/pkg/qdr/qdr_test.go index c68d3f225..090e3ce6c 100644 --- a/pkg/qdr/qdr_test.go +++ b/pkg/qdr/qdr_test.go @@ -559,3 +559,180 @@ func TestGetSslProfilesDifference(t *testing.T) { assert.Assert(t, utils.StringSlicesEqual(deletedSslProfiles, expectedDeletedSslProfiles), "Expected %v but got %v", expectedDeletedSslProfiles, deletedSslProfiles) } + +func TestGetRouterConfigForHeadlessProxy(t *testing.T) { + type test struct { + name string + definition types.ServiceInterface + site string + version string + namespace string + expectedResult BridgeConfig + } + + testTable := []test{ + { + name: "definition-with-targets", + definition: types.ServiceInterface{ + Origin: "", + Address: "address", + Protocol: "tcp", + Ports: []int{9000}, + Headless: &types.Headless{ + Name: "dummy", + }, + Targets: []types.ServiceInterfaceTarget{{ + Name: "target-1", + Selector: "app=dummy", + Namespace: "default", + TargetPorts: map[int]int{ + 3000: 3000, + }, + }}, + }, + site: "site-1", + version: "version-1", + namespace: "namespace-1", + expectedResult: BridgeConfig{ + TcpConnectors: TcpEndpointMap{ + "egress:3000": TcpEndpoint{ + Name: "egress:3000", + Host: "dummy-${POD_ID}.address.default", + Port: "3000", + Address: "address-${POD_ID}:3000", + SiteId: "site-1", + }, + }, + TcpListeners: TcpEndpointMap{}, + HttpConnectors: HttpEndpointMap{}, + HttpListeners: HttpEndpointMap{}, + }, + }, + { + name: "definition-without-targets", + definition: types.ServiceInterface{ + Origin: "", + Address: "address", + Protocol: "tcp", + Ports: []int{9000}, + Headless: &types.Headless{ + Name: "dummy", + }, + }, + site: "site-1", + version: "version-1", + namespace: "namespace-1", + expectedResult: BridgeConfig{ + TcpConnectors: TcpEndpointMap{ + "egress:9000": TcpEndpoint{ + Name: "egress:9000", + Host: "dummy-${POD_ID}.address.namespace-1", + Port: "9000", + Address: "address-${POD_ID}:9000", + SiteId: "site-1", + }, + }, + TcpListeners: TcpEndpointMap{}, + HttpConnectors: HttpEndpointMap{}, + HttpListeners: HttpEndpointMap{}, + }, + }, + { + name: "definition-without-targets-http", + definition: types.ServiceInterface{ + Origin: "", + Address: "address", + Protocol: "http", + Ports: []int{9000}, + Headless: &types.Headless{ + Name: "dummy", + }, + }, + site: "site-1", + version: "version-1", + namespace: "namespace-1", + expectedResult: BridgeConfig{ + TcpConnectors: TcpEndpointMap{}, + TcpListeners: TcpEndpointMap{}, + HttpConnectors: HttpEndpointMap{ + "egress:9000": HttpEndpoint{ + Name: "egress:9000", + Host: "dummy-${POD_ID}.address.namespace-1", + Port: "9000", + Address: "address-${POD_ID}:9000", + SiteId: "site-1", + }, + }, + HttpListeners: HttpEndpointMap{}, + }, + }, + { + name: "definition-external-origin", + definition: types.ServiceInterface{ + Origin: "external", + Address: "address", + Protocol: "tcp", + Ports: []int{9000}, + Headless: &types.Headless{ + Name: "dummy", + }, + }, + site: "site-1", + version: "version-1", + namespace: "namespace-1", + expectedResult: BridgeConfig{ + TcpConnectors: TcpEndpointMap{}, + TcpListeners: TcpEndpointMap{ + "ingress:9000": TcpEndpoint{ + Name: "ingress:9000", + Host: "", + Port: "9000", + Address: "address-${POD_ID}:9000", + SiteId: "site-1", + }, + }, + HttpConnectors: HttpEndpointMap{}, + HttpListeners: HttpEndpointMap{}, + }, + }, + { + name: "definition-external-http", + definition: types.ServiceInterface{ + Origin: "external", + Address: "address", + Protocol: "http", + Ports: []int{9000}, + Headless: &types.Headless{ + Name: "dummy", + }, + }, + site: "site-1", + version: "version-1", + namespace: "namespace-1", + expectedResult: BridgeConfig{ + TcpConnectors: TcpEndpointMap{}, + TcpListeners: TcpEndpointMap{}, + HttpConnectors: HttpEndpointMap{}, + HttpListeners: HttpEndpointMap{ + "ingress:9000": HttpEndpoint{ + Name: "ingress:9000", + Host: "", + Port: "9000", + Address: "address-${POD_ID}:9000", + SiteId: "site-1", + }, + }, + }, + }, + } + + for _, test := range testTable { + t.Run(test.name, func(t *testing.T) { + expectedResult := test.expectedResult + actualResult, _ := GetRouterConfigForHeadlessProxy(test.definition, test.site, test.version, test.namespace) + routerConfig, _ := UnmarshalRouterConfig(actualResult) + + assert.Assert(t, reflect.DeepEqual(expectedResult, routerConfig.Bridges)) + }) + } +} diff --git a/pkg/service/bindings.go b/pkg/service/bindings.go index f072b5d82..79fb96505 100644 --- a/pkg/service/bindings.go +++ b/pkg/service/bindings.go @@ -118,6 +118,17 @@ func (bindings *ServiceBindings) AsServiceInterface() types.ServiceInterface { if bindings.ingressBinding != nil { mode = bindings.ingressBinding.Mode() } + + targets := []types.ServiceInterfaceTarget{} + for key, egress := range bindings.targets { + targets = append(targets, types.ServiceInterfaceTarget{ + Name: egress.name, + Selector: egress.Selector, + Service: egress.service, + Namespace: key.namespace, + }) + } + return types.ServiceInterface{ Address: bindings.Address, Protocol: bindings.protocol, @@ -132,6 +143,7 @@ func (bindings *ServiceBindings) AsServiceInterface() types.ServiceInterface { TlsCredentials: bindings.TlsCredentials, TlsCertAuthority: bindings.TlsCertAuthority, PublishNotReadyAddresses: bindings.PublishNotReadyAddresses, + Targets: targets, } } diff --git a/pkg/service/bindings_test.go b/pkg/service/bindings_test.go index e449e20d5..db9ebac82 100644 --- a/pkg/service/bindings_test.go +++ b/pkg/service/bindings_test.go @@ -530,6 +530,7 @@ func TestNewServiceBindings(t *testing.T) { } } si := b.AsServiceInterface() + si.Targets = nil copy := s.service copy.Targets = nil assert.DeepEqual(t, si, copy)