-
Notifications
You must be signed in to change notification settings - Fork 11
/
envoyfilter.go
260 lines (243 loc) · 8.49 KB
/
envoyfilter.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright The HTNN Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package istio
import (
"fmt"
"sort"
"google.golang.org/protobuf/types/known/structpb"
istioapi "istio.io/api/networking/v1alpha3"
istiov1a3 "istio.io/client-go/pkg/apis/networking/v1alpha3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrlcfg "mosn.io/htnn/controller/internal/config"
"mosn.io/htnn/controller/internal/model"
"mosn.io/htnn/pkg/plugins"
)
func MustNewStruct(fields map[string]interface{}) *structpb.Struct {
st, err := structpb.NewStruct(fields)
if err != nil {
// NewStruct returns error only when the fields contain non-standard type
panic(err)
}
return st
}
const (
DefaultHttpFilter = "htnn-http-filter"
ECDSConsumerName = "htnn-consumer"
)
type configWrapper struct {
name string
pre bool
filter map[string]interface{}
}
func DefaultEnvoyFilters() map[string]*istiov1a3.EnvoyFilter {
efs := map[string]*istiov1a3.EnvoyFilter{}
defaultMatch := &istioapi.EnvoyFilter_EnvoyConfigObjectMatch{
// As currently we only support Gateway cases, here we hardcode the context
// to default envoy filters. We don't need to do that for user-defined envoy
// filters. Because adding that will require a break change to remove it.
// And user-defined envoy filter won't apply to mesh because:
// 1. We don't support attaching policy to mesh.
// 2. Mesh configuration doesn't have Go HTTP filter.
Context: istioapi.EnvoyFilter_GATEWAY,
ObjectTypes: &istioapi.EnvoyFilter_EnvoyConfigObjectMatch_Listener{
Listener: &istioapi.EnvoyFilter_ListenerMatch{
FilterChain: &istioapi.EnvoyFilter_ListenerMatch_FilterChainMatch{
Filter: &istioapi.EnvoyFilter_ListenerMatch_FilterMatch{
Name: "envoy.filters.network.http_connection_manager",
SubFilter: &istioapi.EnvoyFilter_ListenerMatch_SubFilterMatch{
Name: "envoy.filters.http.router",
},
},
},
},
},
}
patches := []*istioapi.EnvoyFilter_EnvoyConfigObjectPatch{}
// Go
patches = append(patches, &istioapi.EnvoyFilter_EnvoyConfigObjectPatch{
ApplyTo: istioapi.EnvoyFilter_HTTP_FILTER,
Match: defaultMatch,
Patch: &istioapi.EnvoyFilter_Patch{
Operation: istioapi.EnvoyFilter_Patch_INSERT_FIRST,
Value: MustNewStruct(map[string]interface{}{
"disabled": true,
"name": "envoy.filters.http.golang",
"typed_config": map[string]interface{}{
"@type": "type.googleapis.com/envoy.extensions.filters.http.golang.v3alpha.Config",
"library_id": "fm",
"library_path": ctrlcfg.GoSoPath(),
"plugin_name": "fm",
},
}),
},
})
// Native filters can only be used before/after Go plugins.
configs := []*configWrapper{}
plugins.IterateHttpPlugin(func(key string, value plugins.Plugin) bool {
nativePlugin, ok := value.(plugins.NativePlugin)
if !ok {
return true
}
filter := nativePlugin.HTTPFilterConfigPlaceholder()
filter["name"] = fmt.Sprintf("envoy.filters.http.%s", key)
filter["disabled"] = true
configs = append(configs, &configWrapper{
name: key,
pre: nativePlugin.Order().Position == plugins.OrderPositionOuter,
filter: filter,
})
return true
})
sort.Slice(configs, func(i, j int) bool {
return plugins.ComparePluginOrder(configs[i].name, configs[j].name)
})
for _, config := range configs {
filter := config.filter
if config.pre {
patches = append(patches, &istioapi.EnvoyFilter_EnvoyConfigObjectPatch{
ApplyTo: istioapi.EnvoyFilter_HTTP_FILTER,
Match: &istioapi.EnvoyFilter_EnvoyConfigObjectMatch{
Context: istioapi.EnvoyFilter_GATEWAY,
ObjectTypes: &istioapi.EnvoyFilter_EnvoyConfigObjectMatch_Listener{
Listener: &istioapi.EnvoyFilter_ListenerMatch{
FilterChain: &istioapi.EnvoyFilter_ListenerMatch_FilterChainMatch{
Filter: &istioapi.EnvoyFilter_ListenerMatch_FilterMatch{
Name: "envoy.filters.network.http_connection_manager",
SubFilter: &istioapi.EnvoyFilter_ListenerMatch_SubFilterMatch{
Name: "envoy.filters.http.golang",
},
},
},
},
},
},
Patch: &istioapi.EnvoyFilter_Patch{
Operation: istioapi.EnvoyFilter_Patch_INSERT_BEFORE,
Value: MustNewStruct(filter),
},
})
} else {
patches = append(patches, &istioapi.EnvoyFilter_EnvoyConfigObjectPatch{
ApplyTo: istioapi.EnvoyFilter_HTTP_FILTER,
Match: defaultMatch,
Patch: &istioapi.EnvoyFilter_Patch{
Operation: istioapi.EnvoyFilter_Patch_INSERT_BEFORE,
Value: MustNewStruct(filter),
},
})
}
}
efs[DefaultHttpFilter] = &istiov1a3.EnvoyFilter{
ObjectMeta: metav1.ObjectMeta{
Name: DefaultHttpFilter,
},
Spec: istioapi.EnvoyFilter{
ConfigPatches: patches,
Priority: 100, // the priority is specific to silent the warning for relative operation
},
}
return efs
}
func GenerateRouteFilter(host *model.VirtualHost, route string, config map[string]interface{}) *istiov1a3.EnvoyFilter {
applyTo := istioapi.EnvoyFilter_HTTP_ROUTE
vhost := &istioapi.EnvoyFilter_RouteConfigurationMatch_VirtualHostMatch{
Name: host.Name,
Route: &istioapi.EnvoyFilter_RouteConfigurationMatch_RouteMatch{
Name: route,
},
}
return &istiov1a3.EnvoyFilter{
Spec: istioapi.EnvoyFilter{
ConfigPatches: []*istioapi.EnvoyFilter_EnvoyConfigObjectPatch{
{
ApplyTo: applyTo,
Match: &istioapi.EnvoyFilter_EnvoyConfigObjectMatch{
ObjectTypes: &istioapi.EnvoyFilter_EnvoyConfigObjectMatch_RouteConfiguration{
RouteConfiguration: &istioapi.EnvoyFilter_RouteConfigurationMatch{
Vhost: vhost,
},
},
},
Patch: &istioapi.EnvoyFilter_Patch{
Operation: istioapi.EnvoyFilter_Patch_MERGE,
Value: MustNewStruct(map[string]interface{}{
"typed_per_filter_config": config,
}),
},
},
},
},
}
}
func GenerateConsumers(consumers map[string]interface{}) *istiov1a3.EnvoyFilter {
return &istiov1a3.EnvoyFilter{
Spec: istioapi.EnvoyFilter{
ConfigPatches: []*istioapi.EnvoyFilter_EnvoyConfigObjectPatch{
{
ApplyTo: istioapi.EnvoyFilter_EXTENSION_CONFIG,
Patch: &istioapi.EnvoyFilter_Patch{
Operation: istioapi.EnvoyFilter_Patch_ADD,
Value: MustNewStruct(map[string]interface{}{
"name": ECDSConsumerName,
"typed_config": map[string]interface{}{
"@type": "type.googleapis.com/envoy.extensions.filters.http.golang.v3alpha.Config",
"library_id": "cm",
"library_path": "/etc/libgolang.so",
"plugin_name": "cm",
"plugin_config": map[string]interface{}{
"@type": "type.googleapis.com/xds.type.v3.TypedStruct",
"value": consumers,
},
},
}),
},
},
{
ApplyTo: istioapi.EnvoyFilter_HTTP_FILTER,
Match: &istioapi.EnvoyFilter_EnvoyConfigObjectMatch{
Context: istioapi.EnvoyFilter_GATEWAY,
ObjectTypes: &istioapi.EnvoyFilter_EnvoyConfigObjectMatch_Listener{
Listener: &istioapi.EnvoyFilter_ListenerMatch{
FilterChain: &istioapi.EnvoyFilter_ListenerMatch_FilterChainMatch{
Filter: &istioapi.EnvoyFilter_ListenerMatch_FilterMatch{
Name: "envoy.filters.network.http_connection_manager",
SubFilter: &istioapi.EnvoyFilter_ListenerMatch_SubFilterMatch{
Name: "envoy.filters.http.router",
},
},
},
},
},
},
// We put the HTTP_FILTER in Consumer's patch, so that deployment which
// doesn't use Consumer won't need to subscribe to this ECDS. The side effect
// is that the first consumer will cause LDS drain, but it's similar to
// deploy a Wasm plugin.
Patch: &istioapi.EnvoyFilter_Patch{
Operation: istioapi.EnvoyFilter_Patch_INSERT_BEFORE,
Value: MustNewStruct(map[string]interface{}{
"name": ECDSConsumerName,
"config_discovery": map[string]interface{}{
"type_urls": []interface{}{"type.googleapis.com/envoy.extensions.filters.http.golang.v3alpha.Config"},
"config_source": map[string]interface{}{
"ads": map[string]interface{}{},
},
},
}),
},
},
},
},
}
}