Esempio n. 1
0
// NewManager ...
func NewManager(kubeClient *client.Client) *Manager {
	ngx := &Manager{
		ConfigFile:        "/etc/nginx/nginx.conf",
		defCfg:            config.NewDefault(),
		defResolver:       strings.Join(getDNSServers(), " "),
		reloadLock:        &sync.Mutex{},
		reloadRateLimiter: flowcontrol.NewTokenBucketRateLimiter(0.1, 1),
	}

	ngx.createCertsDir(config.SSLDirectory)

	ngx.sslDHParam = ngx.SearchDHParamFile(config.SSLDirectory)

	var onChange func()

	onChange = func() {
		template, err := ngx_template.NewTemplate(tmplPath, onChange)
		if err != nil {
			glog.Warningf("invalid NGINX template: %v", err)
			return
		}

		ngx.template.Close()
		ngx.template = template
		glog.Info("new NGINX template loaded")
	}

	template, err := ngx_template.NewTemplate(tmplPath, onChange)
	if err != nil {
		glog.Fatalf("invalid NGINX template: %v", err)
	}

	ngx.template = template
	return ngx
}
Esempio n. 2
0
func TestWithoutAnnotations(t *testing.T) {
	ing := buildIngress()
	_, err := ParseAnnotations(config.NewDefault(), ing)
	if err == nil {
		t.Error("Expected error with ingress without annotations")
	}
}
Esempio n. 3
0
func TestRedirect(t *testing.T) {
	ing := buildIngress()

	data := map[string]string{}
	data[rewriteTo] = defRoute
	ing.SetAnnotations(data)

	redirect, err := ParseAnnotations(config.NewDefault(), ing)
	if err != nil {
		t.Errorf("Uxpected error with ingress: %v", err)
	}

	if redirect.Target != defRoute {
		t.Errorf("Expected %v as redirect but returned %s", defRoute, redirect.Target)
	}
}
Esempio n. 4
0
// NewManager ...
func NewManager(kubeClient *client.Client) *Manager {
	ngx := &Manager{
		ConfigFile:        "/etc/nginx/nginx.conf",
		defCfg:            config.NewDefault(),
		defResolver:       strings.Join(getDNSServers(), " "),
		reloadLock:        &sync.Mutex{},
		reloadRateLimiter: flowcontrol.NewTokenBucketRateLimiter(0.1, 1),
	}

	ngx.createCertsDir(config.SSLDirectory)

	ngx.sslDHParam = ngx.SearchDHParamFile(config.SSLDirectory)

	ngx.loadTemplate()

	return ngx
}
Esempio n. 5
0
// ConfigMapAsString returns a ConfigMap with the default NGINX
// configuration to be used a guide to provide a custom configuration
func ConfigMapAsString() string {
	cfg := &api.ConfigMap{}
	cfg.Name = "custom-name"
	cfg.Namespace = "a-valid-namespace"
	cfg.Data = make(map[string]string)

	data := structs.Map(config.NewDefault())
	for k, v := range data {
		cfg.Data[k] = fmt.Sprintf("%v", v)
	}

	out, err := yaml.Marshal(cfg)
	if err != nil {
		glog.Warningf("Unexpected error creating default configuration: %v", err)
		return ""
	}

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

	cfgCM := config.Configuration{}
	cfgDefault := config.NewDefault()

	metadata := &mapstructure.Metadata{}

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

	cErrors := make([]int, 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 {
				cErrors = append(cErrors, j)
			}
		}
	}

	cSkipUrls := make([]string, 0)
	if val, ok := conf.Data[skipAccessLogUrls]; ok {
		delete(conf.Data, skipAccessLogUrls)
		cSkipUrls = strings.Split(val, ",")
	}

	err = decoder.Decode(conf.Data)
	if err != nil {
		glog.Infof("%v", err)
	}

	keyMap := getConfigKeyToStructKeyMap()

	valCM := reflect.Indirect(reflect.ValueOf(cfgCM))

	for _, key := range metadata.Keys {
		fieldName, ok := keyMap[key]
		if !ok {
			continue
		}

		valDefault := reflect.ValueOf(&cfgDefault).Elem().FieldByName(fieldName)

		fieldCM := valCM.FieldByName(fieldName)

		if valDefault.IsValid() {
			valDefault.Set(fieldCM)
		}
	}

	cfgDefault.CustomHTTPErrors = ngx.filterErrors(cErrors)
	cfgDefault.SkipAccessLogURLs = cSkipUrls
	// no custom resolver means use the system resolver
	if cfgDefault.Resolver == "" {
		cfgDefault.Resolver = ngx.defResolver
	}
	return cfgDefault
}