# kr8s.portforward

Objects for managing a port forward connection.

This module provides a class for managing a port forward connection to a Kubernetes Pod or Service.

## Attributes

| [`LocalPortType`](#kr8s.portforward.LocalPortType)   |    |
|------------------------------------------------------------------|----|

## Classes

| [`PortForward`](#kr8s.portforward.PortForward)   | Start a tcp server and forward all connections to a Pod port.   |
|----------------------------------------------------------------|-----------------------------------------------------------------|

## Module Contents

### kr8s.portforward.LocalPortType

### *class* kr8s.portforward.PortForward(resource: kr8s._objects.APIObject, remote_port: [int](https://docs.python.org/3/builtins/functions.html#int), local_port: LocalPortType = 'match', address: [list](https://docs.python.org/3/builtins/stdtypes.html#list)[[str](https://docs.python.org/3/builtins/stdtypes.html#str)] | [str](https://docs.python.org/3/builtins/stdtypes.html#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`](../objects/index.html.md#kr8s.objects.Pod) or any resource with a `ready_pods` method
such as a [`kr8s.objects.Service`](../objects/index.html.md#kr8s.objects.Service).

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

<!-- warning:
Currently Port Forwards only work when using ``asyncio`` and not ``trio``. -->

Args:
: resource:
  : The Pod or Resource to forward to.
  <br/>
  remote_port:
  : The port on the Pod to forward to.
  <br/>
  local_port:
  : The local port to listen on. Defaults to `"match"`, which will match the `remote_port`.
    Set to `"auto"` or `None` to find an available high port.
    Set to an `int` to specify a specific port.
  <br/>
  address:
  : 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.
  <br/>
  Context manager:
  <br/>
  ```pycon
  >>> async with PortForward(pod, 8888) as port:
  ...     print(f"Forwarding to port {port}")
  ...     # Do something with port 8888 on the Pod
  ```
  <br/>
  Explict start/stop:
  <br/>
  ```pycon
  >>> 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()
  ```
  <br/>
  Explict bind address:
  <br/>
  ```pycon
  >>> 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
  ```

#### run_forever()

Run the port forward forever.

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

#### start()

Start a background thread with the port forward running.

#### stop()

Stop the background thread.

#### server *= None*

#### servers *: [list](https://docs.python.org/3/builtins/stdtypes.html#list)[[asyncio.Server](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.Server)]* *= []*

#### remote_port

#### address *= '127.0.0.1'*

#### pod *= None*

#### *async* async_start() → [int](https://docs.python.org/3/builtins/functions.html#int)

#### *async* async_stop() → [None](https://docs.python.org/3/builtins/constants.html#None)

#### *async* async_run_forever() → [None](https://docs.python.org/3/builtins/constants.html#None)
