lens icon indicating copy to clipboard operation
lens copied to clipboard

pod shell not working while proxy configured

Open funcx27 opened this issue 4 years ago • 16 comments

Describe the bug

When I perform Pod shell with http proxy. I got error

To Reproduce Steps to reproduce the behavior:

  1. configure proxy
  2. go to pod
  3. click Pod shell and see error

Expected behavior kubectl exec into pod

Environment (please complete the following information):

  • Lens Version: 4.0.8
  • OS: windows10
  • Installation method (e.g. snap or AppImage in Linux): scoop

Logs: When you run the application executable from command line you will see some logging output. Please paste them here:

error: unable to upgrade connection: error dialing backend: dial tcp 192.168.7.28:6443: connectex: A connection attempt failed because the connected party did
 not properly respond after a period of time, or established connection failed because connected host has failed to respond.

funcx27 avatar Jan 26 '21 08:01 funcx27

Facing a similar issue. I'm behind a corp proxy. At this point I'm unable to shell into the pods

gtskaushik avatar Feb 03 '21 07:02 gtskaushik

Similar issue on 4.1.1 with corporate proxy and GKE pods, Windows 10. Proxy is set both in preferences and cluster settings.

Running shell to pod will end with error error: unable to upgrade connection: error dialing backend: dial tcp XXX:443: connectex: No connection could be made because the target machine actively refused it.

Logs show Error while proxying request: error dialing backend: dial tcp XXX:443: connectex: No connection could be made because the target machine actively refused it.\n","error":true,"meta":{"id":"XX","name":"XXX","initialized":true,"ready":true,"online":true,"accessible":true,"disconnected":false}}

Running the same via Powershell directly (not in Lens) works fine, running other kubectl commands (in Lens) work fine (e.g. kubectl get pods -n XX from inside Lens terminal works fine) . Adding localhost or 127.0.0.1 to no_proxy env does not help.

CZmikee avatar Feb 18 '21 11:02 CZmikee

I have the same issue. Using a proxy everything works fine except for exec. Exec is missing the HTTPS_PROXY=localhost:8888 settings that are set in the lens config. If I take the same command lens is running to perform the exec and add HTTPS_PROXY environment variable the kubectl exec command works.

lens version = 4.1.2

jeremyd avatar Feb 23 '21 18:02 jeremyd

Can't wait for the fix

mirkoszy avatar Mar 17 '21 10:03 mirkoszy

Waiting for this

gtskaushik avatar Mar 17 '21 10:03 gtskaushik

This is still an issue in Lens 4.2.1 on Macos. All other kubectl commands from within Lens terminal seem to work fine. Only kubectl exec doesn't work when a proxy is set. I see $HTTPS_PROXY env var is set in the terminal though but don't understand why it doesn't work for kubectl exec.

gjaldon avatar Apr 12 '21 01:04 gjaldon

Same issue here. Powershell in terminal has a weird behavior. I can't access to environment vars:

Get-ChildItem -Path Env:
Get-ChildItem : Un élément avec la même clé a déjà été ajouté. Au caractère Ligne:1 : 1

  • Get-ChildItem -Path Env:\
  •   + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], ArgumentException
      + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.GetChildItemCommand
    
    

Same command in another powershell works fine.

jhourque avatar Apr 21 '21 16:04 jhourque

Shell/exec/port-forward do not work in Lens if cluster is behind http proxy because Lens uses "kubectl proxy" to communicate with the cluster (replacing server url in kubeconfig) which is broken. Root cause is "kubectl proxy" ignores http proxy for "connection upgrades" (spdy protocol) and connects directly instead. The result is you get one of connection refused, unknown host, timeout errors. Example:

# should work
HTTPS_PROXY=ip:port kubectl exec somepod -- uname -a
# this is broken, and is what Lens does
HTTPS_PROXY=ip:port kubectl proxy -p 11111 -v8 --reject-paths='^[^/]' &
kubectl -s localhost:11111 exec somepod -- uname -a

tvanderka avatar Jul 05 '21 22:07 tvanderka

A hacky workaround on Win10 using WSL, based on the comment by @tvanderka:

First make sure kubectl works inside WSL. Then set wsl.exe as "TERMINAL SHELL PATH" in Lens. Finally, add the following snippet to your WSL .bashrc (or equivalent):

if [ -n "$LENS_SESSION" ]; then
        c=$(kubectl config current-context)
        export KUBECONFIG=""
        kubectl config use-context "$c"
fi

Lens is creating a temporary kubectl config refering to the "kubectl proxy" and pointing to it via env variable KUBECONFIG inside the shell it creates to enter pods. This snippet reads the context from the temporary config, unsets it and sets the same context, this time from your normal config. This will probably not work if you use more complex setups / multiple configs.

mmslkr avatar Jul 08 '21 06:07 mmslkr

Hey guys while we don't have a solution I've created a bash script to workaround it on linux/macos.

After the exec command I'm restoring on background the original context to avoid unwanted context updates on your environment.

#!/bin/bash
#https://github.com/lensapp/lens/issues/2021
shopt -s expand_aliases
alias kubectlens="/usr/local/bin/kubectl"
LENS_CONTEXT=$(kubectlens config current-context)
echo "Lens Context: $LENS_CONTEXT"
export KUBECONFIG=""
DEFAULT_CONTEXT=$(kubectlens config current-context)
echo "Default Context: $DEFAULT_CONTEXT"
kubectlens config use-context "$LENS_CONTEXT"
(sleep 1 && kubectlens config use-context "$DEFAULT_CONTEXT" >> /dev/null) &
kubectlens "$@"

Create a bash file with it and point your lens kubectl to this file on preferences.

joaogomez avatar Aug 20 '21 11:08 joaogomez

it's been quite a while here, I want to share a solution I found for mac, worked for my case using zsh:

# shell extension to make openlens work with pod terminal
if [[  "${TERM_PROGRAM}" -eq "OpenLens" ]]; then
        echo "OpenLens Terminal detected overriding kubectl to empty kubeconfig"
        lens_current_context=$(kubectl config current-context)
        echo "running in context ${lens_current_context}"
        kubectl() {
          export KUBECONFIG=""
          command kubectl --context ${lens_current_context} $@
        }
fi

Thanks for all the info here for a better understanding here for building this hacky and yet simple solution

kodem-shayel avatar Mar 02 '23 11:03 kodem-shayel

it's been quite a while here, I want to share a solution I found for mac, worked for my case using zsh:

# shell extension to make openlens work with pod terminal
if [[  "${TERM_PROGRAM}" -eq "OpenLens" ]]; then
        echo "OpenLens Terminal detected overriding kubectl to empty kubeconfig"
        lens_current_context=$(kubectl config current-context)
        echo "running in context ${lens_current_context}"
        kubectl() {
          export KUBECONFIG=""
          command kubectl --context ${lens_current_context} $@
        }
fi

Thanks for all the info here for a better understanding here for building this hacky and yet simple solution

Nice one but my lens (not OpenLens) is using command composition like exec /bin/kubectl ....... so this doesn't work for obvious reasons. Went for solution from @joaogomez.

3RX0 avatar Mar 10 '23 08:03 3RX0

Please tell me someone has solved this problem. What is the proxy settings even good for than?

martinwagenmann avatar May 11 '23 14:05 martinwagenmann

Configure OpenLens -> Preferences -> Terminal -> Terminal Shell Path:

C:\Program Files\PowerShell\7\pwsh.exe

In your pwsh profile exclude everything else for a non OpenLens session with:

if (-not(Test-Path env:LENS_SESSION)) {
...
}

For OpenLens Session add:

if (Test-Path env:LENS_SESSION) {
   $context = $(kubectl config current-context)
   $env:KUBECONFIG = $env:USERPROFILE + "\.kube\config"
   kubectl config use-context $context

glitchracer avatar Aug 25 '23 14:08 glitchracer

The problem unfortunately still exists.

Lens: 2024.1.300751-latest
Extension API: 6.10.33
Electron: 27.2.4
Chrome: 118.0.5993.159
Node: 18.17.1
© 2023 Mirantis, Inc.

Any idea how to solve it?

null9beta avatar Feb 09 '24 16:02 null9beta

Reporting the same issue. For me, the GCP Kubernetes pod shell connects directly and not though proxy. The faulty executable is lens-k8s-proxy.exe

Str1ker17 avatar Mar 11 '24 13:03 Str1ker17