-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feedback: de-duplicate loadRESTConfig
- Loading branch information
Showing
3 changed files
with
43 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package kubeconfig | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
// LoadRESTConfig loads the kube config from the provided path. If the path is | ||
// empty, the kube config will be loaded from KUBECONFIG, and if KUBECONFIG | ||
// isn't set, the in-cluster config will be used. | ||
func LoadRESTConfig(path string) (*rest.Config, error) { | ||
switch path { | ||
// If the kubeconfig path is not provided, use the default loading rules | ||
// so we read the regular KUBECONFIG variable or create a non-interactive | ||
// client for agents running in cluster | ||
case "": | ||
loadingrules := clientcmd.NewDefaultClientConfigLoadingRules() | ||
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( | ||
loadingrules, &clientcmd.ConfigOverrides{}).ClientConfig() | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
return cfg, nil | ||
// Otherwise use the explicitly named kubeconfig file. | ||
default: | ||
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( | ||
&clientcmd.ClientConfigLoadingRules{ExplicitPath: path}, | ||
&clientcmd.ConfigOverrides{}).ClientConfig() | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
return cfg, nil | ||
} | ||
} |