This is an automated email from the ASF dual-hosted git repository.
kvn pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-control-plane.git
The following commit(s) were added to refs/heads/master by this push:
new 3334b28 feat: add gateway yaml struct transform && refactor pkg yaml
-> yml (#7)
3334b28 is described below
commit 3334b28433188d727aa8a8f653873b08663fcf5d
Author: kv <[email protected]>
AuthorDate: Thu Aug 13 12:28:31 2020 +0800
feat: add gateway yaml struct transform && refactor pkg yaml -> yml (#7)
* feat: add gateway yaml struct transform && refactor pkg yaml -> yml
* fix: Gateway yaml struct do not need ID field
---
README.md | 8 ++--
go.mod | 1 +
pkg/mem/model.go | 66 +++++++++++++++++++++++++++
pkg/{yaml/yaml_suite_test.go => yml/model.go} | 37 +++++++++++----
pkg/{yaml => yml}/schema.go | 2 +-
pkg/{yaml => yml}/schema_test.go | 12 ++---
pkg/{yaml => yml}/to_json.go | 2 +-
pkg/{yaml/yaml_suite_test.go => yml/trans.go} | 46 +++++++++++++++----
pkg/yml/trans_test.go | 42 +++++++++++++++++
pkg/{yaml => yml}/yaml_suite_test.go | 2 +-
pkg/{yaml => yml}/yaml_test.go | 12 ++---
11 files changed, 194 insertions(+), 36 deletions(-)
diff --git a/README.md b/README.md
index 3bd6c7b..694b9f2 100644
--- a/README.md
+++ b/README.md
@@ -20,15 +20,15 @@
# apisix-control-plane
## what is apisix-control-plane?
-apisix-control-plane provide APISIX with a `yaml` configuration capability,
-We can use `yaml` to define the proxy behavior of APISIX
+apisix-control-plane provide APISIX with a `yml` configuration capability,
+We can use `yml` to define the proxy behavior of APISIX
## Why do you want to do this?
-1. In order to facilitate the integration of k8s, use `yaml` to define APISIX;
+1. In order to facilitate the integration of k8s, use `yml` to define APISIX;
2. For easier synchronization across clusters;
3. Can be better adapted to multiple platforms (k8s, vm);
## DISCUSS
-Here are some examples of `yaml` and we can add comments here.
+Here are some examples of `yml` and we can add comments here.
https://github.com/apache/apisix-control-plane/issues/3
We can also submit a PR to modify this [document](doc/yaml_struct.md).
diff --git a/go.mod b/go.mod
index b82a114..ea95110 100644
--- a/go.mod
+++ b/go.mod
@@ -7,4 +7,5 @@ require (
github.com/onsi/ginkgo v1.14.0
github.com/onsi/gomega v1.10.1
github.com/xeipuuv/gojsonschema v1.2.0
+ gopkg.in/yaml.v2 v2.3.0
)
diff --git a/pkg/mem/model.go b/pkg/mem/model.go
new file mode 100644
index 0000000..b68c5e4
--- /dev/null
+++ b/pkg/mem/model.go
@@ -0,0 +1,66 @@
+/*
+ * 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 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"`
+}
+
+type Upstream struct {
+ ID *string `json:"id,omitempty" yml:"id,omitempty"`
+ 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"`
+ Name *string `json:"name,omitempty" yml:"name,omitempty"`
+ Type *string `json:"type,omitempty" yml:"type,omitempty"`
+ 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"`
+ FromKind *string `json:"from_kind,omitempty"
yml:"from_kind,omitempty"`
+}
+
+type Node struct {
+ IP *string `json:"ip,omitempty" yml:"ip,omitempty"`
+ Port *int `json:"port,omitempty" yml:"port,omitempty"`
+ Weight *int `json:"weight,omitempty" yml:"weight,omitempty"`
+}
+
+type Plugin struct {
+ ID *string `json:"id,omitempty"`
+ Selector Selector `json:"selector"`
+ Sort []PluginSchema `json:"sort"`
+}
+
+type Selector map[string]string
+
+type PluginSchema struct {
+ Name string `json:"name"`
+ Conf interface{} `json:"conf"`
+}
diff --git a/pkg/yaml/yaml_suite_test.go b/pkg/yml/model.go
similarity index 60%
copy from pkg/yaml/yaml_suite_test.go
copy to pkg/yml/model.go
index 59e4d7e..f8c73ed 100644
--- a/pkg/yaml/yaml_suite_test.go
+++ b/pkg/yml/model.go
@@ -14,16 +14,35 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package yaml_test
-import (
- "testing"
+package yml
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
-)
+type YmlModel interface {
+ ToMem() string
+ Type() string
+}
+
+type Gateway struct {
+ 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 Port struct {
+ Number int `json:"number"`
+ Name string `json:"name"`
+ Protocol string `json:"protocol"`
+}
+
+func (g *Gateway) ToMem() string {
+ return "gateway"
+}
-func TestYaml(t *testing.T) {
- RegisterFailHandler(Fail)
- RunSpecs(t, "Yaml Suite")
+func (g *Gateway) Type() string {
+ return "Gateway"
}
diff --git a/pkg/yaml/schema.go b/pkg/yml/schema.go
similarity index 99%
rename from pkg/yaml/schema.go
rename to pkg/yml/schema.go
index 5ff51e8..4cfb127 100644
--- a/pkg/yaml/schema.go
+++ b/pkg/yml/schema.go
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package yaml
+package yml
import (
"fmt"
diff --git a/pkg/yaml/schema_test.go b/pkg/yml/schema_test.go
similarity index 94%
rename from pkg/yaml/schema_test.go
rename to pkg/yml/schema_test.go
index b941370..43af7cf 100644
--- a/pkg/yaml/schema_test.go
+++ b/pkg/yml/schema_test.go
@@ -14,11 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package yaml_test
+package yml_test
import (
"fmt"
- localYaml "github.com/apache/apisix-control-plane/pkg/yaml"
+ localYaml "github.com/apache/apisix-control-plane/pkg/yml"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
@@ -42,7 +42,7 @@ servers:
fmt.Println(okGateway)
})
Context("Gateway schema check ok", func() {
- It("Gateway yaml is ok", func() {
+ It("Gateway yml is ok", func() {
fmt.Println(okGateway)
fmt.Println(2)
if b, err := localYaml.ToJson(okGateway); err
!= nil {
@@ -95,7 +95,7 @@ http:
fmt.Println(okRule)
})
Context("Rule schema check ok", func() {
- It("Rule yaml is ok", func() {
+ It("Rule yml is ok", func() {
fmt.Println(okRule)
if b, err := localYaml.ToJson(okRule); err !=
nil {
fmt.Println(err.Error())
@@ -131,7 +131,7 @@ subsets:
fmt.Println(okTarget)
})
Context("Destination schema check ok", func() {
- It("Destination yaml is ok", func() {
+ It("Destination yml is ok", func() {
fmt.Println(okTarget)
if b, err := localYaml.ToJson(okTarget); err !=
nil {
fmt.Println(err.Error())
@@ -173,7 +173,7 @@ sort:
fmt.Println(okPlugin)
})
Context("Plugin schema check ok", func() {
- It("Plugin yaml is ok", func() {
+ It("Plugin yml is ok", func() {
fmt.Println(okPlugin)
if b, err := localYaml.ToJson(okPlugin); err !=
nil {
fmt.Println(err.Error())
diff --git a/pkg/yaml/to_json.go b/pkg/yml/to_json.go
similarity index 98%
rename from pkg/yaml/to_json.go
rename to pkg/yml/to_json.go
index 3ffa17e..555d899 100644
--- a/pkg/yaml/to_json.go
+++ b/pkg/yml/to_json.go
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package yaml
+package yml
import "github.com/ghodss/yaml"
diff --git a/pkg/yaml/yaml_suite_test.go b/pkg/yml/trans.go
similarity index 52%
copy from pkg/yaml/yaml_suite_test.go
copy to pkg/yml/trans.go
index 59e4d7e..696cd40 100644
--- a/pkg/yaml/yaml_suite_test.go
+++ b/pkg/yml/trans.go
@@ -14,16 +14,46 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package yaml_test
-import (
- "testing"
+package yml
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
+import (
+ "encoding/json"
+ "fmt"
+ "gopkg.in/yaml.v2"
)
-func TestYaml(t *testing.T) {
- RegisterFailHandler(Fail)
- RunSpecs(t, "Yaml Suite")
+func Trans(b []byte, y []byte) YmlModel {
+ // 1.trans with kind
+ var yMap map[string]interface{}
+ if err := json.Unmarshal(b, &yMap); err != nil {
+ fmt.Println("trans to map error")
+ return nil
+ } else {
+ kind := yMap["kind"]
+ switch kind {
+ case "Gateway":
+ // trans to Gateway
+ if g, err := ToGateway(y); err != nil {
+ fmt.Println("trans to Gateway error ", err)
+ return nil
+ } else {
+ fmt.Println(g)
+ return g
+ }
+ default:
+ fmt.Println("nil")
+ return nil
+ }
+ }
+}
+
+func ToGateway(y []byte) (*Gateway, error) {
+ var g *Gateway
+ if err := yaml.Unmarshal(y, &g); err != nil {
+ fmt.Println(err)
+ return nil, err
+ } else {
+ return g, nil
+ }
}
diff --git a/pkg/yml/trans_test.go b/pkg/yml/trans_test.go
new file mode 100644
index 0000000..d5eca5e
--- /dev/null
+++ b/pkg/yml/trans_test.go
@@ -0,0 +1,42 @@
+package yml_test
+
+import (
+ "fmt"
+ "github.com/apache/apisix-control-plane/pkg/yml"
+ "github.com/ghodss/yaml"
+ . "github.com/onsi/ginkgo"
+ . "github.com/onsi/gomega"
+)
+
+var _ = Describe("Trans", func() {
+ Describe("trans to model", func() {
+ var b []byte
+ BeforeEach(func() {
+ b = []byte(`
+kind: Gateway
+name: foo-gw
+servers:
+ - port:
+ number: 80
+ name: http
+ protocol: HTTP
+ hosts:
+ - "a.foo.com"
+ - "b.foo.com"
+`)
+ fmt.Println("BeforeEach executed")
+ })
+ Context("trans", func() {
+ It("trans to gateway no error", func() {
+ y, err := yaml.YAMLToJSON(b)
+ fmt.Println(string(y))
+ ym := yml.Trans(y, b)
+ Expect(err).NotTo(HaveOccurred())
+ Expect(ym.Type()).To(Equal("Gateway"))
+ g, ok := ym.(*yml.Gateway)
+ Expect(ok).To(Equal(true))
+ Expect(len(g.Servers[0].Hosts)).To(Equal(2))
+ })
+ })
+ })
+})
diff --git a/pkg/yaml/yaml_suite_test.go b/pkg/yml/yaml_suite_test.go
similarity index 98%
rename from pkg/yaml/yaml_suite_test.go
rename to pkg/yml/yaml_suite_test.go
index 59e4d7e..baf63d7 100644
--- a/pkg/yaml/yaml_suite_test.go
+++ b/pkg/yml/yaml_suite_test.go
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package yaml_test
+package yml_test
import (
"testing"
diff --git a/pkg/yaml/yaml_test.go b/pkg/yml/yaml_test.go
similarity index 87%
rename from pkg/yaml/yaml_test.go
rename to pkg/yml/yaml_test.go
index 7a62731..ae02889 100644
--- a/pkg/yaml/yaml_test.go
+++ b/pkg/yml/yaml_test.go
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package yaml_test
+package yml_test
import (
"fmt"
@@ -25,14 +25,14 @@ import (
)
var _ = Describe("Yaml", func() {
- Describe("yaml & json", func() {
+ Describe("yml & json", func() {
var b []byte
BeforeEach(func() {
b = []byte(`{"name": "John", "age": 30}`)
fmt.Println("BeforeEach executed")
})
- Context("json to yaml", func() {
- It("json to yaml no error", func() {
+ Context("json to yml", func() {
+ It("json to yml no error", func() {
fmt.Println(string(b))
y, err := yaml.JSONToYAML(b)
fmt.Println(string(y))
@@ -40,9 +40,9 @@ var _ = Describe("Yaml", func() {
})
})
- Context("yaml to json", func() {
+ Context("yml to json", func() {
- It("yaml to json no error", func() {
+ It("yml to json no error", func() {
fmt.Println(5)
y, _ := yaml.JSONToYAML(b)
y2, err := yaml.YAMLToJSON(y)