Skip to content

add header-value annotation #3619

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

Merged
merged 2 commits into from
Feb 1, 2019
Merged
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
3 changes: 3 additions & 0 deletions docs/user-guide/nginx-configuration/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ 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-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 @@ -106,6 +107,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`: The header to use for notifying the Ingress to route the request to the service specified in the Canary Ingress. When the request header is set to `always`, it will be routed to the canary. When the header is set to `never`, it will never be routed to the canary. For any other value, the header will be ignored and the request compared against the other canary rules by precedence.

* `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-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 ingored 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
16 changes: 11 additions & 5 deletions internal/ingress/annotations/canary/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ type canary struct {

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

// NewParser parses the ingress for canary related annotations
Expand Down Expand Up @@ -62,12 +63,17 @@ func (c canary) Parse(ing *extensions.Ingress) (interface{}, error) {
config.Header = ""
}

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

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

Expand Down
14 changes: 8 additions & 6 deletions internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,10 @@ 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,
Cookie: anns.Canary.Cookie,
Weight: anns.Canary.Weight,
Header: anns.Canary.Header,
HeaderValue: anns.Canary.HeaderValue,
Cookie: anns.Canary.Cookie,
}
}

Expand Down Expand Up @@ -757,9 +758,10 @@ 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,
Cookie: anns.Canary.Cookie,
Weight: anns.Canary.Weight,
Header: anns.Canary.Header,
HeaderValue: anns.Canary.HeaderValue,
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 @@ -118,6 +118,8 @@ type TrafficShapingPolicy struct {
Weight int `json:"weight"`
// Header on which to redirect requests to this backend
Header string `json:"header"`
// HeaderValue on which to redirect requests to this backend
HeaderValue string `json:"headerValue"`
// 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 @@ -293,6 +293,9 @@ func (tsp1 TrafficShapingPolicy) Equal(tsp2 TrafficShapingPolicy) bool {
if tsp1.Header != tsp2.Header {
return false
}
if tsp1.HeaderValue != tsp2.HeaderValue {
return false
}
if tsp1.Cookie != tsp2.Cookie {
return false
}
Expand Down
6 changes: 5 additions & 1 deletion rootfs/etc/nginx/lua/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ local function route_to_alternative_balancer(balancer)
local target_header = util.replace_special_char(traffic_shaping_policy.header, "-", "_")
local header = ngx.var["http_" .. target_header]
if header then
if header == "always" then
if traffic_shaping_policy.headerValue and #traffic_shaping_policy.headerValue > 0 then
if traffic_shaping_policy.headerValue == header then
return true
end
elseif header == "always" then
return true
elseif header == "never" then
return false
Expand Down
124 changes: 123 additions & 1 deletion test/e2e/annotations/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ var _ = framework.IngressNginxDescribe("Annotations - canary", func() {
})
})

Context("when canaried by header", func() {
Context("when canaried by header with no value", func() {
It("should route requests to the correct upstream", func() {
host := "foo"
annotations := map[string]string{}
Expand Down Expand Up @@ -458,6 +458,128 @@ var _ = framework.IngressNginxDescribe("Annotations - canary", func() {
})
})

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

ing := framework.NewSingleIngress(host, "/", host, f.IngressController.Namespace, "http-svc", 80, &annotations)
f.EnsureIngress(ing)

f.WaitForNginxServer(host,
func(server string) bool {
return Expect(server).Should(ContainSubstring("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",
}

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

canaryIng := framework.NewSingleIngress(canaryIngName, "/", host, f.IngressController.Namespace, "http-svc-canary",
80, &canaryAnnotations)
f.EnsureIngress(canaryIng)

time.Sleep(waitForLuaSync)

By("routing requests to the canary upstream when header is set to 'DoCanary'")

resp, body, errs := gorequest.New().
Get(f.IngressController.HTTPURL).
Set("Host", host).
Set("CanaryByHeader", "DoCanary").
End()

Expect(errs).Should(BeEmpty())
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
Expect(body).Should(ContainSubstring("http-svc-canary"))

By("routing requests to the mainline upstream when header is set to 'always'")

resp, body, errs = gorequest.New().
Get(f.IngressController.HTTPURL).
Set("Host", host).
Set("CanaryByHeader", "always").
End()

Expect(errs).Should(BeEmpty())
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
Expect(body).Should(ContainSubstring("http-svc"))
Expect(body).ShouldNot(ContainSubstring("http-svc-canary"))

By("routing requests to the mainline upstream when header is set to 'never'")

resp, body, errs = gorequest.New().
Get(f.IngressController.HTTPURL).
Set("Host", host).
Set("CanaryByHeader", "never").
End()

Expect(errs).Should(BeEmpty())
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
Expect(body).Should(ContainSubstring("http-svc"))
Expect(body).ShouldNot(ContainSubstring("http-svc-canary"))

By("routing requests to the mainline upstream when header is set to anything else")

resp, body, errs = gorequest.New().
Get(f.IngressController.HTTPURL).
Set("Host", host).
Set("CanaryByHeader", "otherheadervalue").
End()

Expect(errs).Should(BeEmpty())
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
Expect(body).Should(ContainSubstring("http-svc"))
Expect(body).ShouldNot(ContainSubstring("http-svc-canary"))
})
})

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

ing := framework.NewSingleIngress(host, "/", host, f.IngressController.Namespace, "http-svc", 80, &annotations)
f.EnsureIngress(ing)

f.WaitForNginxServer(host,
func(server string) bool {
return Expect(server).Should(ContainSubstring("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-cookie": "CanaryByCookie",
}

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

canaryIng := framework.NewSingleIngress(canaryIngName, "/", host, f.IngressController.Namespace, "http-svc-canary",
80, &canaryAnnotations)
f.EnsureIngress(canaryIng)

time.Sleep(waitForLuaSync)

By("routing requests to the canary upstream when header value does not match and cookie is set to 'always'")
resp, body, errs := gorequest.New().
Get(f.IngressController.HTTPURL).
Set("Host", host).
Set("CanaryByHeader", "otherheadervalue").
AddCookie(&http.Cookie{Name: "CanaryByCookie", Value: "always"}).
End()

Expect(errs).Should(BeEmpty())
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
Expect(body).Should(ContainSubstring("http-svc-canary"))
})
})

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