Skip to content

Commit c6d12b6

Browse files
committed
feedback: de-duplicate loadRESTConfig
1 parent c8eebb9 commit c6d12b6

File tree

3 files changed

+43
-59
lines changed

3 files changed

+43
-59
lines changed

pkg/agent/run.go

+3-29
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@ import (
1717

1818
"github.com/cenkalti/backoff"
1919
"github.com/hashicorp/go-multierror"
20-
"github.com/jetstack/preflight/pkg/logs"
2120
json "github.com/json-iterator/go"
2221
"github.com/prometheus/client_golang/prometheus"
2322
"github.com/prometheus/client_golang/prometheus/promhttp"
2423
"github.com/spf13/cobra"
25-
"k8s.io/client-go/rest"
26-
"k8s.io/client-go/tools/clientcmd"
2724
"sigs.k8s.io/controller-runtime/pkg/manager"
2825

2926
"github.com/jetstack/preflight/api"
3027
"github.com/jetstack/preflight/pkg/client"
3128
"github.com/jetstack/preflight/pkg/datagatherer"
29+
"github.com/jetstack/preflight/pkg/kubeconfig"
30+
"github.com/jetstack/preflight/pkg/logs"
3231
"github.com/jetstack/preflight/pkg/version"
3332
)
3433

@@ -342,7 +341,7 @@ func getConfiguration() (Config, client.Client) {
342341
logs.Log.Printf(`ignoring venafi-cloud.uploader_id. In Venafi Connection mode, this field is not needed.`)
343342
}
344343

345-
cfg, err := loadRESTConfig("")
344+
cfg, err := kubeconfig.LoadRESTConfig("")
346345
if err != nil {
347346
logs.Log.Fatalf("failed to load kubeconfig: %v", err)
348347
}
@@ -568,28 +567,3 @@ func getInClusterNamespace() (string, error) {
568567
}
569568
return string(namespace), nil
570569
}
571-
572-
func loadRESTConfig(path string) (*rest.Config, error) {
573-
switch path {
574-
// If the kubeconfig path is not provided, use the default loading rules
575-
// so we read the regular KUBECONFIG variable or create a non-interactive
576-
// client for agents running in cluster
577-
case "":
578-
loadingrules := clientcmd.NewDefaultClientConfigLoadingRules()
579-
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
580-
loadingrules, &clientcmd.ConfigOverrides{}).ClientConfig()
581-
if err != nil {
582-
return nil, fmt.Errorf("failed to load kubeconfig: %w", err)
583-
}
584-
return cfg, nil
585-
// Otherwise use the explicitly named kubeconfig file.
586-
default:
587-
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
588-
&clientcmd.ClientConfigLoadingRules{ExplicitPath: path},
589-
&clientcmd.ConfigOverrides{}).ClientConfig()
590-
if err != nil {
591-
return nil, fmt.Errorf("failed to load kubeconfig from %s: %w", path, err)
592-
}
593-
return cfg, nil
594-
}
595-
}

pkg/datagatherer/k8s/client.go

+5-30
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import (
66
"k8s.io/client-go/discovery"
77
"k8s.io/client-go/dynamic"
88
"k8s.io/client-go/kubernetes"
9-
"k8s.io/client-go/rest"
10-
"k8s.io/client-go/tools/clientcmd"
9+
10+
"github.com/jetstack/preflight/pkg/kubeconfig"
1111
)
1212

1313
// NewDynamicClient creates a new 'dynamic' clientset using the provided kubeconfig.
1414
// If kubeconfigPath is not set/empty, it will attempt to load configuration using
1515
// the default loading rules.
1616
func NewDynamicClient(kubeconfigPath string) (dynamic.Interface, error) {
17-
cfg, err := loadRESTConfig(kubeconfigPath)
17+
cfg, err := kubeconfig.LoadRESTConfig(kubeconfigPath)
1818
if err != nil {
1919
return nil, errors.WithStack(err)
2020
}
@@ -31,7 +31,7 @@ func NewDynamicClient(kubeconfigPath string) (dynamic.Interface, error) {
3131
func NewDiscoveryClient(kubeconfigPath string) (discovery.DiscoveryClient, error) {
3232
var discoveryClient *discovery.DiscoveryClient
3333

34-
cfg, err := loadRESTConfig(kubeconfigPath)
34+
cfg, err := kubeconfig.LoadRESTConfig(kubeconfigPath)
3535
if err != nil {
3636
return discovery.DiscoveryClient{}, errors.WithStack(err)
3737
}
@@ -49,7 +49,7 @@ func NewDiscoveryClient(kubeconfigPath string) (discovery.DiscoveryClient, error
4949
// the default loading rules.
5050
func NewClientSet(kubeconfigPath string) (kubernetes.Interface, error) {
5151
var clientset *kubernetes.Clientset
52-
cfg, err := loadRESTConfig(kubeconfigPath)
52+
cfg, err := kubeconfig.LoadRESTConfig(kubeconfigPath)
5353
if err != nil {
5454
return nil, errors.WithStack(err)
5555
}
@@ -59,28 +59,3 @@ func NewClientSet(kubeconfigPath string) (kubernetes.Interface, error) {
5959
}
6060
return clientset, nil
6161
}
62-
63-
func loadRESTConfig(path string) (*rest.Config, error) {
64-
switch path {
65-
// If the kubeconfig path is not provided, use the default loading rules
66-
// so we read the regular KUBECONFIG variable or create a non-interactive
67-
// client for agents running in cluster
68-
case "":
69-
loadingrules := clientcmd.NewDefaultClientConfigLoadingRules()
70-
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
71-
loadingrules, &clientcmd.ConfigOverrides{}).ClientConfig()
72-
if err != nil {
73-
return nil, errors.WithStack(err)
74-
}
75-
return cfg, nil
76-
// Otherwise use the explicitly named kubeconfig file.
77-
default:
78-
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
79-
&clientcmd.ClientConfigLoadingRules{ExplicitPath: path},
80-
&clientcmd.ConfigOverrides{}).ClientConfig()
81-
if err != nil {
82-
return nil, errors.WithStack(err)
83-
}
84-
return cfg, nil
85-
}
86-
}

pkg/kubeconfig/kubeconfig.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package kubeconfig
2+
3+
import (
4+
"github.com/pkg/errors"
5+
"k8s.io/client-go/rest"
6+
"k8s.io/client-go/tools/clientcmd"
7+
)
8+
9+
// LoadRESTConfig loads the kube config from the provided path. If the path is
10+
// empty, the kube config will be loaded from KUBECONFIG, and if KUBECONFIG
11+
// isn't set, the in-cluster config will be used.
12+
func LoadRESTConfig(path string) (*rest.Config, error) {
13+
switch path {
14+
// If the kubeconfig path is not provided, use the default loading rules
15+
// so we read the regular KUBECONFIG variable or create a non-interactive
16+
// client for agents running in cluster
17+
case "":
18+
loadingrules := clientcmd.NewDefaultClientConfigLoadingRules()
19+
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
20+
loadingrules, &clientcmd.ConfigOverrides{}).ClientConfig()
21+
if err != nil {
22+
return nil, errors.WithStack(err)
23+
}
24+
return cfg, nil
25+
// Otherwise use the explicitly named kubeconfig file.
26+
default:
27+
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
28+
&clientcmd.ClientConfigLoadingRules{ExplicitPath: path},
29+
&clientcmd.ConfigOverrides{}).ClientConfig()
30+
if err != nil {
31+
return nil, errors.WithStack(err)
32+
}
33+
return cfg, nil
34+
}
35+
}

0 commit comments

Comments
 (0)