portforward

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

README

A Go library for creating port forwards into pods running in a Kubernetes cluster.

This is a fork of: justinbarrick/go-k8s-portforward

This code is heavily inspired by the implementations in kubectl, fission, and helm:

See godoc.org for full documentation.

Example

A minimal example which will forward to the

package main

import (
	"log"
	"time"
	portforward "elig-salt/go-k8s-portforward/go-k8s-portforward"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func main() {
    namespace := "default"
	port := 8080
	listenPort := 9000
    labels := map[string]string{ "app": "hello-world" }

	pf, err := portforward.NewPortForwarder(namespace, metav1.LabelSelector{
		MatchLabels: labels,
	}, port)
	if err != nil {
		log.Fatal("Error setting up port forwarder: ", err)
	}
	pf.Name = pod
	pf.ListenPort = listenPort

	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
	defer stop()
	err = pf.Start(ctx)
	if err != nil {
		log.Fatal("Error starting port forward: ", err)
	}

	log.Printf("Started tunnel on %d\n", pf.ListenPort)
	time.Sleep(60 * time.Second)
}

Also see cmd/main.go.

Kubeconfig

By default, it will load a Kubernetes configuration file from ~/.kube/config or $KUBECONFIG.

It is possible to provide your own Kubernetes client by instantiating the PortForward struct directly instead of calling the NewPortForwarder method.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PortForward

type PortForward struct {
	// The parsed Kubernetes configuration file.
	Config *rest.Config
	// The initialized Kubernetes client.
	Clientset kubernetes.Interface
	// The pod name to use, required if Labels is empty.
	Name string
	// The labels to use to find the pod.
	Labels metav1.LabelSelector
	// The port on the pod to forward traffic to.
	DestinationPort int
	// The port that the port forward should listen to, random if not set.
	ListenPort int
	// The namespace to look for the pod in.
	Namespace string

	// Setting this to true won't cause an error if multiple pods exist
	// but rather port-forward to the first one it sees
	AllowMultiplePods bool
	// contains filtered or unexported fields
}

Used for creating a port forward into a Kubernetes pod in a Kubernetes cluster.

func NewPortForwarder

func NewPortForwarder(namespace string, labels metav1.LabelSelector, port int) (*PortForward, error)

Initialize a port forwarder, loads the Kubernetes configuration file and creates the client. You do not need to use this function if you have a client to use already - the PortForward struct can be created directly.

func (*PortForward) Start

func (p *PortForward) Start(ctx context.Context) error

Start a port forward to a pod - blocks until the tunnel is ready for use.

func (*PortForward) Stop

func (p *PortForward) Stop()

Stop a port forward.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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