fix port forwarding in k8s environments #51
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem Statement
In Kubernetes environments, the hostname command will return something like
pod-bcf579d67-c9plq
instead of justpod
. And since you can't know the pod name ahead of time, the value being matched for in /config/nat.conf will be just 'pod' and will fail to match, causing the NAT_ENTRY variable to be empty and enter the dhclient if statement instead of setting the static ip logic. This means port forwarding won't work.Description of the change
This change checks for the presence of the
KUBERNETES_SERVICE_HOST
environment variable to determine if the client_init.sh script is being run in k8s. If it is, it strips off the pod name after the first dash, so you'll get just the deployment name, which you can then set in pod-gateway chart values publicPorts.hostname and it will match.If the env variable isn't present, then it proceeds with the full value from hostname.
Benefits
Port forwarding in Kubernetes will now work properly.
Possible drawbacks
The only limitation with this is if the deployment name has a dash in it, under those circumstances the pod name would be cut off too early. You could probably work around this by cutting off the hostname in the pod-gateway chart.
I've tested this in my own cluster in kubernetes and it worked, the static ip was properly set to 10 per my config and the traffic is getting forwarded to my pod. https://github.com/jgilfoil/home-cluster/pull/121/files
I did not test this change in a regular docker environment.