Skip to content

Commit 9c72c30

Browse files
authored
Merge pull request #81 from thetechnick/proxy-protocol
Added configmap option to turn on proxy protocol and set_real_ip_from
2 parents 3840597 + c38bad1 commit 9c72c30

File tree

7 files changed

+97
-2
lines changed

7 files changed

+97
-2
lines changed

nginx-controller/controller/controller.go

+27
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,33 @@ func (lbc *LoadBalancerController) syncCfgm(key string) {
364364
}
365365
}
366366

367+
if proxyProtocol, exists, err := nginx.GetMapKeyAsBool(cfgm.Data, "proxy-protocol", cfgm); exists {
368+
if err != nil {
369+
glog.Error(err)
370+
} else {
371+
cfg.ProxyProtocol = proxyProtocol
372+
}
373+
}
374+
375+
// ngx_http_realip_module
376+
if realIPHeader, exists := cfgm.Data["real-ip-header"]; exists {
377+
cfg.RealIPHeader = realIPHeader
378+
}
379+
if setRealIPFrom, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "set-real-ip-from", cfgm); exists {
380+
if err != nil {
381+
glog.Error(err)
382+
} else {
383+
cfg.SetRealIPFrom = setRealIPFrom
384+
}
385+
}
386+
if realIPRecursive, exists, err := nginx.GetMapKeyAsBool(cfgm.Data, "real-ip-recursive", cfgm); exists {
387+
if err != nil {
388+
glog.Error(err)
389+
} else {
390+
cfg.RealIPRecursive = realIPRecursive
391+
}
392+
}
393+
367394
if logFormat, exists := cfgm.Data["log-format"]; exists {
368395
cfg.MainLogFormat = logFormat
369396
}

nginx-controller/nginx/config.go

+6
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ type Config struct {
1313
ProxyBuffers string
1414
ProxyBufferSize string
1515
ProxyMaxTempFileSize string
16+
ProxyProtocol bool
1617
HSTS bool
1718
HSTSMaxAge int64
1819
HSTSIncludeSubdomains bool
20+
21+
// http://nginx.org/en/docs/http/ngx_http_realip_module.html
22+
RealIPHeader string
23+
SetRealIPFrom []string
24+
RealIPRecursive bool
1925
}
2026

2127
// NewDefaultConfig creates a Config with default values

nginx-controller/nginx/configurator.go

+8
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,13 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
105105
server := Server{
106106
Name: serverName,
107107
HTTP2: ingCfg.HTTP2,
108+
ProxyProtocol: ingCfg.ProxyProtocol,
108109
HSTS: ingCfg.HSTS,
109110
HSTSMaxAge: ingCfg.HSTSMaxAge,
110111
HSTSIncludeSubdomains: ingCfg.HSTSIncludeSubdomains,
112+
RealIPHeader: ingCfg.RealIPHeader,
113+
SetRealIPFrom: ingCfg.SetRealIPFrom,
114+
RealIPRecursive: ingCfg.RealIPRecursive,
111115
}
112116

113117
if pemFile, ok := pems[serverName]; ok {
@@ -149,9 +153,13 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
149153
server := Server{
150154
Name: emptyHost,
151155
HTTP2: ingCfg.HTTP2,
156+
ProxyProtocol: ingCfg.ProxyProtocol,
152157
HSTS: ingCfg.HSTS,
153158
HSTSMaxAge: ingCfg.HSTSMaxAge,
154159
HSTSIncludeSubdomains: ingCfg.HSTSIncludeSubdomains,
160+
RealIPHeader: ingCfg.RealIPHeader,
161+
SetRealIPFrom: ingCfg.SetRealIPFrom,
162+
RealIPRecursive: ingCfg.RealIPRecursive,
155163
}
156164

157165
if pemFile, ok := pems[emptyHost]; ok {

nginx-controller/nginx/convert.go

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package nginx
33
import (
44
"fmt"
55
"strconv"
6+
"strings"
67

78
"k8s.io/kubernetes/pkg/api/meta"
89
"k8s.io/kubernetes/pkg/runtime"
@@ -38,3 +39,12 @@ func GetMapKeyAsInt(m map[string]string, key string, context apiObject) (int64,
3839
}
3940
return 0, false, nil
4041
}
42+
43+
// GetMapKeyAsStringSlice tries to find and parse a key in the map as string slice splitting it on ','
44+
func GetMapKeyAsStringSlice(m map[string]string, key string, context apiObject) ([]string, bool, error) {
45+
if str, exists := m[key]; exists {
46+
slice := strings.Split(str, ",")
47+
return slice, exists, nil
48+
}
49+
return nil, false, nil
50+
}

nginx-controller/nginx/convert_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nginx
22

33
import (
4+
"reflect"
45
"testing"
56

67
"k8s.io/kubernetes/pkg/api"
@@ -153,3 +154,36 @@ func TestGetMapKeyAsIntErrorMessage(t *testing.T) {
153154
t.Errorf("The error message does not match expectations:\nGot: %v\nExpected: %v", err, expected)
154155
}
155156
}
157+
158+
//
159+
// GetMapKeyAsStringSlice
160+
//
161+
func TestGetMapKeyAsStringSlice(t *testing.T) {
162+
configMap := configMap
163+
configMap.Data = map[string]string{
164+
"key": "1.String,2.String,3.String",
165+
}
166+
167+
slice, exists, err := GetMapKeyAsStringSlice(configMap.Data, "key", &configMap)
168+
if err != nil {
169+
t.Errorf("Unexpected error: %v", err)
170+
}
171+
if !exists {
172+
t.Errorf("The key 'key' must exist in the configMap")
173+
}
174+
expected := []string{"1.String", "2.String", "3.String"}
175+
t.Log(expected)
176+
if !reflect.DeepEqual(expected, slice) {
177+
t.Errorf("Unexpected return value:\nGot: %#v\nExpected: %#v", slice, expected)
178+
}
179+
}
180+
181+
func TestGetMapKeyAsStringSliceNotFound(t *testing.T) {
182+
configMap := configMap
183+
configMap.Data = map[string]string{}
184+
185+
_, exists, _ := GetMapKeyAsStringSlice(configMap.Data, "key", &configMap)
186+
if exists {
187+
t.Errorf("The key 'key' must not exist in the configMap")
188+
}
189+
}

nginx-controller/nginx/ingress.tmpl

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ upstream {{$upstream.Name}} {
66

77
{{range $server := .Servers}}
88
server {
9-
listen 80;
9+
listen 80{{if $server.ProxyProtocol}} proxy_protocol{{end}};
1010
{{if $server.SSL}}
11-
listen 443 ssl{{if $server.HTTP2}} http2{{end}};
11+
listen 443 ssl{{if $server.HTTP2}} http2{{end}}{{if $server.ProxyProtocol}} proxy_protocol{{end}};
1212
ssl_certificate {{$server.SSLCertificate}};
1313
ssl_certificate_key {{$server.SSLCertificateKey}};
1414
{{end}}
15+
{{range $setRealIPFrom := $server.SetRealIPFrom}}
16+
set_real_ip_from {{$setRealIPFrom}};{{end}}
17+
{{if $server.RealIPHeader}}real_ip_header {{$server.RealIPHeader}};{{end}}
18+
{{if $server.RealIPRecursive}}real_ip_recursive on;{{end}}
1519

1620
{{if $server.Name}}
1721
server_name {{$server.Name}};

nginx-controller/nginx/nginx.go

+6
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,15 @@ type Server struct {
4444
SSLCertificate string
4545
SSLCertificateKey string
4646
HTTP2 bool
47+
ProxyProtocol bool
4748
HSTS bool
4849
HSTSMaxAge int64
4950
HSTSIncludeSubdomains bool
51+
52+
// http://nginx.org/en/docs/http/ngx_http_realip_module.html
53+
RealIPHeader string
54+
SetRealIPFrom []string
55+
RealIPRecursive bool
5056
}
5157

5258
// Location describes an NGINX location

0 commit comments

Comments
 (0)