From c51d9f7d9f8f6355deca7c73f3169ef264148ad8 Mon Sep 17 00:00:00 2001 From: Sajal Kayan Date: Thu, 17 Aug 2017 22:44:43 +0700 Subject: [PATCH] Implement lb-method fixes nginxinc/kubernetes-ingress#94 --- examples/customization/README.md | 1 + nginx-controller/controller/controller.go | 4 ++++ nginx-controller/nginx/config.go | 1 + nginx-controller/nginx/configurator.go | 14 ++++++++++---- nginx-controller/nginx/nginx.go | 1 + .../nginx/templates/nginx.ingress.tmpl | 1 + 6 files changed, 18 insertions(+), 4 deletions(-) diff --git a/examples/customization/README.md b/examples/customization/README.md index c89f13898a..83fbe7b232 100644 --- a/examples/customization/README.md +++ b/examples/customization/README.md @@ -34,6 +34,7 @@ The table below summarizes some of the options. More options (extensions) are av | N/A | `http-snippets` | Sets a custom snippet in http context. | N/A | | `nginx.org/location-snippets` | `location-snippets` | Sets a custom snippet in location context. | N/A | | `nginx.org/server-snippets` | `server-snippets` | Sets a custom snippet in server context. | N/A | +| `nginx.org/lb-method` | `lb-method` | Sets the [load balancing method](https://www.nginx.com/resources/admin-guide/load-balancer/#method). The default `""` specifies the round-robin method. | `""` | ## Using ConfigMaps diff --git a/nginx-controller/controller/controller.go b/nginx-controller/controller/controller.go index 3c2c7f2b64..3b2e7f7cfc 100644 --- a/nginx-controller/controller/controller.go +++ b/nginx-controller/controller/controller.go @@ -377,6 +377,10 @@ func (lbc *LoadBalancerController) syncCfgm(task Task) { } } + if lbMethod, exists := cfgm.Data["lb-method"]; exists { + cfg.LBMethod = lbMethod + } + if proxyConnectTimeout, exists := cfgm.Data["proxy-connect-timeout"]; exists { cfg.ProxyConnectTimeout = proxyConnectTimeout } diff --git a/nginx-controller/nginx/config.go b/nginx-controller/nginx/config.go index 4a9d60fe13..03039f6768 100644 --- a/nginx-controller/nginx/config.go +++ b/nginx-controller/nginx/config.go @@ -24,6 +24,7 @@ type Config struct { HSTS bool HSTSMaxAge int64 HSTSIncludeSubdomains bool + LBMethod string // http://nginx.org/en/docs/http/ngx_http_realip_module.html RealIPHeader string diff --git a/nginx-controller/nginx/configurator.go b/nginx-controller/nginx/configurator.go index fa1770a22c..8606ce442e 100644 --- a/nginx-controller/nginx/configurator.go +++ b/nginx-controller/nginx/configurator.go @@ -83,7 +83,7 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri if ingEx.Ingress.Spec.Backend != nil { name := getNameForUpstream(ingEx.Ingress, emptyHost, ingEx.Ingress.Spec.Backend.ServiceName) - upstream := cnf.createUpstream(ingEx, name, ingEx.Ingress.Spec.Backend, ingEx.Ingress.Namespace, spServices[ingEx.Ingress.Spec.Backend.ServiceName]) + upstream := cnf.createUpstream(ingEx, name, ingEx.Ingress.Spec.Backend, ingEx.Ingress.Namespace, spServices[ingEx.Ingress.Spec.Backend.ServiceName], ingCfg.LBMethod) upstreams[name] = upstream } @@ -133,7 +133,7 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri upsName := getNameForUpstream(ingEx.Ingress, rule.Host, path.Backend.ServiceName) if _, exists := upstreams[upsName]; !exists { - upstream := cnf.createUpstream(ingEx, upsName, &path.Backend, ingEx.Ingress.Namespace, spServices[path.Backend.ServiceName]) + upstream := cnf.createUpstream(ingEx, upsName, &path.Backend, ingEx.Ingress.Namespace, spServices[path.Backend.ServiceName], ingCfg.LBMethod) upstreams[upsName] = upstream } @@ -199,6 +199,12 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri func (cnf *Configurator) createConfig(ingEx *IngressEx) Config { ingCfg := *cnf.config + + //Override from annotation + if lbMethod, exists := ingEx.Ingress.Annotations["nginx.org/lb-method"]; exists { + ingCfg.LBMethod = lbMethod + } + if serverTokens, exists, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/server-tokens", ingEx.Ingress); exists { if err != nil { if cnf.isPlus() { @@ -421,7 +427,7 @@ func createLocation(path string, upstream Upstream, cfg *Config, websocket bool, return loc } -func (cnf *Configurator) createUpstream(ingEx *IngressEx, name string, backend *extensions.IngressBackend, namespace string, stickyCookie string) Upstream { +func (cnf *Configurator) createUpstream(ingEx *IngressEx, name string, backend *extensions.IngressBackend, namespace string, stickyCookie string, lbMethod string) Upstream { var ups Upstream if cnf.isPlus() { @@ -441,7 +447,7 @@ func (cnf *Configurator) createUpstream(ingEx *IngressEx, name string, backend * ups.UpstreamServers = upsServers } } - + ups.LBMethod = lbMethod return ups } diff --git a/nginx-controller/nginx/nginx.go b/nginx-controller/nginx/nginx.go index c8152a291a..5ad0424331 100644 --- a/nginx-controller/nginx/nginx.go +++ b/nginx-controller/nginx/nginx.go @@ -35,6 +35,7 @@ type Upstream struct { Name string UpstreamServers []UpstreamServer StickyCookie string + LBMethod string } // UpstreamServer describes a server in an NGINX upstream diff --git a/nginx-controller/nginx/templates/nginx.ingress.tmpl b/nginx-controller/nginx/templates/nginx.ingress.tmpl index 651d351728..8ac89e2ac6 100644 --- a/nginx-controller/nginx/templates/nginx.ingress.tmpl +++ b/nginx-controller/nginx/templates/nginx.ingress.tmpl @@ -1,5 +1,6 @@ {{range $upstream := .Upstreams}} upstream {{$upstream.Name}} { + {{if $upstream.LBMethod }}{{$upstream.LBMethod}};{{end}} {{range $server := $upstream.UpstreamServers}} server {{$server.Address}}:{{$server.Port}};{{end}} }{{end}}