@@ -6,15 +6,15 @@ import (
6
6
"k8s.io/client-go/discovery"
7
7
"k8s.io/client-go/dynamic"
8
8
"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 "
11
11
)
12
12
13
13
// NewDynamicClient creates a new 'dynamic' clientset using the provided kubeconfig.
14
14
// If kubeconfigPath is not set/empty, it will attempt to load configuration using
15
15
// the default loading rules.
16
16
func NewDynamicClient (kubeconfigPath string ) (dynamic.Interface , error ) {
17
- cfg , err := loadRESTConfig (kubeconfigPath )
17
+ cfg , err := kubeconfig . LoadRESTConfig (kubeconfigPath )
18
18
if err != nil {
19
19
return nil , errors .WithStack (err )
20
20
}
@@ -31,7 +31,7 @@ func NewDynamicClient(kubeconfigPath string) (dynamic.Interface, error) {
31
31
func NewDiscoveryClient (kubeconfigPath string ) (discovery.DiscoveryClient , error ) {
32
32
var discoveryClient * discovery.DiscoveryClient
33
33
34
- cfg , err := loadRESTConfig (kubeconfigPath )
34
+ cfg , err := kubeconfig . LoadRESTConfig (kubeconfigPath )
35
35
if err != nil {
36
36
return discovery.DiscoveryClient {}, errors .WithStack (err )
37
37
}
@@ -49,7 +49,7 @@ func NewDiscoveryClient(kubeconfigPath string) (discovery.DiscoveryClient, error
49
49
// the default loading rules.
50
50
func NewClientSet (kubeconfigPath string ) (kubernetes.Interface , error ) {
51
51
var clientset * kubernetes.Clientset
52
- cfg , err := loadRESTConfig (kubeconfigPath )
52
+ cfg , err := kubeconfig . LoadRESTConfig (kubeconfigPath )
53
53
if err != nil {
54
54
return nil , errors .WithStack (err )
55
55
}
@@ -59,28 +59,3 @@ func NewClientSet(kubeconfigPath string) (kubernetes.Interface, error) {
59
59
}
60
60
return clientset , nil
61
61
}
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
- }
0 commit comments