Skip to content

Commit

Permalink
Fix statefulset cross-namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
iagodvsantos committed Jun 13, 2023
1 parent 826d37a commit 36d6666
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pkg/qdr/qdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,18 +1056,27 @@ 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
}
}

for iPort, ePort := range ports {
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":
Expand Down
177 changes: 177 additions & 0 deletions pkg/qdr/qdr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
}
12 changes: 12 additions & 0 deletions pkg/service/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -132,6 +143,7 @@ func (bindings *ServiceBindings) AsServiceInterface() types.ServiceInterface {
TlsCredentials: bindings.TlsCredentials,
TlsCertAuthority: bindings.TlsCertAuthority,
PublishNotReadyAddresses: bindings.PublishNotReadyAddresses,
Targets: targets,
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/service/bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ func TestNewServiceBindings(t *testing.T) {
}
}
si := b.AsServiceInterface()
si.Targets = nil
copy := s.service
copy.Targets = nil
assert.DeepEqual(t, si, copy)
Expand Down

0 comments on commit 36d6666

Please sign in to comment.