From 2d19b2071b3a0c72b7b8e70f3f840e4bf522a6d9 Mon Sep 17 00:00:00 2001 From: Alex Zhang Date: Wed, 3 Feb 2021 15:07:52 +0800 Subject: [PATCH] chore: group meta info for APISIX resources to Metadata structure (#225) --- pkg/apisix/cache/memdb_test.go | 60 +++++++++++++------- pkg/apisix/resource.go | 26 +++++---- pkg/apisix/route_test.go | 24 +++++--- pkg/apisix/upstream_test.go | 48 +++++++++------- pkg/ingress/apisix/route.go | 34 ++++++----- pkg/ingress/apisix/service.go | 16 +++--- pkg/ingress/apisix/upstream.go | 14 +++-- pkg/ingress/apisix/upstream_test.go | 18 +++--- pkg/types/apisix/v1/types.go | 55 ++++++++++-------- pkg/types/apisix/v1/zz_generated.deepcopy.go | 2 + 10 files changed, 175 insertions(+), 122 deletions(-) diff --git a/pkg/apisix/cache/memdb_test.go b/pkg/apisix/cache/memdb_test.go index e132962b05..f36b3a54d9 100644 --- a/pkg/apisix/cache/memdb_test.go +++ b/pkg/apisix/cache/memdb_test.go @@ -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") @@ -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") @@ -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)) @@ -167,8 +175,10 @@ 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") @@ -176,12 +186,16 @@ func TestMemDBCacheUpstream(t *testing.T) { 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") @@ -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{ @@ -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() diff --git a/pkg/apisix/resource.go b/pkg/apisix/resource.go index 100a59eeff..bb4db43756 100644 --- a/pkg/apisix/resource.go +++ b/pkg/apisix/resource.go @@ -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, @@ -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 } diff --git a/pkg/apisix/route_test.go b/pkg/apisix/route_test.go index 6205363429..56137985ce 100644 --- a/pkg/apisix/route_test.go +++ b/pkg/apisix/route_test.go @@ -180,11 +180,13 @@ 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", }) @@ -192,11 +194,13 @@ func TestRouteClient(t *testing.T) { 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", }) @@ -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", }) diff --git a/pkg/apisix/upstream_test.go b/pkg/apisix/upstream_test.go index b929cbeb60..d287b59afa 100644 --- a/pkg/apisix/upstream_test.go +++ b/pkg/apisix/upstream_test.go @@ -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") @@ -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()) diff --git a/pkg/ingress/apisix/route.go b/pkg/ingress/apisix/route.go index e43d3a84fd..64d57d5c43 100644 --- a/pkg/ingress/apisix/route.go +++ b/pkg/ingress/apisix/route.go @@ -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 @@ -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 } diff --git a/pkg/ingress/apisix/service.go b/pkg/ingress/apisix/service.go index 18d97d83bd..c09e8c7b8b 100644 --- a/pkg/ingress/apisix/service.go +++ b/pkg/ingress/apisix/service.go @@ -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 diff --git a/pkg/ingress/apisix/upstream.go b/pkg/ingress/apisix/upstream.go index da6b5f2003..31b6da3681 100644 --- a/pkg/ingress/apisix/upstream.go +++ b/pkg/ingress/apisix/upstream.go @@ -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 diff --git a/pkg/ingress/apisix/upstream_test.go b/pkg/ingress/apisix/upstream_test.go index c3cf8e90ef..548d18c298 100644 --- a/pkg/ingress/apisix/upstream_test.go +++ b/pkg/ingress/apisix/upstream_test.go @@ -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 } diff --git a/pkg/types/apisix/v1/types.go b/pkg/types/apisix/v1/types.go index 9f1094cbd4..9b0b13add2 100644 --- a/pkg/types/apisix/v1/types.go +++ b/pkg/types/apisix/v1/types.go @@ -14,7 +14,9 @@ // limitations under the License. package v1 -import "encoding/json" +import ( + "encoding/json" +) const ( // HashOnVars means the hash scope is variable. @@ -40,22 +42,28 @@ const ( LbLeastConn = "least_conn" ) +// Metadata contains all meta information about resources. +type Metadata struct { + ID string `json:"id,omitempty" yaml:"id,omitempty"` + FullName string `json:"full_name,omitempty" yaml:"full_name,omitempty"` + Name string `json:"name,omitempty" yaml:"name,omitempty"` + ResourceVersion string `json:"resource_version,omitempty" yaml:"resource_version,omitempty"` + Group string `json:"group,omitempty" yaml:"group,omitempty"` +} + // Route apisix route object // +k8s:deepcopy-gen=true type Route struct { - ID string `json:"id,omitempty" yaml:"id,omitempty"` - Group string `json:"group,omitempty" yaml:"group,omitempty"` - FullName string `json:"full_name,omitempty" yaml:"full_name,omitempty"` - ResourceVersion string `json:"resource_version,omitempty" yaml:"resource_version,omitempty"` - Host string `json:"host,omitempty" yaml:"host,omitempty"` - Path string `json:"path,omitempty" yaml:"path,omitempty"` - Name string `json:"name,omitempty" yaml:"name,omitempty"` - Methods []string `json:"methods,omitempty" yaml:"methods,omitempty"` - ServiceId string `json:"service_id,omitempty" yaml:"service_id,omitempty"` - ServiceName string `json:"service_name,omitempty" yaml:"service_name,omitempty"` - UpstreamId string `json:"upstream_id,omitempty" yaml:"upstream_id,omitempty"` - UpstreamName string `json:"upstream_name,omitempty" yaml:"upstream_name,omitempty"` - Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"` + Metadata `json:",inline" yaml:",inline"` + + Host string `json:"host,omitempty" yaml:"host,omitempty"` + Path string `json:"path,omitempty" yaml:"path,omitempty"` + Methods []string `json:"methods,omitempty" yaml:"methods,omitempty"` + ServiceId string `json:"service_id,omitempty" yaml:"service_id,omitempty"` + ServiceName string `json:"service_name,omitempty" yaml:"service_name,omitempty"` + UpstreamId string `json:"upstream_id,omitempty" yaml:"upstream_id,omitempty"` + UpstreamName string `json:"upstream_name,omitempty" yaml:"upstream_name,omitempty"` + Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"` } type Plugins map[string]interface{} @@ -88,19 +96,16 @@ type Service struct { FromKind string `json:"from_kind,omitempty" yaml:"from_kind,omitempty"` } -// Upstream apisix upstream +// Upstream is the apisix upstream definition. // +k8s:deepcopy-gen=true type Upstream struct { - ID string `json:"id,omitempty" yaml:"id,omitempty"` - FullName string `json:"full_name,omitempty" yaml:"full_name,omitempty"` - Group string `json:"group,omitempty" yaml:"group,omitempty"` - ResourceVersion string `json:"resource_version,omitempty" yaml:"resource_version,omitempty"` - Name string `json:"name,omitempty" yaml:"name,omitempty"` - Type string `json:"type,omitempty" yaml:"type,omitempty"` - HashOn string `json:"hash_on,omitemtpy" yaml:"hash_on,omitempty"` - Key string `json:"key,omitempty" yaml:"key,omitempty"` - Nodes []Node `json:"nodes,omitempty" yaml:"nodes,omitempty"` - FromKind string `json:"from_kind,omitempty" yaml:"from_kind,omitempty"` + Metadata `json:",inline" yaml:",inline"` + + Type string `json:"type,omitempty" yaml:"type,omitempty"` + HashOn string `json:"hash_on,omitemtpy" yaml:"hash_on,omitempty"` + Key string `json:"key,omitempty" yaml:"key,omitempty"` + Nodes []Node `json:"nodes,omitempty" yaml:"nodes,omitempty"` + FromKind string `json:"from_kind,omitempty" yaml:"from_kind,omitempty"` } // Node the node in upstream diff --git a/pkg/types/apisix/v1/zz_generated.deepcopy.go b/pkg/types/apisix/v1/zz_generated.deepcopy.go index 4eedebe1da..d8d239b299 100644 --- a/pkg/types/apisix/v1/zz_generated.deepcopy.go +++ b/pkg/types/apisix/v1/zz_generated.deepcopy.go @@ -39,6 +39,7 @@ func (in *Node) DeepCopy() *Node { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Route) DeepCopyInto(out *Route) { *out = *in + out.Metadata = in.Metadata if in.Methods != nil { in, out := &in.Methods, &out.Methods *out = make([]string, len(*in)) @@ -99,6 +100,7 @@ func (in *Ssl) DeepCopy() *Ssl { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Upstream) DeepCopyInto(out *Upstream) { *out = *in + out.Metadata = in.Metadata if in.Nodes != nil { in, out := &in.Nodes, &out.Nodes *out = make([]Node, len(*in))