无法在GCP中指定gflag

huangapple go评论91阅读模式
英文:

Unable to specify gflag in GCP

问题

我有一个Go服务器程序,在运行时需要传递特定的gflag值,但我无法在GCP的app.yaml文件中指定。我对GCP还不太熟悉,所以这可能是一个未经启动的问题。

这是我的Go代码结构:

project_root
   |
   |___cmd
        |___ main
                |___ main.go
                |___ foo_handlers.go (定义了FooHandler(w, r))
                |___ bar_handlers.go (定义了BarHandler(w, r))

main.go是一个典型的Golang HTTP服务器程序,大致具有以下结构:

func main() {
	// Flags
	flag.StringVar(
        &SomeFlagVar, 
        "some_flag", // flag名称
        "dev_value", // 默认值
        "Some Flag")
	flag.Parse()

	// Foo
	http.HandleFunc("/foo", FooHandler)

	// Bar
	http.HandleFunc("/bar/", BarHandler)

    // Server starting boiler plate code
    http.ListenAndServe(..)

我的GCP app.yaml文件大致如下:

runtime: go114

main: ./cmd/main

handlers:
- url: /.*
  script: auto

这个工作正常,但是正如你所注意到的,我无法传递任何自定义值给some_flag标志。目前,我只是将默认标志值硬编码为GCP部署所需的值,并且我不会提交这个更改,这显然是一个非常粗糙的实现目标的方式。

我尝试将entrypoint配置设置为以下内容:
entrypoint: ./cmd/main/main --some_flag=gcp
或者
entrypoint: .bin/main --some_flag=gcp

但是对于这两种情况,GCP会报错,错误形式为/bin/sh: 1: exec: bin/main: not found

我可以在Heroku中很容易地实现相同的效果,只需指定一个web dyno,如下所示:
bin/main --some_flag=heroku

如何在GCP应用中设置相同的配置?我正在使用GCP标准环境。我不想使用环境变量来实现相同的效果。

谢谢!

英文:

I have a Go server program which needs to be passed a particular gflag value at runtime, but I am unable to specify that in GCP's app.yaml file. I am pretty new to GCP, so this might be an uninitiated query.

Here's my Go code structure:

project_root
   |
   |___cmd
        |___ main
                |___ main.go
                |___ foo_handlers.go (defines FooHandler(w, r))
                |___ bar_handlers.go (defines BarHandler(w, r))

main.go is your typical Golang HTTP server program roughly having this structure:

func main() {
	// Flags
	flag.StringVar(
        &SomeFlagVar, 
        "some_flag", // flag name
        "dev_value", // default value
        "Some Flag")
	flag.Parse()

	// Foo
	http.HandleFunc("/foo", FooHandler)

	// Bar
	http.HandleFunc("/bar/", BarHandler)

    // Server starting boiler plate code
    http.ListenAndServe(..)

My GCP app.yaml file roughly looks like this:

runtime: go114

main: ./cmd/main

handlers:
- url: /.*
  script: auto

This works fine but as you would note, I am unable to pass any custom value to the some_flag flag. For now, I just hardcode the default flag value to the value preferred for deployment in GCP and I don't check that change in, which is obviously a very crude way of achieving the goal.

I have tried setting the entrypoint configuration to thus:
entrypoint: ./cmd/main/main --some_flag=gcp
or
entrypoint: .bin/main --some_flag=gcp

But for both these cases, GCP errors out with errors of the form /bin/sh: 1: exec: bin/main: not found.

I could achieve the same very easily in Heroku by specifying a web dyno like so:
bin/main --some_flag=heroku

How do I set the same in a GCP app? I am using GCP Standard Environment. And I do not want to use environment variables for the same.

Thanks!

答案1

得分: 0

你不能在App Engine中向主函数传递标志。你有三种解决方案:

  1. 继续使用App Engine标准环境,使用环境变量代替标志。
  2. 使用App Engine弹性环境,自定义运行时。这样,你需要提供一个Dockerfile,并且可以指定所需的入口点。但是,App Engine弹性环境不能缩放到0,因此对于小型和低流量的应用来说,成本相对较高。
  3. 类似于App Engine弹性环境,定义一个Dockerfile,但这次部署到Cloud Run

个人建议使用环境变量,因为如果你想要更改值,你不需要重新构建容器,只需更改值并部署一个新的修订版(相同的容器,但上下文不同)。

英文:

You can't pass flags to your main function with App Engine. You have 3 solutions:

  1. Stay with App Engine standard and use environment variable instead of flags
  2. Use App Engine Flexible, with a custom runtime. Like that, you have to provide a Dockerfile and therefore you can specify the entrypoint that you wish. But, App Engine flexible doesn't scale to 0 and thus is quite expensive for a small and low traffic app
  3. As App Engine Flexible, define a Dockerfile but this time deploy it on Cloud Run.

Personally, I recommend to use Environment variables, because, if you want to change the values, you don't have to rebuild your container, only to change the value and deploy a new revision (same container, but different context)

huangapple
  • 本文由 发表于 2021年7月1日 22:39:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/68212214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定