github.com/zoumo/helm@v2.5.0+incompatible/pkg/helm/portforwarder/portforwarder.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package portforwarder 18 19 import ( 20 "fmt" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "k8s.io/apimachinery/pkg/labels" 24 "k8s.io/client-go/kubernetes" 25 corev1 "k8s.io/client-go/kubernetes/typed/core/v1" 26 "k8s.io/client-go/pkg/api/v1" 27 "k8s.io/client-go/rest" 28 29 "k8s.io/helm/pkg/kube" 30 ) 31 32 // New creates a new and initialized tunnel. 33 func New(namespace string, client kubernetes.Interface, config *rest.Config) (*kube.Tunnel, error) { 34 podName, err := getTillerPodName(client.CoreV1(), namespace) 35 if err != nil { 36 return nil, err 37 } 38 const tillerPort = 44134 39 t := kube.NewTunnel(client.Core().RESTClient(), config, namespace, podName, tillerPort) 40 return t, t.ForwardPort() 41 } 42 43 func getTillerPodName(client corev1.PodsGetter, namespace string) (string, error) { 44 // TODO use a const for labels 45 selector := labels.Set{"app": "helm", "name": "tiller"}.AsSelector() 46 pod, err := getFirstRunningPod(client, namespace, selector) 47 if err != nil { 48 return "", err 49 } 50 return pod.ObjectMeta.GetName(), nil 51 } 52 53 func getFirstRunningPod(client corev1.PodsGetter, namespace string, selector labels.Selector) (*v1.Pod, error) { 54 options := metav1.ListOptions{LabelSelector: selector.String()} 55 pods, err := client.Pods(namespace).List(options) 56 if err != nil { 57 return nil, err 58 } 59 if len(pods.Items) < 1 { 60 return nil, fmt.Errorf("could not find tiller") 61 } 62 for _, p := range pods.Items { 63 if isPodReady(&p) { 64 return &p, nil 65 } 66 } 67 return nil, fmt.Errorf("could not find a ready tiller pod") 68 }