Pages - Menu

2021年9月8日 星期三

[istio] istio的virtualservice match比對

前言

最近因為新的需求,再度跟istio的virtualservice槓上了。
這次要做的是針對domain去解析網址,host不再自定。

這個需求是因為,一個服務會綁定許多的垃圾網址,

但用了istio就必須要由我這邊設定每個domain,才能進去到正確的服務。

所以要使用正則,如果有符合規則的domain就自動進到服務,而不用我這邊做設定。



正文

踩到的坑很特別,根據官方文件上寫的是

For example, the following restricts the rule to match only requests where the URL path starts with /ratings/v2/ and the request contains a custom end-user header with value jason

文章的範例如下,官方文件是說明當url path 符合 /ratings/v2 而且 header裡面的end-user的值為 jason 才會轉到正確的路由。

  apiVersion: networking.istio.io/v1beta1
  kind: VirtualService
  metadata:
  name: ratings-route
  spec:
  hosts:
  - ratings.prod.svc.cluster.local
  http:
  - match:
    - headers:
        end-user:
          exact: jason
      uri:
        prefix: "/ratings/v2/"
      ignoreUriCase: true
    route:
    - destination:
        host: ratings.prod.svc.cluster.local

ref.HTTPMatchRequest

再來就是我踩坑的地方,因為我不管怎麼測試,測試出來的都是 『或』,
也就是說 當流量進入時, 只要 url path 是 /api 『或』 header 的Host是 符合 qa2.777.+.com的正則,才會導去正確的路由。
跟官方文件的講法落差有點大,後來測試了老半天才發現,關鍵在 『 - 』。

下面的程式,條件是 『或』,兩個條件其中一個過都會導到正確的路由

match:
  - headers:
      Host:
        regex: qa2\.777.+\.com
  - uri:
      prefix: /api

下面的程式,條件式 『而且』 ,兩個條件都要符合才會導到正確的路由

match:
  - headers:
      Host:
        regex: qa2.?\.777.+\.com
    uri:
      prefix: /api

本來還有在探討另一種方法,直接在hosts上面做判斷(fig.1),
但是測試出來只能針對 sub domain,如果把萬用字元放在後面,連存檔都會出錯握。



(fig.1)

hosts:
  - "*.777.com"

ref.
Support wildcard/regexp in VirtualService host
config.route.v3.VirtualHost

沒有留言:

張貼留言