Skip to content
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

Implement lb-method fixes #94 #168

Merged
merged 1 commit into from
Aug 18, 2017
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
1 change: 1 addition & 0 deletions examples/customization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions nginx-controller/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions nginx-controller/nginx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions nginx-controller/nginx/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -441,7 +447,7 @@ func (cnf *Configurator) createUpstream(ingEx *IngressEx, name string, backend *
ups.UpstreamServers = upsServers
}
}

ups.LBMethod = lbMethod
return ups
}

Expand Down
1 change: 1 addition & 0 deletions nginx-controller/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Upstream struct {
Name string
UpstreamServers []UpstreamServer
StickyCookie string
LBMethod string
}

// UpstreamServer describes a server in an NGINX upstream
Expand Down
1 change: 1 addition & 0 deletions nginx-controller/nginx/templates/nginx.ingress.tmpl
Original file line number Diff line number Diff line change
@@ -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}}
Expand Down