Merge pull request #3344 from ecosia/jg-customerrors-per-ingress

Adds CustomHTTPErrors ingress annotation and test
This commit is contained in:
k8s-ci-robot 2018-11-06 09:21:49 -08:00 committed by GitHub
commit 265f96bf14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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
}