mirror of https://github.com/helm/helm
68 lines
2.1 KiB
68 lines
2.1 KiB
/*
|
|
Copyright 2016 The Kubernetes Authors All rights reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package portforwarder
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
|
|
"k8s.io/kubernetes/pkg/client/restclient"
|
|
"k8s.io/kubernetes/pkg/labels"
|
|
|
|
"k8s.io/helm/pkg/kube"
|
|
)
|
|
|
|
// New creates a new and initialized tunnel.
|
|
func New(namespace string, client *internalclientset.Clientset, config *restclient.Config) (*kube.Tunnel, error) {
|
|
podName, err := getTillerPodName(client.Core(), namespace)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
const tillerPort = 44134
|
|
t := kube.NewTunnel(client.Core().RESTClient(), config, namespace, podName, tillerPort)
|
|
return t, t.ForwardPort()
|
|
}
|
|
|
|
func getTillerPodName(client internalversion.PodsGetter, namespace string) (string, error) {
|
|
// TODO use a const for labels
|
|
selector := labels.Set{"app": "helm", "name": "tiller"}.AsSelector()
|
|
pod, err := getFirstRunningPod(client, namespace, selector)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return pod.ObjectMeta.GetName(), nil
|
|
}
|
|
|
|
func getFirstRunningPod(client internalversion.PodsGetter, namespace string, selector labels.Selector) (*api.Pod, error) {
|
|
options := api.ListOptions{LabelSelector: selector}
|
|
pods, err := client.Pods(namespace).List(options)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(pods.Items) < 1 {
|
|
return nil, fmt.Errorf("could not find tiller")
|
|
}
|
|
for _, p := range pods.Items {
|
|
if api.IsPodReady(&p) {
|
|
return &p, nil
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("could not find a ready tiller pod")
|
|
}
|