Skip to content

Commit 60aa12d

Browse files
committed
feat(route): support multi backend
1 parent ca004f1 commit 60aa12d

7 files changed

Lines changed: 75 additions & 42 deletions

File tree

api/routes.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,48 @@ type Route struct {
1414
// It changes every time the application settings is changed and cannot be updated.
1515
UUID string `json:"uuid,omitempty"`
1616
Name string `json:"name,omitempty"`
17-
Ptype string `json:"ptype,omitempty"`
1817
Kind string `json:"kind,omitempty"`
19-
Port int `json:"port,omitempty"`
2018
ParentRefs []ParentRef `json:"parent_refs,omitempty"`
19+
Rules []RouteRule `json:"rules,omitempty"`
2120
}
2221

2322
type ParentRef struct {
2423
Name string `json:"name,omitempty"`
2524
Port int `json:"port,omitempty"`
2625
}
2726

28-
// // Routes defines a collection of Route objects.
27+
type RouteRule map[string]interface{}
28+
29+
// Routes defines a collection of Route objects.
2930
type Routes []Route
3031

31-
// RouteCreateRequest is the structure of POST /v2/app/<app id>/routes/.
32+
// RouteCreateRequest is the structure of POST /v2/app/<app_id>/routes/.
33+
3234
type RouteCreateRequest struct {
33-
Name string `json:"name,omitempty"`
34-
Ptype string `json:"ptype,omitempty"`
35-
Port int `json:"port,omitempty"`
36-
Kind string `json:"kind,omitempty"`
35+
Name string `json:"name,omitempty"`
36+
Kind string `json:"kind,omitempty"`
37+
Rules []RequestRouteRule `json:"rules,omitempty"`
3738
}
3839

39-
// RouteAttackRequest is the structure of PATCH /v2/apps/(?P<id>{})/routes/(?P<name>{})/attach/?$.
40-
type RouteAttackRequest struct {
41-
Port int `json:"port,omitempty"`
42-
Gateway string `json:"gateway,omitempty"`
40+
type BackendRefRequest struct {
41+
Kind string `json:"kind,omitempty"`
42+
Name string `json:"name,omitempty"`
43+
Port int32 `json:"port,omitempty"`
44+
Weight int32 `json:"weight,omitempty"`
4345
}
4446

45-
// RouteDetackRequest is the structure of PATCH /v2/apps/(?P<id>{})/routes/(?P<name>{})/detach/?$.
46-
type RouteDetackRequest struct {
47+
type RequestRouteRule struct {
48+
BackendRefs []BackendRefRequest `json:"backendRefs,omitempty"`
49+
}
50+
51+
// RouteAttachRequest is the structure of PATCH /v2/apps/(?P<id>{})/routes/(?P<name>{})/attach/?$.
52+
type RouteAttachRequest struct {
4753
Port int `json:"port,omitempty"`
4854
Gateway string `json:"gateway,omitempty"`
4955
}
5056

51-
// RouteRule is the structure of GET RESPONSE /v2/apps/(?P<id>{})/routes/(?P<name>{})/rules/?$.
52-
type RouteRule struct {
53-
Name string `json:"name,omitempty"`
54-
Ptype string `json:"ptype,omitempty"`
55-
Kind string `json:"kind,omitempty"`
56-
Rules interface{} `json:"rules,omitempty"`
57+
// RouteDetachRequest is the structure of PATCH /v2/apps/(?P<id>{})/routes/(?P<name>{})/detach/?$.
58+
type RouteDetachRequest struct {
59+
Port int `json:"port,omitempty"`
60+
Gateway string `json:"gateway,omitempty"`
5761
}

api/services.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
// Service is the structure of the service object.
44
type Service struct {
5+
Name string `json:"name"`
56
Domain string `json:"domain"`
67
Ptype string `json:"ptype"`
78
Ports []Port `json:"ports"`

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
module github.com/drycc/controller-sdk-go
22

3-
go 1.22
3+
go 1.22.0
44

55
require (
66
github.com/stretchr/testify v1.9.0
7-
golang.org/x/net v0.23.0
7+
golang.org/x/net v0.28.0
88
)
99

1010
require (
11-
github.com/davecgh/go-spew v1.1.1 // indirect
12-
github.com/pmezard/go-difflib v1.0.0 // indirect
11+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
12+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
1313
gopkg.in/yaml.v3 v3.0.1 // indirect
1414
)

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2-
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
2+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
4+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
55
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
66
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7-
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
8-
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
7+
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
8+
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
99
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1010
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1111
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

routes/routes.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ func List(c *drycc.Client, appID string, results int) (api.Routes, int, error) {
2828
}
2929

3030
// New adds a route to an app.
31-
func New(c *drycc.Client, appID string, name string, Ptype string, kind string, port int) error {
31+
func New(c *drycc.Client, appID, name, kind string, backendRefs ...api.BackendRefRequest) error {
3232
u := fmt.Sprintf("/v2/apps/%s/routes/", appID)
3333

34-
req := api.RouteCreateRequest{Name: name, Ptype: Ptype, Kind: kind, Port: port}
34+
req := api.RouteCreateRequest{
35+
Name: name,
36+
Kind: kind,
37+
Rules: []api.RequestRouteRule{{BackendRefs: backendRefs}},
38+
}
3539

3640
body, err := json.Marshal(req)
3741

@@ -51,7 +55,7 @@ func New(c *drycc.Client, appID string, name string, Ptype string, kind string,
5155
func AttachGateway(c *drycc.Client, appID string, name string, port int, gateway string) error {
5256
u := fmt.Sprintf("/v2/apps/%s/routes/%s/attach/", appID, name)
5357

54-
req := api.RouteAttackRequest{Port: port, Gateway: gateway}
58+
req := api.RouteAttachRequest{Port: port, Gateway: gateway}
5559

5660
body, err := json.Marshal(req)
5761

@@ -72,7 +76,7 @@ func AttachGateway(c *drycc.Client, appID string, name string, port int, gateway
7276
func DetachGateway(c *drycc.Client, appID string, name string, port int, gateway string) error {
7377
u := fmt.Sprintf("/v2/apps/%s/routes/%s/detach/", appID, name)
7478

75-
req := api.RouteDetackRequest{Port: port, Gateway: gateway}
79+
req := api.RouteDetachRequest{Port: port, Gateway: gateway}
7680

7781
body, err := json.Marshal(req)
7882

routes/routes_test.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,21 @@ const routesFixture string = `
2424
"owner": "test",
2525
"updated": "2023-04-19T00:00:00UTC",
2626
"name": "example-go",
27-
"ptype": "web",
2827
"kind": "HTTPRoute",
29-
"port": 80,
3028
"parent_refs": [
3129
{
3230
"name": "example-go",
3331
"port": 80
3432
}
35-
]
33+
],
34+
"rules": [{
35+
"backendRefs": [{
36+
"kind": "Service",
37+
"name": "example-go",
38+
"port": 5000,
39+
"weight": 100
40+
}]
41+
}]
3642
}
3743
]
3844
}`
@@ -52,7 +58,7 @@ const routerulesFixture string = `
5258

5359
const routerulesSetFixture string = `"[{\"backendRefs\": [{\"kind\": \"Service\",\"name\": \"py3django3\",\"port\": 80}]}]"`
5460

55-
const routeCreateExpected string = `{"name":"example-go","ptype":"web","port":80,"kind":"HTTPRoute"}`
61+
const routeCreateExpected string = `{"name":"example-go","kind":"HTTPRoute","rules":[{"backendRefs":[{"kind":"Service","name":"example-go","port":80,"weight":100}]}]}`
5662

5763
const routeRulesSetExpected string = `[{"backendRefs": [{"kind": "Service","name": "py3django3","port": 80}]}]`
5864

@@ -171,15 +177,23 @@ func TestRoutesList(t *testing.T) {
171177
Name: "example-go",
172178
Owner: "test",
173179
Updated: "2023-04-19T00:00:00UTC",
174-
Ptype: "web",
175180
Kind: "HTTPRoute",
176-
Port: 80,
177181
ParentRefs: []api.ParentRef{
178182
{
179183
Name: "example-go",
180184
Port: 80,
181185
},
182186
},
187+
Rules: []api.RouteRule{
188+
{
189+
"backendRefs": []map[string]interface{}{{
190+
"kind": "Service",
191+
"name": "example-go",
192+
"port": 5000,
193+
"weight": 100,
194+
}},
195+
},
196+
},
183197
},
184198
}
185199
handler := fakeHTTPServer{}
@@ -197,7 +211,11 @@ func TestRoutesList(t *testing.T) {
197211
t.Fatal(err)
198212
}
199213

200-
if !reflect.DeepEqual(expected, actual) {
214+
fmt.Printf("%v\n", actual)
215+
fmt.Printf("%v\n", expected)
216+
fmt.Printf("%v\n", fmt.Sprintf("%v", actual) == fmt.Sprintf("%v", expected))
217+
218+
if fmt.Sprintf("%v", actual) != fmt.Sprintf("%v", expected) {
201219
t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual))
202220
}
203221
}
@@ -256,7 +274,9 @@ func TestRoutesAdd(t *testing.T) {
256274
t.Fatal(err)
257275
}
258276

259-
err = New(drycc, "example-go", "example-go", "web", "HTTPRoute", 80)
277+
backendRef := api.BackendRefRequest{Kind: "Service", Name: "example-go", Port: 80, Weight: 100}
278+
279+
err = New(drycc, "example-go", "example-go", "HTTPRoute", backendRef)
260280

261281
if err != nil {
262282
t.Fatal(err)

services/services_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const servicesFixture string = `
1616
{
1717
"services": [
1818
{
19+
"name": "example-go",
1920
"domain": "example-go.example-go.svc.cluster.local",
2021
"ptype": "web",
2122
"ports": [
@@ -34,6 +35,7 @@ const servicesFixture string = `
3435
]
3536
},
3637
{
38+
"name": "example-go-worker",
3739
"domain": "example-go-worker.example-go.svc.cluster.local",
3840
"ptype": "worker",
3941
"ports": [
@@ -113,6 +115,7 @@ func TestServicesList(t *testing.T) {
113115

114116
expected := api.Services{
115117
{
118+
Name: "example-go",
116119
Domain: "example-go.example-go.svc.cluster.local",
117120
Ptype: "web",
118121
Ports: []api.Port{
@@ -131,6 +134,7 @@ func TestServicesList(t *testing.T) {
131134
},
132135
},
133136
{
137+
Name: "example-go-worker",
134138
Domain: "example-go-worker.example-go.svc.cluster.local",
135139
Ptype: "worker",
136140
Ports: []api.Port{

0 commit comments

Comments
 (0)