Move cluster client creation to client.NewInCluster()

Built & pushed nettest:1.4.
This commit is contained in:
Daniel Smith
2015-05-29 14:31:00 -07:00
parent 68d0511d4e
commit e3c0e38f1b
2 changed files with 32 additions and 13 deletions

View File

@@ -18,6 +18,7 @@ package client
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
@@ -166,6 +167,34 @@ func NewOrDie(c *Config) *Client {
return client
}
// InClusterConfig returns a config object which uses the service account
// kubernetes gives to pods. It's intended for clients that expect to be
// running inside a pod running on kuberenetes. It will return an error if
// called from a process not running in a kubernetes environment.
func InClusterConfig() (*Config, error) {
token, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token")
if err != nil {
return nil, err
}
return &Config{
// TODO: switch to using cluster DNS.
Host: "https://" + net.JoinHostPort(os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT")),
Version: "v1beta3",
BearerToken: string(token),
// TODO: package certs along with the token
Insecure: true,
}, nil
}
// NewInCluster is a shortcut for calling InClusterConfig() and then New().
func NewInCluster() (*Client, error) {
cc, err := InClusterConfig()
if err != nil {
return nil, err
}
return New(cc)
}
// SetKubernetesDefaults sets default values on the provided client config for accessing the
// Kubernetes API or returns an error if any of the defaults are impossible or invalid.
func SetKubernetesDefaults(config *Config) error {