-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathroute.go
More file actions
97 lines (84 loc) · 2.01 KB
/
route.go
File metadata and controls
97 lines (84 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package coder
import (
"encoding/json"
"github.com/drycc/controller-sdk-go/api"
)
// RouteCoder implements Coder for HTTPRoute resources.
//
// Decode field mapping:
//
// Metadata.name → Name
// kind → Kind
// spec.parentRefs → Parents
// spec.rules[].backends → Rules[].backendRefs
//
// Encode field mapping:
//
// Rules[].backendRefs → spec.rules[].backends
// Routable → status.routable
type RouteCoder struct {
Request api.RouteUpdateRequest
Info api.RouteInfo
}
func (c *RouteCoder) Decode(data []byte) error {
var env Manifest
if err := json.Unmarshal(data, &env); err != nil {
return err
}
c.Request = api.RouteUpdateRequest{
Name: env.Metadata.Name,
Kind: env.Kind,
}
if parents, ok := env.Spec["parents"]; ok {
parentsJSON, err := json.Marshal(parents)
if err != nil {
return err
}
if err := json.Unmarshal(parentsJSON, &c.Request.ParentRefs); err != nil {
return err
}
}
if rules, ok := env.Spec["rules"]; ok {
if rulesList, ok := rules.([]any); ok {
for _, r := range rulesList {
if ruleMap, ok := r.(map[string]any); ok {
routeRule := api.RouteRule{}
for k, v := range ruleMap {
if k == "backends" {
routeRule["backendRefs"] = v
} else {
routeRule[k] = v
}
}
c.Request.Rules = append(c.Request.Rules, routeRule)
}
}
}
}
return nil
}
func (c *RouteCoder) Encode() ([]byte, error) {
var rules []map[string]any
for _, r := range c.Info.Rules {
rule := map[string]any{}
if backends, ok := r["backendRefs"]; ok {
rule["backends"] = backends
}
rules = append(rules, rule)
}
spec := make(map[string]any)
spec["parents"] = normalize(c.Info.ParentRefs)
spec["rules"] = rules
status := make(map[string]any)
if c.Info.Routable != nil {
status["routable"] = c.Info.Routable
}
env := Manifest{
APIVersion: APIVersion,
Kind: c.Info.Kind,
Metadata: Metadata{Name: c.Info.Name},
Spec: spec,
Status: status,
}
return marshalYAML(env)
}