From c5b0c8ab0d43946dd0ffccb8a3ab1a6323b61bb5 Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Mon, 13 Nov 2017 19:57:41 -0300 Subject: [PATCH] Add annotation for setting proxy_redirect --- docs/user-guide/annotations.md | 9 +++++ internal/ingress/annotations/proxy/main.go | 41 ++++++++++++++------ internal/ingress/controller/config/config.go | 1 + internal/ingress/controller/controller.go | 19 ++++----- internal/ingress/defaults/main.go | 10 +++++ rootfs/etc/nginx/template/nginx.tmpl | 6 ++- 6 files changed, 64 insertions(+), 22 deletions(-) diff --git a/docs/user-guide/annotations.md b/docs/user-guide/annotations.md index a0525a93a3..17dfcca5a6 100644 --- a/docs/user-guide/annotations.md +++ b/docs/user-guide/annotations.md @@ -34,6 +34,8 @@ The following annotations are supported: |[ingress.kubernetes.io/proxy-read-timeout](#custom-timeouts)|number| |[ingress.kubernetes.io/proxy-next-upstream](#custom-timeouts)|string| |[ingress.kubernetes.io/proxy-request-buffering](#custom-timeouts)|string| +|[ingress.kubernetes.io/proxy-redirect-from](#proxy-redirect)|string| +|[ingress.kubernetes.io/proxy-redirect-to](#proxy-redirect)|string| |[ingress.kubernetes.io/rewrite-target](#rewrite)|URI| |[ingress.kubernetes.io/secure-backends](#secure-backends)|true or false| |[ingress.kubernetes.io/server-alias](#server-alias)|string| @@ -352,6 +354,13 @@ In some scenarios is required to have different values. To allow this we provide - `ingress.kubernetes.io/proxy-next-upstream` - `ingress.kubernetes.io/proxy-request-buffering` +### Proxy redirect + +With the annotations `ingress.kubernetes.io/proxy-redirect-from` and `ingress.kubernetes.io/proxy-redirect-to` it is possible to set the text that should be changed in the `Location` and `Refresh` header fields of a proxied server response (http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect) +Setting "off" or "default" in the annotation `ingress.kubernetes.io/proxy-redirect-to` disables `ingress.kubernetes.io/proxy-redirect-to` +Both annotations will be used in any other case +By default the value is "off". + ### Custom max body size For NGINX, 413 error will be returned to the client when the size in a request exceeds the maximum allowed size of the client request body. This size can be configured by the parameter [`client_max_body_size`](http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size). diff --git a/internal/ingress/annotations/proxy/main.go b/internal/ingress/annotations/proxy/main.go index e193312de2..67f4581db2 100644 --- a/internal/ingress/annotations/proxy/main.go +++ b/internal/ingress/annotations/proxy/main.go @@ -25,16 +25,18 @@ import ( // Config returns the proxy timeout to use in the upstream server/s type Config struct { - BodySize string `json:"bodySize"` - ConnectTimeout int `json:"connectTimeout"` - SendTimeout int `json:"sendTimeout"` - ReadTimeout int `json:"readTimeout"` - BufferSize string `json:"bufferSize"` - CookieDomain string `json:"cookieDomain"` - CookiePath string `json:"cookiePath"` - NextUpstream string `json:"nextUpstream"` - PassParams string `json:"passParams"` - RequestBuffering string `json:"requestBuffering"` + BodySize string `json:"bodySize"` + ConnectTimeout int `json:"connectTimeout"` + SendTimeout int `json:"sendTimeout"` + ReadTimeout int `json:"readTimeout"` + BufferSize string `json:"bufferSize"` + CookieDomain string `json:"cookieDomain"` + CookiePath string `json:"cookiePath"` + NextUpstream string `json:"nextUpstream"` + PassParams string `json:"passParams"` + ProxyRedirectFrom string `json:"proxyRedirectFrom"` + ProxyRedirectTo string `json:"proxyRedirectTo"` + RequestBuffering string `json:"requestBuffering"` } // Equal tests for equality between two Configuration types @@ -72,10 +74,15 @@ func (l1 *Config) Equal(l2 *Config) bool { if l1.PassParams != l2.PassParams { return false } - if l1.RequestBuffering != l2.RequestBuffering { return false } + if l1.ProxyRedirectFrom != l2.ProxyRedirectFrom { + return false + } + if l1.ProxyRedirectTo != l2.ProxyRedirectTo { + return false + } return true } @@ -143,5 +150,15 @@ func (a proxy) Parse(ing *extensions.Ingress) (interface{}, error) { rb = defBackend.ProxyRequestBuffering } - return &Config{bs, ct, st, rt, bufs, cd, cp, nu, pp, rb}, nil + prf, err := parser.GetStringAnnotation("proxy-redirect-from", ing, a.r) + if err != nil || rb == "" { + prf = defBackend.ProxyRedirectFrom + } + + prt, err := parser.GetStringAnnotation("proxy-redirect-to", ing, a.r) + if err != nil || rb == "" { + prt = defBackend.ProxyRedirectTo + } + + return &Config{bs, ct, st, rt, bufs, cd, cp, nu, pp, prf, prt, rb}, nil } diff --git a/internal/ingress/controller/config/config.go b/internal/ingress/controller/config/config.go index b755a02912..f32dedd7bd 100644 --- a/internal/ingress/controller/config/config.go +++ b/internal/ingress/controller/config/config.go @@ -497,6 +497,7 @@ func NewDefault() Configuration { ProxyCookiePath: "off", ProxyNextUpstream: "error timeout invalid_header http_502 http_503 http_504", ProxyRequestBuffering: "on", + ProxyRedirectFrom: "off", SSLRedirect: true, CustomHTTPErrors: []int{}, WhitelistSourceRange: []string{}, diff --git a/internal/ingress/controller/controller.go b/internal/ingress/controller/controller.go index fb96b12fce..9da69ad591 100644 --- a/internal/ingress/controller/controller.go +++ b/internal/ingress/controller/controller.go @@ -874,15 +874,16 @@ func (n *NGINXController) createServers(data []*extensions.Ingress, bdef := n.GetDefaultBackend() ngxProxy := proxy.Config{ - BodySize: bdef.ProxyBodySize, - ConnectTimeout: bdef.ProxyConnectTimeout, - SendTimeout: bdef.ProxySendTimeout, - ReadTimeout: bdef.ProxyReadTimeout, - BufferSize: bdef.ProxyBufferSize, - CookieDomain: bdef.ProxyCookieDomain, - CookiePath: bdef.ProxyCookiePath, - NextUpstream: bdef.ProxyNextUpstream, - RequestBuffering: bdef.ProxyRequestBuffering, + BodySize: bdef.ProxyBodySize, + ConnectTimeout: bdef.ProxyConnectTimeout, + SendTimeout: bdef.ProxySendTimeout, + ReadTimeout: bdef.ProxyReadTimeout, + BufferSize: bdef.ProxyBufferSize, + CookieDomain: bdef.ProxyCookieDomain, + CookiePath: bdef.ProxyCookiePath, + NextUpstream: bdef.ProxyNextUpstream, + RequestBuffering: bdef.ProxyRequestBuffering, + ProxyRedirectFrom: bdef.ProxyRedirectFrom, } // generated on Start() with createDefaultSSLCertificate() diff --git a/internal/ingress/defaults/main.go b/internal/ingress/defaults/main.go index 998067bf5c..b9ee87626e 100644 --- a/internal/ingress/defaults/main.go +++ b/internal/ingress/defaults/main.go @@ -72,6 +72,16 @@ type Backend struct { // Parameters for proxy-pass directive (eg. Apache web server). ProxyPassParams string `json:"proxy-pass-params"` + // Sets the original text that should be changed in the "Location" and "Refresh" header fields of a proxied server response. + // http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect + // Default: off + ProxyRedirectFrom string `json:"proxy-redirect-from"` + + // Sets the replacement text that should be changed in the "Location" and "Refresh" header fields of a proxied server response. + // http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect + // Default: "" + ProxyRedirectTo string `json:"proxy-redirect-to"` + // Enables or disables buffering of a client request body. // http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_request_buffering ProxyRequestBuffering string `json:"proxy-request-buffering"` diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index c90781d2a4..9bdaffdb51 100644 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -797,7 +797,11 @@ stream { proxy_send_timeout {{ $location.Proxy.SendTimeout }}s; proxy_read_timeout {{ $location.Proxy.ReadTimeout }}s; - proxy_redirect off; + {{ if (or (eq $location.Proxy.ProxyRedirectFrom "default") (eq $location.Proxy.ProxyRedirectFrom "off")) }} + proxy_redirect {{ $location.Proxy.ProxyRedirectFrom }}; + {{ else }} + proxy_redirect {{ $location.Proxy.ProxyRedirectFrom }} {{ $location.Proxy.ProxyRedirectTo }}; + {{ end }} proxy_buffering off; proxy_buffer_size "{{ $location.Proxy.BufferSize }}"; proxy_buffers 4 "{{ $location.Proxy.BufferSize }}";