1
0
Fork 0

Support cluster-external Kubernetes client. (#1159)

Detect whether in-cluster or cluster-external Kubernetes client should
be used based on the KUBERNETES_SERVICE_{HOST,PORT} environment
variables.

Adds bearer token and CA certificate file path parameters.
This commit is contained in:
Timo Reimann 2017-03-07 13:09:11 +01:00 committed by GitHub
parent 3611818eda
commit 920b5bb15d
5 changed files with 122 additions and 34 deletions

View file

@ -1,6 +1,9 @@
package k8s
import (
"errors"
"fmt"
"io/ioutil"
"time"
"k8s.io/client-go/1.5/kubernetes"
@ -39,32 +42,48 @@ type clientImpl struct {
clientset *kubernetes.Clientset
}
// NewInClusterClient returns a new Kubernetes client that expect to run inside the cluster
func NewInClusterClient() (Client, error) {
// NewInClusterClient returns a new Kubernetes client that is expected to run
// inside the cluster.
func NewInClusterClient(endpoint string) (Client, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create in-cluster configuration: %s", err)
}
return &clientImpl{
clientset: clientset,
}, nil
if endpoint != "" {
config.Host = endpoint
}
return createClientFromConfig(config)
}
// NewInClusterClientWithEndpoint is the same as NewInClusterClient but uses the provided endpoint URL
func NewInClusterClientWithEndpoint(endpoint string) (Client, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
// NewExternalClusterClient returns a new Kubernetes client that may run outside
// of the cluster.
// The endpoint parameter must not be empty.
func NewExternalClusterClient(endpoint, token, caFilePath string) (Client, error) {
if endpoint == "" {
return nil, errors.New("endpoint missing for external cluster client")
}
config.Host = endpoint
config := &rest.Config{
Host: endpoint,
BearerToken: token,
}
clientset, err := kubernetes.NewForConfig(config)
if caFilePath != "" {
caData, err := ioutil.ReadFile(caFilePath)
if err != nil {
return nil, fmt.Errorf("failed to read CA file %s: %s", caFilePath, err)
}
config.TLSClientConfig = rest.TLSClientConfig{CAData: caData}
}
return createClientFromConfig(config)
}
func createClientFromConfig(c *rest.Config) (Client, error) {
clientset, err := kubernetes.NewForConfig(c)
if err != nil {
return nil, err
}