1

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?

Alex Flint
  • 6,040
  • 8
  • 41
  • 80

1 Answers1

0

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")

Ryan Dawson
  • 11,832
  • 5
  • 38
  • 61