From 33af01d4d5f5b5916c3d5c49140bd13b61def004 Mon Sep 17 00:00:00 2001 From: literalice Date: Mon, 22 May 2017 05:42:59 +0900 Subject: [PATCH] adds a regexp location path option on configmap --- controllers/nginx/configuration.md | 3 +++ controllers/nginx/pkg/config/config.go | 3 +++ .../nginx/pkg/template/configmap_test.go | 2 ++ controllers/nginx/pkg/template/template.go | 15 ++++++++++++++ .../nginx/pkg/template/template_test.go | 20 +++++++++++++++++++ .../rootfs/etc/nginx/template/nginx.tmpl | 3 ++- 6 files changed, 45 insertions(+), 1 deletion(-) diff --git a/controllers/nginx/configuration.md b/controllers/nginx/configuration.md index f146ef5da..16497c107 100644 --- a/controllers/nginx/configuration.md +++ b/controllers/nginx/configuration.md @@ -425,6 +425,9 @@ The default mime type list to compress is: `application/atom+xml application/jav **worker-processes:** Sets the number of [worker processes](http://nginx.org/en/docs/ngx_core_module.html#worker_processes). The default of "auto" means number of available CPU cores. +**use-regexp-location-path:** Handles a path in Ingress pathes rules as a regexp location directive. +The default is: `false`. + ### Default configuration options The following table shows the options, the default value and a description. diff --git a/controllers/nginx/pkg/config/config.go b/controllers/nginx/pkg/config/config.go index 20169c602..b06a15214 100644 --- a/controllers/nginx/pkg/config/config.go +++ b/controllers/nginx/pkg/config/config.go @@ -275,6 +275,8 @@ type Configuration struct { // Defines the load balancing algorithm to use. The deault is round-robin LoadBalanceAlgorithm string `json:"load-balance,omitempty"` + + UseRegexpLocationPath bool `json:"use-regexp-location-path,omitempty"` } // NewDefault returns the default nginx configuration @@ -328,6 +330,7 @@ func NewDefault() Configuration { WhitelistSourceRange: []string{}, SkipAccessLogURLs: []string{}, }, + UseRegexpLocationPath: false, } if glog.V(5) { diff --git a/controllers/nginx/pkg/template/configmap_test.go b/controllers/nginx/pkg/template/configmap_test.go index 130a452a6..9d3627785 100644 --- a/controllers/nginx/pkg/template/configmap_test.go +++ b/controllers/nginx/pkg/template/configmap_test.go @@ -42,6 +42,7 @@ func TestMergeConfigMapToStruct(t *testing.T) { "use-gzip": "true", "enable-dynamic-tls-records": "false", "gzip-types": "text/html", + "use-regexp-location-path": "false", } def := config.NewDefault() def.CustomHTTPErrors = []int{300, 400} @@ -52,6 +53,7 @@ func TestMergeConfigMapToStruct(t *testing.T) { def.EnableDynamicTLSRecords = false def.UseProxyProtocol = true def.GzipTypes = "text/html" + def.UseRegexpLocationPath = false to := ReadConfig(conf) if diff := pretty.Compare(to, def); diff != "" { diff --git a/controllers/nginx/pkg/template/template.go b/controllers/nginx/pkg/template/template.go index dea4ed310..9c0efc354 100644 --- a/controllers/nginx/pkg/template/template.go +++ b/controllers/nginx/pkg/template/template.go @@ -129,6 +129,7 @@ var ( }, "buildLocation": buildLocation, "buildAuthLocation": buildAuthLocation, + "withLocationPathType": withLocationPathType, "buildAuthResponseHeaders": buildAuthResponseHeaders, "buildProxyPass": buildProxyPass, "buildRateLimitZones": buildRateLimitZones, @@ -191,6 +192,20 @@ func buildLocation(input interface{}) string { return path } +func withLocationPathType(input interface{}, cfgInput interface{}) string { + cfg, ok := cfgInput.(config.Configuration) + if !ok { + glog.Errorf("error an ingress.withLocationPathType type but %T was returned", cfgInput) + } + path := input.(string) + + if cfg.UseRegexpLocationPath && strings.HasPrefix(path, "/") { + return fmt.Sprintf("~ ^%s", path) + } else { + return path + } +} + func buildAuthLocation(input interface{}) string { location, ok := input.(*ingress.Location) if !ok { diff --git a/controllers/nginx/pkg/template/template_test.go b/controllers/nginx/pkg/template/template_test.go index 7dfe83ed4..40b2537dd 100644 --- a/controllers/nginx/pkg/template/template_test.go +++ b/controllers/nginx/pkg/template/template_test.go @@ -102,6 +102,26 @@ func TestBuildLocation(t *testing.T) { } } +func TestWithLocationPathType(t *testing.T) { + conf := config.NewDefault() + + path0 := "/foo" + if "/foo" != withLocationPathType(path0, conf) { + t.Errorf("%s: expected not to be regexp path", path0) + } + + conf.UseRegexpLocationPath = true + path1 := "/foo" + if "~ ^/foo" != withLocationPathType(path1, conf) { + t.Errorf("%s: expected to be regexp path", path1) + } + + path2 := "~* /foo" + if "~* /foo" != withLocationPathType(path2, conf) { + t.Errorf("%s: expected to be regexp path", path2) + } +} + func TestBuildProxyPass(t *testing.T) { for k, tc := range tmplFuncTestcases { loc := &ingress.Location{ diff --git a/controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl b/controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl index 88e40c291..0fc4a2cd0 100644 --- a/controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl +++ b/controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl @@ -280,7 +280,8 @@ http { {{ if $cfg.EnableVtsStatus }}vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name;{{ end }} {{ range $location := $server.Locations }} - {{ $path := buildLocation $location }} + {{ $builtPath := buildLocation $location }} + {{ $path := withLocationPathType $builtPath $cfg }} {{ $authPath := buildAuthLocation $location }} {{ if not (empty $location.CertificateAuth.AuthSSLCert.CAFileName) }}