Skip to content

Commit c3999da

Browse files
committed
Add location-snippets, server-snippets and http-snippets for Plus
1 parent 4bfe9ff commit c3999da

File tree

8 files changed

+95
-12
lines changed

8 files changed

+95
-12
lines changed

nginx-plus-controller/controller/controller.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,14 @@ func (lbc *LoadBalancerController) syncCfgm(key string) {
379379
if proxyReadTimeout, exists := cfgm.Data["proxy-read-timeout"]; exists {
380380
cfg.ProxyReadTimeout = proxyReadTimeout
381381
}
382-
if proxyHideHeaders, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "proxy-hide-headers", cfgm); exists {
382+
if proxyHideHeaders, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "proxy-hide-headers", cfgm, ","); exists {
383383
if err != nil {
384384
glog.Error(err)
385385
} else {
386386
cfg.ProxyHideHeaders = proxyHideHeaders
387387
}
388388
}
389-
if proxyPassHeaders, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "proxy-pass-headers", cfgm); exists {
389+
if proxyPassHeaders, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "proxy-pass-headers", cfgm, ","); exists {
390390
if err != nil {
391391
glog.Error(err)
392392
} else {
@@ -409,7 +409,7 @@ func (lbc *LoadBalancerController) syncCfgm(key string) {
409409
cfg.HTTP2 = HTTP2
410410
}
411411
}
412-
if redirectToHTTPS, exists,err := nginx.GetMapKeyAsBool(cfgm.Data, "redirect-to-https", cfgm); exists {
412+
if redirectToHTTPS, exists, err := nginx.GetMapKeyAsBool(cfgm.Data, "redirect-to-https", cfgm); exists {
413413
if err != nil {
414414
glog.Error(err)
415415
} else {
@@ -461,7 +461,7 @@ func (lbc *LoadBalancerController) syncCfgm(key string) {
461461
if realIPHeader, exists := cfgm.Data["real-ip-header"]; exists {
462462
cfg.RealIPHeader = realIPHeader
463463
}
464-
if setRealIPFrom, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "set-real-ip-from", cfgm); exists {
464+
if setRealIPFrom, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "set-real-ip-from", cfgm, ","); exists {
465465
if err != nil {
466466
glog.Error(err)
467467
} else {
@@ -519,6 +519,27 @@ func (lbc *LoadBalancerController) syncCfgm(key string) {
519519
if proxyMaxTempFileSize, exists := cfgm.Data["proxy-max-temp-file-size"]; exists {
520520
cfg.ProxyMaxTempFileSize = proxyMaxTempFileSize
521521
}
522+
if mainHTTPSnippets, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "http-snippets", cfgm, "\n"); exists {
523+
if err != nil {
524+
glog.Error(err)
525+
} else {
526+
cfg.MainHTTPSnippets = mainHTTPSnippets
527+
}
528+
}
529+
if locationSnippets, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "location-snippets", cfgm, "\n"); exists {
530+
if err != nil {
531+
glog.Error(err)
532+
} else {
533+
cfg.LocationSnippets = locationSnippets
534+
}
535+
}
536+
if serverSnippets, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "server-snippets", cfgm, "\n"); exists {
537+
if err != nil {
538+
glog.Error(err)
539+
} else {
540+
cfg.ServerSnippets = serverSnippets
541+
}
542+
}
522543
}
523544
lbc.cnf.UpdateConfig(cfg)
524545

nginx-plus-controller/nginx/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package nginx
22

33
// Config holds NGINX configuration parameters
44
type Config struct {
5+
LocationSnippets []string
6+
ServerSnippets []string
57
ServerTokens string
68
ProxyConnectTimeout string
79
ProxyReadTimeout string
810
ClientMaxBodySize string
911
HTTP2 bool
1012
RedirectToHTTPS bool
13+
MainHTTPSnippets []string
1114
MainServerNamesHashBucketSize string
1215
MainServerNamesHashMaxSize string
1316
MainLogFormat string

nginx-plus-controller/nginx/configurator.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
131131
RealIPRecursive: ingCfg.RealIPRecursive,
132132
ProxyHideHeaders: ingCfg.ProxyHideHeaders,
133133
ProxyPassHeaders: ingCfg.ProxyPassHeaders,
134+
ServerSnippets: ingCfg.ServerSnippets,
134135
}
135136

136137
if pemFile, ok := pems[serverName]; ok {
@@ -188,6 +189,7 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
188189
RealIPRecursive: ingCfg.RealIPRecursive,
189190
ProxyHideHeaders: ingCfg.ProxyHideHeaders,
190191
ProxyPassHeaders: ingCfg.ProxyPassHeaders,
192+
ServerSnippets: ingCfg.ServerSnippets,
191193
}
192194

193195
if pemFile, ok := pems[emptyHost]; ok {
@@ -223,20 +225,34 @@ func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
223225
}
224226
}
225227
}
228+
if serverSnippets, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/server-snippets", ingEx.Ingress, "\n"); exists {
229+
if err != nil {
230+
glog.Error(err)
231+
} else {
232+
ingCfg.ServerSnippets = serverSnippets
233+
}
234+
}
235+
if locationSnippets, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/location-snippets", ingEx.Ingress, "\n"); exists {
236+
if err != nil {
237+
glog.Error(err)
238+
} else {
239+
ingCfg.LocationSnippets = locationSnippets
240+
}
241+
}
226242
if proxyConnectTimeout, exists := ingEx.Ingress.Annotations["nginx.org/proxy-connect-timeout"]; exists {
227243
ingCfg.ProxyConnectTimeout = proxyConnectTimeout
228244
}
229245
if proxyReadTimeout, exists := ingEx.Ingress.Annotations["nginx.org/proxy-read-timeout"]; exists {
230246
ingCfg.ProxyReadTimeout = proxyReadTimeout
231247
}
232-
if proxyHideHeaders, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/proxy-hide-headers", ingEx.Ingress); exists {
248+
if proxyHideHeaders, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/proxy-hide-headers", ingEx.Ingress, ","); exists {
233249
if err != nil {
234250
glog.Error(err)
235251
} else {
236252
ingCfg.ProxyHideHeaders = proxyHideHeaders
237253
}
238254
}
239-
if proxyPassHeaders, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/proxy-pass-headers", ingEx.Ingress); exists {
255+
if proxyPassHeaders, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/proxy-pass-headers", ingEx.Ingress, ","); exists {
240256
if err != nil {
241257
glog.Error(err)
242258
} else {
@@ -253,7 +269,7 @@ func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
253269
ingCfg.HTTP2 = HTTP2
254270
}
255271
}
256-
if redirectToHTTPS, exists,err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/redirect-to-https", ingEx.Ingress); exists {
272+
if redirectToHTTPS, exists, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/redirect-to-https", ingEx.Ingress); exists {
257273
if err != nil {
258274
glog.Error(err)
259275
} else {
@@ -416,6 +432,7 @@ func createLocation(path string, upstream Upstream, cfg *Config, websocket bool,
416432
ProxyBuffers: cfg.ProxyBuffers,
417433
ProxyBufferSize: cfg.ProxyBufferSize,
418434
ProxyMaxTempFileSize: cfg.ProxyMaxTempFileSize,
435+
LocationSnippets: cfg.LocationSnippets,
419436
}
420437

421438
return loc
@@ -500,6 +517,7 @@ func (cnf *Configurator) UpdateConfig(config *Config) {
500517

501518
cnf.config = config
502519
mainCfg := &NginxMainConfig{
520+
HTTPSnippets: config.MainHTTPSnippets,
503521
ServerNamesHashBucketSize: config.MainServerNamesHashBucketSize,
504522
ServerNamesHashMaxSize: config.MainServerNamesHashMaxSize,
505523
LogFormat: config.MainLogFormat,

nginx-plus-controller/nginx/convert.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ func GetMapKeyAsInt(m map[string]string, key string, context apiObject) (int64,
4040
return 0, false, nil
4141
}
4242

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) {
43+
// GetMapKeyAsStringSlice tries to find and parse a key in the map as string slice splitting it on the delimiter
44+
func GetMapKeyAsStringSlice(m map[string]string, key string, context apiObject, delimiter string) ([]string, bool, error) {
4545
if str, exists := m[key]; exists {
46-
slice := strings.Split(str, ",")
46+
slice := strings.Split(str, delimiter)
4747
return slice, exists, nil
4848
}
4949
return nil, false, nil

nginx-plus-controller/nginx/convert_test.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func TestGetMapKeyAsStringSlice(t *testing.T) {
164164
"key": "1.String,2.String,3.String",
165165
}
166166

167-
slice, exists, err := GetMapKeyAsStringSlice(configMap.Data, "key", &configMap)
167+
slice, exists, err := GetMapKeyAsStringSlice(configMap.Data, "key", &configMap, ",")
168168
if err != nil {
169169
t.Errorf("Unexpected error: %v", err)
170170
}
@@ -178,11 +178,33 @@ func TestGetMapKeyAsStringSlice(t *testing.T) {
178178
}
179179
}
180180

181+
func TestGetMapKeyAsStringSliceMultilineSnippets(t *testing.T) {
182+
configMap := configMap
183+
configMap.Data = map[string]string{
184+
"server-snippets": `
185+
if ($new_uri) {
186+
rewrite ^ $new_uri permanent;
187+
}`,
188+
}
189+
slice, exists, err := GetMapKeyAsStringSlice(configMap.Data, "server-snippets", &configMap, "\n")
190+
if err != nil {
191+
t.Errorf("Unexpected error: %v", err)
192+
}
193+
if !exists {
194+
t.Errorf("The key 'server-snippets' must exist in the configMap")
195+
}
196+
expected := []string{"", "\t\t\tif ($new_uri) {", "\t\t\t\trewrite ^ $new_uri permanent;", "\t\t\t}"}
197+
t.Log(expected)
198+
if !reflect.DeepEqual(expected, slice) {
199+
t.Errorf("Unexpected return value:\nGot: %#v\nExpected: %#v", slice, expected)
200+
}
201+
}
202+
181203
func TestGetMapKeyAsStringSliceNotFound(t *testing.T) {
182204
configMap := configMap
183205
configMap.Data = map[string]string{}
184206

185-
_, exists, _ := GetMapKeyAsStringSlice(configMap.Data, "key", &configMap)
207+
_, exists, _ := GetMapKeyAsStringSlice(configMap.Data, "key", &configMap, ",")
186208
if exists {
187209
t.Errorf("The key 'key' must not exist in the configMap")
188210
}

nginx-plus-controller/nginx/ingress.tmpl

+11
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,24 @@ server {
4646
}
4747
{{- end}}
4848

49+
{{- if $server.ServerSnippets}}
50+
{{range $value := $server.ServerSnippets}}
51+
{{$value}}{{end}}
52+
{{- end}}
53+
4954
{{range $location := $server.Locations}}
5055
location {{$location.Path}} {
5156
proxy_http_version 1.1;
5257
{{if $location.Websocket}}
5358
proxy_set_header Upgrade $http_upgrade;
5459
proxy_set_header Connection $connection_upgrade;
5560
{{end}}
61+
62+
{{- if $location.LocationSnippets}}
63+
{{range $value := $location.LocationSnippets}}
64+
{{$value}}{{end}}
65+
{{- end}}
66+
5667
proxy_connect_timeout {{$location.ProxyConnectTimeout}};
5768
proxy_read_timeout {{$location.ProxyReadTimeout}};
5869
client_max_body_size {{$location.ClientMaxBodySize}};

nginx-plus-controller/nginx/nginx.conf.tmpl

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ http {
1515
include /etc/nginx/mime.types;
1616
default_type application/octet-stream;
1717

18+
{{- if .HTTPSnippets}}
19+
{{range $value := .HTTPSnippets}}
20+
{{$value}}{{end}}
21+
{{- end}}
22+
1823
{{if .LogFormat -}}
1924
log_format main '{{.LogFormat}}';
2025
{{- else -}}

nginx-plus-controller/nginx/nginx.go

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type UpstreamServer struct {
4141

4242
// Server describes an NGINX server
4343
type Server struct {
44+
ServerSnippets []string
4445
Name string
4546
ServerTokens string
4647
Locations []Location
@@ -65,6 +66,7 @@ type Server struct {
6566

6667
// Location describes an NGINX location
6768
type Location struct {
69+
LocationSnippets []string
6870
Path string
6971
Upstream Upstream
7072
ProxyConnectTimeout string
@@ -85,6 +87,7 @@ type NginxMainConfig struct {
8587
ServerNamesHashMaxSize string
8688
LogFormat string
8789
HealthStatus bool
90+
HTTPSnippets []string
8891
// http://nginx.org/en/docs/http/ngx_http_ssl_module.html
8992
SSLProtocols string
9093
SSLPreferServerCiphers bool

0 commit comments

Comments
 (0)