diff --git a/docs/user-guide/nginx-configuration/annotations.md b/docs/user-guide/nginx-configuration/annotations.md index 10202942f..c451d9ae9 100644 --- a/docs/user-guide/nginx-configuration/annotations.md +++ b/docs/user-guide/nginx-configuration/annotations.md @@ -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| @@ -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. diff --git a/internal/ingress/annotations/canary/main.go b/internal/ingress/annotations/canary/main.go index 2e7305934..564536818 100644 --- a/internal/ingress/annotations/canary/main.go +++ b/internal/ingress/annotations/canary/main.go @@ -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 @@ -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") } diff --git a/internal/ingress/controller/controller.go b/internal/ingress/controller/controller.go index f180e905e..a8e72f068 100644 --- a/internal/ingress/controller/controller.go +++ b/internal/ingress/controller/controller.go @@ -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, } } @@ -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, } } diff --git a/internal/ingress/types.go b/internal/ingress/types.go index 01aa1952a..c428e0759 100644 --- a/internal/ingress/types.go +++ b/internal/ingress/types.go @@ -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"` } diff --git a/internal/ingress/types_equals.go b/internal/ingress/types_equals.go index e0a621158..968c36529 100644 --- a/internal/ingress/types_equals.go +++ b/internal/ingress/types_equals.go @@ -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 } diff --git a/rootfs/etc/nginx/lua/balancer.lua b/rootfs/etc/nginx/lua/balancer.lua index 4cfae8e85..05650716d 100644 --- a/rootfs/etc/nginx/lua/balancer.lua +++ b/rootfs/etc/nginx/lua/balancer.lua @@ -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 then + if traffic_shaping_policy.headerValue == header then + return true + end + elseif header == "always" then return true elseif header == "never" then return false diff --git a/test/e2e/annotations/canary.go b/test/e2e/annotations/canary.go index 67e3d7e02..15abcef1a 100644 --- a/test/e2e/annotations/canary.go +++ b/test/e2e/annotations/canary.go @@ -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{} @@ -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"