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

Added configmap option to turn on proxy protocol and set_real_ip_from #81

Merged
merged 1 commit into from
Nov 29, 2016
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
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