diff options
author | 2017-08-23 16:30:19 +0100 | |
---|---|---|
committer | 2017-08-23 08:30:19 -0700 | |
commit | 10b7221067b352b165c69c8cc9796586a1a9f3c2 (patch) | |
tree | 7d52ace0e81ba429fecdfd420229289b4d574d12 /middleware | |
parent | 4b105c761e77873405e40730b07a935eec7cad80 (diff) | |
download | coredns-10b7221067b352b165c69c8cc9796586a1a9f3c2.tar.gz coredns-10b7221067b352b165c69c8cc9796586a1a9f3c2.tar.zst coredns-10b7221067b352b165c69c8cc9796586a1a9f3c2.zip |
mw/kubernetes: revert if-else for health (#970)
Do the return early and dedent the rest of the function.
Diffstat (limited to 'middleware')
-rw-r--r-- | middleware/kubernetes/kubernetes.go | 104 |
1 files changed, 53 insertions, 51 deletions
diff --git a/middleware/kubernetes/kubernetes.go b/middleware/kubernetes/kubernetes.go index 26d784e70..4faa818ec 100644 --- a/middleware/kubernetes/kubernetes.go +++ b/middleware/kubernetes/kubernetes.go @@ -154,69 +154,71 @@ func (k *Kubernetes) getClientConfig() (*rest.Config, error) { overrides := &clientcmd.ConfigOverrides{} clusterinfo := clientcmdapi.Cluster{} authinfo := clientcmdapi.AuthInfo{} - if len(k.APIServerList) > 0 { - endpoint := k.APIServerList[0] - if len(k.APIServerList) > 1 { - // Use a random port for api proxy, will get the value later through listener.Addr() - listener, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - return nil, fmt.Errorf("failed to create kubernetes api proxy: %v", err) - } - k.APIProxy = &apiProxy{ - listener: listener, - handler: proxyHandler{ - HealthCheck: healthcheck.HealthCheck{ - FailTimeout: 3 * time.Second, - MaxFails: 1, - Future: 10 * time.Second, - Path: "/", - Interval: 5 * time.Second, - }, - }, - } - k.APIProxy.handler.Hosts = make([]*healthcheck.UpstreamHost, len(k.APIServerList)) - for i, entry := range k.APIServerList { - uh := &healthcheck.UpstreamHost{ - Name: strings.TrimPrefix(entry, "http://"), + if len(k.APIServerList) == 0 { + cc, err := rest.InClusterConfig() + if err != nil { + return nil, err + } + return cc, err + } - CheckDown: func(upstream *proxyHandler) healthcheck.UpstreamHostDownFunc { - return func(uh *healthcheck.UpstreamHost) bool { + endpoint := k.APIServerList[0] + if len(k.APIServerList) > 1 { + // Use a random port for api proxy, will get the value later through listener.Addr() + listener, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + return nil, fmt.Errorf("failed to create kubernetes api proxy: %v", err) + } + k.APIProxy = &apiProxy{ + listener: listener, + handler: proxyHandler{ + HealthCheck: healthcheck.HealthCheck{ + FailTimeout: 3 * time.Second, + MaxFails: 1, + Future: 10 * time.Second, + Path: "/", + Interval: 5 * time.Second, + }, + }, + } + k.APIProxy.handler.Hosts = make([]*healthcheck.UpstreamHost, len(k.APIServerList)) + for i, entry := range k.APIServerList { - down := false + uh := &healthcheck.UpstreamHost{ + Name: strings.TrimPrefix(entry, "http://"), - uh.CheckMu.Lock() - until := uh.OkUntil - uh.CheckMu.Unlock() + CheckDown: func(upstream *proxyHandler) healthcheck.UpstreamHostDownFunc { + return func(uh *healthcheck.UpstreamHost) bool { - if !until.IsZero() && time.Now().After(until) { - down = true - } + down := false - fails := atomic.LoadInt32(&uh.Fails) - if fails >= upstream.MaxFails && upstream.MaxFails != 0 { - down = true - } - return down + uh.CheckMu.Lock() + until := uh.OkUntil + uh.CheckMu.Unlock() + + if !until.IsZero() && time.Now().After(until) { + down = true } - }(&k.APIProxy.handler), - } - k.APIProxy.handler.Hosts[i] = uh + fails := atomic.LoadInt32(&uh.Fails) + if fails >= upstream.MaxFails && upstream.MaxFails != 0 { + down = true + } + return down + } + }(&k.APIProxy.handler), } - k.APIProxy.Handler = &k.APIProxy.handler - // Find the random port used for api proxy - endpoint = fmt.Sprintf("http://%s", listener.Addr()) + k.APIProxy.handler.Hosts[i] = uh } - clusterinfo.Server = endpoint - } else { - cc, err := rest.InClusterConfig() - if err != nil { - return nil, err - } - return cc, err + k.APIProxy.Handler = &k.APIProxy.handler + + // Find the random port used for api proxy + endpoint = fmt.Sprintf("http://%s", listener.Addr()) } + clusterinfo.Server = endpoint + if len(k.APICertAuth) > 0 { clusterinfo.CertificateAuthority = k.APICertAuth } |