Stop kubectl from printing “pod curl deleted” at e

2019-07-26 10:12发布

问题:

I am using kubectl in a script to get the current GKE cluster name like so:

CURRENT_CLUSTER=$(kubectl run curl --rm --restart=Never -it --image=appropriate/curl -- -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/attributes/cluster-name)

Unfortunately kubect prints pod "curl" deleted to standard output, so the result is this:

my-cluster-us-west1pod "curl" deleted

How can I stop kubectl from printing this string?

回答1:

If there were a space between the cluster name and the unwanted text then you could use awk to take the first word of the output:

CURRENT_CLUSTER=$(kubectl run curl --rm --restart=Never -it --image=appropriate/curl -- -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/attributes/cluster-name | awk '{print $1;}')

But there isn't a space so it has to be removed differently, such as using sed to remove 'pod ' and anything after it:

CURRENT_CLUSTER=$(kubectl run curl --rm --restart=Never -it --image=appropriate/curl -- -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/attributes/cluster-name | sed "s/pod .*//g")



标签: kubectl