@@ -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.
1616func 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) {
3131func 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.
5050func 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- }
0 commit comments