abrochard / kubel

Emacs extension for controlling Kubernetes with limited permissions
GNU General Public License v3.0
259 stars 41 forks source link

unexpected kubel--get-containers output on windows due to single quote (') usage for jsonpath #83

Open nabero opened 2 years ago

nabero commented 2 years ago

Hello,

Getting logs from a single container pod fails under windows. It seems that the container name is single quoted in the cmd string.

Here's the output from kubel-process buffer: command: kubectl --context mycontext logs --tail=100 pod-name-0 'container-name'

Unfortunately single quote doesn't seem to be valid in cmd.exe. The same command with double or without quotes works.

From my understanding, kubel--get-containersoutput is single quoted because single quote usage in jsonpath

C:\Users\user>kubectl get pods -n cvaas pod-name-0 -o jsonpath="{.spec.%s[*].name}"

C:\Users\user>kubectl get pods -n cvaas pod-name-0 -o jsonpath='{.spec.%s[*].name}'
''

Note sure why the command has the single container name inside the quotes (my elisp reading skills are...), but I could confirm replacing the single quotes from jsonpath by escaped double quotes fixes the issue.

Can't guarantee I'll be able to submit a PR with fully tested code before next week. But here's the patch in the meantime : kubel-windows-support.txt, only tested the kubectl log part though.

Tested with : kubectl version : 1.23 kubel version (latest master): b598b21988b8d5b2f44803a31f891f12ea511982 Windows 10.x

abrochard commented 2 years ago

Hi! Thank you for reporting this! Unfortunately I do not use a windows machine so I cannot reproduce or confirm that your patch fix it, but I can test that it won't break for Linux/Mac. No rush at all for a PR, I'm pretty swamped myself, but I thank you in any case.

Suzamax commented 1 year ago

You can use the Linux (WSL2) kubectl :-)

Just use these files:

kubectl.bat

@echo off

@rem Just run the PowerShell script.

powershell -NoProfile -ExecutionPolicy Bypass -F "C:\Users\carlos.canellas\bin\kubectl.ps1" %*

kubectl.ps1

wsl -d Ubuntu-22.04 --exec bash -c "/home/carlos/kubectl.sh $args"

kubectl.sh

IMPORTANT: it must be edited on Linux due to CRLF/LF issues...

#!/usr/bin/env bash

export PATH=/home/carlos/google-cloud-sdk/bin:$PATH

kubectl $@

Replace the paths according to your current setup :-)

jreicher commented 1 year ago

I can confirm this issue, and it's a problem with all the jsonpath queries (namespaces, resources, etc.). The single quotes end up at the ends of the lists literally, and so once the list is split one item has a single quote before, and one item has a single quote after. In the case of a singleton list (viewing log of a pod with one container) the item is entirely quoted.

However I fixed this by removing the single quotes altogether. I'm not sure they are needed on Mac/Linux either?

abrochard commented 1 year ago

Hi @jreicher ,

However I fixed this by removing the single quotes altogether. I'm not sure they are needed on Mac/Linux either?

What is your command extactly? kubectl get pods [pod] -o jsonpath={.spec.%s[*].name} with no single quote around the jsonpath arg?