Google cloud run: Can a service know its own url?

妖精的绣舞 提交于 2021-01-29 22:20:30

问题


I'm wondering if a container deployed on cloud run can somehow obtain its own service url or is it impossible?

I'm wanting to know this because I want a cloud run worker that creates google cloud tasks for itself.

If it is possible, how can it be done?


回答1:


If you know the service name, you can make a GET HTTP request to https://{endpoint}/apis/serving.knative.dev/v1/{name}

Method: namespaces.services.get

For example :

curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)"  https://us-central1-run.googleapis.com/apis/serving.knative.dev/v1/namespaces/your-project/services/your-service| grep url

"url": "https://cloud-run-xxxxxxxxxx-uc.a.run.app"



回答2:


I wrote an article to self call Cloud Run service to prevent Cold Start. The code that I wrote in Go is in my github

The idea is to call the metadata server to find the project number and the region (like this you don't have this hardcoded or in env var), and then you call the namespace API.

If you need help to write it in another language, let me know.



来源:https://stackoverflow.com/questions/65628822/google-cloud-run-can-a-service-know-its-own-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!