k8sportforwarder

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package k8sportforwarder is responsible for forwarding ports from a pod to the local machine.

It uses the k8s.io/client-go library to create a port forwarder that listens on a local port and forwards traffic to a pod in a Kubernetes cluster. The PortForwarder struct contains the necessary information to create a port forwarder, such as the Kubernetes configuration, clientset, pod, local port, and container port.

Example:

package main

import (
  "fmt"
  "log"
  "os"
  "os/signal"
  "sync"
  "syscall"

  "github.com/NorskHelsenett/ror/pkg/helpers/k8sportforwarder"
  v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func main() {
	  //     Create a new PortForwarder instance
	  forwarder := k8sportforwarder.NewPortForwarder()

	  //     Add a pod to the port forwarder
	  err := forwarder.AddPodByLabels(v1.LabelSelector{
	  	MatchLabels: map[string]string{
	  		"app.kubernetes.io/name":     "grafana",
	  		"app.kubernetes.io/instance": "prometheus",
	  	},
	  }, "prometheus-operator")

	  //     Handle error
	  if err != nil {
	  	log.Fatal(err)
	  }

	  //     Create a waitgroup to wait for the port forwarding to finish
	  var wg sync.WaitGroup
	  wg.Add(1)

	  //     Create a channel to stop the port forwarding and a channel to signal when the port forwarding is ready
	  stopCh := make(chan struct{}, 1)
	  readyCh := make(chan struct{})

	  //     Create a signal channel to handle SIGINT and SIGTERM signals
	  sigs := make(chan os.Signal, 1)
	  signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

	  //     Wait for the SIGINT or SIGTERM signal
	  go func() {
	  	<-sigs
	  	fmt.Println("Bye...")
	  	close(stopCh)
	  	wg.Done()
	  }()

	  //     Create a port forwarder instance
	  go func() {
	  	err := forwarder.Forward(readyCh, stopCh)
	  	if err != nil {
	  		panic(err)
	  	}
	  }()

	  //     Wait for the port forwarding to be ready
	  <-readyCh

	  fmt.Println("Port forwarding is ready")

	  //     Wait for the port forwarding to finish
	  wg.Wait()
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PortForwarder

type PortForwarder struct {
	// RestConfig is the kubernetes config
	RestConfig *rest.Config
	// Clientset is the kubernetes clientset
	Clientset kubernetes.Interface
	// Pod is the selected pod for this port forwarding
	Pod *v1.Pod
	// LocalPort is the local port that will be selected to expose the PodPort
	LocalPort int32
	// ContainerPort is the target port for the pod
	ContainerPort int32
	// Steams configures where to write or read input from
	Streams genericclioptions.IOStreams
}

PortForwarder is the request to port forward a pod

func NewPortForwarder

func NewPortForwarder() *PortForwarder

NewPortForwarder creates a new PortForwarder instance

func NewPortForwarderFromRorKubernetesClient

func NewPortForwarderFromRorKubernetesClient(k8sclientset *rorkubernetesclient.K8sClientsets) (*PortForwarder, error)

NewPortForwarderFromRorKubernetesClient creates a new PortForwarder instance from a RorKubernetesClient

func (*PortForwarder) AddPodByLabels

func (p *PortForwarder) AddPodByLabels(labels metav1.LabelSelector, namespace string) error

Add a pod by label, returns an error if the label returns more or less than one pod. It searches for the labels specified by labels.

func (*PortForwarder) AddPodByName

func (p *PortForwarder) AddPodByName(name string, namespace string) error

AddPodByName adds a pod by its name/namespace It returns an error if the pod is not found or is not running.

func (*PortForwarder) AddPodByServiceName

func (p *PortForwarder) AddPodByServiceName(service string, namespace string) error

AddPodByServiceName adds a pod by its service name/namespace It returns an error if the service is not found or is not running.

func (*PortForwarder) Forward

func (p *PortForwarder) Forward(readyChan chan struct{}, stopChan <-chan struct{}) error

Forward starts the port forwarding process

func (*PortForwarder) GetContainerPort

func (p *PortForwarder) GetContainerPort() (int32, error)

GetContainerPort returns the container port for the pod

func (*PortForwarder) GetLocalPort

func (p *PortForwarder) GetLocalPort() (int32, error)

GetLocalPort returns the local port for the pod

func (*PortForwarder) SetContainerPort

func (p *PortForwarder) SetContainerPort(port int32)

SetContainerPort sets the container port for the pod

func (*PortForwarder) SetLocalPort

func (p *PortForwarder) SetLocalPort(local int32)

SetLocalPort sets the local port for the pod

Jump to

Keyboard shortcuts

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