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

2

9

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
Vertical answered 8/12, 2017 at 13:48 Comment(2)
Currently cloud.google.com/pubsub/docs/emulator#using_the_emulator has "The emulator does not support Google Cloud console or gcloud pubsub commands."Bielefeld
BUT see the answer with api_endpoint_overrides/pubsub below, worked for my use case.Bielefeld
L
7

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.
Labbe answered 11/12, 2017 at 16:41 Comment(0)
C
6

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 unset api_endpoint_overrides/pubsub
Calutron answered 20/7, 2022 at 23:27 Comment(2)
This approach worked at least for my very basic use case, thanks! Note: For reverting the override you can also use gcloud config unset api_endpoint_overrides/pubsub (no need to know the actual address)Bielefeld
Alternatively, we can temporarily override the Pub Sub API endpoint that the gcloud pubsub command uses using the official environment variable CLOUDSDK_API_ENDPOINT_OVERRIDES_PUBSUB without modifying the active gcloud CLI config: CLOUDSDK_API_ENDPOINT_OVERRIDES_PUBSUB=http://localhost:PORT/ gcloud pubsub ... This way we can avoid the risk of forgetting to revert changes to the gcloud CLI config.Servant

© 2022 - 2025 — McMap. All rights reserved.