Skip to content

Commit ea43f60

Browse files
authored
Merge pull request #82 from nginxinc/fix-parsing
Fix configmaps and annotation parsing
2 parents 69a005a + d6b3cf6 commit ea43f60

File tree

4 files changed

+108
-91
lines changed

4 files changed

+108
-91
lines changed

nginx-controller/controller/controller.go

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -324,46 +324,55 @@ func (lbc *LoadBalancerController) syncCfgm(key string) {
324324
if serverNamesHashMaxSize, exists := cfgm.Data["server-names-hash-max-size"]; exists {
325325
cfg.MainServerNamesHashMaxSize = serverNamesHashMaxSize
326326
}
327-
HTTP2, err := nginx.GetMapKeyAsBool(cfgm.Data, "http2", cfgm)
328-
if err != nil && err != nginx.ErrorKeyNotFound {
329-
glog.Error(err)
330-
} else {
331-
cfg.HTTP2 = HTTP2
327+
if HTTP2, exists, err := nginx.GetMapKeyAsBool(cfgm.Data, "http2", cfgm); exists {
328+
if err != nil {
329+
glog.Error(err)
330+
} else {
331+
cfg.HTTP2 = HTTP2
332+
}
332333
}
333334

334335
// HSTS block
335-
HSTSErrors := false
336-
HSTS, err := nginx.GetMapKeyAsBool(cfgm.Data, "hsts", cfgm)
337-
if err != nil && err != nginx.ErrorKeyNotFound {
338-
glog.Error(err)
339-
HSTSErrors = true
340-
}
341-
HSTSMaxAge, err := nginx.GetMapKeyAsInt(cfgm.Data, "hsts-max-age", cfgm)
342-
if err != nil && err != nginx.ErrorKeyNotFound {
343-
glog.Error(err)
344-
HSTSErrors = true
345-
}
346-
HSTSIncludeSubdomains, err := nginx.GetMapKeyAsBool(cfgm.Data, "hsts-include-subdomains", cfgm)
347-
if err != nil && err != nginx.ErrorKeyNotFound {
348-
glog.Error(err)
349-
HSTSErrors = true
350-
}
351-
if HSTSErrors {
352-
glog.Warningf("Configmap %s/%s: There are configuration issues with hsts annotations, skipping options for all hsts settings", cfgm.GetNamespace(), cfgm.GetName())
353-
} else {
354-
cfg.HSTS = HSTS
355-
cfg.HSTSMaxAge = HSTSMaxAge
356-
cfg.HSTSIncludeSubdomains = HSTSIncludeSubdomains
336+
if hsts, exists, err := nginx.GetMapKeyAsBool(cfgm.Data, "hsts", cfgm); exists {
337+
if err != nil {
338+
glog.Error(err)
339+
} else {
340+
parsingErrors := false
341+
342+
hstsMaxAge, existsMA, err := nginx.GetMapKeyAsInt(cfgm.Data, "hsts-max-age", cfgm)
343+
if existsMA && err != nil {
344+
glog.Error(err)
345+
parsingErrors = true
346+
}
347+
hstsIncludeSubdomains, existsIS, err := nginx.GetMapKeyAsBool(cfgm.Data, "hsts-include-subdomains", cfgm)
348+
if existsIS && err != nil {
349+
glog.Error(err)
350+
parsingErrors = true
351+
}
352+
353+
if parsingErrors {
354+
glog.Errorf("Configmap %s/%s: There are configuration issues with hsts annotations, skipping options for all hsts settings", cfgm.GetNamespace(), cfgm.GetName())
355+
} else {
356+
cfg.HSTS = hsts
357+
if existsMA {
358+
cfg.HSTSMaxAge = hstsMaxAge
359+
}
360+
if existsIS {
361+
cfg.HSTSIncludeSubdomains = hstsIncludeSubdomains
362+
}
363+
}
364+
}
357365
}
358366

359367
if logFormat, exists := cfgm.Data["log-format"]; exists {
360368
cfg.MainLogFormat = logFormat
361369
}
362-
ProxyBuffering, err := nginx.GetMapKeyAsBool(cfgm.Data, "proxy-buffering", cfgm)
363-
if err != nil && err != nginx.ErrorKeyNotFound {
364-
glog.Error(err)
365-
} else {
366-
cfg.ProxyBuffering = ProxyBuffering
370+
if proxyBuffering, exists, err := nginx.GetMapKeyAsBool(cfgm.Data, "proxy-buffering", cfgm); exists {
371+
if err != nil {
372+
glog.Error(err)
373+
} else {
374+
cfg.ProxyBuffering = proxyBuffering
375+
}
367376
}
368377
if proxyBuffers, exists := cfgm.Data["proxy-buffers"]; exists {
369378
cfg.ProxyBuffers = proxyBuffers

nginx-controller/nginx/configurator.go

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -185,42 +185,50 @@ func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
185185
if clientMaxBodySize, exists := ingEx.Ingress.Annotations["nginx.org/client-max-body-size"]; exists {
186186
ingCfg.ClientMaxBodySize = clientMaxBodySize
187187
}
188-
HTTP2, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/http2", ingEx.Ingress)
189-
if err != nil && err != ErrorKeyNotFound {
190-
glog.Error(err)
191-
} else {
192-
ingCfg.HTTP2 = HTTP2
188+
if HTTP2, exists, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/http2", ingEx.Ingress); exists {
189+
if err != nil {
190+
glog.Error(err)
191+
} else {
192+
ingCfg.HTTP2 = HTTP2
193+
}
193194
}
194-
ProxyBuffering, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/proxy-buffering", ingEx.Ingress)
195-
if err != nil && err != ErrorKeyNotFound {
196-
glog.Error(err)
197-
} else {
198-
ingCfg.ProxyBuffering = ProxyBuffering
195+
if proxyBuffering, exists, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/proxy-buffering", ingEx.Ingress); exists {
196+
if err != nil {
197+
glog.Error(err)
198+
} else {
199+
ingCfg.ProxyBuffering = proxyBuffering
200+
}
199201
}
200202

201-
// HSTS block
202-
HSTSErrors := false
203-
HSTS, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/hsts", ingEx.Ingress)
204-
if err != nil && err != ErrorKeyNotFound {
205-
glog.Error(err)
206-
HSTSErrors = true
207-
}
208-
HSTSMaxAge, err := GetMapKeyAsInt(ingEx.Ingress.Annotations, "nginx.org/hsts-max-age", ingEx.Ingress)
209-
if err != nil && err != ErrorKeyNotFound {
210-
glog.Error(err)
211-
HSTSErrors = true
212-
}
213-
HSTSIncludeSubdomains, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/hsts-include-subdomains", ingEx.Ingress)
214-
if err != nil && err != ErrorKeyNotFound {
215-
glog.Error(err)
216-
HSTSErrors = true
217-
}
218-
if HSTSErrors {
219-
glog.Warningf("Ingress %s/%s: There are configuration issues with hsts annotations, skipping annotions for all hsts settings", ingEx.Ingress.GetNamespace(), ingEx.Ingress.GetName())
220-
} else {
221-
ingCfg.HSTS = HSTS
222-
ingCfg.HSTSMaxAge = HSTSMaxAge
223-
ingCfg.HSTSIncludeSubdomains = HSTSIncludeSubdomains
203+
if hsts, exists, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/hsts", ingEx.Ingress); exists {
204+
if err != nil {
205+
glog.Error(err)
206+
} else {
207+
parsingErrors := false
208+
209+
hstsMaxAge, existsMA, err := GetMapKeyAsInt(ingEx.Ingress.Annotations, "nginx.org/hsts-max-age", ingEx.Ingress)
210+
if existsMA && err != nil {
211+
glog.Error(err)
212+
parsingErrors = true
213+
}
214+
hstsIncludeSubdomains, existsIS, err := GetMapKeyAsBool(ingEx.Ingress.Annotations, "nginx.org/hsts-include-subdomains", ingEx.Ingress)
215+
if existsIS && err != nil {
216+
glog.Error(err)
217+
parsingErrors = true
218+
}
219+
220+
if parsingErrors {
221+
glog.Errorf("Ingress %s/%s: There are configuration issues with hsts annotations, skipping annotions for all hsts settings", ingEx.Ingress.GetNamespace(), ingEx.Ingress.GetName())
222+
} else {
223+
ingCfg.HSTS = hsts
224+
if existsMA {
225+
ingCfg.HSTSMaxAge = hstsMaxAge
226+
}
227+
if existsIS {
228+
ingCfg.HSTSIncludeSubdomains = hstsIncludeSubdomains
229+
}
230+
}
231+
}
224232
}
225233

226234
if proxyBuffers, exists := ingEx.Ingress.Annotations["nginx.org/proxy-buffers"]; exists {

nginx-controller/nginx/convert.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
package nginx
22

33
import (
4-
"errors"
54
"fmt"
65
"strconv"
76

87
"k8s.io/kubernetes/pkg/api/meta"
98
"k8s.io/kubernetes/pkg/runtime"
109
)
1110

12-
var (
13-
// ErrorKeyNotFound is returned if the key was not found in the map
14-
ErrorKeyNotFound = errors.New("Key not found in map")
15-
)
16-
1711
// There seems to be no composite interface in the kubernetes api package,
1812
// so we have to declare our own.
1913
type apiObject interface {
@@ -22,25 +16,25 @@ type apiObject interface {
2216
}
2317

2418
// GetMapKeyAsBool searches the map for the given key and parses the key as bool
25-
func GetMapKeyAsBool(m map[string]string, key string, context apiObject) (bool, error) {
19+
func GetMapKeyAsBool(m map[string]string, key string, context apiObject) (bool, bool, error) {
2620
if str, exists := m[key]; exists {
2721
b, err := strconv.ParseBool(str)
2822
if err != nil {
29-
return false, fmt.Errorf("%s %v/%v '%s' contains invalid bool: %v, ignoring", context.GetObjectKind().GroupVersionKind().Kind, context.GetNamespace(), context.GetName(), key, err)
23+
return false, exists, fmt.Errorf("%s %v/%v '%s' contains invalid bool: %v, ignoring", context.GetObjectKind().GroupVersionKind().Kind, context.GetNamespace(), context.GetName(), key, err)
3024
}
31-
return b, nil
25+
return b, exists, nil
3226
}
33-
return false, ErrorKeyNotFound
27+
return false, false, nil
3428
}
3529

3630
// GetMapKeyAsInt tries to find and parse a key in a map as int64
37-
func GetMapKeyAsInt(m map[string]string, key string, context apiObject) (int64, error) {
31+
func GetMapKeyAsInt(m map[string]string, key string, context apiObject) (int64, bool, error) {
3832
if str, exists := m[key]; exists {
3933
i, err := strconv.ParseInt(str, 10, 64)
4034
if err != nil {
41-
return 0, fmt.Errorf("%s %v/%v '%s' contains invalid integer: %v, ignoring", context.GetObjectKind().GroupVersionKind().Kind, context.GetNamespace(), context.GetName(), key, err)
35+
return 0, exists, fmt.Errorf("%s %v/%v '%s' contains invalid integer: %v, ignoring", context.GetObjectKind().GroupVersionKind().Kind, context.GetNamespace(), context.GetName(), key, err)
4236
}
43-
return i, nil
37+
return i, exists, nil
4438
}
45-
return 0, ErrorKeyNotFound
39+
return 0, false, nil
4640
}

nginx-controller/nginx/convert_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ func TestGetMapKeyAsBool(t *testing.T) {
3838
"key": "True",
3939
}
4040

41-
b, err := GetMapKeyAsBool(configMap.Data, "key", &configMap)
41+
b, exists, err := GetMapKeyAsBool(configMap.Data, "key", &configMap)
42+
if !exists {
43+
t.Errorf("The key 'key' must exist in the configMap")
44+
}
4245
if err != nil {
4346
t.Errorf("Unexpected error: %v", err)
4447
}
@@ -51,9 +54,9 @@ func TestGetMapKeyAsBoolNotFound(t *testing.T) {
5154
configMap := configMap
5255
configMap.Data = map[string]string{}
5356

54-
_, err := GetMapKeyAsBool(configMap.Data, "key", &configMap)
55-
if err != ErrorKeyNotFound {
56-
t.Errorf("ErrorKeyNotFound was expected, got: %v", err)
57+
_, exists, _ := GetMapKeyAsBool(configMap.Data, "key", &configMap)
58+
if exists {
59+
t.Errorf("The key 'key' must not exist in the configMap")
5760
}
5861
}
5962

@@ -64,7 +67,7 @@ func TestGetMapKeyAsBoolErrorMessage(t *testing.T) {
6467
}
6568

6669
// Test with configmap
67-
_, err := GetMapKeyAsBool(cfgm.Data, "key", &cfgm)
70+
_, _, err := GetMapKeyAsBool(cfgm.Data, "key", &cfgm)
6871
if err == nil {
6972
t.Error("An error was expected")
7073
}
@@ -78,7 +81,7 @@ func TestGetMapKeyAsBoolErrorMessage(t *testing.T) {
7881
ingress.Annotations = map[string]string{
7982
"key": "other_string",
8083
}
81-
_, err = GetMapKeyAsBool(ingress.Annotations, "key", &ingress)
84+
_, _, err = GetMapKeyAsBool(ingress.Annotations, "key", &ingress)
8285
if err == nil {
8386
t.Error("An error was expected")
8487
}
@@ -97,10 +100,13 @@ func TestGetMapKeyAsInt(t *testing.T) {
97100
"key": "123456789",
98101
}
99102

100-
i, err := GetMapKeyAsInt(configMap.Data, "key", &configMap)
103+
i, exists, err := GetMapKeyAsInt(configMap.Data, "key", &configMap)
101104
if err != nil {
102105
t.Errorf("Unexpected error: %v", err)
103106
}
107+
if !exists {
108+
t.Errorf("The key 'key' must exist in the configMap")
109+
}
104110
var expected int64 = 123456789
105111
if i != expected {
106112
t.Errorf("Unexpected return value:\nGot: %v\nExpected: %v", i, expected)
@@ -111,9 +117,9 @@ func TestGetMapKeyAsIntNotFound(t *testing.T) {
111117
configMap := configMap
112118
configMap.Data = map[string]string{}
113119

114-
_, err := GetMapKeyAsInt(configMap.Data, "key", &configMap)
115-
if err != ErrorKeyNotFound {
116-
t.Errorf("ErrorKeyNotFound was expected, got: %v", err)
120+
_, exists, _ := GetMapKeyAsInt(configMap.Data, "key", &configMap)
121+
if exists {
122+
t.Errorf("The key 'key' must not exist in the configMap")
117123
}
118124
}
119125

@@ -124,7 +130,7 @@ func TestGetMapKeyAsIntErrorMessage(t *testing.T) {
124130
}
125131

126132
// Test with configmap
127-
_, err := GetMapKeyAsInt(cfgm.Data, "key", &cfgm)
133+
_, _, err := GetMapKeyAsInt(cfgm.Data, "key", &cfgm)
128134
if err == nil {
129135
t.Error("An error was expected")
130136
}
@@ -138,7 +144,7 @@ func TestGetMapKeyAsIntErrorMessage(t *testing.T) {
138144
ingress.Annotations = map[string]string{
139145
"key": "other_string",
140146
}
141-
_, err = GetMapKeyAsInt(ingress.Annotations, "key", &ingress)
147+
_, _, err = GetMapKeyAsInt(ingress.Annotations, "key", &ingress)
142148
if err == nil {
143149
t.Error("An error was expected")
144150
}

0 commit comments

Comments
 (0)