Exemplo n.º 1
0
func (v *vulcan) NewMiddlewares(rsc *kubernetes.Resource) ([]loadbalancer.Middleware, error) {
	mids := make([]loadbalancer.Middleware, 0, 1)
	for key, def := range DefaultMiddleware {
		if val, ok := rsc.GetAnnotation(key); ok && len(val) > 0 {
			switch key {
			case RedirectSSLID:
				if b, er := strconv.ParseBool(val); er != nil || !b {
					continue
				}
			case TraceID:
				re := regexp.MustCompile(`\s+`)
				list, er := json.Marshal(strings.Split(re.ReplaceAllString(val, ""), ","))
				if er != nil || string(list) == "" {
					logger.Warnf("Unable to json-ify trace headers: %v", er)
					list = []byte("[]")
				}
				def = fmt.Sprintf(def, string(list), string(list))
			case AuthID:
				bits := strings.SplitN(val, ":", 2)
				switch len(bits) {
				case 1:
					def = fmt.Sprintf(def, bits[0], "")
				case 2:
					def = fmt.Sprintf(def, bits[0], bits[1])
				default:
					logger.Errorf("Failed to parse provided basic auth, using default (admin:admin)")
					def = fmt.Sprintf(def, "admin", "admin")
				}
			case MaintenanceID:
				def = fmt.Sprintf(def, val)
			}

			m, er := engine.MiddlewareFromJSON([]byte(def), v.Registry.GetSpec, key)
			if er != nil {
				logger.Warnf("Failed to parse Middleware %s: %v", key, er)
				logger.Debugf("%q", def)
				continue
			}
			mids = append(mids, newMiddleware(m))
		}
	}

	rg := regexp.MustCompile(CustomMiddlewareKeyPattern)
	matches, _ := rsc.GetAnnotations(CustomMiddlewareKeyPattern)
	for key, val := range matches {
		if match := rg.FindStringSubmatch(key); match != nil {
			id := match[1]
			m, er := engine.MiddlewareFromJSON([]byte(val), v.Registry.GetSpec, id)
			if er != nil {
				logger.Warnf("Failed to parse Middleware %s: %v", id, er)
				continue
			}
			mids = append(mids, newMiddleware(m))
		}
	}

	return mids, nil
}
Exemplo n.º 2
0
func (t *traefik) NewFrontend(rsc *kubernetes.Resource) (loadbalancer.Frontend, error) {
	f := types.Frontend{Backend: rsc.ID(), PassHostHeader: false}
	f.Routes = NewRoute(rsc.Route)
	if phh, ok := rsc.GetAnnotation(loadbalancer.PassHostHeaderKey); ok {
		if val, er := strconv.ParseBool(phh); er == nil {
			f.PassHostHeader = val
		}
	}

	return &frontend{Frontend: f, id: rsc.ID(), middlewares: make([]*middleware, 0, 1)}, nil
}
Exemplo n.º 3
0
func (v *vulcan) NewBackend(rsc *kubernetes.Resource) (loadbalancer.Backend, error) {
	s := engine.HTTPBackendSettings{
		Timeouts:  engine.HTTPBackendTimeouts{},
		KeepAlive: engine.HTTPBackendKeepAlive{},
	}
	if val, ok := rsc.GetAnnotation(DailTimeoutKey); ok {
		s.Timeouts.Dial = val
	}
	if val, ok := rsc.GetAnnotation(ReadTimeoutKey); ok {
		s.Timeouts.Read = val
	}
	if val, ok := rsc.GetAnnotation(MaxIdleConnsKey); ok {
		if i, er := strconv.Atoi(val); er == nil {
			s.KeepAlive.MaxIdleConnsPerHost = i
		}
	}
	if val, ok := rsc.GetAnnotation(loadbalancer.BackendSettingsKey); ok {
		if er := json.Unmarshal([]byte(val), &s); er != nil {
			logger.Warnf("Failed to parse settings for frontend %q: %v", rsc.ID, er)
		}
	}

	b, er := engine.NewHTTPBackend(rsc.ID(), s)
	if er != nil {
		return nil, er
	}
	if rsc.IsWebsocket() {
		b.Type = ws
	}
	return newBackend(b), nil
}
Exemplo n.º 4
0
func (v *vulcan) NewFrontend(rsc *kubernetes.Resource) (loadbalancer.Frontend, error) {
	s := engine.HTTPFrontendSettings{}
	if val, ok := rsc.GetAnnotation(loadbalancer.PassHostHeaderKey); ok {
		b, _ := strconv.ParseBool(val)
		s.PassHostHeader = b
	}
	if val, ok := rsc.GetAnnotation(loadbalancer.TrustForwardHeadersKey); ok {
		b, _ := strconv.ParseBool(val)
		s.TrustForwardHeader = b
	}
	if val, ok := rsc.GetAnnotation(loadbalancer.FailoverExpressionKey); ok {
		s.FailoverPredicate = val
	}
	if val, ok := rsc.GetAnnotation(loadbalancer.FrontendSettingsKey); ok {
		if er := json.Unmarshal([]byte(val), &s); er != nil {
			logger.Warnf("Failed to parse settings for frontend %q: %v", rsc.ID, er)
		}
	}

	f, er := engine.NewHTTPFrontend(vroute.NewMux(), rsc.ID(), rsc.ID(), NewRoute(rsc.Route).String(), s)
	if er != nil {
		return nil, er
	}
	return newFrontend(f), nil
}
Exemplo n.º 5
0
func (t *traefik) NewBackend(rsc *kubernetes.Resource) (loadbalancer.Backend, error) {
	// b := new(types.Backend)
	b := &types.Backend{
		LoadBalancer:   defaultLoadBalancerMethod,
		CircuitBreaker: defaultCircuitBreaker,
	}
	b.Servers = make(map[string]types.Server)
	if lbm, ok := rsc.GetAnnotation(LoadbalancingMethodKey); ok && validLBM(lbm) {
		b.LoadBalancer = &types.LoadBalancer{Method: lbm}
	}
	if exp, ok := rsc.GetAnnotation(loadbalancer.FailoverExpressionKey); ok {
		b.CircuitBreaker = &types.CircuitBreaker{Expression: exp}
	}

	return &backend{Backend: *b, id: rsc.ID()}, nil
}