How do I make this work with a service?
drawal1 opened this issue Β· comments
- portforward version: latest
- Python version: 3.10.8
- Operating System: ubuntu in wsl
Description
I would like to port forward a service instead of a pod. Can this functionality be added?
What I Did
ran the example code, except I specified the name of a service instead of a pod.
Never mind, I'd set up a deployment instead of a pod. This is a non-issue.
@drawal1 I like this feature request. Therefore, I will reopen the issue so it wont be forget. π
Anything I can do to expedite this feature request? I have multiple use cases right now where this would be very helpful.
Do you have an idea or suggestion how it could be integrate into the API? Or should the current API be changed? π€
I understand your point. Personally I use portforwarding to debug a specific pod. But I can check first the services.
So I would only change the naming in the API like - please notice the pod_or_service
:
@contextlib.contextmanager
def forward(
namespace: str,
pod_or_service: str,
from_port: int,
to_port: int,
config_path: Optional[str] = None,
waiting: float = 0.1,
log_level: LogLevel = LogLevel.DEBUG,
kube_context: str = "",
) -> Generator[None, None, None]:
"""
Connects to a Pod and tunnels traffic from a local port to this pod.
It uses the kubectl kube config from the home dir if no path is provided.
Caution: Go and the port-forwarding needs some ms to be ready. ``waiting``
can be used to wait until the port-forward is ready.
(Best consumed as context manager.)
Example:
>>> import portforward
>>> with portforward.forward("test", "web", 9000, 80):
>>> # Do work
:param namespace: Target namespace
:param pod_or_service: Name of target Pod or service
:param from_port: Local port
:param to_port: Port inside the pod
:param config_path: Path for loading kube config
:param waiting: Delay in seconds
:param log_level: Level of logging
:param kube_context: Target kubernetes context (fallback is current context)
:return: None
"""
The feature is now in the main branch and is ready for testing. But I checked how kubectl
is working and it is prioritizing pod before services. I adapted this way. However you should not notice it while using portforward
.
Looking forward to your feedback.
I would create a release for pypi.org in case everything is fine.
(Edit: My tests look fine so far :D )
The first error is fine. It is used to determine some flags for the compiler.
The second is unknown to me.
Regarding the third: Do you use something like venv?
What OS and CPU architecture are you using?
Could please provide the output of the following commands?
go version
gcc -v
This is also for verifying that both are available to the CLI.
And in addition - when your venv
is active: Could you try to install the portforward like
pip install git+https://github.com/pytogo/portforward.git
```
and share the complete output?
That helps me understanding what is going wrong. Thx!
π I have a alias for setting up a venv which includes automatically upgrade pip and installing wheel.
Does import portforward
work fine? When the import succeeds it is already a win.
Do you mean this comment #11 (comment)?
I dont see any error. What happens in the REPL?
ImportError while importing test module
'/home/drawal/rl/portforward/tests/test_portforward.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.8/importlib/init.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/test_portforward.py:10: in
import portforward
E ModuleNotFoundError: No module named 'portforward'
Strange in this comment it looks like the installation worked.
But your output states that there is no portforward. Could you re-check if portforward was installed in the correct venv? Thx!
I decided to create the release v0.4.0. It is available on pypi now.
Meanwhile creating the release I discovered and fixed some issues while building which occurred only on other machines. o.O
So, you can decide: Retry to install it from git or install portforward=0.4.0
.
Does this als happen when you install portforward in a fresh venv from pypi?
This is really suspicious when the previous version of portforward suddenly fail.
Meanwhile I detected an issue but only when I try to forward a service.
This is my venv
(pf_test) > $ pip list [Β±main β]
Package Version
------------------ ---------
certifi 2022.12.7
charset-normalizer 3.0.1
idna 3.4
pip 23.0
portforward 0.4.0
requests 2.28.2
setuptools 58.1.0
urllib3 1.26.14
wheel 0.38.4
Here the resources
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app.kubernetes.io/name: proxy
spec:
containers:
- name: nginx
image: nginx:stable
ports:
- containerPort: 80
name: http-web-svc
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app.kubernetes.io/name: proxy
ports:
- name: name-of-service-port
protocol: TCP
port: 80
targetPort: http-web-svc
It works with this script for pods
import requests
import portforward
def main():
namespace = "pftest"
pod_name = "nginx"
local_port = 9000 # from port
pod_port = 80 # to port
# No path to kube config provided - will use default from $HOME/.kube/config
with portforward.forward(namespace, pod_name, local_port, pod_port):
response = requests.get("http://localhost:9000")
print(f"Done: \n'{response.status_code}'\n'{response.text[:20]}...'")
if __name__ == "__main__":
main()
And it does not work with services like in this script:
import requests
import portforward
def main():
namespace = "pftest"
pod_name = "nginx-service"
local_port = 9000 # from port
pod_port = 80 # to port
# No path to kube config provided - will use default from $HOME/.kube/config
with portforward.forward(namespace, pod_name, local_port, pod_port):
response = requests.get("http://localhost:9000")
print(f"Done: \n'{response.status_code}'\n'{response.text[:20]}...'")
if __name__ == "__main__":
main()
The error output looks very similar to yours
panic: error upgrading connection: the server could not find the requested resource
goroutine 15 [running]:
github.com/pytogo/pytogo/portforward.startForward.func2(0xc0000d22c0, 0xc0004d4440)
/tmp/tmp6ix1mkxq/pkg/mod/github.com/pytogo/pytogo/portforward@v0.0.0-20230126064027-46f3694e074b/portforward.go:228 +0x7d
created by github.com/pytogo/pytogo/portforward.startForward
/tmp/tmp6ix1mkxq/pkg/mod/github.com/pytogo/pytogo/portforward@v0.0.0-20230126064027-46f3694e074b/portforward.go:226 +0x285
[1] 18327 IOT instruction (core dumped) python main-service.py
For now, I will focus on this issue.
Oh, in a "revert" mess I used the wrong version of the go part. Therefore, the version from pypi is broken and the full local version is fine. I have to create a new release 0.4.1 and mark the 0.4.0 version as not useable.
I yanked version 0.4.0 and published 0.4.1. The issue is in the repo also fixed. Could you test it again? Either with git version or 0.4.1.
Nice, thanks for testing