diff --git a/internal/ingress/annotations/backendprotocol/main.go b/internal/ingress/annotations/backendprotocol/main.go index 66dbae7833..4f9417a2f8 100644 --- a/internal/ingress/annotations/backendprotocol/main.go +++ b/internal/ingress/annotations/backendprotocol/main.go @@ -27,6 +27,9 @@ import ( "k8s.io/ingress-nginx/internal/ingress/resolver" ) +// HTTP protocol +const HTTP = "HTTP" + var ( validProtocols = regexp.MustCompile(`^(HTTP|HTTPS|AJP|GRPC|GRPCS)$`) ) @@ -44,18 +47,18 @@ func NewParser(r resolver.Resolver) parser.IngressAnnotation { // rule used to indicate the backend protocol. func (a backendProtocol) Parse(ing *extensions.Ingress) (interface{}, error) { if ing.GetAnnotations() == nil { - return "HTTP", nil + return HTTP, nil } proto, err := parser.GetStringAnnotation("backend-protocol", ing) if err != nil { - return "HTTP", nil + return HTTP, nil } proto = strings.TrimSpace(strings.ToUpper(proto)) if !validProtocols.MatchString(proto) { glog.Warningf("Protocol %v is not a valid value for the backend-protocol annotation. Using HTTP as protocol", proto) - return "HTTP", nil + return HTTP, nil } return proto, nil diff --git a/internal/ingress/annotations/rewrite/main_test.go b/internal/ingress/annotations/rewrite/main_test.go index 71721ad936..899379826c 100644 --- a/internal/ingress/annotations/rewrite/main_test.go +++ b/internal/ingress/annotations/rewrite/main_test.go @@ -191,7 +191,7 @@ func TestUseRegex(t *testing.T) { if !ok { t.Errorf("expected a App Context") } - if redirect.UseRegex != true { + if !redirect.UseRegex { t.Errorf("Unexpected value got in UseRegex") } } diff --git a/internal/ingress/controller/config/config.go b/internal/ingress/controller/config/config.go index d6520dd5e9..2089ec9954 100644 --- a/internal/ingress/controller/config/config.go +++ b/internal/ingress/controller/config/config.go @@ -102,10 +102,10 @@ type Configuration struct { // By default access logs go to /var/log/nginx/access.log AccessLogPath string `json:"access-log-path,omitempty"` - // WorkerCpuAffinity bind nginx worker processes to CPUs this will improve response latency + // WorkerCPUAffinity bind nginx worker processes to CPUs this will improve response latency // http://nginx.org/en/docs/ngx_core_module.html#worker_cpu_affinity // By default this is disabled - WorkerCpuAffinity string `json:"worker-cpu-affinity,omitempty"` + WorkerCPUAffinity string `json:"worker-cpu-affinity,omitempty"` // ErrorLogPath sets the path of the error logs // http://nginx.org/en/docs/ngx_core_module.html#error_log // By default error logs go to /var/log/nginx/error.log @@ -442,11 +442,11 @@ type Configuration struct { // If the request does not have a request-id, should we generate a random value? // Default: true - GenerateRequestId bool `json:"generate-request-id,omitempty"` + GenerateRequestID bool `json:"generate-request-id,omitempty"` // Adds an X-Original-Uri header with the original request URI to the backend request // Default: true - ProxyAddOriginalUriHeader bool `json:"proxy-add-original-uri-header"` + ProxyAddOriginalURIHeader bool `json:"proxy-add-original-uri-header"` // EnableOpentracing enables the nginx Opentracing extension // https://github.com/opentracing-contrib/nginx-opentracing @@ -574,7 +574,7 @@ func NewDefault() Configuration { cfg := Configuration{ AllowBackendServerHeader: false, AccessLogPath: "/var/log/nginx/access.log", - WorkerCpuAffinity: "", + WorkerCPUAffinity: "", ErrorLogPath: "/var/log/nginx/error.log", BlockCIDRs: defBlockEntity, BlockUserAgents: defBlockEntity, @@ -591,8 +591,8 @@ func NewDefault() Configuration { UseForwardedHeaders: true, ForwardedForHeader: "X-Forwarded-For", ComputeFullForwardedFor: false, - ProxyAddOriginalUriHeader: true, - GenerateRequestId: true, + ProxyAddOriginalURIHeader: true, + GenerateRequestID: true, HTTP2MaxFieldSize: "4k", HTTP2MaxHeaderSize: "16k", HTTP2MaxRequests: 1000, diff --git a/internal/ingress/controller/nginx.go b/internal/ingress/controller/nginx.go index e5ab10063e..f99b8d58e7 100644 --- a/internal/ingress/controller/nginx.go +++ b/internal/ingress/controller/nginx.go @@ -811,12 +811,7 @@ func configureCertificates(pcfg *ingress.Configuration, port int) error { } url := fmt.Sprintf("http://localhost:%d/configuration/servers", port) - err := post(url, servers) - if err != nil { - return err - } - - return nil + return post(url, servers) } func post(url string, data interface{}) error { diff --git a/internal/ingress/controller/template/configmap_test.go b/internal/ingress/controller/template/configmap_test.go index 1bb7b773b0..535f105a9f 100644 --- a/internal/ingress/controller/template/configmap_test.go +++ b/internal/ingress/controller/template/configmap_test.go @@ -90,7 +90,7 @@ func TestMergeConfigMapToStruct(t *testing.T) { def.WorkerShutdownTimeout = "99s" def.NginxStatusIpv4Whitelist = []string{"127.0.0.1", "10.0.0.0/24"} def.NginxStatusIpv6Whitelist = []string{"::1", "2001::/16"} - def.ProxyAddOriginalUriHeader = false + def.ProxyAddOriginalURIHeader = false hash, err := hashstructure.Hash(def, &hashstructure.HashOptions{ TagName: "json", diff --git a/internal/ingress/controller/template/template.go b/internal/ingress/controller/template/template.go index 4dd3461e01..0f8b3ee659 100644 --- a/internal/ingress/controller/template/template.go +++ b/internal/ingress/controller/template/template.go @@ -722,19 +722,6 @@ func buildUpstreamName(loc interface{}) string { return upstreamName } -// TODO: Needs Unit Tests -func isSticky(host string, loc *ingress.Location, stickyLocations map[string][]string) bool { - if _, ok := stickyLocations[host]; ok { - for _, sl := range stickyLocations[host] { - if sl == loc.Path { - return true - } - } - } - - return false -} - func buildNextUpstream(i, r interface{}) string { nextUpstream, ok := i.(string) if !ok { diff --git a/internal/ingress/status/status.go b/internal/ingress/status/status.go index c091281309..320ad7c0a1 100644 --- a/internal/ingress/status/status.go +++ b/internal/ingress/status/status.go @@ -110,8 +110,15 @@ func (s statusSync) Run() { // start a new context ctx := context.Background() - // allow to cancel the context in case we stop being the leader - leaderCtx, cancel := context.WithCancel(ctx) + + var cancelContext context.CancelFunc + + var newLeaderCtx = func(ctx context.Context) context.CancelFunc { + // allow to cancel the context in case we stop being the leader + leaderCtx, cancel := context.WithCancel(ctx) + go s.elector.Run(leaderCtx) + return cancel + } var stopCh chan struct{} callbacks := leaderelection.LeaderCallbacks{ @@ -133,11 +140,9 @@ func (s statusSync) Run() { close(stopCh) // cancel the context - cancel() + cancelContext() - // start a new context and run the elector - leaderCtx, cancel = context.WithCancel(ctx) - go s.elector.Run(leaderCtx) + cancelContext = newLeaderCtx(ctx) }, OnNewLeader: func(identity string) { glog.Infof("new leader elected: %v", identity) @@ -162,7 +167,8 @@ func (s statusSync) Run() { } ttl := 30 * time.Second - le, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{ + var err error + s.elector, err = leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{ Lock: &lock, LeaseDuration: ttl, RenewDeadline: ttl / 2, @@ -172,9 +178,8 @@ func (s statusSync) Run() { if err != nil { glog.Fatalf("unexpected error starting leader election: %v", err) } - s.elector = le - go le.Run(leaderCtx) + cancelContext = newLeaderCtx(ctx) } // Shutdown stop the sync. In case the instance is the leader it will remove the current IP diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index 0e855953d4..22c7f893db 100644 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -27,8 +27,8 @@ load_module /etc/nginx/modules/ngx_http_opentracing_module.so; daemon off; worker_processes {{ $cfg.WorkerProcesses }}; -{{ if gt (len $cfg.WorkerCpuAffinity) 0 }} -worker_cpu_affinity {{ $cfg.WorkerCpuAffinity }}; +{{ if gt (len $cfg.WorkerCPUAffinity) 0 }} +worker_cpu_affinity {{ $cfg.WorkerCPUAffinity }}; {{ end }} {{ if ne .MaxOpenFiles 0 }} @@ -346,7 +346,7 @@ http { # If no such header is provided, it can provide a random value. map $http_x_request_id $req_id { default $http_x_request_id; - {{ if $cfg.GenerateRequestId }} + {{ if $cfg.GenerateRequestID }} "" $request_id; {{ end }} } @@ -1115,7 +1115,7 @@ stream { {{ $proxySetHeader }} X-Forwarded-Host $best_http_host; {{ $proxySetHeader }} X-Forwarded-Port $pass_port; {{ $proxySetHeader }} X-Forwarded-Proto $pass_access_scheme; - {{ if $all.Cfg.ProxyAddOriginalUriHeader }} + {{ if $all.Cfg.ProxyAddOriginalURIHeader }} {{ $proxySetHeader }} X-Original-URI $request_uri; {{ end }} {{ $proxySetHeader }} X-Scheme $pass_access_scheme;