-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgateway.go
More file actions
65 lines (54 loc) · 1.23 KB
/
gateway.go
File metadata and controls
65 lines (54 loc) · 1.23 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
package coder
import (
"encoding/json"
"github.com/drycc/controller-sdk-go/api"
)
// GatewayCoder implements Coder for Gateway resources.
//
// Decode field mapping:
//
// Metadata.name → Name
// spec.ports → Ports
//
// Encode field mapping:
//
// Ports → spec.ports
// Addresses → status.addresses
type GatewayCoder struct {
Request api.GatewayUpdateRequest
Info api.GatewayInfo
}
func (c *GatewayCoder) Decode(data []byte) error {
var env Manifest
if err := json.Unmarshal(data, &env); err != nil {
return err
}
c.Request = api.GatewayUpdateRequest{
Name: env.Metadata.Name,
}
if ports, ok := env.Spec["ports"]; ok {
portsJSON, err := json.Marshal(ports)
if err != nil {
return err
}
if err := json.Unmarshal(portsJSON, &c.Request.Ports); err != nil {
return err
}
}
return nil
}
func (c *GatewayCoder) Encode() ([]byte, error) {
spec := map[string]any{"ports": normalize(c.Info.Ports)}
status := make(map[string]any)
if len(c.Info.Addresses) > 0 {
status["addresses"] = normalize(c.Info.Addresses)
}
env := Manifest{
APIVersion: APIVersion,
Kind: "Gateway",
Metadata: Metadata{Name: c.Info.Name},
Spec: spec,
Status: status,
}
return marshalYAML(env)
}