mark
mark

Reputation: 62804

How to display constant values using custom-columns format of kubectl?

I have multiple clusters and I want to check which ingresses do not specify explicit certificate. Right now I use the following command:

~$ k config get-contexts -o name | grep -E 'app(5|3)41.+-admin' | xargs -n1 -I {} kubectl --context {} get ingress -A -o 'custom-columns=NS:{.metadata.namespace},NAME:{.metadata.name},CERT:{.spec.tls.*.secretName}' | grep '<none>'
argocd               argo-cd-argocd-server    <none>
argocd                argo-cd-argocd-server                                <none>
reference-app         reference-app-netcore-ingress                        <none>
argocd                  argo-cd-argocd-server                 <none>
argocd         argo-cd-argocd-server   <none>
test-ingress   my-nginx                <none>

~$

I want to improve the output by including the context name, but I can't figure out how to modify the custom-columns format to do that.

Upvotes: 0

Views: 358

Answers (1)

P....
P....

Reputation: 18381

The below command would Not yield the exact desired output, but it will be close. using jsonpath, it's possible:

kubectl config get-contexts -o name  |  xargs -n1 -I {} kubectl  get ingress -A -o jsonpath="{range .items[*]}{} {.metadata.namespace} {.metadata.name} {.spec.tls.*.secretName}{'\n'}{end}" --context {}

If the exact output is needed, then the kubectl output needs to be looped in the bash loop. Example:

kubectl config get-contexts -o name  |  while read context; do k get ingress -A -o 'custom-columns=NS:{.metadata.namespace},NAME:{.metadata.name},CERT:{.spec.tls.*.secretName}' --context "$context" |awk -vcon="$context" 'NR==1{$0=$0FS"CONTEXT"}NR>1{$0=$0 FS con}1'; done |column -t
NS       NAME                  CERT            CONTEXT
default  tls-example-ingress   testsecret-tls  [email protected]
default  tls-example-ingress1  testsecret-tls  [email protected]
default  tls-example-ingress2  <none>          [email protected]

To perform post-processing around the header and context, the awk command was used. Here is some details about it:

Command:

awk -vcon="$context" 'NR==1{$0=$0FS"CONTEXT"}NR>1{$0=$0 FS con}1'; done |column -t

-vcon="$context": This is to create a variable called con inside awk to store the value of bash variable($context).
NR==1: Here NR is the record number(in this case line number) and $0 means record/line.
NR==1{$0=$0FS"CONTEXT"}: This means, on the 1st line, reset the line to itself followed by FS(default is space) followed by a string "CONTEXT".
Similarly, NR>1{$0=$0 FS con} means, from the 2nd line onwards, append the line with FS followed by con.
1 in the end is the tell awk to do the print.

Upvotes: 1

Related Questions