不懂Envoyfilter 也敢说精通Istio-ExtensionWithMatcher- 按条件执行过滤器

开发 前端
条件过滤就是HttpConnectionManager的过滤器可以有条件的执行,比如请求头或响应头是某个值时执行这个过滤器,如果匹配某个值就跳过。他可以和Composite过滤器结合,当条件满足时执行某个过滤器。

 [[433326]]

1.什么是按条件使用过滤器

条件过滤就是HttpConnectionManager的过滤器可以有条件的执行,比如请求头或响应头是某个值时执行这个过滤器,如果匹配某个值就跳过。他可以和Composite过滤器结合,当条件满足时执行某个过滤器。按条件的过滤器是通过ExtensionWithMatcher实现的,其中extension配置要执行的过滤器,matcher匹配条件,条件匹配后执行某个动作。

2.相关配置

2.1ExtensionWithMatcher

  1.   "matcher""{...}",匹配 
  2.   "xds_matcher""{...}",匹配条件和动作,还没实现 
  3.   "extension_config""{...}"过滤器扩展配置 

xds_matcher:

  1.   "matcher_list""{...}",线性matcher 
  2.   "matcher_tree""{...}",树性matcher 
  3.   "on_no_match""{...}"没有match情况处理 

matcher_list:

  1.   "matchers": []匹配 

matchers:

  1.   "predicate""{...}",判断是否match 
  2.   "on_match""{...}"如果match做什么 

predicate:

  1.   "single_predicate""{...}",单条match 
  2.   "or_matcher""{...}",或match 
  3.   "and_matcher""{...}",与match 
  4.   "not_matcher""{...}"非match 

single_predicate:

  1.   "input""{...}",输入 
  2.   "value_match""{...}",值匹配 
  3.   "custom_match""{...}"自定义匹配 

input:

  • envoy.matching.common_inputs.environment_variable
  1.   "exact""...",精确匹配 
  2.   "prefix""...",前缀匹配 
  3.   "suffix""...",后缀匹配 
  4.   "safe_regex""{...}",正则匹配 
  5.   "contains""...",包含匹配 
  6.   "ignore_case""..."忽略大小写 

custom_match:

  • envoy.matching.input_matchers.consistent_hashing
  • envoy.matching.input_matchers.ip

matcher_tree:

  1.   "input""{...}",输入 
  2.   "exact_match_map""{...}",精确或前缀匹配 
  3.   "prefix_match_map""{...}",前缀匹配 
  4.   "custom_match""{...}"自定义前缀或精确匹配 

exact_match_map,prefix_match_map

  1.   "map""{...}" 

map:

  1.   "matcher""{...}",匹配 
  2.   "action""{...}"动作 

matcher:

  1.   "matcher_list""{...}",线性matcher 
  2.   "matcher_tree""{...}",树性matcher 
  3.   "on_no_match""{...}"没有match情况处理 

input:

type.matcher.v3.HttpRequestHeaderMatchInput

type.matcher.v3.HttpRequestTrailerMatchInput

type.matcher.v3.HttpResponseHeaderMatchInput

type.matcher.v3.HttpResponseTrailerMatchInput

config.common.matcher.v3.MatchPredicate

  1.   "or_match""{...}"
  2.   "and_match""{...}"
  3.   "not_match""{...}"
  4.   "any_match""..."
  5.   "http_request_headers_match""{...}"
  6.   "http_request_trailers_match""{...}"
  7.   "http_response_headers_match""{...}"
  8.   "http_response_trailers_match""{...}"
  9.   "http_request_generic_body_match""{...}"
  10.   "http_response_generic_body_match""{...}" 

3.实战

默认istio的envoy是没有启用这个功能的,通过注解启用。

  1. apiVersion: v1 
  2. kind: ConfigMap 
  3. metadata: 
  4.   name: istio-custom-bootstrap-config 
  5. data: 
  6.   custom_bootstrap.json: | 
  7.     { 
  8.       "layered_runtime": { 
  9.         "layers": [ 
  10.           { 
  11.             "name""static-layer"
  12.             "static_layer": { 
  13.               "envoy": { 
  14.                 "reloadable_features": { 
  15.                   "experimental_matching_api"true 
  16.                 } 
  17.               } 
  18.             } 
  19.           } 
  20.         ] 
  21.       } 
  22.     } 

productpage-deploy.yaml

kubectl apply -f productpage-deploy.yaml -n istio

  1. apiVersion: apps/v1 
  2. kind: Deployment 
  3. metadata: 
  4.   name: productpage-v1 
  5.   labels: 
  6.     app: productpage 
  7.     version: v1 
  8. spec: 
  9.   replicas: 1 
  10.   selector: 
  11.     matchLabels: 
  12.       app: productpage 
  13.       version: v1 
  14.   template: 
  15.     metadata: 
  16.       labels: 
  17.         app: productpage 
  18.         version: v1 
  19.       annotations: 
  20.         sidecar.istio.io/bootstrapOverride: "istio-custom-bootstrap-config" 
  21.     spec: 
  22.       serviceAccountName: bookinfo-productpage 
  23.       containers: 
  24.       - name: productpage 
  25.         image: docker.io/istio/examples-bookinfo-productpage-v1:1.16.2 
  26.         imagePullPolicy: IfNotPresent 
  27.         ports: 
  28.         - containerPort: 9080 
  29.         volumeMounts: 
  30.         - name: tmp 
  31.           mountPath: /tmp 
  32.         securityContext: 
  33.           runAsUser: 1000 
  34.       volumes: 
  35.       - name: tmp 
  36.         emptyDir: {} 
  37. --- 

3.1ExtensionWithMatcher

3.1.1matcher_list

  1. apiVersion: networking.istio.io/v1alpha3 
  2. kind: EnvoyFilter 
  3. metadata: 
  4.   name: extension 
  5. spec: 
  6.   workloadSelector: 
  7.     labels: 
  8.       app: productpage 
  9.   configPatches: 
  10.   - applyTo: HTTP_FILTER 
  11.     match: 
  12.       context: SIDECAR_OUTBOUND 
  13.       listener: 
  14.         name: 0.0.0.0_9080 
  15.         filterChain: 
  16.           filter: 
  17.             name"envoy.filters.network.http_connection_manager" 
  18.             subFilter: 
  19.               name"envoy.filters.http.router" 
  20.     patch: 
  21.       operation: INSERT_BEFORE 
  22.       value:  
  23.        name: test 
  24.        typed_config: 
  25.               "@type": type.googleapis.com/envoy.extensions.common.matching.v3.ExtensionWithMatcher 
  26.               extension_config: 
  27.                 name: envoy.filters.http.fault 
  28.                 typed_config: 
  29.                   "@type": type.googleapis.com/envoy.extensions.filters.http.fault.v3.HTTPFault 
  30.                   abort: 
  31.                     http_status: 503 
  32.                     percentage: 
  33.                       numerator: 100 
  34.                       denominator: HUNDRED 
  35.                   delay: 
  36.                     fixed_delay: 3s 
  37.                     percentage: 
  38.                       numerator: 100 
  39.                       denominator: HUNDRED 
  40.               matcher: 
  41.                 matcher_list: 
  42.                   matchers: 
  43.                   - predicate: 
  44.                       or_matcher: 
  45.                         predicate: 
  46.                         - single_predicate: 
  47.                             input: 
  48.                               name: request-headers 
  49.                               typed_config: 
  50.                                 "@type": type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInput 
  51.                                 header_name: end-user 
  52.                             value_match: 
  53.                               exact: jason 
  54.                         - single_predicate: 
  55.                             input: 
  56.                               name: request-headers 
  57.                               typed_config: 
  58.                                 "@type": type.googleapis.com/envoy.type.matcher.v3.HttpResponseHeaderMatchInput 
  59.                                 header_name: test 
  60.                             value_match: 
  61.                               exact: bar 
  62.                     on_match: 
  63.                       action
  64.                         name: skip 
  65.                         typed_config: 
  66.                           "@type": type.googleapis.com/envoy.extensions.filters.common.matcher.action.v3.SkipFilter 

3.1.2matcher_tree

ef-ExtensionWithMatcher-matcher_tree.yaml

kubectl apply -f ef-ExtensionWithMatcher-matcher_tree.yaml -n istio

  1. apiVersion: networking.istio.io/v1alpha3 
  2. kind: EnvoyFilter 
  3. metadata: 
  4.   name: extension 
  5. spec: 
  6.   workloadSelector: 
  7.     labels: 
  8.       app: productpage 
  9.   configPatches: 
  10.   - applyTo: HTTP_FILTER 
  11.     match: 
  12.       context: SIDECAR_OUTBOUND 
  13.       listener: 
  14.         name: 0.0.0.0_9080 
  15.         filterChain: 
  16.           filter: 
  17.             name"envoy.filters.network.http_connection_manager" 
  18.             subFilter: 
  19.               name"envoy.filters.http.router" 
  20.     patch: 
  21.       operation: INSERT_BEFORE 
  22.       value:  
  23.        name: test 
  24.        typed_config: 
  25.               "@type": type.googleapis.com/envoy.extensions.common.matching.v3.ExtensionWithMatcher 
  26.               extension_config: 
  27.                 name: envoy.filters.http.fault 
  28.                 typed_config: 
  29.                   "@type": type.googleapis.com/envoy.extensions.filters.http.fault.v3.HTTPFault 
  30.                   abort: 
  31.                     http_status: 503 
  32.                     percentage: 
  33.                       numerator: 100 
  34.                       denominator: HUNDRED 
  35.                   delay: 
  36.                     fixed_delay: 3s 
  37.                     percentage: 
  38.                       numerator: 100 
  39.                       denominator: HUNDRED 
  40.               matcher: 
  41.                 matcher_tree: 
  42.                   input: 
  43.                     name: request-headers 
  44.                     typed_config: 
  45.                       "@type": type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInput 
  46.                       header_name: end-user 
  47.                   exact_match_map: 
  48.                     map: 
  49.                       jason:  
  50.                         matcher: 
  51.                           matcher_list: 
  52.                             matchers: 
  53.                             - predicate: 
  54.                                 or_matcher: 
  55.                                   predicate: 
  56.                                   - single_predicate: 
  57.                                       input: 
  58.                                         name: request-headers 
  59.                                         typed_config: 
  60.                                           "@type": type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInput 
  61.                                           header_name: end-user 
  62.                                       value_match: 
  63.                                         exact: jason 
  64.                                   - single_predicate: 
  65.                                       input: 
  66.                                         name: request-headers 
  67.                                         typed_config: 
  68.                                           "@type": type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInput 
  69.                                           header_name: end-user 
  70.                                       value_match: 
  71.                                         exact: bar 
  72.                               on_match: 
  73.                                 action
  74.                                   name: skip 
  75.                                   typed_config: 
  76.                                     "@type": type.googleapis.com/envoy.extensions.filters.common.matcher.action.v3.SkipFilter 

3.1.3Composite

  1. apiVersion: networking.istio.io/v1alpha3 
  2. kind: EnvoyFilter 
  3. metadata: 
  4.   name: extension 
  5. spec: 
  6.   workloadSelector: 
  7.     labels: 
  8.       app: productpage 
  9.   configPatches: 
  10.   - applyTo: HTTP_FILTER 
  11.     match: 
  12.       context: SIDECAR_OUTBOUND 
  13.       listener: 
  14.         name: 0.0.0.0_9080 
  15.         filterChain: 
  16.           filter: 
  17.             name"envoy.filters.network.http_connection_manager" 
  18.             subFilter: 
  19.               name"envoy.filters.http.router" 
  20.     patch: 
  21.       operation: INSERT_BEFORE 
  22.       value:  
  23.             name: composite 
  24.             typed_config: 
  25.               "@type": type.googleapis.com/envoy.extensions.common.matching.v3.ExtensionWithMatcher 
  26.               extension_config: 
  27.                 name: composite 
  28.                 typed_config: 
  29.                   "@type": type.googleapis.com/envoy.extensions.filters.http.composite.v3.Composite 
  30.               matcher: 
  31.                 matcher_tree: 
  32.                   input: 
  33.                     name: request-headers 
  34.                     typed_config: 
  35.                       "@type": type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInput 
  36.                       header_name: end-user 
  37.                   exact_match_map: 
  38.                     map: 
  39.                       "mark":   
  40.                         action
  41.                           name: composite-action 
  42.                           typed_config: 
  43.                             "@type": type.googleapis.com/envoy.extensions.filters.http.composite.v3.ExecuteFilterAction 
  44.                             typed_config: 
  45.                               name: http-fault 
  46.                               typed_config: 
  47.                                 "@type": type.googleapis.com/envoy.extensions.filters.http.fault.v3.HTTPFault 
  48.                                 delay: 
  49.                                   fixed_delay: 1s 
  50.                                   percentage: 
  51.                                     numerator: 100 
  52.                                     denominator: HUNDRED 
  53.                       "jason":   
  54.                         action
  55.                           name: composite-action 
  56.                           typed_config: 
  57.                             "@type": type.googleapis.com/envoy.extensions.filters.http.composite.v3.ExecuteFilterAction 
  58.                             typed_config: 
  59.                               name: http-fault 
  60.                               typed_config: 
  61.                                 "@type": type.googleapis.com/envoy.extensions.filters.http.fault.v3.HTTPFault 
  62.                                 abort: 
  63.                                   http_status: 503 
  64.                                   percentage: 
  65.                                     numerator: 100 
  66.                                     denominator: HUNDRED 

本文转载自微信公众号「k8s实战」,可以通过以下二维码关注。转载本文请联系k8s实战公众号。

 

 

责任编辑:武晓燕 来源: k8s实战
相关推荐

2021-07-05 15:22:03

Servlet过滤器客户端

2024-01-05 09:04:35

隆过滤器数据结构哈希函数

2009-07-08 15:30:56

Servlet过滤器

2009-09-29 13:55:23

Hibernate设置

2011-06-29 16:14:59

Qt 事件 过滤器

2009-07-08 16:07:04

Servlet过滤器配

2009-07-14 09:09:08

Swing模型过滤器

2009-06-18 10:13:00

Hibernate过滤

2009-07-08 17:33:37

Servlet过滤器

2009-09-25 15:19:44

Hibernate过滤

2017-07-18 14:10:31

大数据Apache Flum过滤器

2023-04-14 09:01:25

2009-07-06 13:02:49

Servlet过滤器

2024-03-15 11:21:22

布隆过滤器数据库数据

2023-01-26 01:41:27

核心全局过滤器

2016-12-07 09:56:13

JavaFilter过滤器

2017-04-12 14:43:01

Spring ClouZuul过滤器

2010-03-01 14:45:07

Linux文件重定向

2009-07-03 18:26:11

Servlet过滤器

2010-12-27 13:14:15

Openbsd PFOpenBSD数据包过滤
点赞
收藏

51CTO技术栈公众号