kpoward

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2022 License: MIT Imports: 11 Imported by: 0

README

kpoward

Go GoDoc

Kubernetes Port Fowarding utility library in Go

If you specify the name and port number of the pod on which the server is running, port forwarding will be performed using a free port in your local system.

Install

go get github.com/goccy/kpoward

Synopsis

Create a Kpoward instance with rest.Config and the name and port number of the pod. If necessary, use the Setter method to change a value such as Namespace and then call Run will call back the local bound free port. You can send any request to this port in the callback. Upon exiting the callback, port forwarding will automatically end and the port will be released.

var (
    restCfg *rest.Config
    targetPodName = "pod-xxx-yyy"
    targetPort = 8080
)
kpow := kpoward.New(restCfg, targetPodName, targetPort)
if err := kpow.Run(context.Background(), func(ctx context.Context, localPort uint16) error {
  log.Printf("localPort: %d", localPort)
  resp, err := http.Get(fmt.Sprintf("http://localhost:%d", localPort))
  if err != nil {
    return err
  }
  defer resp.Body.Close()
  return nil
}); err != nil {
  panic(err)
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Kpoward

type Kpoward struct {
	// contains filtered or unexported fields
}

Kpoward.

func New

func New(cfg *rest.Config, podName string, remotePort uint16) *Kpoward

New create Kpoward instance.

func (*Kpoward) Run

func (k *Kpoward) Run(ctx context.Context, cb func(ctx context.Context, localPort uint16) error) error

Run will call back the local bound free port. You can send any request to this port in the callback. Upon exiting the callback, port forwarding will automatically end and the port will be released.

func (*Kpoward) SetLocalPort

func (k *Kpoward) SetLocalPort(port uint16)

SetLocalPort specify fixed local port.

func (*Kpoward) SetNamespace

func (k *Kpoward) SetNamespace(ns string)

SetNamespace changes target namespace from default.

func (*Kpoward) SetStderr

func (k *Kpoward) SetStderr(stderr io.Writer)

SetStderr changes output destination for stderr.

func (*Kpoward) SetStdout

func (k *Kpoward) SetStdout(stdout io.Writer)

SetStdout changes output destination for stdout.

Jump to

Keyboard shortcuts

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