github.com/strongmonkey/helm@v2.7.2+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  	close(t.readyChan)
    63  }
    64  
    65  // ForwardPort opens a tunnel to a kubernetes pod
    66  func (t *Tunnel) ForwardPort() error {
    67  	// Build a url to the portforward endpoint
    68  	// example: http://localhost:8080/api/v1/namespaces/helm/pods/tiller-deploy-9itlq/portforward
    69  	u := t.client.Post().
    70  		Resource("pods").
    71  		Namespace(t.Namespace).
    72  		Name(t.PodName).
    73  		SubResource("portforward").URL()
    74  
    75  	transport, upgrader, err := spdy.RoundTripperFor(t.config)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", u)
    80  
    81  	local, err := getAvailablePort()
    82  	if err != nil {
    83  		return fmt.Errorf("could not find an available port: %s", err)
    84  	}
    85  	t.Local = local
    86  
    87  	ports := []string{fmt.Sprintf("%d:%d", t.Local, t.Remote)}
    88  
    89  	pf, err := portforward.New(dialer, ports, t.stopChan, t.readyChan, t.Out, t.Out)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	errChan := make(chan error)
    95  	go func() {
    96  		errChan <- pf.ForwardPorts()
    97  	}()
    98  
    99  	select {
   100  	case err = <-errChan:
   101  		return fmt.Errorf("forwarding ports: %v", err)
   102  	case <-pf.Ready:
   103  		return nil
   104  	}
   105  }
   106  
   107  func getAvailablePort() (int, error) {
   108  	l, err := net.Listen("tcp", ":0")
   109  	if err != nil {
   110  		return 0, err
   111  	}
   112  	defer l.Close()
   113  
   114  	_, p, err := net.SplitHostPort(l.Addr().String())
   115  	if err != nil {
   116  		return 0, err
   117  	}
   118  	port, err := strconv.Atoi(p)
   119  	if err != nil {
   120  		return 0, err
   121  	}
   122  	return port, err
   123  }