Skip to content
Permalink
Browse files
feat: add mem struct transform and store (#11)
  • Loading branch information
gxthrj committed Aug 17, 2020
1 parent 8585d67 commit b7489fac9e2ba9181a6efe2327fbf18ef91361ea
Showing 15 changed files with 524 additions and 64 deletions.
@@ -220,8 +220,9 @@ http:
exact: "json"
age:
Greater: 18
- uri:
prefix: "/"
- uris:
- prefix: "/"
-------------------
kind: destinations
1 go.mod
@@ -4,6 +4,7 @@ go 1.13

require (
github.com/ghodss/yaml v1.0.0
github.com/hashicorp/go-memdb v1.2.1
github.com/onsi/ginkgo v1.14.0
github.com/onsi/gomega v1.10.1
github.com/xeipuuv/gojsonschema v1.2.0
8 go.sum
@@ -15,6 +15,14 @@ github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/hashicorp/go-immutable-radix v1.2.0 h1:l6UW37iCXwZkZoAbEYnptSHVE/cQ5bOTPYG5W3vf9+8=
github.com/hashicorp/go-immutable-radix v1.2.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-memdb v1.2.1 h1:wI9btDjYUOJJHTCnRlAG/TkRyD/ij7meJMrLK9X31Cc=
github.com/hashicorp/go-memdb v1.2.1/go.mod h1:OSvLJ662Jim8hMM+gWGyhktyWk2xPCnWMc7DWIqtkGA=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package mem

type GatewayDB struct {
Gateways []*Gateway
}

// insert Gateway to memDB
func (db *GatewayDB) Insert() error {
txn := DB.Txn(true)
defer txn.Abort()
for _, r := range db.Gateways {
if err := txn.Insert(GatewayTable, r); err != nil {
return err
}
}
txn.Commit()
return nil
}

func (g *Gateway) Diff(t MemModel) bool {
return true
}
@@ -17,24 +17,29 @@

package mem

type MemModel interface {
Diff(m MemModel) bool
}

type Route struct {
ID *string `json:"id,omitempty" yml:"id,omitempty"`
Group *string `json:"group,omitempty" yml:"group,omitempty"`
FullName *string `json:"full_name,omitempty" yml:"full_name,omitempty"`
ResourceVersion *string `json:"resource_version,omitempty" yml:"resource_version,omitempty"`
Host *string `json:"host,omitempty" yml:"host,omitempty"`
Path *string `json:"path,omitempty" yml:"path,omitempty"`
Name *string `json:"name,omitempty" yml:"name,omitempty"`
Methods []*string `json:"methods,omitempty" yml:"methods,omitempty"`
ServiceId *string `json:"service_id,omitempty" yml:"service_id,omitempty"`
ServiceName *string `json:"service_name,omitempty" yml:"service_name,omitempty"`
UpstreamId *string `json:"upstream_id,omitempty" yml:"upstream_id,omitempty"`
UpstreamName *string `json:"upstream_name,omitempty" yml:"upstream_name,omitempty"`
Plugins []*Plugin `json:"plugins,omitempty" yml:"plugins,omitempty"`
ID *string `json:"id,omitempty" yml:"id,omitempty"`
Kind *string `json:"kind"`
FullName *string `json:"full_name,omitempty" yml:"full_name,omitempty"`
Hosts []*string `json:"hosts,omitempty" yml:"hosts,omitempty"`
Match []map[string]interface{} `json:"paths,omitempty" yml:"paths,omitempty"`
Name *string `json:"name,omitempty" yml:"name,omitempty"`
Methods []*string `json:"methods,omitempty" yml:"methods,omitempty"`
ServiceId *string `json:"service_id,omitempty" yml:"service_id,omitempty"`
ServiceName *string `json:"service_name,omitempty" yml:"service_name,omitempty"`
UpstreamId *string `json:"upstream_id,omitempty" yml:"upstream_id,omitempty"`
UpstreamName *string `json:"upstream_name,omitempty" yml:"upstream_name,omitempty"`
Plugins []*Plugin `json:"plugins,omitempty" yml:"plugins,omitempty"`
}

type Upstream struct {
ID *string `json:"id,omitempty" yml:"id,omitempty"`
Kind *string `json:"kind"`
Host *string `json:"host"`
FullName *string `json:"full_name,omitempty" yml:"full_name,omitempty"`
Group *string `json:"group,omitempty" yml:"group,omitempty"`
ResourceVersion *string `json:"resource_version,omitempty" yml:"resource_version,omitempty"`
@@ -43,6 +48,7 @@ type Upstream struct {
HashOn *string `json:"hash_on,omitemtpy" yml:"hash_on,omitempty"`
Key *string `json:"key,omitempty" yml:"key,omitempty"`
Nodes []*Node `json:"nodes,omitempty" yml:"nodes,omitempty"`
Weight int64 `json:"weight"`
FromKind *string `json:"from_kind,omitempty" yml:"from_kind,omitempty"`
}

@@ -53,14 +59,32 @@ type Node struct {
}

type Plugin struct {
ID *string `json:"id,omitempty"`
Selector Selector `json:"selector"`
Sort []PluginSchema `json:"sort"`
ID *string `json:"id,omitempty"`
Kind *string `json:"kind"`
Selector map[string]string `json:"selector"`
Sets []*PluginSet `json:"sets"`
}

type PluginSet struct {
Name *string `json:"name"`
Conf map[string]interface{} `json:"conf"`
}

type Selector map[string]string
type Gateway struct {
ID *string `json:"id,omitempty"`
FullName *string `json:"full_name,omitempty" yml:"full_name,omitempty"`
Kind *string `json:"kind"`
Name *string `json:"name"`
Servers []*Server `json:"servers"`
}

type Server struct {
Port *Port `json:"port,omitempty"`
Hosts []string `json:"host,omitempty"`
}

type PluginSchema struct {
Name string `json:"name"`
Conf interface{} `json:"conf"`
type Port struct {
Number int `json:"number"`
Name string `json:"name"`
Protocol string `json:"protocol"`
}
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package mem

type PluginDB struct {
Plugins []*Plugin
}

// insert Plugin to memDB
func (db *PluginDB) Insert() error {
txn := DB.Txn(true)
defer txn.Abort()
for _, r := range db.Plugins {
if err := txn.Insert(PluginTable, r); err != nil {
return err
}
}
txn.Commit()
return nil
}

func (g *Plugin) Diff(t MemModel) bool {
return true
}
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package mem

type RouteDB struct {
Routes []*Route
}

// InsertRoute insert route to memDB
func (db *RouteDB) Insert() error {
txn := DB.Txn(true)
defer txn.Abort()
for _, r := range db.Routes {
if err := txn.Insert(RouteTable, r); err != nil {
return err
}
}
txn.Commit()
return nil
}

func (g *Route) Diff(t MemModel) bool {
return true
}
@@ -0,0 +1,121 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package mem

import "github.com/hashicorp/go-memdb"

var DB *memdb.MemDB

const (
GatewayTable = "Gateway"
RouteTable = "Route"
UpstreamTable = "Upstream"
PluginTable = "Plugin"
)

func init() {
if db, err := NewDB(); err != nil {
panic(err)
} else {
DB = db
}
}

func NewDB() (*memdb.MemDB, error) {
var schema = &memdb.DBSchema{
Tables: map[string]*memdb.TableSchema{
GatewayTable: gatewaySchema,
RouteTable: routeSchema,
UpstreamTable: upstreamSchema,
PluginTable: pluginSchema,
},
}
if memDB, err := memdb.NewMemDB(schema); err != nil {
return nil, err
} else {
return memDB, nil
}
}

var gatewaySchema = &memdb.TableSchema{
Name: GatewayTable,
Indexes: map[string]*memdb.IndexSchema{
"id": {
Name: "id",
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "FullName"},
},
"name": {
Name: "name",
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "Name"},
AllowMissing: true,
},
},
}

var routeSchema = &memdb.TableSchema{
Name: RouteTable,
Indexes: map[string]*memdb.IndexSchema{
"id": {
Name: "id",
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "FullName"},
},
"name": {
Name: "name",
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "Name"},
AllowMissing: true,
},
},
}

var upstreamSchema = &memdb.TableSchema{
Name: UpstreamTable,
Indexes: map[string]*memdb.IndexSchema{
"id": {
Name: "id",
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "FullName"},
},
"name": {
Name: "name",
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "Name"},
AllowMissing: true,
},
},
}

var pluginSchema = &memdb.TableSchema{
Name: PluginTable,
Indexes: map[string]*memdb.IndexSchema{
"id": {
Name: "id",
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "FullName"},
},
"name": {
Name: "name",
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "Name"},
AllowMissing: true,
},
},
}

0 comments on commit b7489fa

Please sign in to comment.