Is it possible to connect the gcloud CLI to a Pubsub emulator instance

1.9k Views Asked by At

After starting the PubSub emulator using the gcloud CLI and loading the environment variables accordingly, the gcloud CLI remains authenticated with our Google production account. Basically, we did:

# Starting the emulator in one shell
gcloud beta emulators pubsub start

# In a different shell
$(gcloud beta emulators pubsub env-init)
gcloud pubsub topics list

However, our production topics are listed here. The expected output would be an empty list, as we're trying to list the topics from the freshly started PubSub emulator, which contains nothing at start up.

We are used to start the Bigtable emulator and connect to it without issues with the cbt CLI which is aware of the BIGTABLE_EMULATOR_HOST. Is it possible to have the same kind of tool to manage/check PubSub emulator resources? It simply seems that gcloud is not aware of *_EMULATOR_HOST variables.

Thank you in advance,

PS: Output of our gloud version:

Google Cloud SDK 182.0.0
alpha 2017.09.15
beta 2017.09.15
bigtable
bq 2.0.27
cbt
core 2017.12.01
gcloud
gsutil 4.28
kubectl
pubsub-emulator 2017.09.15
2

There are 2 best solutions below

0
On

There is a Feature Request that exists for this functionality in the Google Public Issue Tracker. The engineering team is aware of this request and are currently investigating it with no ETA. All further communications will occur in that Feature Request.

  • Note it is recommended to report all Google-end feature requests and issues in the Google Public Issue Tracker, as to notify the appropriate team.
1
On

Had the same issue and this worked for me

The Google team is not really in favor of this feature and their reason makes sense to me. You can read it in the link @Jordan provided.

The workaround is to override the API endpoint manually.

First, find out the port number of the emulator (you already have it as you run the command :) )

# gcloud beta emulators pubsub env-init
export PUBSUB_EMULATOR_HOST=0.0.0.0:8538

Then override the API endpoint of gcloud to point to your emulator. Note that the format MUST be http://localhost:PORT/ (it has to be http protocol not https and it has to end with /).

gcloud config set api_endpoint_overrides/pubsub http://$PUBSUB_EMULATOR_HOST/

Don't forget to revert it back

gcloud config set api_endpoint_overrides/pubsub https://pubsub.googleapis.com/