Skip to content

Add location-snippets, server-snippets and http-snippets for Plus #134

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

Merged
merged 1 commit into from
Apr 14, 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
29 changes: 25 additions & 4 deletions nginx-plus-controller/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,14 @@ func (lbc *LoadBalancerController) syncCfgm(key string) {
if proxyReadTimeout, exists := cfgm.Data["proxy-read-timeout"]; exists {
cfg.ProxyReadTimeout = proxyReadTimeout
}
if proxyHideHeaders, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "proxy-hide-headers", cfgm); exists {
if proxyHideHeaders, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "proxy-hide-headers", cfgm, ","); exists {
if err != nil {
glog.Error(err)
} else {
cfg.ProxyHideHeaders = proxyHideHeaders
}
}
if proxyPassHeaders, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "proxy-pass-headers", cfgm); exists {
if proxyPassHeaders, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "proxy-pass-headers", cfgm, ","); exists {
if err != nil {
glog.Error(err)
} else {
Expand All @@ -409,7 +409,7 @@ func (lbc *LoadBalancerController) syncCfgm(key string) {
cfg.HTTP2 = HTTP2
}
}
if redirectToHTTPS, exists,err := nginx.GetMapKeyAsBool(cfgm.Data, "redirect-to-https", cfgm); exists {
if redirectToHTTPS, exists, err := nginx.GetMapKeyAsBool(cfgm.Data, "redirect-to-https", cfgm); exists {
if err != nil {
glog.Error(err)
} else {
Expand Down Expand Up @@ -461,7 +461,7 @@ func (lbc *LoadBalancerController) syncCfgm(key string) {
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 setRealIPFrom, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "set-real-ip-from", cfgm, ","); exists {
if err != nil {
glog.Error(err)
} else {
Expand Down Expand Up @@ -519,6 +519,27 @@ func (lbc *LoadBalancerController) syncCfgm(key string) {
if proxyMaxTempFileSize, exists := cfgm.Data["proxy-max-temp-file-size"]; exists {
cfg.ProxyMaxTempFileSize = proxyMaxTempFileSize
}
if mainHTTPSnippets, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "http-snippets", cfgm, "\n"); exists {
if err != nil {
glog.Error(err)
} else {
cfg.MainHTTPSnippets = mainHTTPSnippets
}
}
if locationSnippets, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "location-snippets", cfgm, "\n"); exists {
if err != nil {
glog.Error(err)
} else {
cfg.LocationSnippets = locationSnippets
}
}
if serverSnippets, exists, err := nginx.GetMapKeyAsStringSlice(cfgm.Data, "server-snippets", cfgm, "\n"); exists {
if err != nil {
glog.Error(err)
} else {
cfg.ServerSnippets = serverSnippets
}
}
}
lbc.cnf.UpdateConfig(cfg)

Expand Down
3 changes: 3 additions & 0 deletions nginx-plus-controller/nginx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package nginx

// Config holds NGINX configuration parameters
type Config struct {
LocationSnippets []string
ServerSnippets []string
ServerTokens string
ProxyConnectTimeout string
ProxyReadTimeout string
ClientMaxBodySize string
HTTP2 bool
RedirectToHTTPS bool
MainHTTPSnippets []string
MainServerNamesHashBucketSize string
MainServerNamesHashMaxSize string
MainLogFormat string
Expand Down
24 changes: 21 additions & 3 deletions nginx-plus-controller/nginx/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
RealIPRecursive: ingCfg.RealIPRecursive,
ProxyHideHeaders: ingCfg.ProxyHideHeaders,
ProxyPassHeaders: ingCfg.ProxyPassHeaders,
ServerSnippets: ingCfg.ServerSnippets,
}

if pemFile, ok := pems[serverName]; ok {
Expand Down Expand Up @@ -188,6 +189,7 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
RealIPRecursive: ingCfg.RealIPRecursive,
ProxyHideHeaders: ingCfg.ProxyHideHeaders,
ProxyPassHeaders: ingCfg.ProxyPassHeaders,
ServerSnippets: ingCfg.ServerSnippets,
}

if pemFile, ok := pems[emptyHost]; ok {
Expand Down Expand Up @@ -223,20 +225,34 @@ func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
}
}
}
if serverSnippets, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/server-snippets", ingEx.Ingress, "\n"); exists {
if err != nil {
glog.Error(err)
} else {
ingCfg.ServerSnippets = serverSnippets
}
}
if locationSnippets, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/location-snippets", ingEx.Ingress, "\n"); exists {
if err != nil {
glog.Error(err)
} else {
ingCfg.LocationSnippets = locationSnippets
}
}
if proxyConnectTimeout, exists := ingEx.Ingress.Annotations["nginx.org/proxy-connect-timeout"]; exists {
ingCfg.ProxyConnectTimeout = proxyConnectTimeout
}
if proxyReadTimeout, exists := ingEx.Ingress.Annotations["nginx.org/proxy-read-timeout"]; exists {
ingCfg.ProxyReadTimeout = proxyReadTimeout
}
if proxyHideHeaders, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/proxy-hide-headers", ingEx.Ingress); exists {
if proxyHideHeaders, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/proxy-hide-headers", ingEx.Ingress, ","); exists {
if err != nil {
glog.Error(err)
} else {
ingCfg.ProxyHideHeaders = proxyHideHeaders
}
}
if proxyPassHeaders, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/proxy-pass-headers", ingEx.Ingress); exists {
if proxyPassHeaders, exists, err := GetMapKeyAsStringSlice(ingEx.Ingress.Annotations, "nginx.org/proxy-pass-headers", ingEx.Ingress, ","); exists {
if err != nil {
glog.Error(err)
} else {
Expand All @@ -253,7 +269,7 @@ func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
ingCfg.HTTP2 = HTTP2
}
}
if redirectToHTTPS, exists,err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/redirect-to-https", ingEx.Ingress); exists {
if redirectToHTTPS, exists, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/redirect-to-https", ingEx.Ingress); exists {
if err != nil {
glog.Error(err)
} else {
Expand Down Expand Up @@ -416,6 +432,7 @@ func createLocation(path string, upstream Upstream, cfg *Config, websocket bool,
ProxyBuffers: cfg.ProxyBuffers,
ProxyBufferSize: cfg.ProxyBufferSize,
ProxyMaxTempFileSize: cfg.ProxyMaxTempFileSize,
LocationSnippets: cfg.LocationSnippets,
}

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

cnf.config = config
mainCfg := &NginxMainConfig{
HTTPSnippets: config.MainHTTPSnippets,
ServerNamesHashBucketSize: config.MainServerNamesHashBucketSize,
ServerNamesHashMaxSize: config.MainServerNamesHashMaxSize,
LogFormat: config.MainLogFormat,
Expand Down
6 changes: 3 additions & 3 deletions nginx-plus-controller/nginx/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ 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) {
// GetMapKeyAsStringSlice tries to find and parse a key in the map as string slice splitting it on the delimiter
func GetMapKeyAsStringSlice(m map[string]string, key string, context apiObject, delimiter string) ([]string, bool, error) {
if str, exists := m[key]; exists {
slice := strings.Split(str, ",")
slice := strings.Split(str, delimiter)
return slice, exists, nil
}
return nil, false, nil
Expand Down
26 changes: 24 additions & 2 deletions nginx-plus-controller/nginx/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestGetMapKeyAsStringSlice(t *testing.T) {
"key": "1.String,2.String,3.String",
}

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

func TestGetMapKeyAsStringSliceMultilineSnippets(t *testing.T) {
configMap := configMap
configMap.Data = map[string]string{
"server-snippets": `
if ($new_uri) {
rewrite ^ $new_uri permanent;
}`,
}
slice, exists, err := GetMapKeyAsStringSlice(configMap.Data, "server-snippets", &configMap, "\n")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !exists {
t.Errorf("The key 'server-snippets' must exist in the configMap")
}
expected := []string{"", "\t\t\tif ($new_uri) {", "\t\t\t\trewrite ^ $new_uri permanent;", "\t\t\t}"}
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)
_, exists, _ := GetMapKeyAsStringSlice(configMap.Data, "key", &configMap, ",")
if exists {
t.Errorf("The key 'key' must not exist in the configMap")
}
Expand Down
11 changes: 11 additions & 0 deletions nginx-plus-controller/nginx/ingress.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,24 @@ server {
}
{{- end}}

{{- if $server.ServerSnippets}}
{{range $value := $server.ServerSnippets}}
{{$value}}{{end}}
{{- end}}

{{range $location := $server.Locations}}
location {{$location.Path}} {
proxy_http_version 1.1;
{{if $location.Websocket}}
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
{{end}}

{{- if $location.LocationSnippets}}
{{range $value := $location.LocationSnippets}}
{{$value}}{{end}}
{{- end}}

proxy_connect_timeout {{$location.ProxyConnectTimeout}};
proxy_read_timeout {{$location.ProxyReadTimeout}};
client_max_body_size {{$location.ClientMaxBodySize}};
Expand Down
5 changes: 5 additions & 0 deletions nginx-plus-controller/nginx/nginx.conf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

{{- if .HTTPSnippets}}
{{range $value := .HTTPSnippets}}
{{$value}}{{end}}
{{- end}}

{{if .LogFormat -}}
log_format main '{{.LogFormat}}';
{{- else -}}
Expand Down
3 changes: 3 additions & 0 deletions nginx-plus-controller/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type UpstreamServer struct {

// Server describes an NGINX server
type Server struct {
ServerSnippets []string
Name string
ServerTokens string
Locations []Location
Expand All @@ -65,6 +66,7 @@ type Server struct {

// Location describes an NGINX location
type Location struct {
LocationSnippets []string
Path string
Upstream Upstream
ProxyConnectTimeout string
Expand All @@ -85,6 +87,7 @@ type NginxMainConfig struct {
ServerNamesHashMaxSize string
LogFormat string
HealthStatus bool
HTTPSnippets []string
// http://nginx.org/en/docs/http/ngx_http_ssl_module.html
SSLProtocols string
SSLPreferServerCiphers bool
Expand Down