github.com/migueleliasweb/helm@v2.6.1+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  	"strconv"
    25  
    26  	"k8s.io/client-go/rest"
    27  	"k8s.io/client-go/tools/portforward"
    28  	"k8s.io/client-go/tools/remotecommand"
    29  )
    30  
    31  // Tunnel describes a ssh-like tunnel to a kubernetes pod
    32  type Tunnel struct {
    33  	Local     int
    34  	Remote    int
    35  	Namespace string
    36  	PodName   string
    37  	Out       io.Writer
    38  	stopChan  chan struct{}
    39  	readyChan chan struct{}
    40  	config    *rest.Config
    41  	client    rest.Interface
    42  }
    43  
    44  // NewTunnel creates a new tunnel
    45  func NewTunnel(client rest.Interface, config *rest.Config, namespace, podName string, remote int) *Tunnel {
    46  	return &Tunnel{
    47  		config:    config,
    48  		client:    client,
    49  		Namespace: namespace,
    50  		PodName:   podName,
    51  		Remote:    remote,
    52  		stopChan:  make(chan struct{}, 1),
    53  		readyChan: make(chan struct{}, 1),
    54  		Out:       ioutil.Discard,
    55  	}
    56  }
    57  
    58  // Close disconnects a tunnel connection
    59  func (t *Tunnel) Close() {
    60  	close(t.stopChan)
    61  	close(t.readyChan)
    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  	dialer, err := remotecommand.NewExecutor(t.config, "POST", u)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	local, err := getAvailablePort()
    80  	if err != nil {
    81  		return fmt.Errorf("could not find an available port: %s", err)
    82  	}
    83  	t.Local = local
    84  
    85  	ports := []string{fmt.Sprintf("%d:%d", t.Local, t.Remote)}
    86  
    87  	pf, err := portforward.New(dialer, ports, t.stopChan, t.readyChan, t.Out, t.Out)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	errChan := make(chan error)
    93  	go func() {
    94  		errChan <- pf.ForwardPorts()
    95  	}()
    96  
    97  	select {
    98  	case err = <-errChan:
    99  		return fmt.Errorf("forwarding ports: %v", err)
   100  	case <-pf.Ready:
   101  		return nil
   102  	}
   103  }
   104  
   105  func getAvailablePort() (int, error) {
   106  	l, err := net.Listen("tcp", ":0")
   107  	if err != nil {
   108  		return 0, err
   109  	}
   110  	defer l.Close()
   111  
   112  	_, p, err := net.SplitHostPort(l.Addr().String())
   113  	if err != nil {
   114  		return 0, err
   115  	}
   116  	port, err := strconv.Atoi(p)
   117  	if err != nil {
   118  		return 0, err
   119  	}
   120  	return port, err
   121  }