1。什么是按条件过滤?当指定值时执行此过滤器,如果匹配到值则跳过。他可以和Compositefilter结合,在满足条件的时候执行某个filter。条件过滤器是通过ExtensionWithMatcher实现的,其中extension配置要执行的过滤器,matcher匹配条件,条件匹配后执行action。2.相关配置2.1ExtensionWithMatcher{"matcher":"{...}",匹配"xds_matcher":"{...}",匹配条件和动作,"extension_config":"{...}还没有已实施“过滤器扩展配置}xds_matcher:{“matcher_list”:“{...}”,线性匹配器“matcher_tree”:“{...}”,树匹配器“on_no_match”:“{...}”没有匹配大小写处理}matcher_list:{"matchers":[]match}matchers:{"predicate":"{...}",判断是否匹配"on_match":"{...}"如果匹配怎么办}predicate:{"single_predicate":"{...}",单匹配"or_matcher":"{...}",或者匹配"and_matcher":"{...}",匹配"not_matcher":"{...}"不匹配}single_predicate:{"input":"{...}",输入"value_match":"{...}",值匹配"custom_match":"{...}"fromDefinematching}input:envoy.matching.common_inputs.environment_variable{"exact":"...",exactmatch"prefix":"...",前缀匹配"suffix":"...",后缀匹配"safe_regex":"{...}",正则匹配"contains":"...",containsmatch"ignore_case":"..."忽略大小写}custom_match:envoy.matching.input_matchers.consistent_hashingenvoy.matching.input_matchers.ipmatcher_tree:{"input":"{...}",输入"exact_match_map":"{。..}",精确或前缀匹配"prefix_match_map":"{...}",前缀匹配"custom_match":"{...}"自定义前缀或精确匹配}exact_match_map,prefix_match_map{"map":"{...}"}map:{"matcher":"{...}",匹配"action":"{...}"action}matcher:{"matcher_list":"{...}",线性匹配器“matcher_tree”:“{...}”,树匹配器“on_no_match”:“{...}”没有匹配处理}输入:type.matcher.v3.HttpRequestHeaderMatchInputtype.matcher.v3.HttpRequestTrailerMatchInputtype.matcher.v3.HttpResponseHeaderMatchInputtype.matcher.v3.HttpResponseTrailerMatchInputconfig.common.matcher.v3.MatchPredicate{"or_match":"{...}","and_match":"{...}","not_match":"{...}","any_match":"...","http_request_headers_match":"{...}","http_request_trailers_match":"{...}","http_response_headers_match":"{...}","http_response_trailers_match":"{...}","http_request_generic_body_match":"{...}","http_response_generic_body_match":"{...}"}3.实战中,istio默认的envoy并没有开启该功能,通过注解启用apiVersion:v1kind:ConfigMapmetadata:name:istio-custom-bootstrap-configdata:custom_bootstrap.json:|{"layered_runtime":{"layers":[{"name":"static-layer","static_layer":{"envoy":{"reloadable_features":{"experimental_matching_api":true}}}}]}}productpage-deploy.yamlkubectlapply-fproductpage-deploy.yaml-nistioapiVersion:apps/v1kind:Deploymentmetadata:name:productpage-v1labels:app:productpageversion:v1spec:replicas:1selector:matchLabels:app:productpageversion:v1template:metadata:labels:app:productpageversion:v1annotations:sidecar.istio.io/bootstrapOverride:"istio-custom-bootstrap-config"spec:serviceAccountName:bookinfo-productpagecontainers:-name:productpageimage:docker.io/istio/examples-bookinfo-productpage-v1:1.16.2imagePullPolicy:IfNotPresentports:-containerPort:9080volumeMounts:-name:tmpmountPath:/tmpsecurityContext:runAsUser:1000volumes:-name:tmpemptyDir:{}---3.1ExtensionWithMatcher3.1.1matcher_listapiVersion:networking.istio.io/v1alpha3kind:EnvoyFiltermetadata:name:extensionspec:workloadSelector:labels:app:productpageconfigPatches:-applyTo:HTTP_FILTERmatch:context:SIDECAR_OUTBOUNDlistener:name:0.0.0.0_9080filterChain:filter:名称:“envoy.filters.network.http_connection_manager”子过滤器:名称:“envoy.filters.http.router”补丁:操作:INSERT_BEFORE值:名称:testtyped_config:“@type”:type.googleapis.com/envoy.extensions。common.matching.v3.ExtensionWithMatcherextension_config:name:envoy.filters.http.faulttyped_config:"@type":type.googleapis.com/envoy.extensions.filters.http.fault.v3.HTTPFaultabort:http_status:503percentage:numerator:100denominator:HUNDREDdelay:fixed_delay:3spercentage:numerator:100denominator:HUNDREDmatcher:matcher_list:matchers:-predicate:or_matcher:predicate:-single_predicate:input:name:request-headerstyped_config:"@type":type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInputheader_name:end-uservalue_match:exact:jason-single_predicate:input:name:request-headerstyped_config:"@type":type.googleapis.com/envoy.type.matcher.v3.HttpResponseHeaderMatchInputheader_name:testvalue_match:exact:baron_match:action:name:skiptyped_config:"@type":type.googleapis.com/envoy.extensions.filters.common.matcher.action.v3.SkipFilter3.1.2matcher_treeef-ExtensionWithMatcher-matcher_tree.yamlkubectl应用-fef-ExtensionWithMatcher-matcher_tree.yaml-nistioapiVersion:networking.istio.io/v1alpha3kind:EnvoyFiltermetadata:name:extensionspec:workloadSelector:labels:app:productpageconfigPatches:-applyTo:HTTP_FILTERmatch:context:SIDECAR_OUTBOUNDlistener:name:0.0.0.0_9080filterChain:filter:name:"envoy.filters.network.http_connection_manager"subFilter:name:"envoy.filters.http.router"patch:operation:INSERT_BEFOREvalue:name:testtyped_config:"@type":type.googleapis.com/envoy.extensions.common.matching.v3.ExtensionWithMatcherextension_config:name:envoy.filters.http.faulttyped_config:"@type":type.googleapis.com/envoy.extensions.filters.http。fault.v3.HTTPFaultabort:http_status:503percentage:numerator:100denominator:HUNDREDdelay:fixed_delay:3spercentage:numerator:100denominator:HUNDREDmatcher:matcher_tree:input:name:request-headerstyped_config:"@type":type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInputheader_name:end-userexact_match_map:map:jason:matcher:matcher_list:matchers:-predicate:or_matcher:predicate:-single_predicate:input:name:request-headerstyped_config:"@type":type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInputheader_name:end-uservalue_match:exact:jason-single_predicate:input:name:request-headerstyped_config:"@type":type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInputheader_name:end-用户服务alue_match:exact:baron_match:action:name:skiptyped_config:"@type":type.googleapis.com/envoy.extensions.filters.common.matcher.action.v3.SkipFilter3.1.3CompositeapiVersion:networking.istio.io/v1alpha3kind:EnvoyFiltermetadata:name:extensionspec:workloadSelector:labels:app:productpageconfigPatches:-applyTo:HTTP_FILTERmatch:context:SIDECAR_OUTBOUNDlistener:name:0.0.0.0_9080filterChain:filter:name:"envoy.filters.network.http_connection_manager"subFilter:name:"特使。filters.http.router"patch:operation:INSERT_BEFOREvalue:name:compositetyped_config:"@type":type.googleapis.com/envoy.extensions.common.matching.v3.ExtensionWithMatcherextension_config:name:compositetyped_config:"@type":type。googleapis.com/envoy.extensions.filters.http.composite.v3.Compositematcher:matcher_tree:input:name:request-headerstyped_config:"@type":type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInputheader_name:end-userexact_match_map:map:"mark":action:name:composite-actiontyped_config:"@type":type.googleapis.com/envoy.extensions.filters.http.composite.v3.ExecuteFilterActiontyped_config:name:http-faulttyped_config:"@type":type.googleapis.com/envoy.extensions.filters.http.fault.v3.HTTPFaultdelay:fixed_delay:1spercentage:numerator:100denominator:HUNDRED"jason":action:name:composite-actiontyped_config:"@type":type.googleapis.com/envoy.extensions.filters.http.composite.v3.ExecuteFilterActiontyped_config:name:http-faulttyped_config:"@type":type.googleapis.com/envoy.extensions.filters.http.fault.v3.HTTPFaultabort:http_status:503percentage:numerator:100denominator:HUNDRED本文转载自微信公众号《k8s实战》。
