revportforward

package
v0.0.0-...-2887efe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2024 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package revportforward establishes a reverse port-forward from a kubernetes pod to localhost.

Setting up a port-forward from a kubernetes pod is simple:

$ kubectl port-forward mypod 8888:5000

The above will setup a port-forward, i.e. it will listen on port 8888 locally, forwarding the traffic to 5000 in the pod named "mypod".

What is more involved is setting up a port-forward in the reverse direction, which this code does.

Note that for this to work, netcat (nc) must be installed in the pod.

The code works by running netcat (nc) in the pod in listen mode and then connects the exec streams to local target address.

This also support having ncrev installed on the pod, a safer version of nc in that it checks that there are no other listeners on the given port before starting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ReversePortForward

type ReversePortForward struct {
	// contains filtered or unexported fields
}

ReversePortForward creates reverse port-forwards to pods running in a kubernetes cluster.

func New

func New(kubeconfig []byte, localaddress string, useNcRev bool) (*ReversePortForward, error)

New returns a new RevPortForward instance.

kubeconfig - The contents of the kubeconfig file. podName - The name of the pod found in the cluster pointed to by the kubeconfig file. podPort - The port to forward from within the pod. localaddress - The address we want the incoming connection to be forwarded

to, something like "localhost:22"

func (*ReversePortForward) Start

func (r *ReversePortForward) Start(ctx context.Context, podName string, podPort int) error

Start a reverse port-forward. This function does not return as long as an active connection exists.

Note that as connections are made and then closed this function may return, so it should be called from within a loop, e.g.:

for {
   if err := rpf.Start(ctx); err != nil {
		sklog.Error(err)
	  }
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL