Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monitor kubectl logs when port forwarding and retry on error #2543

Merged
merged 1 commit into from
Jul 25, 2019

Conversation

nkubala
Copy link
Contributor

@nkubala nkubala commented Jul 24, 2019

When kubectl port-forward encounters an error (most commonly from the container backing a resource being restarted), an error is logged to stderr, but the process does not actually return with an exit code, or notify the caller in any way than an error occurred. During port forwarding, skaffold verifies that a port is correctly forwarded by attempting to attach to the specified port and ensuring that a listening connection cannot be established, but since kubectl hangs onto the port and continues to run successfully when an error is actually encountered, this check succeeds erroneously.

To handle this, the only indication we have from kubectl that something has gone wrong is from its logs to stderr, so we attach to these logs and search the stream for "error forwarding port". A Monitor() call is kicked off in a separate goroutine for each forwarded port, and on seeing this error string in the logs for the associated kubectl port-forward process, it will issue a callback to the EntryManager to restart the port forwarding operation entirely.

Additionally, kubectl itself will not actually log this error until the first failed connection to the forwarded port, so in the Monitor() call we issue a ping to the port once per second, to tease out any errors that kubectl might encounter. This is very suboptimal, but the implementation (and shortcomings) of kubectl port-forward give us very little to work with here.

update: I removed the ping from the monitor, since this could be really spammy for services where connection logs actually matter (e.g. nginx). after a redeploy, the first attempted connection to a forwarded service will fail, but skaffold will see this and trigger a reconnection. again, not great UX, but this is ultimately a workaround for a bug in kubectl.

Fixes #2523

@nkubala
Copy link
Contributor Author

nkubala commented Jul 25, 2019

thinking about this more, i think we might not want the ping in the monitor, it could get really spammy. i'm thinking either make it much less frequent, or remove it entirely. thoughts?

@nkubala nkubala force-pushed the port-forward-monitor branch from 044b2f8 to 72c1f26 Compare July 25, 2019 19:47
Copy link
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I was worried about continuously pinging the app as that can create spammy connection logs.

@nkubala nkubala merged commit 180f1af into GoogleContainerTools:master Jul 25, 2019
@nkubala nkubala deleted the port-forward-monitor branch July 25, 2019 20:55
@codecov
Copy link

codecov bot commented Jul 25, 2019

Codecov Report

Merging #2543 into master will decrease coverage by 0.14%.
The diff coverage is 3.84%.

Impacted Files Coverage Δ
...ffold/kubernetes/portforward/port_forward_entry.go 100% <ø> (ø) ⬆️
...affold/kubernetes/portforward/kubectl_forwarder.go 0% <0%> (ø) ⬆️
...g/skaffold/kubernetes/portforward/entry_manager.go 87.23% <14.28%> (-5.95%) ⬇️

1 similar comment
@codecov
Copy link

codecov bot commented Jul 25, 2019

Codecov Report

Merging #2543 into master will decrease coverage by 0.14%.
The diff coverage is 3.84%.

Impacted Files Coverage Δ
...ffold/kubernetes/portforward/port_forward_entry.go 100% <ø> (ø) ⬆️
...affold/kubernetes/portforward/kubectl_forwarder.go 0% <0%> (ø) ⬆️
...g/skaffold/kubernetes/portforward/entry_manager.go 87.23% <14.28%> (-5.95%) ⬇️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Port forwarding doesn't work anymore since update 0.33.0 -> 0.34.0
3 participants