txn2 / kubefwd

Bulk port forwarding Kubernetes services for local development.
https://imti.co/kubernetes-port-forwarding/
Apache License 2.0
3.78k stars 205 forks source link

Context names with special characters break generated hostnames #240

Closed hosswald closed 2 years ago

hosswald commented 2 years ago

First of all, thanks for the amazing tool. I've been using Kube Forwarder so far and was missing the ability wo forward one or more clusters all at once, and having to come up with ports because everything is accessible via localhost only.

However, I found two problems that make my use-case more complicated. I have multiple OpenShift contexts that I need to connect to at the same time and that I log into using oc login. This command creates contexts in my .kube/config that are named like this: project-name/cluster-name:6443/username

Based on this, kubefwd creates hostnames like this from that: service-name.namespace.project-name/cluster-name:6443/username

Forward slashes and colons in hostnames are obviously not so great. While some programs don't have a problem with that (e.g. psql --host), most programs don't like this at all because it cannot be parsed. Add a port and it becomes even more obvious: service-name.namespace.project-name/cluster-name:6443/username:5432

My suggestion is to add for each hostname generated in the hosts file a variant where any characters not allowed in hostnames (in my case: "/" and ":") are replaced by "-" or "_".

My workaround was using kubectl config rename-context. When I log in again (oc login tokens expire after 24h), it will add a new context, but the credentials are also used for the renamed context, which I can pass to kubefwd moving forward.

Bonus question: I tried to work around this using --domain, however I could not find a way to make this work with multiple contexts and namespaces(use --domain d1 for --context c1 --namespace n1 and --domain d2 for --context c2 --namespace n2).