portforward module

Easy Kubernetes Port-Forward For Python

class portforward.AsyncPortForwarder(namespace: str, pod_or_service: str, from_port: int, to_port: int, config_path: str | None = None, waiting: float = 0.1, log_level: LogLevel = LogLevel.INFO, kube_context: str = '', bind_ip: IPv4Address | IPv6Address | str | None = None)[source]

Bases: object

Use the same args as the portforward.forward method.

async forward()[source]
is_stopped()[source]
async stop()[source]
class portforward.LogLevel(value)[source]

Bases: Enum

An enumeration.

DEBUG = 0
ERROR = 3
INFO = 1
OFF = 4
WARN = 2
class portforward.PortForwarder(namespace: str, pod_or_service: str, from_port: int, to_port: int, config_path: str | None = None, waiting: float = 0.1, log_level: LogLevel = LogLevel.INFO, kube_context: str = '', bind_ip: IPv4Address | IPv6Address | str | None = None)[source]

Bases: object

Use the same args as the portforward.forward method.

forward()[source]
property from_port

The local port that was actually used for the portforward.

property is_stopped
stop()[source]
exception portforward.PortforwardError[source]

Bases: Exception

Will be raised when something went wrong while the port-forward process.

portforward.forward(namespace: str, pod_or_service: str, from_port: int, to_port: int, config_path: str | None = None, waiting: float = 0.1, log_level: LogLevel = LogLevel.INFO, kube_context: str = '', bind_ip: IPv4Address | IPv6Address | str | None = None) Generator[PortForwarder, None, None][source]

Connects to a pod or service and tunnels traffic from a local port to this target. It uses the kubectl kube config from the home dir if no path is provided.

The libary will figure out for you if it has to target a pod or service.

It will fall back to in-cluster-config in case no kube config file exists.

(Best consumed as context manager.)

Example:
>>> import portforward
>>> with portforward.forward("test", "web-svc", 9000, 80):
>>>     # Do work
Parameters:
  • namespace – Target namespace

  • pod_or_service – Name of target Pod or service

  • from_port – Local port, or 0 to use any free port

  • to_port – Port inside the pod

  • config_path – Path for loading kube config

  • waiting – Delay in seconds

  • log_level – Level of logging

  • kube_context – Target kubernetes context (fallback is current context)

  • bind_ip – To which IP shall the portforward be bind

Returns:

forwarder to manual stop the forwarding