Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins/bk-traffic-label.lua): add new plugin #86

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

wklken
Copy link
Collaborator

@wklken wklken commented Nov 6, 2024

Description

参考 api7: traffic-labels 文档实现的插件

流量染色插件,支持 match 匹配规则 + actions 执行染色两部分配置

  • match 支持参数/header 头/cookie/nginx 内置变量/apisix内置变量等
  • actions 目前支持多组操作,且支持配置权重;当前只支持 set_headers 操作
# 一个match条件
      "traffic-label": {
        "rules": [
          {
            "match": [
              ["uri", "==", "/headers"]
            ],
            "actions": [
              {
                "set_headers": {
                  "X-Server-Id": 100
                }
              }
            ]
          }
        ]
      }

# 一个match多个条件,有逻辑关系
      "traffic-label": {
        "rules": [
          {
            "match": [
              "OR",
              ["arg_version", "==", "v1"],
              ["arg_env", "==", "dev"]
            ],
            "actions": [
              {
                "set_headers": {
                  "X-Server-Id": 100
                }
              }
            ]
          }
        ]
      }

# 一个match多个action,有权重的
# ❶ 30% of the requests should have the `X-Server-Id: 100` request header.
# ❷ 20% of the requests should have the `X-API-Version: v2` request header.
# ❸ 50% of the requests should not have any action performed on them.
      "traffic-label": {
        "rules": [
          {
            "match": [
              ["uri", "==", "/headers"]
            ],
            "actions": [
              {
                "set_headers": {
                  "X-Server-Id": 100
                },
                "weight": 3
              },
              {
                "set_headers": {
                  "X-API-Version": "v2"
                },
                "weight": 2
              },
              {
                "weight": 5
              }
            ]
          }
        ]
      }

# 多个match   => 顺序执行
      "traffic-label": {
        "rules": [
          {
            "match": [
              ["arg_version", "==", "v1"]
            ],
            "actions": [
              {
                "set_headers": {
                  "X-Server-Id": 100
                }
              }
            ]
          },
          {
            "match": [
              ["arg_version", "==", "v2"]
            ],
            "actions": [
              {
                "set_headers": {
                  "X-Server-Id": 200
                }
              }
            ]
          }
        ]
      }

actions 处理规则

  • 目前仅支持 set_headers 操作
  • 权重 weight 如果没有配置,默认值为 1
  • 会汇总所有 action 的 weight 后进行规范化
    • 只有一个 action, action.weight=100
    • 多个action,其weight 分别为 3/2/5, 那么规范化后 weight 为 30/20/50
    • 如果规范化后总值非 100,会将差值放最后一个操作的权重, 例如weight 为 1/1/1, 规范化后为 33/33/34
  • 如果只指定weight,不指定 set_headers,那么分布在这个weight 下的请求什么都不做

Checklist

  • 填写 PR 描述及相关 issue (write PR description and related issue)
  • 代码风格检查通过 (code style check passed)
  • PR 中包含单元测试 (include unit test)
  • 单元测试通过 (unit test passed)
  • 本地开发联调环境验证通过 (local development environment verification passed)

@wklken wklken marked this pull request as ready for review November 7, 2024 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant