Merge pull request #3328 from aledbf/linit

Code linting
This commit is contained in:
k8s-ci-robot 2018-10-31 05:41:04 -07:00 committed by GitHub
commit cb87676689
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 44 deletions

View file

@ -27,6 +27,9 @@ import (
"k8s.io/ingress-nginx/internal/ingress/resolver" "k8s.io/ingress-nginx/internal/ingress/resolver"
) )
// HTTP protocol
const HTTP = "HTTP"
var ( var (
validProtocols = regexp.MustCompile(`^(HTTP|HTTPS|AJP|GRPC|GRPCS)$`) 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. // rule used to indicate the backend protocol.
func (a backendProtocol) Parse(ing *extensions.Ingress) (interface{}, error) { func (a backendProtocol) Parse(ing *extensions.Ingress) (interface{}, error) {
if ing.GetAnnotations() == nil { if ing.GetAnnotations() == nil {
return "HTTP", nil return HTTP, nil
} }
proto, err := parser.GetStringAnnotation("backend-protocol", ing) proto, err := parser.GetStringAnnotation("backend-protocol", ing)
if err != nil { if err != nil {
return "HTTP", nil return HTTP, nil
} }
proto = strings.TrimSpace(strings.ToUpper(proto)) proto = strings.TrimSpace(strings.ToUpper(proto))
if !validProtocols.MatchString(proto) { if !validProtocols.MatchString(proto) {
glog.Warningf("Protocol %v is not a valid value for the backend-protocol annotation. Using HTTP as protocol", 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 return proto, nil

View file

@ -191,7 +191,7 @@ func TestUseRegex(t *testing.T) {
if !ok { if !ok {
t.Errorf("expected a App Context") t.Errorf("expected a App Context")
} }
if redirect.UseRegex != true { if !redirect.UseRegex {
t.Errorf("Unexpected value got in UseRegex") t.Errorf("Unexpected value got in UseRegex")
} }
} }

View file

@ -102,10 +102,10 @@ type Configuration struct {
// By default access logs go to /var/log/nginx/access.log // By default access logs go to /var/log/nginx/access.log
AccessLogPath string `json:"access-log-path,omitempty"` 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 // http://nginx.org/en/docs/ngx_core_module.html#worker_cpu_affinity
// By default this is disabled // 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 // ErrorLogPath sets the path of the error logs
// http://nginx.org/en/docs/ngx_core_module.html#error_log // http://nginx.org/en/docs/ngx_core_module.html#error_log
// By default error logs go to /var/log/nginx/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? // If the request does not have a request-id, should we generate a random value?
// Default: true // 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 // Adds an X-Original-Uri header with the original request URI to the backend request
// Default: true // Default: true
ProxyAddOriginalUriHeader bool `json:"proxy-add-original-uri-header"` ProxyAddOriginalURIHeader bool `json:"proxy-add-original-uri-header"`
// EnableOpentracing enables the nginx Opentracing extension // EnableOpentracing enables the nginx Opentracing extension
// https://github.com/opentracing-contrib/nginx-opentracing // https://github.com/opentracing-contrib/nginx-opentracing
@ -574,7 +574,7 @@ func NewDefault() Configuration {
cfg := Configuration{ cfg := Configuration{
AllowBackendServerHeader: false, AllowBackendServerHeader: false,
AccessLogPath: "/var/log/nginx/access.log", AccessLogPath: "/var/log/nginx/access.log",
WorkerCpuAffinity: "", WorkerCPUAffinity: "",
ErrorLogPath: "/var/log/nginx/error.log", ErrorLogPath: "/var/log/nginx/error.log",
BlockCIDRs: defBlockEntity, BlockCIDRs: defBlockEntity,
BlockUserAgents: defBlockEntity, BlockUserAgents: defBlockEntity,
@ -591,8 +591,8 @@ func NewDefault() Configuration {
UseForwardedHeaders: true, UseForwardedHeaders: true,
ForwardedForHeader: "X-Forwarded-For", ForwardedForHeader: "X-Forwarded-For",
ComputeFullForwardedFor: false, ComputeFullForwardedFor: false,
ProxyAddOriginalUriHeader: true, ProxyAddOriginalURIHeader: true,
GenerateRequestId: true, GenerateRequestID: true,
HTTP2MaxFieldSize: "4k", HTTP2MaxFieldSize: "4k",
HTTP2MaxHeaderSize: "16k", HTTP2MaxHeaderSize: "16k",
HTTP2MaxRequests: 1000, HTTP2MaxRequests: 1000,

View file

@ -811,12 +811,7 @@ func configureCertificates(pcfg *ingress.Configuration, port int) error {
} }
url := fmt.Sprintf("http://localhost:%d/configuration/servers", port) url := fmt.Sprintf("http://localhost:%d/configuration/servers", port)
err := post(url, servers) return post(url, servers)
if err != nil {
return err
}
return nil
} }
func post(url string, data interface{}) error { func post(url string, data interface{}) error {

View file

@ -90,7 +90,7 @@ func TestMergeConfigMapToStruct(t *testing.T) {
def.WorkerShutdownTimeout = "99s" def.WorkerShutdownTimeout = "99s"
def.NginxStatusIpv4Whitelist = []string{"127.0.0.1", "10.0.0.0/24"} def.NginxStatusIpv4Whitelist = []string{"127.0.0.1", "10.0.0.0/24"}
def.NginxStatusIpv6Whitelist = []string{"::1", "2001::/16"} def.NginxStatusIpv6Whitelist = []string{"::1", "2001::/16"}
def.ProxyAddOriginalUriHeader = false def.ProxyAddOriginalURIHeader = false
hash, err := hashstructure.Hash(def, &hashstructure.HashOptions{ hash, err := hashstructure.Hash(def, &hashstructure.HashOptions{
TagName: "json", TagName: "json",

View file

@ -722,19 +722,6 @@ func buildUpstreamName(loc interface{}) string {
return upstreamName 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 { func buildNextUpstream(i, r interface{}) string {
nextUpstream, ok := i.(string) nextUpstream, ok := i.(string)
if !ok { if !ok {

View file

@ -110,8 +110,15 @@ func (s statusSync) Run() {
// start a new context // start a new context
ctx := context.Background() 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{} var stopCh chan struct{}
callbacks := leaderelection.LeaderCallbacks{ callbacks := leaderelection.LeaderCallbacks{
@ -133,11 +140,9 @@ func (s statusSync) Run() {
close(stopCh) close(stopCh)
// cancel the context // cancel the context
cancel() cancelContext()
// start a new context and run the elector cancelContext = newLeaderCtx(ctx)
leaderCtx, cancel = context.WithCancel(ctx)
go s.elector.Run(leaderCtx)
}, },
OnNewLeader: func(identity string) { OnNewLeader: func(identity string) {
glog.Infof("new leader elected: %v", identity) glog.Infof("new leader elected: %v", identity)
@ -162,7 +167,8 @@ func (s statusSync) Run() {
} }
ttl := 30 * time.Second ttl := 30 * time.Second
le, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{ var err error
s.elector, err = leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
Lock: &lock, Lock: &lock,
LeaseDuration: ttl, LeaseDuration: ttl,
RenewDeadline: ttl / 2, RenewDeadline: ttl / 2,
@ -172,9 +178,8 @@ func (s statusSync) Run() {
if err != nil { if err != nil {
glog.Fatalf("unexpected error starting leader election: %v", err) 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 // Shutdown stop the sync. In case the instance is the leader it will remove the current IP

View file

@ -27,8 +27,8 @@ load_module /etc/nginx/modules/ngx_http_opentracing_module.so;
daemon off; daemon off;
worker_processes {{ $cfg.WorkerProcesses }}; worker_processes {{ $cfg.WorkerProcesses }};
{{ if gt (len $cfg.WorkerCpuAffinity) 0 }} {{ if gt (len $cfg.WorkerCPUAffinity) 0 }}
worker_cpu_affinity {{ $cfg.WorkerCpuAffinity }}; worker_cpu_affinity {{ $cfg.WorkerCPUAffinity }};
{{ end }} {{ end }}
{{ if ne .MaxOpenFiles 0 }} {{ if ne .MaxOpenFiles 0 }}
@ -346,7 +346,7 @@ http {
# If no such header is provided, it can provide a random value. # If no such header is provided, it can provide a random value.
map $http_x_request_id $req_id { map $http_x_request_id $req_id {
default $http_x_request_id; default $http_x_request_id;
{{ if $cfg.GenerateRequestId }} {{ if $cfg.GenerateRequestID }}
"" $request_id; "" $request_id;
{{ end }} {{ end }}
} }
@ -1115,7 +1115,7 @@ stream {
{{ $proxySetHeader }} X-Forwarded-Host $best_http_host; {{ $proxySetHeader }} X-Forwarded-Host $best_http_host;
{{ $proxySetHeader }} X-Forwarded-Port $pass_port; {{ $proxySetHeader }} X-Forwarded-Port $pass_port;
{{ $proxySetHeader }} X-Forwarded-Proto $pass_access_scheme; {{ $proxySetHeader }} X-Forwarded-Proto $pass_access_scheme;
{{ if $all.Cfg.ProxyAddOriginalUriHeader }} {{ if $all.Cfg.ProxyAddOriginalURIHeader }}
{{ $proxySetHeader }} X-Original-URI $request_uri; {{ $proxySetHeader }} X-Original-URI $request_uri;
{{ end }} {{ end }}
{{ $proxySetHeader }} X-Scheme $pass_access_scheme; {{ $proxySetHeader }} X-Scheme $pass_access_scheme;