Skip to content

Commit

Permalink
Add configmap keys for Hash Related variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Raul Marrero authored and Rulox committed May 15, 2019
1 parent 7ddf921 commit c2e74c9
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 20 deletions.
3 changes: 2 additions & 1 deletion docs/configmap-and-annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ spec:
| N/A | `resolver-timeout` | Sets the [resolver_timeout](http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver_timeout) for name resolution. Supported in NGINX Plus only. | `30s` | [Support for Type ExternalName Services](../examples/externalname-services). |
| N/A | `keepalive-timeout` | Sets the value of the [keepalive_timeout](http://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_timeout) directive. | `65s` | |
| N/A | `keepalive-requests` | Sets the value of the [keepalive_requests](http://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_requests) directive. | `100` | |

| N/A | `variables-hash-bucket-size` | Sets the value of the [variables_hash_bucket_size](http://nginx.org/en/docs/http/ngx_http_core_module.html#variables_hash_bucket_size) directive. | `256` | |
| N/A | `variables-hash-max-size` | Sets the value of the [variables-hash-max-size](http://nginx.org/en/docs/http/ngx_http_core_module.html#variables_hash_max_size) directive. | `1024` | |

### Logging

Expand Down
4 changes: 4 additions & 0 deletions internal/configs/config_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type ConfigParams struct {
ResolverTimeout string
MainKeepaliveTimeout string
MainKeepaliveRequests int64
VariablesHashBucketSize uint64
VariablesHashMaxSize uint64

RealIPHeader string
SetRealIPFrom []string
Expand Down Expand Up @@ -105,5 +107,7 @@ func NewDefaultConfigParams() *ConfigParams {
ResolverIPV6: true,
MainKeepaliveTimeout: "65s",
MainKeepaliveRequests: 100,
VariablesHashBucketSize: 256,
VariablesHashMaxSize: 1024,
}
}
20 changes: 19 additions & 1 deletion internal/configs/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/golang/glog"
"github.com/nginxinc/kubernetes-ingress/internal/configs/version1"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
)

// ParseConfigMap parses ConfigMap into ConfigParams.
Expand Down Expand Up @@ -375,6 +375,22 @@ func ParseConfigMap(cfgm *v1.ConfigMap, nginxPlus bool) *ConfigParams {
}
}

if varHashBucketSize, exists, err := GetMapKeyAsUint64(cfgm.Data, "variables-hash-bucket-size", cfgm, true); exists {
if err != nil {
glog.Error(err)
} else {
cfgParams.VariablesHashBucketSize = varHashBucketSize
}
}

if varHashMaxSize, exists, err := GetMapKeyAsUint64(cfgm.Data, "variables-hash-max-size", cfgm, false); exists {
if err != nil {
glog.Error(err)
} else {
cfgParams.VariablesHashMaxSize = varHashMaxSize
}
}

return cfgParams
}

Expand Down Expand Up @@ -413,6 +429,8 @@ func GenerateNginxMainConfig(staticCfgParams *StaticConfigParams, config *Config
ResolverTimeout: config.ResolverTimeout,
KeepaliveTimeout: config.MainKeepaliveTimeout,
KeepaliveRequests: config.MainKeepaliveRequests,
VariablesHashBucketSize: config.VariablesHashBucketSize,
VariablesHashMaxSize: config.VariablesHashMaxSize,
}
return nginxCfg
}
20 changes: 19 additions & 1 deletion internal/configs/parsing_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strconv"
"strings"

"k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

Expand Down Expand Up @@ -58,6 +58,24 @@ func GetMapKeyAsInt64(m map[string]string, key string, context apiObject) (int64
return 0, false, nil
}

// GetMapKeyAsUint64 tries to find and parse a key in a map as uint64.
func GetMapKeyAsUint64(m map[string]string, key string, context apiObject, nonZero bool) (uint64, bool, error) {
if str, exists := m[key]; exists {
i, err := strconv.ParseUint(str, 10, 64)
if err != nil {
return 0, exists, fmt.Errorf("%s %v/%v '%s' contains invalid uint64: %v, ignoring", context.GetObjectKind().GroupVersionKind().Kind, context.GetNamespace(), context.GetName(), key, err)
}

if nonZero && i == 0 {
return 0, exists, fmt.Errorf("%s %v/%v '%s' must be greater than 0, ignoring", context.GetObjectKind().GroupVersionKind().Kind, context.GetNamespace(), context.GetName(), key)
}

return i, exists, nil
}

return 0, false, nil
}

// GetMapKeyAsStringSlice tries to find and parse a key in the map as string slice splitting it on delimiter.
func GetMapKeyAsStringSlice(m map[string]string, key string, context apiObject, delimiter string) ([]string, bool, error) {
if str, exists := m[key]; exists {
Expand Down
2 changes: 2 additions & 0 deletions internal/configs/version1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ type MainConfig struct {
ResolverTimeout string
KeepaliveTimeout string
KeepaliveRequests int64
VariablesHashBucketSize uint64
VariablesHashMaxSize uint64
}

// NewUpstreamWithDefaultServer creates an upstream with the default server.
Expand Down
3 changes: 2 additions & 1 deletion internal/configs/version1/nginx-plus.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ http {
server_names_hash_max_size {{.ServerNamesHashMaxSize}};
{{if .ServerNamesHashBucketSize}}server_names_hash_bucket_size {{.ServerNamesHashBucketSize}};{{end}}

variables_hash_bucket_size 256;
variables_hash_bucket_size {{.VariablesHashBucketSize}};
variables_hash_max_size {{.VariablesHashMaxSize}};

map $http_upgrade $connection_upgrade {
default upgrade;
Expand Down
3 changes: 2 additions & 1 deletion internal/configs/version1/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ http {
server_names_hash_max_size {{.ServerNamesHashMaxSize}};
{{if .ServerNamesHashBucketSize}}server_names_hash_bucket_size {{.ServerNamesHashBucketSize}};{{end}}

variables_hash_bucket_size 256;
variables_hash_bucket_size {{.VariablesHashBucketSize}};
variables_hash_max_size {{.VariablesHashMaxSize}};

map $http_upgrade $connection_upgrade {
default upgrade;
Expand Down
32 changes: 17 additions & 15 deletions internal/configs/version1/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,23 @@ var ingCfg = IngressNginxConfig{
}

var mainCfg = MainConfig{
ServerNamesHashMaxSize: "512",
ServerTokens: "off",
WorkerProcesses: "auto",
WorkerCPUAffinity: "auto",
WorkerShutdownTimeout: "1m",
WorkerConnections: "1024",
WorkerRlimitNofile: "65536",
StreamSnippets: []string{"# comment"},
StreamLogFormat: "$remote_addr",
ResolverAddresses: []string{"example.com", "127.0.0.1"},
ResolverIPV6: false,
ResolverValid: "10s",
ResolverTimeout: "15s",
KeepaliveTimeout: "65s",
KeepaliveRequests: 100,
ServerNamesHashMaxSize: "512",
ServerTokens: "off",
WorkerProcesses: "auto",
WorkerCPUAffinity: "auto",
WorkerShutdownTimeout: "1m",
WorkerConnections: "1024",
WorkerRlimitNofile: "65536",
StreamSnippets: []string{"# comment"},
StreamLogFormat: "$remote_addr",
ResolverAddresses: []string{"example.com", "127.0.0.1"},
ResolverIPV6: false,
ResolverValid: "10s",
ResolverTimeout: "15s",
KeepaliveTimeout: "65s",
KeepaliveRequests: 100,
VariablesHashBucketSize: 256,
VariablesHashMaxSize: 1024,
}

func TestIngressForNGINXPlus(t *testing.T) {
Expand Down

0 comments on commit c2e74c9

Please sign in to comment.