Skip to content

Commit

Permalink
Merge pull request #5114 from whalecold/match
Browse files Browse the repository at this point in the history
Feat: add header-pattern annotation.
  • Loading branch information
k8s-ci-robot authored Feb 25, 2020
2 parents 6cd2235 + 0b33650 commit 35264d6
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 18 deletions.
5 changes: 4 additions & 1 deletion docs/user-guide/nginx-configuration/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ You can add these Kubernetes annotations to specific Ingress objects to customiz
|[nginx.ingress.kubernetes.io/backend-protocol](#backend-protocol)|string|HTTP,HTTPS,GRPC,GRPCS,AJP|
|[nginx.ingress.kubernetes.io/canary](#canary)|"true" or "false"|
|[nginx.ingress.kubernetes.io/canary-by-header](#canary)|string|
|[nginx.ingress.kubernetes.io/canary-by-header-value](#canary)|string
|[nginx.ingress.kubernetes.io/canary-by-header-value](#canary)|string|
|[nginx.ingress.kubernetes.io/canary-by-header-pattern](#canary)|string|
|[nginx.ingress.kubernetes.io/canary-by-cookie](#canary)|string|
|[nginx.ingress.kubernetes.io/canary-weight](#canary)|number|
|[nginx.ingress.kubernetes.io/client-body-buffer-size](#client-body-buffer-size)|string|
Expand Down Expand Up @@ -129,6 +130,8 @@ In some cases, you may want to "canary" a new set of changes by sending a small

* `nginx.ingress.kubernetes.io/canary-by-header-value`: The header value to match for notifying the Ingress to route the request to the service specified in the Canary Ingress. When the request header is set to this value, it will be routed to the canary. For any other header value, the header will be ignored and the request compared against the other canary rules by precedence. This annotation has to be used together with . The annotation is an extension of the `nginx.ingress.kubernetes.io/canary-by-header` to allow customizing the header value instead of using hardcoded values. It doesn't have any effect if the `nginx.ingress.kubernetes.io/canary-by-header` annotation is not defined.

* `nginx.ingress.kubernetes.io/canary-by-header-pattern`: This works the same way as `canary-by-header-value` except it does PCRE Regex matching. Note that when `canary-by-header-value` is set this annotation will be ignored. When the given Regex causes error during request processing, the request will be considered as not matching.

* `nginx.ingress.kubernetes.io/canary-by-cookie`: The cookie to use for notifying the Ingress to route the request to the service specified in the Canary Ingress. When the cookie value is set to `always`, it will be routed to the canary. When the cookie is set to `never`, it will never be routed to the canary. For any other value, the cookie will be ignored and the request compared against the other canary rules by precedence.

* `nginx.ingress.kubernetes.io/canary-weight`: The integer based (0 - 100) percent of random requests that should be routed to the service specified in the canary Ingress. A weight of 0 implies that no requests will be sent to the service in the Canary ingress by this canary rule. A weight of 100 means implies all requests will be sent to the alternative service specified in the Ingress.
Expand Down
19 changes: 13 additions & 6 deletions internal/ingress/annotations/canary/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ type canary struct {

// Config returns the configuration rules for setting up the Canary
type Config struct {
Enabled bool
Weight int
Header string
HeaderValue string
Cookie string
Enabled bool
Weight int
Header string
HeaderValue string
HeaderPattern string
Cookie string
}

// NewParser parses the ingress for canary related annotations
Expand Down Expand Up @@ -68,12 +69,18 @@ func (c canary) Parse(ing *networking.Ingress) (interface{}, error) {
config.HeaderValue = ""
}

config.HeaderPattern, err = parser.GetStringAnnotation("canary-by-header-pattern", ing)
if err != nil {
config.HeaderPattern = ""
}

config.Cookie, err = parser.GetStringAnnotation("canary-by-cookie", ing)
if err != nil {
config.Cookie = ""
}

if !config.Enabled && (config.Weight > 0 || len(config.Header) > 0 || len(config.HeaderValue) > 0 || len(config.Cookie) > 0) {
if !config.Enabled && (config.Weight > 0 || len(config.Header) > 0 || len(config.HeaderValue) > 0 || len(config.Cookie) > 0 ||
len(config.HeaderPattern) > 0) {
return nil, errors.NewInvalidAnnotationConfiguration("canary", "configured but not enabled")
}

Expand Down
18 changes: 10 additions & 8 deletions internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,10 +738,11 @@ func (n *NGINXController) createUpstreams(data []*ingress.Ingress, du *ingress.B
if anns.Canary.Enabled {
upstreams[defBackend].NoServer = true
upstreams[defBackend].TrafficShapingPolicy = ingress.TrafficShapingPolicy{
Weight: anns.Canary.Weight,
Header: anns.Canary.Header,
HeaderValue: anns.Canary.HeaderValue,
Cookie: anns.Canary.Cookie,
Weight: anns.Canary.Weight,
Header: anns.Canary.Header,
HeaderValue: anns.Canary.HeaderValue,
HeaderPattern: anns.Canary.HeaderPattern,
Cookie: anns.Canary.Cookie,
}
}

Expand Down Expand Up @@ -801,10 +802,11 @@ func (n *NGINXController) createUpstreams(data []*ingress.Ingress, du *ingress.B
if anns.Canary.Enabled {
upstreams[name].NoServer = true
upstreams[name].TrafficShapingPolicy = ingress.TrafficShapingPolicy{
Weight: anns.Canary.Weight,
Header: anns.Canary.Header,
HeaderValue: anns.Canary.HeaderValue,
Cookie: anns.Canary.Cookie,
Weight: anns.Canary.Weight,
Header: anns.Canary.Header,
HeaderValue: anns.Canary.HeaderValue,
HeaderPattern: anns.Canary.HeaderPattern,
Cookie: anns.Canary.Cookie,
}
}

Expand Down
2 changes: 2 additions & 0 deletions internal/ingress/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ type TrafficShapingPolicy struct {
Header string `json:"header"`
// HeaderValue on which to redirect requests to this backend
HeaderValue string `json:"headerValue"`
// HeaderPattern the header value match pattern, support exact, regex.
HeaderPattern string `json:"headerPattern"`
// Cookie on which to redirect requests to this backend
Cookie string `json:"cookie"`
}
Expand Down
3 changes: 3 additions & 0 deletions internal/ingress/types_equals.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ func (tsp1 TrafficShapingPolicy) Equal(tsp2 TrafficShapingPolicy) bool {
if tsp1.HeaderValue != tsp2.HeaderValue {
return false
}
if tsp1.HeaderPattern != tsp2.HeaderPattern {
return false
}
if tsp1.Cookie != tsp2.Cookie {
return false
}
Expand Down
14 changes: 11 additions & 3 deletions rootfs/etc/nginx/lua/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,22 @@ local function route_to_alternative_balancer(balancer)
local header = ngx.var["http_" .. target_header]
if header then
if traffic_shaping_policy.headerValue
and #traffic_shaping_policy.headerValue > 0 then
and #traffic_shaping_policy.headerValue > 0 then
if traffic_shaping_policy.headerValue == header then
return true
end

elseif traffic_shaping_policy.headerPattern
and #traffic_shaping_policy.headerPattern > 0 then
local m, err = ngx.re.match(header, traffic_shaping_policy.headerPattern)
if m then
return true
elseif err then
ngx.log(ngx.ERR, "error when matching canary-by-header-pattern: '",
traffic_shaping_policy.headerPattern, "', error: ", err)
return false
end
elseif header == "always" then
return true

elseif header == "never" then
return false
end
Expand Down
114 changes: 114 additions & 0 deletions test/e2e/annotations/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,120 @@ var _ = framework.DescribeAnnotation("canary-*", func() {
})
})

ginkgo.Context("when canaried by header with value and pattern", func() {
ginkgo.It("should route requests to the correct upstream", func() {
host := "foo"
annotations := map[string]string{}

ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, annotations)
f.EnsureIngress(ing)

f.WaitForNginxServer(host,
func(server string) bool {
return strings.Contains(server, "server_name foo")
})

canaryAnnotations := map[string]string{
"nginx.ingress.kubernetes.io/canary": "true",
"nginx.ingress.kubernetes.io/canary-by-header": "CanaryByHeader",
"nginx.ingress.kubernetes.io/canary-by-header-pattern": "^Do[A-Z][a-z]+y$",
}

canaryIngName := fmt.Sprintf("%v-canary", host)

canaryIng := framework.NewSingleIngress(canaryIngName, "/", host, f.Namespace, canaryService,
80, canaryAnnotations)
f.EnsureIngress(canaryIng)

ginkgo.By("routing requests to the canary upstream when header pattern is matched")
f.HTTPTestClient().
GET("/").
WithHeader("Host", host).
WithHeader("CanaryByHeader", "DoCanary").
Expect().
Status(http.StatusOK).
Body().Contains(canaryService)

ginkgo.By("routing requests to the mainline upstream when header failed to match header value")
f.HTTPTestClient().
GET("/").
WithHeader("Host", host).
WithHeader("CanaryByHeader", "Docanary").
Expect().
Status(http.StatusOK).
Body().Contains(framework.EchoService).NotContains(canaryService)
})
ginkgo.It("should route requests to the correct upstream", func() {
host := "foo"
annotations := map[string]string{}

ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, annotations)
f.EnsureIngress(ing)

f.WaitForNginxServer(host,
func(server string) bool {
return strings.Contains(server, "server_name foo")
})

canaryAnnotations := map[string]string{
"nginx.ingress.kubernetes.io/canary": "true",
"nginx.ingress.kubernetes.io/canary-by-header": "CanaryByHeader",
"nginx.ingress.kubernetes.io/canary-by-header-value": "DoCanary",
"nginx.ingress.kubernetes.io/canary-by-header-pattern": "^Do[A-Z][a-z]+y$",
}

canaryIngName := fmt.Sprintf("%v-canary", host)

canaryIng := framework.NewSingleIngress(canaryIngName, "/", host, f.Namespace, canaryService,
80, canaryAnnotations)
f.EnsureIngress(canaryIng)

ginkgo.By("routing requests to the mainline upstream when header is set to 'DoCananry' and header-value is 'DoCanary'")
f.HTTPTestClient().
GET("/").
WithHeader("Host", host).
WithHeader("CanaryByHeader", "DoCananry").
Expect().
Status(http.StatusOK).
Body().Contains(framework.EchoService).NotContains(canaryService)
})
ginkgo.It("should routes to mainline upstream when the given Regex causes error", func() {
host := "foo"
annotations := map[string]string{}

ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, annotations)
f.EnsureIngress(ing)

f.WaitForNginxServer(host,
func(server string) bool {
return strings.Contains(server, "server_name foo")
})

canaryAnnotations := map[string]string{
"nginx.ingress.kubernetes.io/canary": "true",
"nginx.ingress.kubernetes.io/canary-by-header": "CanaryByHeader",
"nginx.ingress.kubernetes.io/canary-by-header-pattern": "[][**da?$&*",
"nginx.ingress.kubernetes.io/canary-by-cookie": "CanaryByCookie",
}

canaryIngName := fmt.Sprintf("%v-canary", host)

canaryIng := framework.NewSingleIngress(canaryIngName, "/", host, f.Namespace, canaryService,
80, canaryAnnotations)
f.EnsureIngress(canaryIng)

ginkgo.By("routing requests to the mainline upstream when the given Regex causes error")
f.HTTPTestClient().
GET("/").
WithHeader("Host", host).
WithHeader("CanaryByHeader", "DoCanary").
WithCookie("CanaryByCookie", "always").
Expect().
Status(http.StatusOK).
Body().Contains(framework.EchoService).NotContains(canaryService)
})
})

ginkgo.Context("when canaried by header with value and cookie", func() {
ginkgo.It("should route requests to the correct upstream", func() {
host := "foo"
Expand Down

0 comments on commit 35264d6

Please sign in to comment.