Skip to content

Commit 46c0ce4

Browse files
committed
Clarify rest.Config construction
I was told this function was messy and unclear, so let's try to make it a bit more explicit, commented and straightfoward (hopefuly). While at it, unhide issues at healthcheck server start.
1 parent e795dd7 commit 46c0ce4

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type KdnConfig struct {
4545

4646
// Init initialize the config
4747
func (c *KdnConfig) Init(apiserver string, kubeconfig string) (err error) {
48-
c.Client, err = client.BuildConfig(apiserver, kubeconfig)
48+
c.Client, err = client.NewRestConfig(apiserver, kubeconfig)
4949
if err != nil {
5050
return fmt.Errorf("Failed init Kubernetes clientset: %+v", err)
5151
}

pkg/client/client.go

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Package client initialize a Kubernete's client-go rest.Config
1+
// Package client initialize a Kubernete's client-go rest.Config or clientset
22
package client
33

44
import (
@@ -14,31 +14,41 @@ import (
1414
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
1515
)
1616

17-
// BuildConfig create a *rest.Config
18-
func BuildConfig(apiserver string, kubeconfig string) (*rest.Config, error) {
19-
// if we're not provided a kubeconfig path, try to find one in user's home
17+
// NewRestConfig create a *rest.Config, using the kubectl paths and priorities:
18+
// - Command line flags (api-server and/or kubeconfig path) have higher priorities
19+
// - Else, use the config file path in KUBECONFIG environment variable, if any
20+
// - Else, use the config file in ~/.kube/config, if any
21+
// - Else, consider we're running in cluster (in a pod), and use the pod's service
22+
// account and cluster's kubernetes.default service.
23+
func NewRestConfig(apiserver string, kubeconfig string) (*rest.Config, error) {
24+
// if not passed as an argument, kubeconfig can be provided as env var
2025
if kubeconfig == "" {
21-
if home := homedir.HomeDir(); home != "" {
22-
if _, err := os.Stat(filepath.Join(home, ".kube", "config")); err == nil {
23-
kubeconfig = filepath.Join(home, ".kube", "config")
24-
}
25-
}
26+
kubeconfig = os.Getenv("KUBECONFIG")
2627
}
2728

28-
// if we're provided an api-server url, or found a kubeconfig file, use that
29+
// if we're not provided an explicit kubeconfig path (via env, or argument),
30+
// try to find one at the standard place (in user's home/.kube/config).
31+
homeCfg := filepath.Join(homedir.HomeDir(), ".kube", "config")
32+
_, err := os.Stat(homeCfg)
33+
if kubeconfig == "" && err == nil {
34+
kubeconfig = homeCfg
35+
}
36+
37+
// if we were provided or found a kubeconfig,
38+
// or if we were provided an api-server url, use that
2939
if apiserver != "" || kubeconfig != "" {
3040
return clientcmd.BuildConfigFromFlags(apiserver, kubeconfig)
3141
}
3242

33-
// else assume we're running in cluster
43+
// else assume we're running in a pod, in cluster
3444
return rest.InClusterConfig()
3545
}
3646

3747
// NewClientSet create a clientset (a client connection to a Kubernetes cluster).
3848
// It will connect using the optional apiserver or kubeconfig options, or will
3949
// default to the automatic, in cluster settings.
4050
func NewClientSet(apiserver string, kubeconfig string) (*kubernetes.Clientset, error) {
41-
config, err := BuildConfig(apiserver, kubeconfig)
51+
config, err := NewRestConfig(apiserver, kubeconfig)
4252
if err != nil {
4353
return nil, err
4454
}

pkg/health/health.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ func (h *Listener) Start() (*Listener, error) {
4646

4747
go func() {
4848
defer close(h.donech)
49-
_ = h.srv.ListenAndServe()
49+
err := h.srv.ListenAndServe()
50+
if err != nil && err.Error() != "http: Server closed" {
51+
h.config.Logger.Errorf("healthcheck server failed: %v", err)
52+
}
5053
}()
5154

5255
return h, nil

0 commit comments

Comments
 (0)