forwarder

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2022 License: MIT Imports: 20 Imported by: 0

README

Forwarder

A forwarder for forwarding kubernetes cluster service and pod programmatically.

Installation

go get github.com/4thel00z/k8forwarder

Usage

forwarding by kubeconfig path

	import (
		"github.com/4thel00z/k8forwarder"
	)

	options := []*forwarder.Option{
		{
			// the local port for forwarding
			LocalPort: 	8080,
			// the k8s pod port
			RemotePort: 80,
			// the forwarding service name
			ServiceName: "my-nginx-svc",
			// the k8s source string, eg: svc/my-nginx-svc po/my-nginx-666
			// the Source field will be parsed and override ServiceName or RemotePort field
			Source: "svc/my-nginx-66b6c48dd5-ttdb2",
			// namespace default is "default"
			Namespace: "default"
		},
		{	
			// if local port isn't provided, forwarder will generate a random port number
			// LocalPort: 8081,
			// 
			// if target port isn't provided, forwarder find the first container port of the pod or service
			// RemotePort: 80,
			Source: "po/my-nginx-66b6c48dd5-ttdb2",
		},
	}

	// it's to create a forwarder, and you need provide a path of kubeconfig
	// the path of kubeconfig, default is "~/.kube/config"
	ret, err := forwarder.WithForwarders(context.Background(), options, "./kubecfg")
	if err != nil {
		panic(err)
	}
	// remember to close the forwarding
	defer ret.Close()
	// wait forwarding ready
	// the remote and local ports are listed
	ports, err := ret.Ready()
	if err != nil {
		panic(err)
	}
	// ...

	// if you want to block the goroutine and listen IOStreams close signal, you can do as following:
	ret.Wait()
forwarding embed kubeconfig
//go:embed kubeconfig
var kubeconfigBytes []byte

func main() {
	options := []*forwarder.Option{
		{
			// LocalPort: 8080,
			// RemotePort:  80,
			ServiceName: "my-nginx-svc",
		},
	}
	// use kubeconfig bytes to config forward
	ret, err := forwarder.WithForwardersEmbedConfig(context.Background(), options, kubeconfigBytes)
	if err != nil {
		panic(err)
	}
	// remember to close the forwarding
	defer ret.Close()
	// wait forwarding ready
	// the remote and local ports are listed
	ports, err := ret.Ready()
	if err != nil {
		panic(err)
	}
	fmt.Printf("ports: %+v\n", ports)
	// ...

	// if you want to block the goroutine and listen IOStreams close signal, you can do as following:
	ret.Wait()
}

If you want to learn more about forwarder, you can read example, test cases and source code.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PortForward added in v0.2.1

func PortForward(req *Request, errChan chan<- error) (*portforward.PortForwarder, error)

PortForward is to forward port, and return the forwarder.

Types

type Option

type Option struct {
	LocalPort   int    // the local port for forwarding
	RemotePort  int    // the remote port port for forwarding
	Namespace   string // the k8s namespace metadata
	PodName     string // the k8s pod metadata
	ServiceName string // the k8s service metadata
	Source      string // the k8s source string, eg: svc/my-nginx-svc po/my-nginx-66b6c48dd5-ttdb2
}

type PodOption

type PodOption struct {
	LocalPort int    // the local port for forwarding
	PodPort   int    // the k8s pod port
	Pod       v1.Pod // the k8s pod metadata
}

type Request added in v0.2.1

type Request struct {
	RestConfig *rest.Config                // RestConfig is the kubernetes config
	Pod        v1.Pod                      // Pod is the selected pod for this port forwarding
	LocalPort  int                         // LocalPort is the local port that will be selected to expose the PodPort
	PodPort    int                         // PodPort is the target port for the pod
	Streams    genericclioptions.IOStreams // Steams configures where to write or read input from
	StopCh     <-chan struct{}             // StopCh is the channel used to manage the port forward lifecycle
	ReadyCh    chan struct{}               // ReadyCh communicates when the tunnel is ready to receive traffic
}

type Result

type Result struct {
	Close func()                                        // close the port forwarding
	Ready func() ([][]portforward.ForwardedPort, error) // block till the forwarding ready
	Wait  func()                                        // block and listen IOStreams close signal
}

func WithForwarders

func WithForwarders(ctx context.Context, options []*Option, kubeconfigPath string, errChan chan<- error) (*Result, error)

WithForwarders is to forward port for k8s cloud services

func WithForwardersEmbedConfig

func WithForwardersEmbedConfig(ctx context.Context, options []*Option, kubeconfigBytes []byte, errChan chan<- error) (*Result, error)

WithForwardersEmbedConfig is to forward port with kubeconfig bytes

func WithRestConfig

func WithRestConfig(ctx context.Context, options []*Option, config *restclient.Config, errChan chan<- error) (*Result, error)

WithRestConfig is to forward port for k8s cloud services

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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