Skip to content

Commit

Permalink
chore: group meta info for APISIX resources to Metadata structure (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
tokers authored Feb 3, 2021
1 parent fb53d65 commit 2d19b20
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 122 deletions.
60 changes: 40 additions & 20 deletions pkg/apisix/cache/memdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ func TestMemDBCacheRoute(t *testing.T) {
assert.Nil(t, err, "NewMemDBCache")

r1 := &v1.Route{
FullName: "abc",
Name: "abc",
Metadata: v1.Metadata{
FullName: "abc",
Name: "abc",
},
ServiceId: "1",
}
assert.Nil(t, c.InsertRoute(r1), "inserting route 1")
Expand All @@ -38,13 +40,17 @@ func TestMemDBCacheRoute(t *testing.T) {
assert.Equal(t, r1, r)

r2 := &v1.Route{
FullName: "def",
Name: "def",
Metadata: v1.Metadata{
FullName: "def",
Name: "def",
},
ServiceId: "2",
}
r3 := &v1.Route{
FullName: "ghi",
Name: "ghi",
Metadata: v1.Metadata{
FullName: "ghi",
Name: "ghi",
},
ServiceId: "3",
}
assert.Nil(t, c.InsertRoute(r2), "inserting route r2")
Expand All @@ -65,8 +71,10 @@ func TestMemDBCacheRoute(t *testing.T) {
assert.Equal(t, routes[1], r2)

r4 := &v1.Route{
FullName: "name4",
Name: "name4",
Metadata: v1.Metadata{
FullName: "name4",
Name: "name4",
},
ServiceId: "4",
}
assert.Error(t, ErrNotFound, c.DeleteRoute(r4))
Expand Down Expand Up @@ -167,21 +175,27 @@ func TestMemDBCacheUpstream(t *testing.T) {
assert.Nil(t, err, "NewMemDBCache")

u1 := &v1.Upstream{
FullName: "abc",
Name: "abc",
Metadata: v1.Metadata{
FullName: "abc",
Name: "abc",
},
}
assert.Nil(t, c.InsertUpstream(u1), "inserting upstream 1")

u, err := c.GetUpstream("abc")
assert.Equal(t, u1, u)

u2 := &v1.Upstream{
FullName: "def",
Name: "def",
Metadata: v1.Metadata{
FullName: "def",
Name: "def",
},
}
u3 := &v1.Upstream{
FullName: "ghi",
Name: "ghi",
Metadata: v1.Metadata{
FullName: "ghi",
Name: "ghi",
},
}
assert.Nil(t, c.InsertUpstream(u2), "inserting upstream 2")
assert.Nil(t, c.InsertUpstream(u3), "inserting upstream 3")
Expand All @@ -201,16 +215,20 @@ func TestMemDBCacheUpstream(t *testing.T) {
assert.Equal(t, upstreams[1], u2)

u4 := &v1.Upstream{
FullName: "name4",
Name: "name4",
Metadata: v1.Metadata{
FullName: "name4",
Name: "name4",
},
}
assert.Error(t, ErrNotFound, c.DeleteUpstream(u4))
}

func TestMemDBCacheReference(t *testing.T) {
r := &v1.Route{
FullName: "route",
Name: "route",
Metadata: v1.Metadata{
FullName: "route",
Name: "route",
},
ServiceId: "service",
}
s := &v1.Service{
Expand All @@ -219,8 +237,10 @@ func TestMemDBCacheReference(t *testing.T) {
UpstreamId: "upstream",
}
u := &v1.Upstream{
FullName: "upstream",
Name: "upstream",
Metadata: v1.Metadata{
FullName: "upstream",
Name: "upstream",
},
}

db, err := NewMemDBCache()
Expand Down
26 changes: 15 additions & 11 deletions pkg/apisix/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ func (i *item) route(clusterName string) (*v1.Route, error) {
fullName := genFullName(route.Desc, clusterName)

return &v1.Route{
ID: list[len(list)-1],
FullName: fullName,
Group: clusterName,
Name: route.Desc,
Metadata: v1.Metadata{
ID: list[len(list)-1],
FullName: fullName,
Group: clusterName,
Name: route.Desc,
},
Host: route.Host,
Path: route.URI,
Methods: route.Methods,
Expand Down Expand Up @@ -141,13 +143,15 @@ func (i *item) upstream(clusterName string) (*v1.Upstream, error) {
fullName := genFullName(ups.Desc, clusterName)

return &v1.Upstream{
ID: id,
FullName: fullName,
Group: clusterName,
Name: name,
Type: LBType,
Key: key,
Nodes: nodes,
Metadata: v1.Metadata{
ID: id,
FullName: fullName,
Group: clusterName,
Name: name,
},
Type: LBType,
Key: key,
Nodes: nodes,
}, nil
}

Expand Down
24 changes: 15 additions & 9 deletions pkg/apisix/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,27 @@ func TestRouteClient(t *testing.T) {

// Create
obj, err := cli.Create(context.Background(), &v1.Route{
ID: "1",
Metadata: v1.Metadata{
ID: "1",
Name: "test",
FullName: "test",
},
Host: "www.foo.com",
Path: "/bar",
Name: "test",
FullName: "test",
ServiceId: "1",
UpstreamId: "1",
})
assert.Nil(t, err)
assert.Equal(t, obj.ID, "1")

obj, err = cli.Create(context.Background(), &v1.Route{
ID: "2",
Metadata: v1.Metadata{
ID: "2",
Name: "test",
FullName: "test",
},
Host: "www.foo.com",
Path: "/bar",
Name: "test",
FullName: "test",
ServiceId: "1",
UpstreamId: "1",
})
Expand All @@ -219,11 +223,13 @@ func TestRouteClient(t *testing.T) {

// Patch then List
_, err = cli.Update(context.Background(), &v1.Route{
ID: "2",
Metadata: v1.Metadata{
ID: "2",
Name: "test",
FullName: "test",
},
Host: "www.foo.com",
Path: "/bar",
Name: "test",
FullName: "test",
ServiceId: "112",
UpstreamId: "112",
})
Expand Down
48 changes: 27 additions & 21 deletions pkg/apisix/upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,26 +174,30 @@ func TestUpstreamClient(t *testing.T) {
}

obj, err := cli.Create(context.TODO(), &v1.Upstream{
ID: "1",
FullName: fullName,
Group: group,
Name: name,
Type: lbType,
Key: key,
Nodes: nodes,
Metadata: v1.Metadata{
ID: "1",
FullName: fullName,
Group: group,
Name: name,
},
Type: lbType,
Key: key,
Nodes: nodes,
})
assert.Nil(t, err)
assert.Equal(t, obj.ID, "1")

id2 := "2"
obj, err = cli.Create(context.TODO(), &v1.Upstream{
ID: id2,
FullName: fullName,
Group: group,
Name: name,
Type: lbType,
Key: key,
Nodes: nodes,
Metadata: v1.Metadata{
ID: id2,
FullName: fullName,
Group: group,
Name: name,
},
Type: lbType,
Key: key,
Nodes: nodes,
})
assert.Nil(t, err)
assert.Equal(t, obj.ID, "2")
Expand All @@ -214,13 +218,15 @@ func TestUpstreamClient(t *testing.T) {

// Patch then List
_, err = cli.Update(context.Background(), &v1.Upstream{
ID: "2",
FullName: fullName,
Group: group,
Name: name,
Type: "chash",
Key: key,
Nodes: nodes,
Metadata: v1.Metadata{
ID: "2",
FullName: fullName,
Group: group,
Name: name,
},
Type: "chash",
Key: key,
Nodes: nodes,
})
assert.Nil(t, err)
objs, err = cli.List(context.Background())
Expand Down
34 changes: 19 additions & 15 deletions pkg/ingress/apisix/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,17 @@ func (ar *ApisixRoute) Convert() ([]*apisix.Route, []*apisix.Service, []*apisix.

// routes
route := &apisix.Route{
Group: group,
FullName: fullRouteName,
ResourceVersion: rv,
Name: apisixRouteName,
Host: host,
Path: uri,
ServiceName: apisixSvcName,
UpstreamName: apisixUpstreamName,
Plugins: pluginRet,
Metadata: apisix.Metadata{
Group: group,
FullName: fullRouteName,
ResourceVersion: rv,
Name: apisixRouteName,
},
Host: host,
Path: uri,
ServiceName: apisixSvcName,
UpstreamName: apisixUpstreamName,
Plugins: pluginRet,
}
routes = append(routes, route)
// services
Expand Down Expand Up @@ -127,12 +129,14 @@ func (ar *ApisixRoute) Convert() ([]*apisix.Route, []*apisix.Service, []*apisix.
port, _ := strconv.Atoi(svcPort)
nodes := endpoint.BuildEps(ns, svcName, port)
upstream := &apisix.Upstream{
FullName: fullUpstreamName,
Group: group,
ResourceVersion: rv,
Name: apisixUpstreamName,
Type: LBType,
Nodes: nodes,
Metadata: apisix.Metadata{
FullName: fullUpstreamName,
Group: group,
ResourceVersion: rv,
Name: apisixUpstreamName,
},
Type: LBType,
Nodes: nodes,
}
upstreamMap[upstream.FullName] = upstream
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/ingress/apisix/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ func (as *ApisixServiceCRD) Convert() ([]*apisix.Service, []*apisix.Upstream, er
LBType := DefaultLBType
nodes := endpoint.BuildEps(ns, upstreamName, int(port))
upstream := &apisix.Upstream{
FullName: fullUpstreamName,
Group: group,
ResourceVersion: rv,
Name: apisixUpstreamName,
Type: LBType,
Nodes: nodes,
FromKind: fromKind,
Metadata: apisix.Metadata{
FullName: fullUpstreamName,
Group: group,
ResourceVersion: rv,
Name: apisixUpstreamName,
},
Type: LBType,
Nodes: nodes,
FromKind: fromKind,
}
upstreams = append(upstreams, upstream)
return services, upstreams, nil
Expand Down
14 changes: 8 additions & 6 deletions pkg/ingress/apisix/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ func (aub *ApisixUpstreamBuilder) Convert() ([]*apisix.Upstream, error) {
fullName = group + "_" + apisixUpstreamName
}
upstream := &apisix.Upstream{
FullName: fullName,
Group: group,
ResourceVersion: rv,
Name: apisixUpstreamName,
Nodes: nodes,
FromKind: fromKind,
Metadata: apisix.Metadata{
FullName: fullName,
Group: group,
ResourceVersion: rv,
Name: apisixUpstreamName,
},
Nodes: nodes,
FromKind: fromKind,
}
if lb == nil || lb.Type == "" {
upstream.Type = apisix.LbRoundRobin
Expand Down
18 changes: 10 additions & 8 deletions pkg/ingress/apisix/upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,16 @@ func buildExpectUpstream() *v1.Upstream {
fromKind := "ApisixUpstream"
group := ""
upstreamExpect := &v1.Upstream{
Group: group,
ResourceVersion: group,
FullName: fullName,
Name: fullName,
Type: LBType,
HashOn: HashOn,
Key: Key,
FromKind: fromKind,
Metadata: v1.Metadata{
Group: group,
ResourceVersion: group,
FullName: fullName,
Name: fullName,
},
Type: LBType,
HashOn: HashOn,
Key: Key,
FromKind: fromKind,
}
return upstreamExpect
}
Expand Down
Loading

0 comments on commit 2d19b20

Please sign in to comment.