github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/cmd/helm/tunnel.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 main
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/kubernetes/pkg/api"
    23  	"k8s.io/kubernetes/pkg/client/unversioned"
    24  	"k8s.io/kubernetes/pkg/labels"
    25  
    26  	"k8s.io/helm/pkg/kube"
    27  )
    28  
    29  // TODO refactor out this global var
    30  var tunnel *kube.Tunnel
    31  
    32  func newTillerPortForwarder(namespace string) (*kube.Tunnel, error) {
    33  	kc := kube.New(nil)
    34  	client, err := kc.Client()
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	podName, err := getTillerPodName(client, namespace)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	const tillerPort = 44134
    44  	return kc.ForwardPort(namespace, podName, tillerPort)
    45  }
    46  
    47  func getTillerPodName(client unversioned.PodsNamespacer, namespace string) (string, error) {
    48  	// TODO use a const for labels
    49  	selector := labels.Set{"app": "helm", "name": "tiller"}.AsSelector()
    50  	pod, err := getFirstRunningPod(client, namespace, selector)
    51  	if err != nil {
    52  		return "", err
    53  	}
    54  	return pod.ObjectMeta.GetName(), nil
    55  }
    56  
    57  func getFirstRunningPod(client unversioned.PodsNamespacer, namespace string, selector labels.Selector) (*api.Pod, error) {
    58  	options := api.ListOptions{LabelSelector: selector}
    59  	pods, err := client.Pods(namespace).List(options)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	if len(pods.Items) < 1 {
    64  		return nil, fmt.Errorf("could not find tiller")
    65  	}
    66  	for _, p := range pods.Items {
    67  		if api.IsPodReady(&p) {
    68  			return &p, nil
    69  		}
    70  	}
    71  	return nil, fmt.Errorf("could not find a ready pod")
    72  }