how to merge output from two kubectl commands

4/5/2019

Looking for a way to merge / join output from two different kubectl get commands. Output below modified to protect the innocent...

I've confirmed that the json output of the kubectl get pods does not contain the node labels desired to be displayed alongside the pod output.

WORKS - command to get boshid label from the node

$ kubectl get no -L bosh.id -o=custom-columns=NODE:.metadata.name,BOSHID:.metadata.labels."bosh\.id"
NODE                                   BOSHID
89a7a2dc-7468-4163-90fe-f043e408d6af   fec06254-467a-4bdf-983d-f99b7143a667
d4674474-7e0c-49aa-847a-287aa6c1e803   898fff19-3bd5-42d2-8697-0710b0b8baff
fe2be367-a407-4c15-92e7-b0d8918b7e7b   cd9179dd-731a-4d01-8541-4e86355d4457

WORKS - command to get the nodes each pod is on

$ kubectl get po -n pks-system -o wide
NAME                               READY   STATUS    RESTARTS   AGE   IP             NODE                                   NOMINATED NODE
fluent-bit-4kmzx                   1/1     Running   0          1d    ************   fe2be367-a407-4c15-92e7-b0d8918b7e7b   <none>
fluent-bit-cg26h                   1/1     Running   0          1d    ************   89a7a2dc-7468-4163-90fe-f043e408d6af   <none>
fluent-bit-ddqzh                   1/1     Running   0          1d    ************   d4674474-7e0c-49aa-847a-287aa6c1e803   <none>
sink-controller-57df674b84-mbvcz   1/1     Running   0          1d    ************   89a7a2dc-7468-4163-90fe-f043e408d6af   <none>

DESIRED RESULTS - command that lists the node and boshid each pod is on

$ kubectl get po (some magic here)
NAME                               READY   STATUS    RESTARTS   AGE   IP             NODE                                   BOSHID
fluent-bit-4kmzx                   1/1     Running   0          1d    ************   fe2be367-a407-4c15-92e7-b0d8918b7e7b   cd9179dd-731a-4d01-8541-4e86355d4457
fluent-bit-cg26h                   1/1     Running   0          1d    ************   89a7a2dc-7468-4163-90fe-f043e408d6af   fec06254-467a-4bdf-983d-f99b7143a667
fluent-bit-ddqzh                   1/1     Running   0          1d    ************   d4674474-7e0c-49aa-847a-287aa6c1e803   898fff19-3bd5-42d2-8697-0710b0b8baff
sink-controller-57df674b84-mbvcz   1/1     Running   0          1d    ************   89a7a2dc-7468-4163-90fe-f043e408d6af   fec06254-467a-4bdf-983d-f99b7143a667
-- sitnam81
kubectl
kubernetes
linux

2 Answers

5/14/2019

I am afraid that create your desired output in Kubernetes is impossible. However it can be done by script (i.e. python or bash)

I am not good at scripting but I was able to create short script in Bash which shows almost desired view.

script.sh

#!/bin/bash
pods=$(kubectl get pods -owide | tr -s " " |cut -d " " -f 1-7 | tail -n +2)

nodes=$(kubectl get nodes -L node.sh -o=custom-columns=NODE:.metadata.name,ContainerID:.metadata.annotations."container\.googleapis\.com/instance_id" | tail -n +2)

echo -e "POD                   READY STATUS RESTARTS AGE IP NODE                              BoshID"
echo "$pods" | while read LINE
  do
    nodeName=$(echo "$LINE" | cut -d ' ' -f 7)
    goutput=$(echo "$nodes" | grep "$nodeName" | tr -s ' '| cut -d ' ' -f 2)
    echo "$LINE $goutput"
  done

My output

$ ./skrypt.sh
POD                   READY STATUS RESTARTS AGE IP NODE                              ContainerID
nginx-7b9899ff5f-6lk87 1/1 Running 0 16h 10.48.4.3 gke-stc-default-pool-ba33922c-fsf3 7950529300866259659
nginx-7b9899ff5f-cwwrp 1/1 Running 0 16h 10.48.4.2 gke-stc-default-pool-ba33922c-fsf3 7950529300866259659
nginx-7b9899ff5f-x5jwv 1/1 Running 0 17m 10.48.6.3 gke-stc-default-pool-ba33922c-kzcx 8511204661082446539

In your case script should looks like:

#!/bin/bash
pods=$(kubectl get pods -n pks-system -owide | tr -s " " |cut -d " " -f 1-7 | tail -n +2)

nodes=$(kubectl get nodes -L node.sh -o=custom-columns=NODE:.metadata.name,BOSHID:.metadata.labels."bosh\.id" | tail -n +2)

echo -e "POD                   READY STATUS RESTARTS AGE IP NODE                              BOSHID"
echo "$pods" | while read LINE
  do
    nodeName=$(echo "$LINE" | cut -d ' ' -f 7)
    goutput=$(echo "$nodes" | grep "$nodeName" | tr -s ' '| cut -d ' ' -f 2)
    echo "$LINE $goutput"
  done
-- PjoterS
Source: StackOverflow

10/6/2019

Came back to this and decided to go with a function and using the join command. Added the following to my ~/.bashrc -

function pks-po() {
  # using namespace input, joins kubectl get pods output
  #   +NAME  +READY  +STATUS  +RESTARTS  +AGE  -IP  +NODE*  -NOMINATED NODE  -READINESS GATES
  # with kubectl get nodes output
  #   -NAME*  -STATUS  -ROLES  -AGE  -VERSION  +BOSH.ID  +SPEC.IP  +ZONE  +PKS.UUID
  ns=${1:-default}
  kubectl get pods -n ${ns} --no-headers -o wide|sort -nk 7b > /tmp/k1.txt
  kubectl get nodes --no-headers -L bosh.id,spec.ip,failure-domain.beta.kubernetes.io/zone,pks-system/cluster.uuid|sort -nk 1b > /tmp/k2.txt
  join -1 7 -2 1 -o 1.1 1.2 1.3 1.4 1.5 1.7 2.6 2.7 2.8 2.9 /tmp/k{1,2}.txt | \
    sed -e '1i\NAME READY STATUS RESTARTS AGE NODE BOSH.ID NODE.IP ZONE PKS.UUID' | \
    column -t
}

Now I can just run the following:

pks-po default
-- sitnam81
Source: StackOverflow