Running remote kubernetes command creates errors, even if the same comand on the machine doesn't

9/13/2019

I'm trying to run remotely a kubernetes command using python and ssh. The command doesn't work if is run remote, but works if is run directly on the machine.

"kubectl get po --all-namespaces -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[0].image}{"\t"}{.metadata.labels.k8s-app}{"\n"}{end}"

If it runs as it is, the recive error is

"Pods [NotFound] .items[*]}"

If I replace ' with " and viceversa the eror is "-

Expecting 'EOF'

Taking in consideration that the command run on machine directly, something is interpreted when is passed remotely to the shell. I tried different combination but are not working.

-- user3541631
bash
kubernetes
python
remote-access

1 Answer

9/17/2019

Posting this as community wiki

As @Orion pointed in comments it seems that your command is not full. I've tested it on mine GKE cluster.

$ kubectl get po --all-namespaces -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[0].
image}{"\t"}{.metadata.labels.k8s-app}{"\n"}{end}
>

When you used jsonpath= you started it with sign ', however you did not close it. That's why Kubernetes is still expecting some values. However, if you will end jasonpath with ' you will receive output like:

$ kubectl get po --all-namespaces -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[0].
image}{"\t"}{.metadata.labels.k8s-app}{"\n"}{end}'
event-exporter-v0.2.4-5f88c66fb7-xwwh6  k8s.gcr.io/event-exporter:v0.2.4        event-exporter
fluentd-gcp-scaler-59b7b75cd7-pw9kf     k8s.gcr.io/fluentd-gcp-scaler:0.5.2     fluentd-gcp-scaler
fluentd-gcp-v3.2.0-6lxd9        gcr.io/stackdriver-agents/stackdriver-logging-agent:1.6.8       fluentd-gcp
fluentd-gcp-v3.2.0-zhtds        gcr.io/stackdriver-agents/stackdriver-logging-agent:1.6.8       fluentd-gcp
heapster-v1.6.1-9bbcd7f79-ld4z7 gcr.io/stackdriver-agents/heapster-amd64:v1.6.1 heapster
kube-dns-6987857fdb-l2dt8       k8s.gcr.io/k8s-dns-kube-dns-amd64:1.14.13       kube-dns
kube-dns-6987857fdb-r97b8       k8s.gcr.io/k8s-dns-kube-dns-amd64:1.14.13       kube-dns
kube-dns-autoscaler-bb58c6784-8vq5d     k8s.gcr.io/cluster-proportional-autoscaler-amd64:1.3.0  kube-dns-autoscaler
kube-proxy-gke-stc1-default-pool-094e5c74-6vk4  k8s.gcr.io/kube-proxy:v1.13.7-gke.8
kube-proxy-gke-stc1-default-pool-094e5c74-htr0  k8s.gcr.io/kube-proxy:v1.13.7-gke.8
l7-default-backend-fd59995cd-xz72d      k8s.gcr.io/defaultbackend-amd64:1.5     glbc
metrics-server-v0.3.1-57c75779f-t2rfb   k8s.gcr.io/metrics-server-amd64:v0.3.1  metrics-server
prometheus-to-sd-2jxr5  k8s.gcr.io/prometheus-to-sd:v0.5.2      prometheus-to-sd
prometheus-to-sd-xmfsl  k8s.gcr.io/prometheus-to-sd:v0.5.2      prometheus-to-sd

As you want to run exact command remotely you have to use " at beginning and the end, same with jsonpatch.

Based on information you have provided, solution for your problem should be below command:

"kubectl get po --all-namespaces -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[0].image}{"\t"}{.metadata.labels.k8s-app}{"\n"}{end}'"
-- PjoterS
Source: StackOverflow