How to override Go environment variables with Helm

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

How to override Go environment variables with Helm

问题

如何使用Helm在Go中覆盖.env文件中的环境变量?

对于C#,我会这样做:

appsettings.json中:

{
    "Animals":{
        "Pig": "Squeek"
    },
}

values.yaml中:

animals:
  pig: "Oink"

configmap.yaml中:

apiVersion: v1
kind: ConfigMap
metadata:
  name: animal-configmap
pig: {{ .Values.animals.pig }}

最后,在deployment.yaml中:

spec:
  ...
  template:
    ...
    spec:
      ...
      containers:
          ...
          env:
            - name: Animals__Pig
              valueFrom:
                configMapKeyRef:
                  name: animal-configmap  
                  key: pig

注意双下划线__。如何更新Go中的环境变量值?

以下是Go的.env文件示例:

PIG=SQUEEK
英文:

How do I override environment variables in a .env file for Go with Helm?

With C# I do the following:

In appsettings.json:

{
    "Animals":{
        "Pig": "Squeek"
    },
}

In values.yaml:

animals:
  pig: "Oink"

In configmap.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: animal-configmap
pig: {{ .Values.animals.pig }}

And finally in deployment.yaml:

spec:
  ...
  template:
    ...
    spec:
      ...
      containers:
          ...
          env:
            - name: Animals__Pig
              valueFrom:
                configMapKeyRef:
                  name: animal-configmap  
                  key: pig

Not the double __.
How would one go about updating an environment value for Go?

Here is the Go .env file example:

PIG=SQUEEK

答案1

得分: 1

如果你的Go代码正在检索一个普通的环境变量

pig := os.Getenv("PIG")

那么Kubernetes清单应该将该名称用作环境变量的 name:

env:
  - name: PIG
    valueFrom: {...}

双下划线在Unix环境变量或Kubernetes清单中没有任何特殊含义,在你的初始示例中,它看起来像是C#框架将环境变量映射到应用程序属性时分隔组件的方式。如果你直接使用环境变量,你不需要做任何特殊处理。

英文:

If your Go code is retrieving an ordinary environment variable

pig := os.Getenv("PIG")

then the Kubernetes manifest should use that name as the environment variable name:

env:
  - name: PIG
    valueFrom: {...}

The double-underscore doesn't have any special meaning in Unix environment variables or the Kubernetes manifest, in your initial example it looks like the way the C# framework separates components when it maps environment variables to application properties. If you're using environment variables directly you don't need to do anything special.

答案2

得分: 0

你可以使用"github.com/joho/godotenv"包来读取.env文件。由于你不想将现有的环境变量与.env文件中的变量混淆,你可以创建一个映射并将变量设置到其中。

如果你有一个像这样的.env文件:

HELLO=word

你可以这样读取它:

package main

import (
	"fmt"
	"log"

	"github.com/joho/godotenv"
)

func main() {
	var envs map[string]string
	envs, err := godotenv.Read(".env")

	if err != nil {
		panic("Error loading .env file")
	}

	name := envs["HELLO"]

	fmt.Println(name)
}

如果你设置了一个环境变量,你仍然可以访问文件中定义的值:

$ HELLO=ping go run main.go

world

然后你可以从envs变量中访问文件中的变量。

英文:

You can use the "github.com/joho/godotenv" package to read .env files. Since you don't want to mix up existing environment variables with those from the .env file, you can create a map and set the vars to it.

If you have a .env file like this:

HELLO=word

You can read it like this:

package main

import (
	"fmt"
	"log"

	"github.com/joho/godotenv"
)

func main() {
	var envs map[string]string
	envs, err := godotenv.Read(".env")

	if err != nil {
		panic("Error loading .env file")
	}

	name := envs["HELLO"]

	fmt.Println(name)
}

If you set a var env, you can still access the value defined on the file:

$ HELLO=ping go run main.go

world

Then you access the file vars from the envs var.

huangapple
  • 本文由 发表于 2022年9月8日 22:23:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/73650678.html
匿名

发表评论

匿名网友

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

确定