-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1209 from kubecost/ramich-kubeclient
Add options to load kubeconfig
- Loading branch information
Showing
3 changed files
with
34 additions
and
28 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,29 @@ | ||
package kubeconfig | ||
|
||
import ( | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
// LoadKubeconfig attempts to load a kubeconfig based on default locations. | ||
// If a path is passed in then only that path is checked and will error | ||
// if not found | ||
func LoadKubeconfig(path string) (*rest.Config, error) { | ||
// Use the default load order: KUBECONFIG env > $HOME/.kube/config > In cluster config | ||
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() | ||
if path != "" { | ||
loadingRules.ExplicitPath = path | ||
} | ||
loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}) | ||
return loader.ClientConfig() | ||
} | ||
|
||
// LoadKubeClient accepts a path to a kubeconfig to load and returns the clientset | ||
func LoadKubeClient(path string) (*kubernetes.Clientset, error) { | ||
config, err := LoadKubeconfig(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return kubernetes.NewForConfig(config) | ||
} |