Code Monkey home page Code Monkey logo

forwarder's Introduction

Forwarder

A forwarder for forwarding kubernetes cluster service and pod programmatically.

Installation

go get github.com/anthhub/forwarder

Usage

forwarding by kubeconfig path

	import (
		"github.com/anthhub/forwarder"
	)

	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.

forwarder's People

Contributors

anthhub avatar crandles avatar bcho avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.