github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+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  	"fmt"
    21  	"io"
    22  	"io/ioutil"
    23  	"net"
    24  	"net/http"
    25  	"strconv"
    26  
    27  	"k8s.io/client-go/rest"
    28  	"k8s.io/client-go/tools/portforward"
    29  	"k8s.io/client-go/transport/spdy"
    30  )
    31  
    32  // Tunnel describes a ssh-like tunnel to a kubernetes pod
    33  type Tunnel struct {
    34  	Local     int
    35  	Remote    int
    36  	Namespace string
    37  	PodName   string
    38  	Out       io.Writer
    39  	stopChan  chan struct{}
    40  	readyChan chan struct{}
    41  	config    *rest.Config
    42  	client    rest.Interface
    43  }
    44  
    45  // NewTunnel creates a new tunnel
    46  func NewTunnel(client rest.Interface, config *rest.Config, namespace, podName string, remote int) *Tunnel {
    47  	return &Tunnel{
    48  		config:    config,
    49  		client:    client,
    50  		Namespace: namespace,
    51  		PodName:   podName,
    52  		Remote:    remote,
    53  		stopChan:  make(chan struct{}, 1),
    54  		readyChan: make(chan struct{}, 1),
    55  		Out:       ioutil.Discard,
    56  	}
    57  }
    58  
    59  // Close disconnects a tunnel connection
    60  func (t *Tunnel) Close() {
    61  	close(t.stopChan)
    62  }
    63  
    64  // ForwardPort opens a tunnel to a kubernetes pod
    65  func (t *Tunnel) ForwardPort() error {
    66  	// Build a url to the portforward endpoint
    67  	// example: http://localhost:8080/api/v1/namespaces/helm/pods/tiller-deploy-9itlq/portforward
    68  	u := t.client.Post().
    69  		Resource("pods").
    70  		Namespace(t.Namespace).
    71  		Name(t.PodName).
    72  		SubResource("portforward").URL()
    73  
    74  	transport, upgrader, err := spdy.RoundTripperFor(t.config)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", u)
    79  
    80  	local, err := getAvailablePort()
    81  	if err != nil {
    82  		return fmt.Errorf("could not find an available port: %s", err)
    83  	}
    84  	t.Local = local
    85  
    86  	ports := []string{fmt.Sprintf("%d:%d", t.Local, t.Remote)}
    87  
    88  	pf, err := portforward.New(dialer, ports, t.stopChan, t.readyChan, t.Out, t.Out)
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	errChan := make(chan error)
    94  	go func() {
    95  		errChan <- pf.ForwardPorts()
    96  	}()
    97  
    98  	select {
    99  	case err = <-errChan:
   100  		return fmt.Errorf("forwarding ports: %v", err)
   101  	case <-pf.Ready:
   102  		return nil
   103  	}
   104  }
   105  
   106  func getAvailablePort() (int, error) {
   107  	l, err := net.Listen("tcp", ":0")
   108  	if err != nil {
   109  		return 0, err
   110  	}
   111  	defer l.Close()
   112  
   113  	_, p, err := net.SplitHostPort(l.Addr().String())
   114  	if err != nil {
   115  		return 0, err
   116  	}
   117  	port, err := strconv.Atoi(p)
   118  	if err != nil {
   119  		return 0, err
   120  	}
   121  	return port, err
   122  }