From e01332404cab4add1db0b30e724df65521ac9dd6 Mon Sep 17 00:00:00 2001 From: Chad Ostler Date: Tue, 27 Feb 2018 17:33:52 -0700 Subject: [PATCH] Add utility function to get port forwarded tiller hostname I didn't change any binaries to use this to minimize possible conflicts. Main purpose of adding this is to simplify external developers creating a helm client. --- pkg/helm/client.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pkg/helm/client.go b/pkg/helm/client.go index c7ef4d89e..509307dfa 100644 --- a/pkg/helm/client.go +++ b/pkg/helm/client.go @@ -17,6 +17,7 @@ limitations under the License. package helm // import "k8s.io/helm/pkg/helm" import ( + "fmt" "io" "time" @@ -25,9 +26,14 @@ import ( "google.golang.org/grpc/credentials" "google.golang.org/grpc/keepalive" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" "k8s.io/helm/pkg/chartutil" + "k8s.io/helm/pkg/helm/portforwarder" + "k8s.io/helm/pkg/kube" "k8s.io/helm/pkg/proto/hapi/chart" rls "k8s.io/helm/pkg/proto/hapi/services" + "k8s.io/helm/pkg/tiller/environment" ) // maxMsgSize use 20MB as the default message size limit. @@ -39,6 +45,32 @@ type Client struct { opts options } +// TillerHost returns a port forwarded tiller hostname +func TillerHost(namespace, context string) (string, error) { + config, err := rest.InClusterConfig() + if err != nil { + config, err = kube.GetConfig(context).ClientConfig() + } + if err != nil { + return "", fmt.Errorf("could not get Kubernetes config for context %q: %s", context, err) + } + + client, err := kubernetes.NewForConfig(config) + if err != nil { + return "", fmt.Errorf("could not get Kubernetes client: %s", err) + } + + if namespace == "" { + namespace = environment.DefaultTillerNamespace + } + tunnel, err := portforwarder.New(namespace, client, config) + if err != nil { + return "", err + } + + return fmt.Sprintf("127.0.0.1:%d", tunnel.Local), nil +} + // NewClient creates a new client. func NewClient(opts ...Option) *Client { var c Client