Change errors to a list of codes

This commit is contained in:
Manuel de Brito Fontes 2016-05-23 20:15:13 -03:00
parent 5faa855e66
commit 28f982845d
4 changed files with 60 additions and 109 deletions

View file

@ -124,19 +124,11 @@ type nginxConfiguration struct {
// accessed using HTTPS.
HSTSMaxAge string `structs:"hsts-max-age,omitempty"`
// enables or disable if HTTP codes should be passed to a client or be redirected to nginx for
// processing with the error_page directive
// enables which HTTP codes should be passed for processing with the error_page directive
// http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors
// http://nginx.org/en/docs/http/ngx_http_core_module.html#error_page
// By default this is disabled
InterceptHTTP403 bool `structs:"intercept-error-403,omitempty"`
InterceptHTTP404 bool `structs:"intercept-error-404,omitempty"`
InterceptHTTP405 bool `structs:"intercept-error-405,omitempty"`
InterceptHTTP408 bool `structs:"intercept-error-408,omitempty"`
InterceptHTTP413 bool `structs:"intercept-error-413,omitempty"`
InterceptHTTP501 bool `structs:"intercept-error-502,omitempty"`
InterceptHTTP502 bool `structs:"intercept-error-502,omitempty"`
InterceptHTTP503 bool `structs:"intercept-error-503,omitempty"`
InterceptHTTP504 bool `structs:"intercept-error-504,omitempty"`
CustomHTTPErrors []int
// Time during which a keep-alive client connection will stay open on the server side.
// The zero value disables keep-alive client connections
@ -290,6 +282,7 @@ func newDefaultNginxCfg() nginxConfiguration {
WorkerProcesses: strconv.Itoa(runtime.NumCPU()),
VtsStatusZoneSize: "10m",
UseHTTP2: true,
CustomHTTPErrors: []int{},
}
if glog.V(5) {

View file

@ -56,15 +56,9 @@ func (ngx *Manager) writeCfg(cfg nginxConfiguration, ingressCfg IngressConfig) (
conf["udpUpstreams"] = ingressCfg.UDPUpstreams
conf["defResolver"] = ngx.defResolver
conf["sslDHParam"] = ngx.sslDHParam
conf["customErrors"] = len(cfg.CustomHTTPErrors) > 0
conf["cfg"] = fixKeyNames(structs.Map(cfg))
buffer := new(bytes.Buffer)
err := ngx.template.Execute(buffer, conf)
if err != nil {
glog.Infof("NGINX error: %v", err)
return false, err
}
if glog.V(3) {
b, err := json.Marshal(conf)
if err != nil {
@ -73,6 +67,13 @@ func (ngx *Manager) writeCfg(cfg nginxConfiguration, ingressCfg IngressConfig) (
glog.Infof("NGINX configuration: %v", string(b))
}
buffer := new(bytes.Buffer)
err := ngx.template.Execute(buffer, conf)
if err != nil {
glog.Infof("NGINX error: %v", err)
return false, err
}
changed, err := ngx.needsReload(buffer)
if err != nil {
return false, err

View file

@ -22,6 +22,7 @@ import (
"os"
"os/exec"
"reflect"
"strconv"
"strings"
"github.com/golang/glog"
@ -30,6 +31,10 @@ import (
"k8s.io/kubernetes/pkg/api"
)
const (
customHTTPErrors = "custom-http-errors"
)
// getDNSServers returns the list of nameservers located in the file /etc/resolv.conf
func getDNSServers() []string {
file, err := ioutil.ReadFile("/etc/resolv.conf")
@ -91,6 +96,19 @@ func (ngx *Manager) ReadConfig(config *api.ConfigMap) nginxConfiguration {
Metadata: metadata,
})
cErrors := make([]int, 0)
if val, ok := config.Data[customHTTPErrors]; ok {
delete(config.Data, customHTTPErrors)
for _, i := range strings.Split(val, ",") {
j, err := strconv.Atoi(i)
if err != nil {
glog.Warningf("%v is not a valid http code", i)
} else {
cErrors = append(cErrors, j)
}
}
}
err = decoder.Decode(config.Data)
if err != nil {
glog.Infof("%v", err)
@ -115,9 +133,23 @@ func (ngx *Manager) ReadConfig(config *api.ConfigMap) nginxConfiguration {
}
}
cfgDefault.CustomHTTPErrors = ngx.filterErrors(cErrors)
return cfgDefault
}
func (ngx *Manager) filterErrors(errCodes []int) []int {
fa := make([]int, 0)
for _, errCode := range errCodes {
if errCode > 299 && errCode < 600 {
fa = append(fa, errCode)
} else {
glog.Warningf("error code %v is not valid for custom error pages", errCode)
}
}
return fa
}
func (ngx *Manager) needsReload(data *bytes.Buffer) (bool, error) {
filename := ngx.ConfigFile
in, err := os.Open(filename)
@ -179,17 +211,3 @@ func diff(b1, b2 []byte) (data []byte, err error) {
}
return
}
func toMap(iface interface{}) (map[string]interface{}, bool) {
value := reflect.ValueOf(iface)
if value.Kind() == reflect.Map {
m := map[string]interface{}{}
for _, k := range value.MapKeys() {
m[k.String()] = value.MapIndex(k).Interface()
}
return m, true
}
return map[string]interface{}{}, false
}