pytogo / portforward

Kubernetes Port-Forward Go-Edition For Python
https://portforward.readthedocs.io
MIT License
17 stars 6 forks source link

Host value wrongly trimmed #27

Closed caiobrentano closed 1 year ago

caiobrentano commented 1 year ago

Description

I tried the very basic example from README, but the Host server from my kubeconfig is being wrongly trimmed, resulting in a no such host error.

ERROR: error upgrading connection: error sending request: Post "https://eleport-nlb.<REDACTED>/api/v1/namespaces/monitor/pods/vmselect-victoria-metrics-cluster-0/portforward": dial tcp: lookup eleport-nlb.infra.datastax.com: no such host
Traceback (most recent call last):
  File "/Users/caio.passos/Documents/code/external/portforward/cndb-test.py", line 10, in <module>
    with portforward.forward(namespace, pod_name, local_port, pod_port, log_level=portforward.LogLevel.DEBUG):
  File "/Users/caio.passos/.pyenv/versions/3.10.7/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "/Users/caio.passos/Documents/code/external/portforward/portforward.py", line 140, in forward
    raise PortforwardError(err) from None
portforward.PortforwardError: error upgrading connection: error sending request: Post "https://eleport-nlb.<REDACTED>/api/v1/namespaces/monitor/pods/vmselect-victoria-metrics-cluster-0/portforward": dial tcp: lookup eleport-nlb.<REDACTED>: no such host

The problematic code is here: https://github.com/pytogo/portforward/blob/main/internal/portforward.go#L311 It uses strings.TrimLeft, but I believe the best option is strings.TrimPrefix

fmt.Println(strings.TrimLeft("https://teleport.domain", "https://"))
>> eleport.domain
fmt.Println(strings.TrimPrefix("https://teleport.domain", "https://"))
>> teleport.domain

fmt.Println(strings.TrimLeft("https://another.domain", "https://"))
>> another.domain
fmt.Println(strings.TrimPrefix("https://another.domain", "https://"))
>> another.domain

I will push a PR with the small fix

corka149 commented 1 year ago

Hi @caiobrentano.

Thx for the report and the pull request. Everything is clear.