-
Notifications
You must be signed in to change notification settings - Fork 39
/
envoy_filter.go
119 lines (106 loc) · 3.13 KB
/
envoy_filter.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
package filter
import (
"github.com/solo-io/gloo/projects/gloo/pkg/api/external/envoy/api/v2/config"
"github.com/solo-io/solo-kit/pkg/api/external/envoy/api/v2/core"
"github.com/solo-io/wasme/pkg/util"
"github.com/gogo/protobuf/types"
"github.com/pkg/errors"
envoyhttp "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/http_connection_manager/v2"
corev3 "github.com/solo-io/gloo/projects/gloo/pkg/api/external/envoy/config/core/v3"
wasmv3 "github.com/solo-io/gloo/projects/gloo/pkg/api/external/envoy/extensions/wasm/v3"
wasmev1 "github.com/solo-io/wasme/pkg/operator/api/wasme.io/v1"
)
func MakeRemoteDataSource(uri, cluster string) *core.AsyncDataSource {
return &core.AsyncDataSource{
Specifier: &core.AsyncDataSource_Remote{
Remote: &core.RemoteDataSource{
HttpUri: &core.HttpUri{
Uri: uri,
HttpUpstreamType: &core.HttpUri_Cluster{
Cluster: cluster,
},
Timeout: &types.Duration{
Seconds: 5, // TODO: customize
},
},
},
},
}
}
func MakeLocalDatasource(path string) *core.AsyncDataSource {
return &core.AsyncDataSource{
Specifier: &core.AsyncDataSource_Local{
Local: &core.DataSource{
Specifier: &core.DataSource_Filename{
Filename: path,
},
},
},
}
}
func MakeWasmFilter(filter *wasmev1.FilterSpec, dataSrc *corev3.AsyncDataSource) *envoyhttp.HttpFilter {
filterCfg := &wasmv3.WasmService{
Config: &wasmv3.PluginConfig{
Name: filter.Id,
RootId: filter.RootID,
Configuration: filter.Config,
Vm: &wasmv3.PluginConfig_VmConfig{
VmConfig: &wasmv3.VmConfig{
Runtime: "envoy.wasm.runtime.v8", // default to v8
Code: dataSrc,
},
},
},
}
// here we need to use the golang proto marshal
marshalledConf, err := util.MarshalStruct(filterCfg)
if err != nil {
// this should NEVER HAPPEN!
panic(err)
}
return &envoyhttp.HttpFilter{
Name: util.WasmFilterName,
ConfigType: &envoyhttp.HttpFilter_Config{
Config: marshalledConf,
},
}
}
func MakeIstioWasmFilter(filter *wasmev1.FilterSpec, dataSrc *core.AsyncDataSource) (*envoyhttp.HttpFilter, error) {
var cfgVal string
if filter.Config != nil {
// As the config's value is a StringValue, we need to unmarshall it,
// typecheck it, then get the value out of the result.
var da types.DynamicAny
if err := types.UnmarshalAny(filter.Config, &da); err != nil {
return nil, err
}
cfg, ok := da.Message.(*types.StringValue)
if !ok {
return nil, errors.Errorf("wasm filter configuration has an invalid type, should be StringValue")
}
cfgVal = cfg.GetValue()
}
filterCfg := &config.WasmService{
Config: &config.PluginConfig{
Name: filter.Id,
RootId: filter.RootID,
Configuration: cfgVal,
VmConfig: &config.VmConfig{
Runtime: "envoy.wasm.runtime.v8", // default to v8
Code: dataSrc,
VmId: filter.Id,
},
},
}
// here we need to use the golang proto marshal
marshalledConf, err := util.MarshalStruct(filterCfg)
if err != nil {
return nil, err
}
return &envoyhttp.HttpFilter{
Name: util.WasmFilterName,
ConfigType: &envoyhttp.HttpFilter_Config{
Config: marshalledConf,
},
}, nil
}