loader.go 961 B

1234567891011121314151617181920212223242526272829
  1. package kubeconfig
  2. import (
  3. "k8s.io/client-go/kubernetes"
  4. "k8s.io/client-go/rest"
  5. "k8s.io/client-go/tools/clientcmd"
  6. )
  7. // LoadKubeconfig attempts to load a kubeconfig based on default locations.
  8. // If a path is passed in then only that path is checked and will error
  9. // if not found
  10. func LoadKubeconfig(path string) (*rest.Config, error) {
  11. // Use the default load order: KUBECONFIG env > $HOME/.kube/config > In cluster config
  12. loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
  13. if path != "" {
  14. loadingRules.ExplicitPath = path
  15. }
  16. loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
  17. return loader.ClientConfig()
  18. }
  19. // LoadKubeClient accepts a path to a kubeconfig to load and returns the clientset
  20. func LoadKubeClient(path string) (*kubernetes.Clientset, error) {
  21. config, err := LoadKubeconfig(path)
  22. if err != nil {
  23. return nil, err
  24. }
  25. return kubernetes.NewForConfig(config)
  26. }