loader.go 1006 B

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