-
Notifications
You must be signed in to change notification settings - Fork 12
/
serializer.go
168 lines (143 loc) · 7.52 KB
/
serializer.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package envoy
import (
"bytes"
"encoding/base64"
"fmt"
envoy "github.com/3scale-ops/marin3r/pkg/envoy"
envoy_api_v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoy_api_v2_auth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
envoy_service_discovery_v2 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v2"
"github.com/ghodss/yaml"
"github.com/golang/protobuf/jsonpb"
// This is the list of imports so all proto types are registered.
// Generated with the following command in go-control-plane@v0.9.7
//
// for proto in $(find envoy -name '*.pb.go' | grep v2 | grep -v v2alpha); do echo "_ \"github.com/envoyproxy/go-control-plane/$(dirname $proto)\""; done | sort | uniq
_ "github.com/envoyproxy/go-control-plane/envoy/api/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
_ "github.com/envoyproxy/go-control-plane/envoy/api/v2/cluster"
_ "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
_ "github.com/envoyproxy/go-control-plane/envoy/api/v2/endpoint"
_ "github.com/envoyproxy/go-control-plane/envoy/api/v2/listener"
_ "github.com/envoyproxy/go-control-plane/envoy/api/v2/ratelimit"
_ "github.com/envoyproxy/go-control-plane/envoy/api/v2/route"
_ "github.com/envoyproxy/go-control-plane/envoy/config/accesslog/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/bootstrap/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/accesslog/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/fault/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/buffer/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/compressor/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/cors/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/csrf/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/dynamo/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/ext_authz/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/fault/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/grpc_http1_bridge/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/grpc_web/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/gzip/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/header_to_metadata/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/health_check/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/ip_tagging/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/lua/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/on_demand/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/rate_limit/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/rbac/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/router/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/squash/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/transcoder/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/listener/http_inspector/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/listener/original_dst/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/listener/proxy_protocol/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/listener/tls_inspector/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/client_ssl_auth/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/direct_response/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/echo/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/ext_authz/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/http_connection_manager/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/mongo_proxy/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/rate_limit/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/rbac/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/redis_proxy/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/sni_cluster/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/tcp_proxy/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/health_checker/redis/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/listener/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/metrics/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/ratelimit/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/rbac/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/retry/omit_canary_hosts/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/retry/omit_host_metadata/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/retry/previous_hosts/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/trace/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/transport_socket/raw_buffer/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/data/accesslog/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/service/accesslog/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/service/auth/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/service/load_stats/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/service/metrics/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/service/status/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/service/trace/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/type/metadata/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/type/tracing/v2"
)
type JSON struct{}
func (s JSON) Marshal(res envoy.Resource) (string, error) {
m := jsonpb.Marshaler{OrigName: true}
json := bytes.NewBuffer([]byte{})
err := m.Marshal(json, res)
if err != nil {
return "", err
}
return string(json.Bytes()), nil
}
func (s JSON) Unmarshal(str string, res envoy.Resource) error {
var err error
switch o := res.(type) {
case *envoy_api_v2.ClusterLoadAssignment:
err = jsonpb.Unmarshal(bytes.NewReader([]byte(str)), o)
case *envoy_api_v2.Cluster:
err = jsonpb.Unmarshal(bytes.NewReader([]byte(str)), o)
case *envoy_api_v2.RouteConfiguration:
err = jsonpb.Unmarshal(bytes.NewReader([]byte(str)), o)
case *envoy_api_v2.Listener:
err = jsonpb.Unmarshal(bytes.NewReader([]byte(str)), o)
case *envoy_service_discovery_v2.Runtime:
err = jsonpb.Unmarshal(bytes.NewReader([]byte(str)), o)
case *envoy_api_v2_auth.Secret:
err = jsonpb.Unmarshal(bytes.NewReader([]byte(str)), o)
default:
err = fmt.Errorf("Unknown resource type")
}
if err != nil {
return fmt.Errorf("Error deserializing resource: '%s'", err)
}
return nil
}
type B64JSON struct{}
func (s B64JSON) Unmarshal(str string, res envoy.Resource) error {
b, err := base64.StdEncoding.DecodeString(str)
if err != nil {
return fmt.Errorf("Error decoding base64 string: '%s'", err)
}
js := JSON{}
err = js.Unmarshal(string(b), res)
if err != nil {
return err
}
return nil
}
type YAML struct{}
func (s YAML) Unmarshal(str string, res envoy.Resource) error {
b, err := yaml.YAMLToJSON([]byte(str))
if err != nil {
return fmt.Errorf("Error converting yaml to json: '%s'", err)
}
js := JSON{}
err = js.Unmarshal(string(b), res)
if err != nil {
return err
}
return nil
}