Adds CustomHTTPErrors ingress annotation and test

Adds per-server/location error-catch functionality to nginx template

Adds documentation

Reduces template duplication with helper function for CUSTOM_ERRORS data

Updates documentation

Adds e2e test for customerrors

Removes AllCustomHTTPErrors, replaces with template function with deduplication and adds e2e test of deduplication

Fixes copy-paste error in test, adds additional test cases

Reverts noop change in controller.go (unused now)
This commit is contained in:
jasongwartz 2018-10-25 18:35:48 +02:00
parent 1f76acfa6a
commit 0ebf0354cb
9 changed files with 294 additions and 11 deletions

View file

@ -151,14 +151,16 @@ var (
"serverConfig": func(all config.TemplateConfig, server *ingress.Server) interface{} {
return struct{ First, Second interface{} }{all, server}
},
"isValidClientBodyBufferSize": isValidClientBodyBufferSize,
"buildForwardedFor": buildForwardedFor,
"buildAuthSignURL": buildAuthSignURL,
"buildOpentracing": buildOpentracing,
"proxySetHeader": proxySetHeader,
"buildInfluxDB": buildInfluxDB,
"enforceRegexModifier": enforceRegexModifier,
"stripLocationModifer": stripLocationModifer,
"isValidClientBodyBufferSize": isValidClientBodyBufferSize,
"buildForwardedFor": buildForwardedFor,
"buildAuthSignURL": buildAuthSignURL,
"buildOpentracing": buildOpentracing,
"proxySetHeader": proxySetHeader,
"buildInfluxDB": buildInfluxDB,
"enforceRegexModifier": enforceRegexModifier,
"stripLocationModifer": stripLocationModifer,
"buildCustomErrorDeps": buildCustomErrorDeps,
"collectCustomErrorsPerServer": collectCustomErrorsPerServer,
}
)
@ -941,3 +943,40 @@ func proxySetHeader(loc interface{}) string {
return "proxy_set_header"
}
// buildCustomErrorDeps is a utility function returning a struct wrapper with
// the data required to build the 'CUSTOM_ERRORS' template
func buildCustomErrorDeps(proxySetHeaders map[string]string, errorCodes []int) interface{} {
return struct {
ProxySetHeaders map[string]string
ErrorCodes []int
}{
ProxySetHeaders: proxySetHeaders,
ErrorCodes: errorCodes,
}
}
// collectCustomErrorsPerServer is a utility function which will collect all
// custom error codes for all locations of a server block, deduplicates them,
// and returns a unique set (for the template to create @custom_xxx locations)
func collectCustomErrorsPerServer(input interface{}) []int {
server, ok := input.(*ingress.Server)
if !ok {
glog.Errorf("expected a '*ingress.Server' type but %T was returned", input)
return nil
}
codesMap := make(map[int]bool)
for _, loc := range server.Locations {
for _, code := range loc.CustomHTTPErrors {
codesMap[code] = true
}
}
uniqueCodes := make([]int, 0, len(codesMap))
for key := range codesMap {
uniqueCodes = append(uniqueCodes, key)
}
return uniqueCodes
}