Configure kube client the same way as kubectl.
package kube
import (
- "os"
- "path/filepath"
-
"github.com/pkg/errors"
"k8s.io/client-go/kubernetes"
// Load the GCP plugin - required to authenticate against
@@ -13,16 +10,16 @@ import (
"k8s.io/client-go/tools/clientcmd"
)
-func homeConfig() string {
- return filepath.Join(os.Getenv("HOME"), ".kube/config")
+func newClientConfig() clientcmd.ClientConfig {
+ return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
+ clientcmd.NewDefaultClientConfigLoadingRules(),
+ &clientcmd.ConfigOverrides{},
+ )
}
// ConfigNamespace returns the namespace from config
func ConfigNamespace() (string, error) {
- cc := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
- &clientcmd.ClientConfigLoadingRules{ExplicitPath: homeConfig()},
- &clientcmd.ConfigOverrides{},
- )
+ cc := newClientConfig()
ns, _, err := cc.Namespace()
if err != nil {
return "", errors.Wrap(err, "Could not get namespace from config")
@@ -31,18 +28,11 @@ func ConfigNamespace() (string, error) {
}
// LoadConfig returns a kubernetes client config based on global settings.
-func LoadConfig() (config *rest.Config, err error) {
- potentialConfigs := []string{
- "", // An empty config path is used when we're in-cluster.
- homeConfig(),
- }
- for _, pc := range potentialConfigs {
- config, err = clientcmd.BuildConfigFromFlags("", pc)
- if err == nil {
- return
- }
+func LoadConfig() (*rest.Config, error) {
+ if c, err := rest.InClusterConfig(); err == nil {
+ return c, nil
}
- return // result of last clientcmd.BuildConfigFromFlags
+ return newClientConfig().ClientConfig()
}
// NewClient returns a k8 client configured by the k10 environment.
