Skip to content

Commit

Permalink
Added options to enable proxy_protocol and ngx_http_realip_module
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico Schieder committed Nov 29, 2016
1 parent 3840597 commit c38bad1
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 2 deletions.
27 changes: 27 additions & 0 deletions nginx-controller/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,33 @@ func (lbc *LoadBalancerController) syncCfgm(key string) {
}
}

if proxyProtocol, exists, err := nginx.GetMapKeyAsBool(cfgm.Data, "proxy-protocol", cfgm); exists {
if err != nil {
glog.Error(err)
} else {
cfg.ProxyProtocol = proxyProtocol
}
}

// ngx_http_realip_module
if realIPHeader, exists := cfgm.Data["real-ip-header"]; exists {
cfg.RealIPHeader = realIPHeader
}
if setRealIPFrom, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "set-real-ip-from", cfgm); exists {
if err != nil {
glog.Error(err)
} else {
cfg.SetRealIPFrom = setRealIPFrom
}
}
if realIPRecursive, exists, err := nginx.GetMapKeyAsBool(cfgm.Data, "real-ip-recursive", cfgm); exists {
if err != nil {
glog.Error(err)
} else {
cfg.RealIPRecursive = realIPRecursive
}
}

if logFormat, exists := cfgm.Data["log-format"]; exists {
cfg.MainLogFormat = logFormat
}
Expand Down
6 changes: 6 additions & 0 deletions nginx-controller/nginx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ type Config struct {
ProxyBuffers string
ProxyBufferSize string
ProxyMaxTempFileSize string
ProxyProtocol bool
HSTS bool
HSTSMaxAge int64
HSTSIncludeSubdomains bool

// http://nginx.org/en/docs/http/ngx_http_realip_module.html
RealIPHeader string
SetRealIPFrom []string
RealIPRecursive bool
}

// NewDefaultConfig creates a Config with default values
Expand Down
8 changes: 8 additions & 0 deletions nginx-controller/nginx/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,13 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
server := Server{
Name: serverName,
HTTP2: ingCfg.HTTP2,
ProxyProtocol: ingCfg.ProxyProtocol,
HSTS: ingCfg.HSTS,
HSTSMaxAge: ingCfg.HSTSMaxAge,
HSTSIncludeSubdomains: ingCfg.HSTSIncludeSubdomains,
RealIPHeader: ingCfg.RealIPHeader,
SetRealIPFrom: ingCfg.SetRealIPFrom,
RealIPRecursive: ingCfg.RealIPRecursive,
}

if pemFile, ok := pems[serverName]; ok {
Expand Down Expand Up @@ -149,9 +153,13 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
server := Server{
Name: emptyHost,
HTTP2: ingCfg.HTTP2,
ProxyProtocol: ingCfg.ProxyProtocol,
HSTS: ingCfg.HSTS,
HSTSMaxAge: ingCfg.HSTSMaxAge,
HSTSIncludeSubdomains: ingCfg.HSTSIncludeSubdomains,
RealIPHeader: ingCfg.RealIPHeader,
SetRealIPFrom: ingCfg.SetRealIPFrom,
RealIPRecursive: ingCfg.RealIPRecursive,
}

if pemFile, ok := pems[emptyHost]; ok {
Expand Down
10 changes: 10 additions & 0 deletions nginx-controller/nginx/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nginx
import (
"fmt"
"strconv"
"strings"

"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/runtime"
Expand Down Expand Up @@ -38,3 +39,12 @@ func GetMapKeyAsInt(m map[string]string, key string, context apiObject) (int64,
}
return 0, false, nil
}

// GetMapKeyAsStringSlice tries to find and parse a key in the map as string slice splitting it on ','
func GetMapKeyAsStringSlice(m map[string]string, key string, context apiObject) ([]string, bool, error) {
if str, exists := m[key]; exists {
slice := strings.Split(str, ",")
return slice, exists, nil
}
return nil, false, nil
}
34 changes: 34 additions & 0 deletions nginx-controller/nginx/convert_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nginx

import (
"reflect"
"testing"

"k8s.io/kubernetes/pkg/api"
Expand Down Expand Up @@ -153,3 +154,36 @@ func TestGetMapKeyAsIntErrorMessage(t *testing.T) {
t.Errorf("The error message does not match expectations:\nGot: %v\nExpected: %v", err, expected)
}
}

//
// GetMapKeyAsStringSlice
//
func TestGetMapKeyAsStringSlice(t *testing.T) {
configMap := configMap
configMap.Data = map[string]string{
"key": "1.String,2.String,3.String",
}

slice, exists, err := GetMapKeyAsStringSlice(configMap.Data, "key", &configMap)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !exists {
t.Errorf("The key 'key' must exist in the configMap")
}
expected := []string{"1.String", "2.String", "3.String"}
t.Log(expected)
if !reflect.DeepEqual(expected, slice) {
t.Errorf("Unexpected return value:\nGot: %#v\nExpected: %#v", slice, expected)
}
}

func TestGetMapKeyAsStringSliceNotFound(t *testing.T) {
configMap := configMap
configMap.Data = map[string]string{}

_, exists, _ := GetMapKeyAsStringSlice(configMap.Data, "key", &configMap)
if exists {
t.Errorf("The key 'key' must not exist in the configMap")
}
}
8 changes: 6 additions & 2 deletions nginx-controller/nginx/ingress.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ upstream {{$upstream.Name}} {

{{range $server := .Servers}}
server {
listen 80;
listen 80{{if $server.ProxyProtocol}} proxy_protocol{{end}};
{{if $server.SSL}}
listen 443 ssl{{if $server.HTTP2}} http2{{end}};
listen 443 ssl{{if $server.HTTP2}} http2{{end}}{{if $server.ProxyProtocol}} proxy_protocol{{end}};
ssl_certificate {{$server.SSLCertificate}};
ssl_certificate_key {{$server.SSLCertificateKey}};
{{end}}
{{range $setRealIPFrom := $server.SetRealIPFrom}}
set_real_ip_from {{$setRealIPFrom}};{{end}}
{{if $server.RealIPHeader}}real_ip_header {{$server.RealIPHeader}};{{end}}
{{if $server.RealIPRecursive}}real_ip_recursive on;{{end}}

{{if $server.Name}}
server_name {{$server.Name}};
Expand Down
6 changes: 6 additions & 0 deletions nginx-controller/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ type Server struct {
SSLCertificate string
SSLCertificateKey string
HTTP2 bool
ProxyProtocol bool
HSTS bool
HSTSMaxAge int64
HSTSIncludeSubdomains bool

// http://nginx.org/en/docs/http/ngx_http_realip_module.html
RealIPHeader string
SetRealIPFrom []string
RealIPRecursive bool
}

// Location describes an NGINX location
Expand Down

0 comments on commit c38bad1

Please sign in to comment.