Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ test-coverage: fmt test-codegen
go tool cover -html=cover.out
rm cover.out

test-router:
@echo "Running router tests"
@go test -v ./pkg/router/...

crd:
cat artifacts/flagger/crd.yaml > charts/flagger/crds/crd.yaml
cat artifacts/flagger/crd.yaml > kustomize/base/flagger/crd.yaml
Expand Down
49 changes: 49 additions & 0 deletions artifacts/examples/istio-attribute-routing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
apiVersion: flagger.app/v1beta1
kind: Canary
metadata:
name: podinfo-attribute-routing
namespace: test
spec:
provider: istio
targetRef:
apiVersion: apps/v1
kind: Deployment
name: podinfo
progressDeadlineSeconds: 60
service:
port: 80
targetPort: 9898
attributeRangeRouting:
enabled: true
headerName: "X-User-ID"
strategy: consistent-hash
initialPercentage: 10
stepPercentage: 10
maxPercentage: 50
hashFunction: fnv
slotCount: 1000
analysis:
interval: 15s
threshold: 15
maxWeight: 50
stepWeight: 10
metrics:
- name: request-success-rate
threshold: 99
interval: 1m
- name: request-duration
threshold: 500
interval: 1m
webhooks:
- name: "gate"
type: confirm-rollout
url: http://flagger-loadtester.test/gate/approve
- name: "confirm-traffic-increase"
type: confirm-traffic-increase
url: http://flagger-loadtester.test/gate/approve
- name: load-test
url: http://flagger-loadtester.test/
timeout: 5s
metadata:
type: cmd
cmd: "hey -z 2m -q 5 -c 2 http://podinfo.test:9898"
48 changes: 48 additions & 0 deletions artifacts/examples/istio-ip-routing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
apiVersion: flagger.app/v1beta1
kind: Canary
metadata:
name: podinfo-ip-routing
namespace: test
spec:
provider: istio
targetRef:
apiVersion: apps/v1
kind: Deployment
name: podinfo
progressDeadlineSeconds: 60
service:
port: 80
targetPort: 9898
ipRangeRouting:
enabled: true
strategy: consistent-hash
initialPercentage: 10
stepPercentage: 10
maxPercentage: 50
hashFunction: fnv
slotCount: 1000
analysis:
interval: 15s
threshold: 15
maxWeight: 50
stepWeight: 10
metrics:
- name: request-success-rate
threshold: 99
interval: 1m
- name: request-duration
threshold: 500
interval: 1m
webhooks:
- name: "gate"
type: confirm-rollout
url: http://flagger-loadtester.test/gate/approve
- name: "confirm-traffic-increase"
type: confirm-traffic-increase
url: http://flagger-loadtester.test/gate/approve
- name: load-test
url: http://flagger-loadtester.test/
timeout: 5s
metadata:
type: cmd
cmd: "hey -z 2m -q 5 -c 2 http://podinfo.test:9898"
86 changes: 86 additions & 0 deletions artifacts/flagger/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,92 @@ spec:
resume:
description: Indicates if the canary should resume automated traffic shifting
type: boolean
ipRangeRouting:
description: IP-based canary traffic routing configuration
type: object
properties:
enabled:
description: Enable IP-based routing
type: boolean
strategy:
description: IP routing strategy (consistent-hash or range-based)
type: string
enum:
- consistent-hash
- range-based
initialPercentage:
description: Initial percentage of IP ranges to route to canary
type: integer
minimum: 0
maximum: 100
stepPercentage:
description: Percentage increment for each traffic increase step
type: integer
minimum: 1
maximum: 100
maxPercentage:
description: Maximum percentage of IP ranges to route to canary
type: integer
minimum: 0
maximum: 100
hashFunction:
description: Hash function for consistent hashing (fnv, md5, sha256)
type: string
enum:
- fnv
- md5
- sha256
slotCount:
description: Total number of hash slots for consistent hashing
type: integer
minimum: 100
maximum: 10000
attributeRangeRouting:
description: Attribute-based canary traffic routing configuration
type: object
properties:
enabled:
description: Enable attribute-based routing
type: boolean
headerName:
description: Header name to use for routing decisions
type: string
parameterName:
description: Query parameter name to use for routing decisions
type: string
strategy:
description: Routing strategy (consistent-hash or range-based)
type: string
enum:
- consistent-hash
- range-based
initialPercentage:
description: Initial percentage of attribute values to route to canary
type: integer
minimum: 0
maximum: 100
stepPercentage:
description: Percentage increment for each traffic increase step
type: integer
minimum: 1
maximum: 100
maxPercentage:
description: Maximum percentage of attribute values to route to canary
type: integer
minimum: 0
maximum: 100
hashFunction:
description: Hash function for consistent hashing (fnv, md5, sha256)
type: string
enum:
- fnv
- md5
- sha256
slotCount:
description: Total number of hash slots for consistent hashing
type: integer
minimum: 100
maximum: 10000
analysis:
description: Canary analysis for this canary
type: object
Expand Down
86 changes: 86 additions & 0 deletions charts/flagger/crds/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,92 @@ spec:
resume:
description: Indicates if the canary should resume automated traffic shifting
type: boolean
ipRangeRouting:
description: IP-based canary traffic routing configuration
type: object
properties:
enabled:
description: Enable IP-based routing
type: boolean
strategy:
description: IP routing strategy (consistent-hash or range-based)
type: string
enum:
- consistent-hash
- range-based
initialPercentage:
description: Initial percentage of IP ranges to route to canary
type: integer
minimum: 0
maximum: 100
stepPercentage:
description: Percentage increment for each traffic increase step
type: integer
minimum: 1
maximum: 100
maxPercentage:
description: Maximum percentage of IP ranges to route to canary
type: integer
minimum: 0
maximum: 100
hashFunction:
description: Hash function for consistent hashing (fnv, md5, sha256)
type: string
enum:
- fnv
- md5
- sha256
slotCount:
description: Total number of hash slots for consistent hashing
type: integer
minimum: 100
maximum: 10000
attributeRangeRouting:
description: Attribute-based canary traffic routing configuration
type: object
properties:
enabled:
description: Enable attribute-based routing
type: boolean
headerName:
description: Header name to use for routing decisions
type: string
parameterName:
description: Query parameter name to use for routing decisions
type: string
strategy:
description: Routing strategy (consistent-hash or range-based)
type: string
enum:
- consistent-hash
- range-based
initialPercentage:
description: Initial percentage of attribute values to route to canary
type: integer
minimum: 0
maximum: 100
stepPercentage:
description: Percentage increment for each traffic increase step
type: integer
minimum: 1
maximum: 100
maxPercentage:
description: Maximum percentage of attribute values to route to canary
type: integer
minimum: 0
maximum: 100
hashFunction:
description: Hash function for consistent hashing (fnv, md5, sha256)
type: string
enum:
- fnv
- md5
- sha256
slotCount:
description: Total number of hash slots for consistent hashing
type: integer
minimum: 100
maximum: 10000
analysis:
description: Canary analysis for this canary
type: object
Expand Down
83 changes: 83 additions & 0 deletions docs/gitbook/usage/attribute-range-routing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# 基于请求属性的流量路由

## 概述

基于请求属性的流量路由功能允许您根据请求的特定属性(如请求头或查询参数)将流量路由到金丝雀部署。这在需要对特定用户群体或请求类型进行金丝雀发布时非常有用。

## 使用场景

1. **用户ID分片**: 根据用户ID将特定比例的用户流量路由到金丝雀版本
2. **客户端版本控制**: 根据客户端版本将特定客户端路由到金丝雀版本
3. **地理位置路由**: 根据地理位置信息将特定区域的用户路由到金丝雀版本

## 配置选项

基于请求属性的流量路由通过 `attributeRangeRouting` 字段进行配置:

```yaml
attributeRangeRouting:
enabled: true
headerName: "X-User-ID" # 用于路由决策的请求头名称
parameterName: "userId" # 用于路由决策的查询参数名称(与headerName互斥)
strategy: "consistent-hash" # 路由策略(consistent-hash 或 range-based)
initialPercentage: 10 # 初始路由到金丝雀的属性值百分比
stepPercentage: 10 # 每次迭代增加的百分比
maxPercentage: 50 # 最大路由到金丝雀的属性值百分比
hashFunction: "fnv" # 一致性哈希函数(fnv、md5、sha256)
slotCount: 1000 # 一致性哈希的槽位数量
```

## 路由策略

### 一致性哈希 (consistent-hash)

一致性哈希策略使用哈希函数将请求属性值映射到固定数量的槽位中。这种方法确保相同的属性值始终路由到相同的目标(主版本或金丝雀版本),并且在调整流量比例时最小化重新路由的请求量。

### 范围基础 (range-based)

范围基础策略将属性值转换为数字,并根据配置的百分比范围决定路由目标。如果转换后的数字在指定范围内,则请求路由到金丝雀版本。

## 示例

以下示例展示了如何根据 `X-User-ID` 请求头的值进行流量路由:

```yaml
apiVersion: flagger.app/v1beta1
kind: Canary
metadata:
name: podinfo
namespace: test
spec:
provider: istio
targetRef:
apiVersion: apps/v1
kind: Deployment
name: podinfo
service:
port: 9898
attributeRangeRouting:
enabled: true
headerName: "X-User-ID"
strategy: consistent-hash
initialPercentage: 10
stepPercentage: 10
maxPercentage: 50
hashFunction: fnv
slotCount: 1000
analysis:
interval: 15s
threshold: 15
maxWeight: 50
stepWeight: 10
```

在此示例中:
1. 初始时,具有特定 `X-User-ID` 值的 10% 用户将被路由到金丝雀版本
2. 每次迭代时,额外的 10% 用户将被路由到金丝雀版本
3. 最多 50% 的用户将被路由到金丝雀版本

## 注意事项

1. `headerName` 和 `parameterName` 是互斥的,只能指定其中一个
2. 当使用一致性哈希策略时,建议使用较大的 `slotCount` 值以获得更均匀的分布
3. 如果同时配置了基于权重的路由和基于属性的路由,基于属性的路由规则将优先匹配
Loading