Skip to content

Commit 6adc9d1

Browse files
authored
Merge pull request #141 from ferryproxy/feat/self-mapping
Add self mapping
2 parents 5e908a1 + bec0aff commit 6adc9d1

File tree

2 files changed

+142
-9
lines changed

2 files changed

+142
-9
lines changed

pkg/router/hubschain.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,22 @@ func mergeStrings(a, b []string) []string {
220220
}
221221

222222
func (h *HubsChain) buildRaw(name string, origin, destination objref.ObjectRef, originPort, peerPort int32, ways []string) (hubsChains map[string][]*Chain, err error) {
223-
224223
hubsChains = map[string][]*Chain{}
225224

225+
if len(ways) == 1 {
226+
hubName := ways[0]
227+
hubChain, err := h.buildSelf(
228+
name, origin, destination, originPort, peerPort,
229+
)
230+
if err != nil {
231+
return nil, err
232+
}
233+
if hubChain != nil {
234+
hubsChains[hubName] = append(hubsChains[hubName], hubChain)
235+
}
236+
return hubsChains, nil
237+
}
238+
226239
for i := 0; i < len(ways)-1; i++ {
227240
exportHubName := ways[i]
228241
importHubName := ways[i+1]
@@ -254,6 +267,23 @@ func (h *HubsChain) buildRaw(name string, origin, destination objref.ObjectRef,
254267
return hubsChains, nil
255268
}
256269

270+
func (h *HubsChain) buildSelf(
271+
name string, origin, destination objref.ObjectRef, originPort, peerPort int32,
272+
) (hubChain *Chain, err error) {
273+
chain := &Chain{
274+
Bind: []string{},
275+
Proxy: []string{},
276+
}
277+
278+
destinationAddress := fmt.Sprintf(":%d", peerPort)
279+
chain.Bind = append(chain.Bind, destinationAddress)
280+
281+
originSvc := fmt.Sprintf("%s.%s.svc:%d", origin.Name, origin.Namespace, originPort)
282+
chain.Proxy = append(chain.Proxy, originSvc)
283+
284+
return chain, nil
285+
}
286+
257287
func (h *HubsChain) buildPeer(
258288
name string, origin, destination objref.ObjectRef, originPort, peerPort int32,
259289
exportHubName string, exportRepeater bool, exportGateway v1alpha2.HubSpecGateway,

pkg/router/router_test.go

Lines changed: 111 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,110 @@ func TestRouter(t *testing.T) {
3636
args fakeRouter
3737
want map[string][]objref.KMetadata
3838
}{
39+
{
40+
name: "self",
41+
args: fakeRouter{
42+
Services: []*corev1.Service{
43+
{
44+
ObjectMeta: metav1.ObjectMeta{
45+
Name: "svc1",
46+
Namespace: "test",
47+
},
48+
Spec: corev1.ServiceSpec{
49+
Ports: []corev1.ServicePort{
50+
{
51+
Name: "http",
52+
Port: 80,
53+
Protocol: corev1.ProtocolTCP,
54+
},
55+
},
56+
},
57+
},
58+
},
59+
Hubs: []*v1alpha2.Hub{
60+
{
61+
ObjectMeta: metav1.ObjectMeta{
62+
Name: "self",
63+
},
64+
Spec: v1alpha2.HubSpec{
65+
Gateway: v1alpha2.HubSpecGateway{
66+
Reachable: true,
67+
Address: "10.0.0.1:8080",
68+
},
69+
},
70+
},
71+
},
72+
Routes: []*v1alpha2.Route{
73+
{
74+
ObjectMeta: metav1.ObjectMeta{
75+
Name: "svc1",
76+
Namespace: "test",
77+
},
78+
Spec: v1alpha2.RouteSpec{
79+
Import: v1alpha2.RouteSpecRule{
80+
HubName: "self",
81+
Service: v1alpha2.RouteSpecRuleService{
82+
Name: "svc1-new",
83+
Namespace: "test",
84+
},
85+
},
86+
Export: v1alpha2.RouteSpecRule{
87+
HubName: "self",
88+
Service: v1alpha2.RouteSpecRuleService{
89+
Name: "svc1",
90+
Namespace: "test",
91+
},
92+
},
93+
},
94+
},
95+
},
96+
},
97+
98+
want: map[string][]objref.KMetadata{
99+
"self": {
100+
&corev1.ConfigMap{
101+
ObjectMeta: metav1.ObjectMeta{
102+
Name: "svc1-service",
103+
Namespace: "ferry-tunnel-system",
104+
Labels: map[string]string{
105+
"tunnel.ferryproxy.io/config": "service",
106+
},
107+
},
108+
Data: map[string]string{
109+
"export_hub_name": "self",
110+
"export_service_name": "svc1",
111+
"export_service_namespace": "test",
112+
"import_service_name": "svc1-new",
113+
"import_service_namespace": "test",
114+
"ports": `[{"name":"http","protocol":"TCP","port":80,"targetPort":10001}]`,
115+
},
116+
},
117+
&corev1.ConfigMap{
118+
ObjectMeta: metav1.ObjectMeta{
119+
Name: "svc1-tunnel-80-10001",
120+
Namespace: "ferry-tunnel-system",
121+
Labels: map[string]string{
122+
"tunnel.ferryproxy.io/config": "rules",
123+
},
124+
},
125+
Data: map[string]string{
126+
consts.TunnelRulesKey: toJson(
127+
[]Chain{
128+
{
129+
Bind: []string{
130+
":10001",
131+
},
132+
Proxy: []string{
133+
"svc1.test.svc:80",
134+
},
135+
},
136+
},
137+
),
138+
},
139+
},
140+
},
141+
},
142+
},
39143
{
40144
name: "export reachable",
41145
args: fakeRouter{
@@ -104,7 +208,7 @@ func TestRouter(t *testing.T) {
104208
},
105209
Spec: v1alpha2.RouteSpec{
106210
Import: v1alpha2.RouteSpecRule{
107-
HubName: "export",
211+
HubName: "import",
108212
Service: v1alpha2.RouteSpecRuleService{
109213
Name: "svc1",
110214
Namespace: "test",
@@ -275,7 +379,7 @@ func TestRouter(t *testing.T) {
275379
},
276380
Spec: v1alpha2.RouteSpec{
277381
Import: v1alpha2.RouteSpecRule{
278-
HubName: "export",
382+
HubName: "import",
279383
Service: v1alpha2.RouteSpecRuleService{
280384
Name: "svc1",
281385
Namespace: "test",
@@ -448,7 +552,7 @@ func TestRouter(t *testing.T) {
448552
},
449553
Spec: v1alpha2.RouteSpec{
450554
Import: v1alpha2.RouteSpecRule{
451-
HubName: "export",
555+
HubName: "import",
452556
Service: v1alpha2.RouteSpecRuleService{
453557
Name: "svc1",
454558
Namespace: "test",
@@ -634,22 +738,21 @@ func (f *fakeRouter) BuildResource() (out map[string][]objref.KMetadata, err err
634738
portCache: map[string]int{},
635739
}
636740

637-
exportHubName := "export"
638-
importHubName := "import"
741+
route := f.Routes[0]
639742

640743
solution := Solution{
641744
getHubGateway: fake.GetHubGateway,
642745
}
643746

644-
ways, err := solution.CalculateWays(exportHubName, importHubName)
747+
ways, err := solution.CalculateWays(route.Spec.Export.HubName, route.Spec.Import.HubName)
645748
if err != nil {
646749
return nil, err
647750
}
648751

649752
router := NewRouter(RouterConfig{
650753
Labels: map[string]string{},
651-
ExportHubName: exportHubName,
652-
ImportHubName: importHubName,
754+
ExportHubName: route.Spec.Export.HubName,
755+
ImportHubName: route.Spec.Import.HubName,
653756
HubInterface: fake,
654757
})
655758

0 commit comments

Comments
 (0)