github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/pkg/kube/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 kube
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"net"
    23  	"strconv"
    24  
    25  	"k8s.io/kubernetes/pkg/client/unversioned/portforward"
    26  	"k8s.io/kubernetes/pkg/client/unversioned/remotecommand"
    27  )
    28  
    29  // Tunnel describes a ssh-like tunnel to a kubernetes pod
    30  type Tunnel struct {
    31  	Local    int
    32  	Remote   int
    33  	stopChan chan struct{}
    34  }
    35  
    36  // Close disconnects a tunnel connection
    37  func (t *Tunnel) Close() {
    38  	close(t.stopChan)
    39  }
    40  
    41  // ForwardPort opens a tunnel to a kubernetes pod
    42  func (c *Client) ForwardPort(namespace, podName string, remote int) (*Tunnel, error) {
    43  	client, err := c.Client()
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	config, err := c.ClientConfig()
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	// Build a url to the portforward endpoing
    54  	// example: http://localhost:8080/api/v1/namespaces/helm/pods/tiller-deploy-9itlq/portforward
    55  	u := client.RESTClient.Post().
    56  		Resource("pods").
    57  		Namespace(namespace).
    58  		Name(podName).
    59  		SubResource("portforward").URL()
    60  
    61  	dialer, err := remotecommand.NewExecutor(config, "POST", u)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	local, err := getAvailablePort()
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	t := &Tunnel{
    72  		Local:    local,
    73  		Remote:   remote,
    74  		stopChan: make(chan struct{}, 1),
    75  	}
    76  
    77  	ports := []string{fmt.Sprintf("%d:%d", local, remote)}
    78  
    79  	var b bytes.Buffer
    80  	pf, err := portforward.New(dialer, ports, t.stopChan, &b, &b)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	errChan := make(chan error)
    86  	go func() {
    87  		errChan <- pf.ForwardPorts()
    88  	}()
    89  
    90  	select {
    91  	case err = <-errChan:
    92  		return t, fmt.Errorf("Error forwarding ports: %v\n", err)
    93  	case <-pf.Ready:
    94  		return t, nil
    95  	}
    96  }
    97  
    98  func getAvailablePort() (int, error) {
    99  	l, err := net.Listen("tcp", ":0")
   100  	if err != nil {
   101  		return 0, err
   102  	}
   103  	defer l.Close()
   104  
   105  	_, p, err := net.SplitHostPort(l.Addr().String())
   106  	port, err := strconv.Atoi(p)
   107  	if err != nil {
   108  		return 0, err
   109  	}
   110  	return port, err
   111  }