Skip to content

Fix broken port forwarding on K8S 1.31+#218

Open
skissane-medallia wants to merge 1 commit intokubecost:mainfrom
skissane-medallia:fix-port-forwarding-broken-k8s-1.31
Open

Fix broken port forwarding on K8S 1.31+#218
skissane-medallia wants to merge 1 commit intokubecost:mainfrom
skissane-medallia:fix-port-forwarding-broken-k8s-1.31

Conversation

@skissane-medallia
Copy link

What does this PR change?

In K8S 1.31 and later, SPDY-based port forwarding is no longer supported – see project announcement. But the code in pkg/query/portforward.go is written to use that. As a result, attempting to use kubectl cost will fail with this error:

panic: error upgrading connection: Upgrade request required

goroutine 22 [running]:
github.com/kubecost/kubectl-cost/pkg/query.CreatePortForwardForService.func1()
	/home/runner/work/kubectl-cost/kubectl-cost/pkg/query/portforward.go:101 +0x6c
created by github.com/kubecost/kubectl-cost/pkg/query.CreatePortForwardForService in goroutine 1
	/home/runner/work/kubectl-cost/kubectl-cost/pkg/query/portforward.go:98 +0x79c

I modified the code in pkg/query/portforward.go to use the new Websocket API instead.

I was worried that might break things for anyone trying to use this with an older K8S version which doesn't support WebSockets. So I added an environment variable KUBECOST_DISABLE_WEBSOCKETS to revert to the old code (if set to true) if it breaks it for anybody. Maybe it should be a command line option instead, but I was just trying to do the minimal thing necessary to fix my own problem.

Does this PR rely on any other PRs?

How does this PR impact users? (This is the kind of thing that goes in release notes!)

  • Support was added for talking to newer Kubernetes versions (1.31 and higher).
  • Environment variable KUBECOST_DISABLE_WEBSOCKETS=true added to revert to the old pre-1.31 communication mechanism, in case this change breaks using kubectl cost with older Kubernetes versions

Links to Issues or ZD tickets this PR addresses or fixes

None

How was this PR tested?

I have kubecost running under Kubernetes 1.33.

Before this PR, any attempt to use kubectl cost failed on it.

With this PR, it works.

Have you made an update to documentation?

No

http.MethodPost,
reqURL,
)
var dialer httpstream.Dialer
Copy link
Member

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants