Skip to content

Commit

Permalink
Merge pull request #94 from stefanprodan/fix-abtest-routing
Browse files Browse the repository at this point in the history
Fix A/B Testing HTTP URI match conditions
  • Loading branch information
stefanprodan authored Mar 11, 2019
2 parents 1cd0c49 + c720fee commit 3adfcc8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
4 changes: 2 additions & 2 deletions artifacts/ab-testing/canary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ spec:
regex: "^(?!.*Chrome)(?=.*\bSafari\b).*$"
- headers:
cookie:
regex: "^(.*?;)?(user=test)(;.*)?$"
regex: "^(.*?;)?(type=insider)(;.*)?$"
metrics:
- name: istio_requests_total
# minimum req success rate (non 5xx responses)
Expand All @@ -58,4 +58,4 @@ spec:
url: http://flagger-loadtester.test/
timeout: 5s
metadata:
cmd: "hey -z 1m -q 10 -c 2 http://podinfo.test:9898/"
cmd: "hey -z 1m -q 10 -c 2 -H 'Cookie: type=insider' http://podinfo.test:9898/"
2 changes: 1 addition & 1 deletion docs/gitbook/how-it-works.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ You can enable A/B testing by specifying the HTTP match conditions and the numbe
match:
- headers:
user-agent:
regex: "^(?!.*Chrome)(?=.*\bSafari\b).*$"
regex: "^(?!.*Chrome).*Safari.*"
- headers:
cookie:
regex: "^(.*?;)?(user=test)(;.*)?$"
Expand Down
4 changes: 2 additions & 2 deletions docs/gitbook/usage/ab-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ spec:
match:
- headers:
user-agent:
regex: "^(?!.*Chrome)(?=.*\bSafari\b).*$"
regex: "^(?!.*Chrome).*Safari.*"
- headers:
cookie:
regex: "^(.*?;)?(type=insider)(;.*)?$"
Expand All @@ -93,7 +93,7 @@ spec:
url: http://flagger-loadtester.test/
timeout: 5s
metadata:
cmd: "hey -z 1m -q 10 -c 2 http://podinfo.test:9898/"
cmd: "hey -z 1m -q 10 -c 2 -H 'Cookie: type=insider' http://podinfo.test:9898/"
```
The above configuration will run an analysis for ten minutes targeting Safari users and those that have an insider cookie.
Expand Down
17 changes: 15 additions & 2 deletions pkg/router/istio.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (ir *IstioRouter) Sync(canary *flaggerv1.Canary) error {
}

if len(canary.Spec.CanaryAnalysis.Match) > 0 {
canaryMatch := append(canary.Spec.Service.Match, canary.Spec.CanaryAnalysis.Match...)
canaryMatch := mergeMatchConditions(canary.Spec.CanaryAnalysis.Match, canary.Spec.Service.Match)
newSpec.Http = []istiov1alpha3.HTTPRoute{
{
Match: canaryMatch,
Expand Down Expand Up @@ -274,7 +274,7 @@ func (ir *IstioRouter) SetRoutes(
// fix routing (A/B testing)
if len(canary.Spec.CanaryAnalysis.Match) > 0 {
// merge the common routes with the canary ones
canaryMatch := append(canary.Spec.Service.Match, canary.Spec.CanaryAnalysis.Match...)
canaryMatch := mergeMatchConditions(canary.Spec.CanaryAnalysis.Match, canary.Spec.Service.Match)
vsCopy.Spec.Http = []istiov1alpha3.HTTPRoute{
{
Match: canaryMatch,
Expand Down Expand Up @@ -345,3 +345,16 @@ func addHeaders(canary *flaggerv1.Canary) (headers map[string]string) {

return
}

// mergeMatchConditions appends the URI match rules to canary conditions
func mergeMatchConditions(canary, defaults []istiov1alpha3.HTTPMatchRequest) []istiov1alpha3.HTTPMatchRequest {
for i := range canary {
for _, d := range defaults {
if d.Uri != nil {
canary[i].Uri = d.Uri
}
}
}

return canary
}

0 comments on commit 3adfcc8

Please sign in to comment.