https://github.com/errordeveloper/go-k8s-portforward
Go portforwarding library for Kubernetes
https://github.com/errordeveloper/go-k8s-portforward
Last synced: about 2 months ago
JSON representation
Go portforwarding library for Kubernetes
- Host: GitHub
- URL: https://github.com/errordeveloper/go-k8s-portforward
- Owner: errordeveloper
- License: apache-2.0
- Fork: true (oliviabarrick/go-k8s-portforward)
- Created: 2020-09-03T16:19:20.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-09-03T16:44:24.000Z (over 5 years ago)
- Last Synced: 2024-06-20T01:55:58.318Z (over 1 year ago)
- Language: Go
- Size: 26.5 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A Go library for creating port forwards into pods running in a Kubernetes cluster.

This code is heavily inspired by the implementations in kubectl, fission, and helm:
* https://github.com/kubernetes/helm/blob/master/pkg/kube/tunnel.go
* https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/portforward.go
* https://github.com/fission/fission/blob/master/fission/portforward/portforward.go
See [godoc.org](https://godoc.org/github.com/justinbarrick/go-k8s-portforward) 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.