Esempio n. 1
0
// ReadConfig obtains the configuration defined by the user merged with the defaults.
func ReadConfig(conf *api.ConfigMap) config.Configuration {
	if len(conf.Data) == 0 {
		return config.NewDefault()
	}

	errors := make([]int, 0)
	skipUrls := make([]string, 0)
	whitelist := make([]string, 0)

	if val, ok := conf.Data[customHTTPErrors]; ok {
		delete(conf.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: %v", i, err)
			} else {
				errors = append(errors, j)
			}
		}
	}
	if val, ok := conf.Data[skipAccessLogUrls]; ok {
		delete(conf.Data, skipAccessLogUrls)
		skipUrls = strings.Split(val, ",")
	}
	if val, ok := conf.Data[whitelistSourceRange]; ok {
		delete(conf.Data, whitelistSourceRange)
		whitelist = append(whitelist, strings.Split(val, ",")...)
	}

	to := config.Configuration{}
	to.Backend = defaults.Backend{
		CustomHTTPErrors:     filterErrors(errors),
		SkipAccessLogURLs:    skipUrls,
		WhitelistSourceRange: whitelist,
	}
	def := config.NewDefault()
	if err := mergo.Merge(&to, def); err != nil {
		glog.Warningf("unexpected error merging defaults: %v", err)
	}

	metadata := &mapstructure.Metadata{}
	decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
		TagName:          "structs",
		Result:           &to,
		WeaklyTypedInput: true,
		Metadata:         metadata,
	})

	err = decoder.Decode(conf.Data)
	if err != nil {
		glog.Infof("%v", err)
	}
	return to
}
Esempio n. 2
0
func TestMergeConfigMapToStruct(t *testing.T) {
	/*conf := &api.ConfigMap{
		Data: map[string]string{
			"custom-http-errors":   "300,400",
			"proxy-read-timeout":   "1",
			"proxy-send-timeout":   "2",
			"skip-access-log-urls": "/log",
		},
	}*/
	def := config.NewDefault()
	def.CustomHTTPErrors = []int{300, 400}
	def.SkipAccessLogURLs = []string{"/log"}
	//to := ReadConfig(conf)
	//if !reflect.DeepEqual(def, to) {
	//	t.Errorf("expected %v but retuned %v", def, to)
	//}
}
Esempio n. 3
0
// BackendDefaults returns the nginx defaults
func (n NGINXController) BackendDefaults() defaults.Backend {
	d := config.NewDefault()
	return d.Backend
}