Skip to content
Permalink
Browse files
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
  • Loading branch information
gxthrj committed Aug 13, 2020
1 parent f8ce5a0 commit 3334b28433188d727aa8a8f653873b08663fcf5d
Showing 11 changed files with 235 additions and 19 deletions.
@@ -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).
1 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
)
@@ -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"`
}
@@ -0,0 +1,48 @@
/*
* 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 yml

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 (g *Gateway) Type() string {
return "Gateway"
}
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package yaml
package yml

import (
"fmt"
@@ -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())
@@ -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"

@@ -0,0 +1,59 @@
/*
* 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 yml

import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v2"
)

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
}
}
@@ -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))
})
})
})
})
@@ -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"
@@ -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,24 +25,24 @@ 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))
Expect(err).NotTo(HaveOccurred())
})
})

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)

0 comments on commit 3334b28

Please sign in to comment.