kr8s.portforward

Module Contents

Classes

PortForward

Start a tcp server and forward all connections to a Pod port.

class kr8s.portforward.PortForward(resource: kr8s.objects.APIObject, remote_port: int, local_port: int = None, address: List[str] | str = '127.0.0.1')

Bases: kr8s._portforward.PortForward

Start a tcp server and forward all connections to a Pod port.

You can either pass a kr8s.objects.Pod or any resource with a ready_pods method such as a kr8s.objects.Service.

Note

The ready_pods method should return a list of Pods that are ready to accept connections.

Args:

resource (Pod or Resource): The Pod or Resource to forward to.

remote_port (int): The port on the Pod to forward to.

local_port (int, optional): The local port to listen on. Defaults to 0, which will choose a random port.

``address``(List[str] | str, optional): List of addresses or address to listen on. Defaults to [“127.0.0.1”],

will listen only on 127.0.0.1

Example:

This class can be used as a an async context manager or with explicit start/stop methods.

Context manager:

>>> async with PortForward(pod, 8888) as port:
...     print(f"Forwarding to port {port}")
...     # Do something with port 8888 on the Pod

Explict start/stop:

>>> pf = PortForward(pod, 8888)
>>> await pf.start()
>>> print(f"Forwarding to port {pf.local_port}")
>>> # Do something with port 8888 on the Pod
>>> await pf.stop()

Explict bind address:

>>> async with PortForward(pod, 8888, address=["127.0.0.1", "10.20.0.1"]) as port:
...     print(f"Forwarding to port {port}")
...     # Do something with port 8888 on the Pod, port will be bind to 127.0.0.1 and 10.20.0.1
start()

Start a background thread with the port forward running.

stop()

Stop the background thread.

async run_forever() None

Run the port forward forever.

Example:
>>> pf = pod.portforward(remote_port=8888, local_port=8889)
>>> # or
>>> pf = PortForward(pod, remote_port=8888, local_port=8889)
>>> await pf.run_forever()