fix: when multiple http2Rpc config constructHttp2RpcEnvoyFilter (… (#935)

This commit is contained in:
韩贤涛
2024-05-14 19:17:48 +08:00
committed by GitHub
parent 333f9b48f3
commit 8043780de0

View File

@@ -513,6 +513,7 @@ func (m *IngressConfig) convertEnvoyFilter(convertOptions *common.ConvertOptions
var envoyFilters []config.Config var envoyFilters []config.Config
mappings := map[string]*common.Rule{} mappings := map[string]*common.Rule{}
initHttp2RpcGlobalConfig := true
for _, routes := range convertOptions.HTTPRoutes { for _, routes := range convertOptions.HTTPRoutes {
for _, route := range routes { for _, route := range routes {
if strings.HasSuffix(route.HTTPRoute.Name, "app-root") { if strings.HasSuffix(route.HTTPRoute.Name, "app-root") {
@@ -522,12 +523,13 @@ func (m *IngressConfig) convertEnvoyFilter(convertOptions *common.ConvertOptions
http2rpc := route.WrapperConfig.AnnotationsConfig.Http2Rpc http2rpc := route.WrapperConfig.AnnotationsConfig.Http2Rpc
if http2rpc != nil { if http2rpc != nil {
IngressLog.Infof("Found http2rpc for name %s", http2rpc.Name) IngressLog.Infof("Found http2rpc for name %s", http2rpc.Name)
envoyFilter, err := m.constructHttp2RpcEnvoyFilter(http2rpc, route, m.namespace) envoyFilter, err := m.constructHttp2RpcEnvoyFilter(http2rpc, route, m.namespace, initHttp2RpcGlobalConfig)
if err != nil { if err != nil {
IngressLog.Infof("Construct http2rpc EnvoyFilter error %v", err) IngressLog.Infof("Construct http2rpc EnvoyFilter error %v", err)
} else { } else {
IngressLog.Infof("Append http2rpc EnvoyFilter for name %s", http2rpc.Name) IngressLog.Infof("Append http2rpc EnvoyFilter for name %s", http2rpc.Name)
envoyFilters = append(envoyFilters, *envoyFilter) envoyFilters = append(envoyFilters, *envoyFilter)
initHttp2RpcGlobalConfig = false
} }
} }
@@ -1143,7 +1145,7 @@ func (m *IngressConfig) applyCanaryIngresses(convertOptions *common.ConvertOptio
} }
} }
func (m *IngressConfig) constructHttp2RpcEnvoyFilter(http2rpcConfig *annotations.Http2RpcConfig, route *common.WrapperHTTPRoute, namespace string) (*config.Config, error) { func (m *IngressConfig) constructHttp2RpcEnvoyFilter(http2rpcConfig *annotations.Http2RpcConfig, route *common.WrapperHTTPRoute, namespace string, initHttp2RpcGlobalConfig bool) (*config.Config, error) {
mappings := m.http2rpcs mappings := m.http2rpcs
IngressLog.Infof("Found http2rpc mappings %v", mappings) IngressLog.Infof("Found http2rpc mappings %v", mappings)
if _, exist := mappings[http2rpcConfig.Name]; !exist { if _, exist := mappings[http2rpcConfig.Name]; !exist {
@@ -1163,75 +1165,39 @@ func (m *IngressConfig) constructHttp2RpcEnvoyFilter(http2rpcConfig *annotations
if err != nil { if err != nil {
return nil, errors.New(err.Error()) return nil, errors.New(err.Error())
} }
configPatches := []*networking.EnvoyFilter_EnvoyConfigObjectPatch{
return &config.Config{ {
Meta: config.Meta{ ApplyTo: networking.EnvoyFilter_HTTP_ROUTE,
GroupVersionKind: gvk.EnvoyFilter, Match: &networking.EnvoyFilter_EnvoyConfigObjectMatch{
Name: common.CreateConvertedName(constants.IstioIngressGatewayName, http2rpcConfig.Name), Context: networking.EnvoyFilter_GATEWAY,
Namespace: namespace, ObjectTypes: &networking.EnvoyFilter_EnvoyConfigObjectMatch_RouteConfiguration{
RouteConfiguration: &networking.EnvoyFilter_RouteConfigurationMatch{
Vhost: &networking.EnvoyFilter_RouteConfigurationMatch_VirtualHostMatch{
Route: &networking.EnvoyFilter_RouteConfigurationMatch_RouteMatch{
Name: httpRoute.Name,
},
},
},
},
},
Patch: &networking.EnvoyFilter_Patch{
Operation: networking.EnvoyFilter_Patch_MERGE,
Value: typeStruct,
},
}, },
Spec: &networking.EnvoyFilter{ {
ConfigPatches: []*networking.EnvoyFilter_EnvoyConfigObjectPatch{ ApplyTo: networking.EnvoyFilter_CLUSTER,
{ Match: &networking.EnvoyFilter_EnvoyConfigObjectMatch{
ApplyTo: networking.EnvoyFilter_HTTP_FILTER, Context: networking.EnvoyFilter_GATEWAY,
Match: &networking.EnvoyFilter_EnvoyConfigObjectMatch{ ObjectTypes: &networking.EnvoyFilter_EnvoyConfigObjectMatch_Cluster{
Context: networking.EnvoyFilter_GATEWAY, Cluster: &networking.EnvoyFilter_ClusterMatch{
ObjectTypes: &networking.EnvoyFilter_EnvoyConfigObjectMatch_Listener{ Service: httpRouteDestination.Destination.Host,
Listener: &networking.EnvoyFilter_ListenerMatch{
FilterChain: &networking.EnvoyFilter_ListenerMatch_FilterChainMatch{
Filter: &networking.EnvoyFilter_ListenerMatch_FilterMatch{
Name: "envoy.filters.network.http_connection_manager",
SubFilter: &networking.EnvoyFilter_ListenerMatch_SubFilterMatch{
Name: "envoy.filters.http.router",
},
},
},
},
},
},
Patch: &networking.EnvoyFilter_Patch{
Operation: networking.EnvoyFilter_Patch_INSERT_BEFORE,
Value: buildPatchStruct(`{
"name":"envoy.filters.http.http_dubbo_transcoder",
"typed_config":{
"@type":"type.googleapis.com/udpa.type.v1.TypedStruct",
"type_url":"type.googleapis.com/envoy.extensions.filters.http.http_dubbo_transcoder.v3.HttpDubboTranscoder"
}
}`),
}, },
}, },
{ },
ApplyTo: networking.EnvoyFilter_HTTP_ROUTE, Patch: &networking.EnvoyFilter_Patch{
Match: &networking.EnvoyFilter_EnvoyConfigObjectMatch{ Operation: networking.EnvoyFilter_Patch_MERGE,
Context: networking.EnvoyFilter_GATEWAY, Value: buildPatchStruct(`{
ObjectTypes: &networking.EnvoyFilter_EnvoyConfigObjectMatch_RouteConfiguration{
RouteConfiguration: &networking.EnvoyFilter_RouteConfigurationMatch{
Vhost: &networking.EnvoyFilter_RouteConfigurationMatch_VirtualHostMatch{
Route: &networking.EnvoyFilter_RouteConfigurationMatch_RouteMatch{
Name: httpRoute.Name,
},
},
},
},
},
Patch: &networking.EnvoyFilter_Patch{
Operation: networking.EnvoyFilter_Patch_MERGE,
Value: typeStruct,
},
},
{
ApplyTo: networking.EnvoyFilter_CLUSTER,
Match: &networking.EnvoyFilter_EnvoyConfigObjectMatch{
Context: networking.EnvoyFilter_GATEWAY,
ObjectTypes: &networking.EnvoyFilter_EnvoyConfigObjectMatch_Cluster{
Cluster: &networking.EnvoyFilter_ClusterMatch{
Service: httpRouteDestination.Destination.Host,
},
},
},
Patch: &networking.EnvoyFilter_Patch{
Operation: networking.EnvoyFilter_Patch_MERGE,
Value: buildPatchStruct(`{
"upstream_config": { "upstream_config": {
"name":"envoy.upstreams.http.dubbo_tcp", "name":"envoy.upstreams.http.dubbo_tcp",
"typed_config":{ "typed_config":{
@@ -1240,9 +1206,47 @@ func (m *IngressConfig) constructHttp2RpcEnvoyFilter(http2rpcConfig *annotations
} }
} }
}`), }`),
},
},
}
if initHttp2RpcGlobalConfig {
configPatches = append(configPatches, &networking.EnvoyFilter_EnvoyConfigObjectPatch{
ApplyTo: networking.EnvoyFilter_HTTP_FILTER,
Match: &networking.EnvoyFilter_EnvoyConfigObjectMatch{
Context: networking.EnvoyFilter_GATEWAY,
ObjectTypes: &networking.EnvoyFilter_EnvoyConfigObjectMatch_Listener{
Listener: &networking.EnvoyFilter_ListenerMatch{
FilterChain: &networking.EnvoyFilter_ListenerMatch_FilterChainMatch{
Filter: &networking.EnvoyFilter_ListenerMatch_FilterMatch{
Name: "envoy.filters.network.http_connection_manager",
SubFilter: &networking.EnvoyFilter_ListenerMatch_SubFilterMatch{
Name: "envoy.filters.http.router",
},
},
},
}, },
}, },
}, },
Patch: &networking.EnvoyFilter_Patch{
Operation: networking.EnvoyFilter_Patch_INSERT_BEFORE,
Value: buildPatchStruct(`{
"name":"envoy.filters.http.http_dubbo_transcoder",
"typed_config":{
"@type":"type.googleapis.com/udpa.type.v1.TypedStruct",
"type_url":"type.googleapis.com/envoy.extensions.filters.http.http_dubbo_transcoder.v3.HttpDubboTranscoder"
}
}`),
},
})
}
return &config.Config{
Meta: config.Meta{
GroupVersionKind: gvk.EnvoyFilter,
Name: common.CreateConvertedName(constants.IstioIngressGatewayName, http2rpcConfig.Name),
Namespace: namespace,
},
Spec: &networking.EnvoyFilter{
ConfigPatches: configPatches,
}, },
}, nil }, nil
} }