feat: add Destination yaml tranformer (#9)
diff --git a/doc/yaml_struct.md b/doc/yaml_struct.md
index 56527e1..2579a75 100644
--- a/doc/yaml_struct.md
+++ b/doc/yaml_struct.md
@@ -96,18 +96,17 @@
### Define the target service
```yaml
-kind: destinations
+kind: Destination
name: foo-dest
host: foo-server
subsets:
- name: foo-v1
- ips:
+ ips:
- 127.0.0.1
- 127.0.0.2
- name: v2
selector:
- labels:
- tag: v2
+ tag: v2
```
diff --git a/pkg/yml/model.go b/pkg/yml/model.go
index e27382f..4d381da 100644
--- a/pkg/yml/model.go
+++ b/pkg/yml/model.go
@@ -49,14 +49,14 @@
Gateways []string `json:"gateways"`
HTTP []HTTP `json:"http"`
}
-type Destination struct {
+type RouteDestination struct {
Port int64 `json:"port"`
Host string `json:"host"`
Subset string `json:"subset"`
Weight int64 `json:"weight"`
}
type Route struct {
- Destination Destination `json:"destination"`
+ Destination RouteDestination `json:"destination"`
}
type Label map[string]string
@@ -74,3 +74,20 @@
func (r *Rule) ToMem() string {
return "Rule"
}
+
+type Destination struct {
+ Kind string `json:"kind"`
+ Name string `json:"name"`
+ Host string `json:"host"`
+ Subsets []Subset `json:"subsets"`
+}
+
+type Subset struct {
+ Name string `json:"name"`
+ Ips []string `json:"ips,omitempty"`
+ Selector map[string]string `json:"selector,omitempty"`
+}
+
+func (g *Destination) ToMem() string {
+ return "destination"
+}
diff --git a/pkg/yml/trans.go b/pkg/yml/trans.go
index 2aa1977..4e81fc7 100644
--- a/pkg/yml/trans.go
+++ b/pkg/yml/trans.go
@@ -40,6 +40,14 @@
} else {
return g
}
+ case "Destination":
+ // trans to Destination
+ if g, err := ToDestination(y); err != nil {
+ fmt.Println("trans to destination error ", err)
+ return nil
+ } else {
+ return g
+ }
case "Rule":
// trans to Rule
if r, err := ToRule(y); err != nil {
@@ -74,3 +82,13 @@
return r, nil
}
}
+
+func ToDestination(y []byte) (*Destination, error) {
+ var g *Destination
+ 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
index 29c085d..ee06609 100644
--- a/pkg/yml/trans_test.go
+++ b/pkg/yml/trans_test.go
@@ -46,7 +46,6 @@
ym := yml.Trans(y, b)
Expect(err).NotTo(HaveOccurred())
v := typeof(ym)
- fmt.Println(v)
Expect(v).To(Equal("*yml.Gateway"))
g, ok := ym.(*yml.Gateway)
Expect(ok).To(Equal(true))
@@ -84,18 +83,44 @@
app: foo
version: v2
`)
+
y, err := yaml.YAMLToJSON(b)
fmt.Println(string(y))
ym := yml.Trans(y, b)
Expect(err).NotTo(HaveOccurred())
v := typeof(ym)
- fmt.Println(v)
Expect(v).To(Equal("*yml.Rule"))
r, ok := ym.(*yml.Rule)
Expect(ok).To(Equal(true))
Expect(r.Kind).To(Equal("Rule"))
Expect(r.Kind).To(Equal("Rule"))
})
+
+ It("trans to destination no error", func() {
+ b := []byte(`
+kind: Destination
+name: foo-dest
+host: foo-server
+subsets:
+- name: foo-v1
+ ips:
+ - 127.0.0.1
+ - 127.0.0.2
+- name: v2
+ selector:
+ tag: v2
+`)
+ y, err := yaml.YAMLToJSON(b)
+ fmt.Println(string(y))
+ ym := yml.Trans(y, b)
+ Expect(err).NotTo(HaveOccurred())
+ v := typeof(ym)
+ Expect(v).To(Equal("*yml.Destination"))
+ g, ok := ym.(*yml.Destination)
+ Expect(ok).To(Equal(true))
+ Expect(g.Kind).To(Equal("Destination"))
+ Expect(g.Host).To(Equal("foo-server"))
+ })
})
})
})