Skip to content

oliviabarrick/go-k8s-portforward

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

build status

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"
	"github.com/justinbarrick/go-k8s-portforward"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func main() {
	pf, err := portforward.NewPortForwarder("default", metav1.LabelSelector{
		MatchLabels: map[string]string{
			"app": "nginx",
		},
	}, 80)
	if err != nil {
		log.Fatal("Error setting up port forwarder: ", err)
	}

	err = pf.Start()
	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.