sergi / go-k8s-portforward

Go portforwarding library for Kubernetes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

Go portforwarding library for Kubernetes

License:Apache License 2.0


Languages

Language:Go 98.6%Language:Makefile 1.4%