portforward 0.6.1__cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of portforward might be problematic. Click here for more details.

@@ -0,0 +1,231 @@
1
+ """
2
+ Easy Kubernetes Port-Forward For Python
3
+ """
4
+
5
+ __version__ = "0.6.1"
6
+
7
+ import asyncio
8
+ import contextlib
9
+ import os
10
+ from enum import Enum
11
+ from pathlib import Path
12
+ from typing import Generator, Optional
13
+
14
+ from portforward import _portforward
15
+
16
+
17
+ class PortforwardError(Exception):
18
+ """Will be raised when something went wrong while the port-forward process."""
19
+
20
+
21
+ class LogLevel(Enum):
22
+ DEBUG = 0
23
+ INFO = 1
24
+ WARN = 2
25
+ ERROR = 3
26
+ OFF = 4
27
+
28
+
29
+ @contextlib.contextmanager
30
+ def forward(
31
+ namespace: str,
32
+ pod_or_service: str,
33
+ from_port: int,
34
+ to_port: int,
35
+ config_path: Optional[str] = None,
36
+ waiting: float = 0.1,
37
+ log_level: LogLevel = LogLevel.INFO,
38
+ kube_context: str = "",
39
+ ) -> Generator["PortForwarder", None, None]:
40
+ """
41
+ Connects to a **pod or service** and tunnels traffic from a local port to
42
+ this target. It uses the kubectl kube config from the home dir if no path
43
+ is provided.
44
+
45
+ The libary will figure out for you if it has to target a pod or service.
46
+
47
+ It will fall back to in-cluster-config in case no kube config file exists.
48
+
49
+ (Best consumed as context manager.)
50
+
51
+ Example:
52
+ >>> import portforward
53
+ >>> with portforward.forward("test", "web-svc", 9000, 80):
54
+ >>> # Do work
55
+
56
+ :param namespace: Target namespace
57
+ :param pod_or_service: Name of target Pod or service
58
+ :param from_port: Local port
59
+ :param to_port: Port inside the pod
60
+ :param config_path: Path for loading kube config
61
+ :param waiting: Delay in seconds
62
+ :param log_level: Level of logging
63
+ :param kube_context: Target kubernetes context (fallback is current context)
64
+ :return: forwarder to manual stop the forwarding
65
+ """
66
+
67
+ forwarder = PortForwarder(
68
+ namespace,
69
+ pod_or_service,
70
+ from_port,
71
+ to_port,
72
+ config_path,
73
+ waiting,
74
+ log_level,
75
+ kube_context,
76
+ )
77
+
78
+ try:
79
+ forwarder.forward()
80
+
81
+ yield forwarder
82
+
83
+ except RuntimeError as err:
84
+ # Suppress extension exception
85
+ raise PortforwardError(err) from None
86
+
87
+ finally:
88
+ forwarder.stop()
89
+
90
+
91
+ class PortForwarder:
92
+ """Use the same args as the `portforward.forward` method."""
93
+
94
+ def __init__(
95
+ self,
96
+ namespace: str,
97
+ pod_or_service: str,
98
+ from_port: int,
99
+ to_port: int,
100
+ config_path: Optional[str] = None,
101
+ waiting: float = 0.1,
102
+ log_level: LogLevel = LogLevel.INFO,
103
+ kube_context: str = "",
104
+ ) -> None:
105
+ self._async_forwarder = AsyncPortForwarder(
106
+ namespace,
107
+ pod_or_service,
108
+ from_port,
109
+ to_port,
110
+ config_path,
111
+ waiting,
112
+ log_level,
113
+ kube_context,
114
+ )
115
+
116
+ def forward(self):
117
+ asyncio.run(self._async_forwarder.forward())
118
+
119
+ def stop(self):
120
+ asyncio.run(self._async_forwarder.stop())
121
+
122
+ @property
123
+ def is_stopped(self):
124
+ return self._async_forwarder.is_stopped
125
+
126
+
127
+ class AsyncPortForwarder:
128
+ """Use the same args as the `portforward.forward` method."""
129
+
130
+ def __init__(
131
+ self,
132
+ namespace: str,
133
+ pod_or_service: str,
134
+ from_port: int,
135
+ to_port: int,
136
+ config_path: Optional[str] = None,
137
+ waiting: float = 0.1,
138
+ log_level: LogLevel = LogLevel.INFO,
139
+ kube_context: str = "",
140
+ ) -> None:
141
+ self.namespace: str = _validate_str("namespace", namespace)
142
+ self.pod_or_service: str = _validate_str("pod_or_service", pod_or_service)
143
+ self.from_port: int = _validate_port("from_port", from_port)
144
+ self.to_port: int = _validate_port("to_port", to_port)
145
+ self.log_level: LogLevel = _validate_log(log_level)
146
+ self.waiting: float = waiting
147
+
148
+ self.config_path: str = _config_path(config_path)
149
+ self.kube_context: str = _kube_context(kube_context)
150
+
151
+ self.actual_pod_name: str = ""
152
+ self._is_stopped: bool = False
153
+
154
+ async def forward(self):
155
+ self.actual_pod_name = await _portforward.forward(
156
+ self.namespace,
157
+ self.pod_or_service,
158
+ self.from_port,
159
+ self.to_port,
160
+ self.config_path,
161
+ self.log_level.value,
162
+ self.kube_context,
163
+ )
164
+ self._is_stopped = False
165
+
166
+ async def stop(self):
167
+ await _portforward.stop(
168
+ self.namespace, self.actual_pod_name, self.to_port, self.log_level.value
169
+ )
170
+ self._is_stopped = True
171
+
172
+ def is_stopped(self):
173
+ return self._is_stopped
174
+
175
+
176
+ # ===== PRIVATE =====
177
+
178
+
179
+ def _validate_str(arg_name, arg) -> str:
180
+ if arg is None or not isinstance(arg, str):
181
+ raise ValueError(f"{arg_name}={arg} is not a valid str")
182
+
183
+ if len(arg) == 0:
184
+ raise ValueError(f"{arg_name} cannot be an empty str")
185
+
186
+ if "/" in arg:
187
+ raise ValueError(f"{arg_name} contains illegal character '/'")
188
+
189
+ return arg
190
+
191
+
192
+ def _validate_port(arg_name, arg) -> int:
193
+ in_range = arg and 0 < arg < 65536
194
+ if arg is None or not isinstance(arg, int) or not in_range:
195
+ raise ValueError(f"{arg_name}={arg} is not a valid port")
196
+
197
+ return arg
198
+
199
+
200
+ def _validate_log(log_level):
201
+ if not isinstance(log_level, LogLevel):
202
+ raise ValueError(f"log_level={log_level} is not a valid LogLevel")
203
+
204
+ return log_level
205
+
206
+
207
+ def _config_path(config_path_arg) -> str:
208
+ if config_path_arg and not isinstance(config_path_arg, str):
209
+ raise ValueError(f"config_path={config_path_arg} is not a valid str")
210
+
211
+ elif config_path_arg:
212
+ return config_path_arg
213
+
214
+ alt_path = str(Path.home() / ".kube" / "config")
215
+
216
+ config_path = os.environ.get("KUBECONFIG", alt_path)
217
+
218
+ return config_path if os.path.isfile(config_path) else ""
219
+
220
+
221
+ def _kube_context(context):
222
+ if not context:
223
+ return ""
224
+
225
+ if not isinstance(context, str):
226
+ raise ValueError(f"kube_context={context} is not a valid str")
227
+
228
+ if "/" in context:
229
+ raise ValueError("kube_context contains illegal character '/'")
230
+
231
+ return context
@@ -0,0 +1,17 @@
1
+ """
2
+ Rust native module / Python C Extension
3
+ """
4
+
5
+ async def forward(
6
+ namespace: str,
7
+ pod_or_service: str,
8
+ from_port: int,
9
+ to_port: int,
10
+ config_path: str,
11
+ log_level: int,
12
+ kube_context: str,
13
+ ) -> None:
14
+ pass
15
+
16
+ async def stop(namespace: str, actual_pod: str, to_port: int, log_level: int) -> None:
17
+ pass
portforward/py.typed ADDED
File without changes
@@ -0,0 +1,141 @@
1
+ Metadata-Version: 2.1
2
+ Name: portforward
3
+ Version: 0.6.1
4
+ Classifier: Programming Language :: Rust
5
+ Classifier: Programming Language :: Python :: Implementation :: CPython
6
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Programming Language :: Python :: 3.7
9
+ Classifier: Programming Language :: Python :: 3.8
10
+ Classifier: Programming Language :: Python :: 3.9
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ License-File: LICENSE
14
+ License-File: AUTHORS.rst
15
+ Summary: Easy Kubernetes Port-Forward For Python
16
+ Keywords: portforward,kubernetes,k8s
17
+ Author-email: Sebastian Ziemann <corka149@mailbox.org>
18
+ License: MIT License
19
+ Requires-Python: >=3.7
20
+ Description-Content-Type: text/x-rst; charset=UTF-8
21
+ Project-URL: Documentation, https://portforward.readthedocs.io
22
+ Project-URL: Repository, https://github.com/pytogo/portforward.git
23
+ Project-URL: Changelog, https://github.com/pytogo/portforward/blob/main/HISTORY.rst
24
+
25
+ ===========
26
+ portforward
27
+ ===========
28
+
29
+
30
+ .. image:: https://img.shields.io/pypi/v/portforward.svg
31
+ :target: https://pypi.python.org/pypi/portforward
32
+
33
+ .. image:: https://img.shields.io/pypi/status/portforward.svg
34
+ :target: https://pypi.python.org/pypi/portforward
35
+
36
+ .. image:: https://img.shields.io/pypi/dm/portforward
37
+ :alt: PyPI - Downloads
38
+
39
+ .. image:: https://readthedocs.org/projects/portforward/badge/?version=latest
40
+ :target: https://portforward.readthedocs.io/en/latest/?version=latest
41
+ :alt: Documentation Status
42
+
43
+ .. image:: https://github.com/pytogo/portforward/actions/workflows/python-app.yml/badge.svg
44
+ :target: https://github.com/pytogo/portforward/actions
45
+ :alt: Build status
46
+
47
+
48
+
49
+ Easy Kubernetes Port-Forward For Python
50
+
51
+
52
+ * Free software: MIT license
53
+ * Documentation: https://portforward.readthedocs.io.
54
+
55
+
56
+ Installation
57
+ -----------------------------
58
+
59
+ Wheels are available for:
60
+
61
+ * Windows
62
+ * MacOS X
63
+ * Linux
64
+
65
+ with Python versions:
66
+
67
+ * 3.8
68
+ * 3.9
69
+ * 3.10
70
+ * 3.11
71
+ * 3.12
72
+
73
+ and architectures:
74
+
75
+ * x84_64
76
+ * arm64 (known as M1/Apple Chip - MacOS only)
77
+
78
+ **Requirements for installation from source**
79
+
80
+ The following things are required when there is no wheel available for the target system.
81
+
82
+ * `Rust` installed and available in the path (https://www.rust-lang.org/tools/install)
83
+ * `Python` (at least v3.7 - below was never tested but might work)
84
+
85
+ Pip knows how to install ``portforward``.
86
+
87
+ .. code-block::
88
+
89
+ pip install portforward
90
+
91
+
92
+ Quickstart
93
+ ----------
94
+
95
+ .. code-block:: Python
96
+
97
+ import requests
98
+
99
+ import portforward
100
+
101
+
102
+ def main():
103
+ namespace = "test"
104
+ pod_name = "web" # You can also use a service name instead
105
+ local_port = 9000 # from port
106
+ pod_port = 80 # to port
107
+
108
+ # No path to kube config provided - will use default from $HOME/.kube/config
109
+ with portforward.forward(namespace, pod_name, local_port, pod_port):
110
+ response = requests.get("http://localhost:9000")
111
+ print(f"Done: \n'{response.status_code}'\n'{response.text[:20]}...'")
112
+
113
+
114
+ if __name__ == "__main__":
115
+ main()
116
+
117
+
118
+ Features
119
+ --------
120
+
121
+ * Native Kubernetes port-forwarding with the ``.kube/config`` from the home dir
122
+ or any other path to config.
123
+ * Portforward for pods and services - the lib will first look for a pod with matching name then for
124
+ a service
125
+ * Waiting for a pod to become ready
126
+ * Multiple forwards per pod or service
127
+ * As context manager, sync or async client
128
+
129
+
130
+ Development
131
+ -----------
132
+
133
+ In case you want to develop on this library itself please take a look at the CONTRIBUTING page.
134
+
135
+ Credits
136
+ -------
137
+
138
+ This project is enabled by PyO3_.
139
+
140
+ .. _PyO3: https://pyo3.rs
141
+
@@ -0,0 +1,9 @@
1
+ portforward-0.6.1.dist-info/METADATA,sha256=HIoucdS-BgE8ysbhC7_bQr9ZB-on2jkHBqBVbvVtXgY,3712
2
+ portforward-0.6.1.dist-info/WHEEL,sha256=d8eojpIuPlsQbJ4FKdUz2YDixpvufEnIqL0WYNwi7C0,125
3
+ portforward-0.6.1.dist-info/license_files/LICENSE,sha256=n2aq0UH0YZkyF_5hougjNi5gzXIUapEEqEUn6FwAcFE,1075
4
+ portforward-0.6.1.dist-info/license_files/AUTHORS.rst,sha256=YQwE3_FUEuGVHbxYUa4NQMlG28QBGwcCYEoa2MP9Xgk,163
5
+ portforward/__init__.py,sha256=XQhlOqqoiF7J9T5uWG38BMM0XMW24Jqj0ow079SjAAw,6048
6
+ portforward/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ portforward/_portforward.pyi,sha256=cAkWB_L4LhZAfqkeflWBoC_kHfcoT7hTfZnoWzFQxp4,333
8
+ portforward/_portforward.cpython-39-i386-linux-gnu.so,sha256=dR0UijYDFeZIHjNCQRxNT1WQR0Pn8tJAflLzZ4VfxzU,14304996
9
+ portforward-0.6.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: maturin (0.14.17)
3
+ Root-Is-Purelib: false
4
+ Tag: cp39-cp39-manylinux_2_17_i686.manylinux2014_i686
@@ -0,0 +1,13 @@
1
+ =======
2
+ Credits
3
+ =======
4
+
5
+ Development Lead
6
+ ----------------
7
+
8
+ * Sebastian Ziemann <corka149@mailbox.org>
9
+
10
+ Contributors
11
+ ------------
12
+
13
+ None yet. Why not be the first?
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021, Sebastian Ziemann
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.