Skip to content

Commit

Permalink
Merge pull request #141 from ferryproxy/feat/self-mapping
Browse files Browse the repository at this point in the history
Add self mapping
  • Loading branch information
wzshiming authored Sep 29, 2022
2 parents 5e908a1 + bec0aff commit 6adc9d1
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 9 deletions.
32 changes: 31 additions & 1 deletion pkg/router/hubschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,22 @@ func mergeStrings(a, b []string) []string {
}

func (h *HubsChain) buildRaw(name string, origin, destination objref.ObjectRef, originPort, peerPort int32, ways []string) (hubsChains map[string][]*Chain, err error) {

hubsChains = map[string][]*Chain{}

if len(ways) == 1 {
hubName := ways[0]
hubChain, err := h.buildSelf(
name, origin, destination, originPort, peerPort,
)
if err != nil {
return nil, err
}
if hubChain != nil {
hubsChains[hubName] = append(hubsChains[hubName], hubChain)
}
return hubsChains, nil
}

for i := 0; i < len(ways)-1; i++ {
exportHubName := ways[i]
importHubName := ways[i+1]
Expand Down Expand Up @@ -254,6 +267,23 @@ func (h *HubsChain) buildRaw(name string, origin, destination objref.ObjectRef,
return hubsChains, nil
}

func (h *HubsChain) buildSelf(
name string, origin, destination objref.ObjectRef, originPort, peerPort int32,
) (hubChain *Chain, err error) {
chain := &Chain{
Bind: []string{},
Proxy: []string{},
}

destinationAddress := fmt.Sprintf(":%d", peerPort)
chain.Bind = append(chain.Bind, destinationAddress)

originSvc := fmt.Sprintf("%s.%s.svc:%d", origin.Name, origin.Namespace, originPort)
chain.Proxy = append(chain.Proxy, originSvc)

return chain, nil
}

func (h *HubsChain) buildPeer(
name string, origin, destination objref.ObjectRef, originPort, peerPort int32,
exportHubName string, exportRepeater bool, exportGateway v1alpha2.HubSpecGateway,
Expand Down
119 changes: 111 additions & 8 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,110 @@ func TestRouter(t *testing.T) {
args fakeRouter
want map[string][]objref.KMetadata
}{
{
name: "self",
args: fakeRouter{
Services: []*corev1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc1",
Namespace: "test",
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "http",
Port: 80,
Protocol: corev1.ProtocolTCP,
},
},
},
},
},
Hubs: []*v1alpha2.Hub{
{
ObjectMeta: metav1.ObjectMeta{
Name: "self",
},
Spec: v1alpha2.HubSpec{
Gateway: v1alpha2.HubSpecGateway{
Reachable: true,
Address: "10.0.0.1:8080",
},
},
},
},
Routes: []*v1alpha2.Route{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc1",
Namespace: "test",
},
Spec: v1alpha2.RouteSpec{
Import: v1alpha2.RouteSpecRule{
HubName: "self",
Service: v1alpha2.RouteSpecRuleService{
Name: "svc1-new",
Namespace: "test",
},
},
Export: v1alpha2.RouteSpecRule{
HubName: "self",
Service: v1alpha2.RouteSpecRuleService{
Name: "svc1",
Namespace: "test",
},
},
},
},
},
},

want: map[string][]objref.KMetadata{
"self": {
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "svc1-service",
Namespace: "ferry-tunnel-system",
Labels: map[string]string{
"tunnel.ferryproxy.io/config": "service",
},
},
Data: map[string]string{
"export_hub_name": "self",
"export_service_name": "svc1",
"export_service_namespace": "test",
"import_service_name": "svc1-new",
"import_service_namespace": "test",
"ports": `[{"name":"http","protocol":"TCP","port":80,"targetPort":10001}]`,
},
},
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "svc1-tunnel-80-10001",
Namespace: "ferry-tunnel-system",
Labels: map[string]string{
"tunnel.ferryproxy.io/config": "rules",
},
},
Data: map[string]string{
consts.TunnelRulesKey: toJson(
[]Chain{
{
Bind: []string{
":10001",
},
Proxy: []string{
"svc1.test.svc:80",
},
},
},
),
},
},
},
},
},
{
name: "export reachable",
args: fakeRouter{
Expand Down Expand Up @@ -104,7 +208,7 @@ func TestRouter(t *testing.T) {
},
Spec: v1alpha2.RouteSpec{
Import: v1alpha2.RouteSpecRule{
HubName: "export",
HubName: "import",
Service: v1alpha2.RouteSpecRuleService{
Name: "svc1",
Namespace: "test",
Expand Down Expand Up @@ -275,7 +379,7 @@ func TestRouter(t *testing.T) {
},
Spec: v1alpha2.RouteSpec{
Import: v1alpha2.RouteSpecRule{
HubName: "export",
HubName: "import",
Service: v1alpha2.RouteSpecRuleService{
Name: "svc1",
Namespace: "test",
Expand Down Expand Up @@ -448,7 +552,7 @@ func TestRouter(t *testing.T) {
},
Spec: v1alpha2.RouteSpec{
Import: v1alpha2.RouteSpecRule{
HubName: "export",
HubName: "import",
Service: v1alpha2.RouteSpecRuleService{
Name: "svc1",
Namespace: "test",
Expand Down Expand Up @@ -634,22 +738,21 @@ func (f *fakeRouter) BuildResource() (out map[string][]objref.KMetadata, err err
portCache: map[string]int{},
}

exportHubName := "export"
importHubName := "import"
route := f.Routes[0]

solution := Solution{
getHubGateway: fake.GetHubGateway,
}

ways, err := solution.CalculateWays(exportHubName, importHubName)
ways, err := solution.CalculateWays(route.Spec.Export.HubName, route.Spec.Import.HubName)
if err != nil {
return nil, err
}

router := NewRouter(RouterConfig{
Labels: map[string]string{},
ExportHubName: exportHubName,
ImportHubName: importHubName,
ExportHubName: route.Spec.Export.HubName,
ImportHubName: route.Spec.Import.HubName,
HubInterface: fake,
})

Expand Down

0 comments on commit 6adc9d1

Please sign in to comment.