[英]How to override Go environment variables with Helm
How do I override environment variables in a.env file for Go with Helm?如何使用 Helm 覆盖 Go 的 .env 文件中的环境变量?
With C# I do the following:使用 C# 我执行以下操作:
In appsettings.json
:在
appsettings.json
:
{
"Animals":{
"Pig": "Squeek"
},
}
In values.yaml
:在
values.yaml
:
animals:
pig: "Oink"
In configmap.yaml
:在
configmap.yaml
中:
apiVersion: v1
kind: ConfigMap
metadata:
name: animal-configmap
pig: {{ .Values.animals.pig }}
And finally in deployment.yaml
:最后在
deployment.yaml
中。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? go 如何更新 Go 的环境值?
Here is the Go .env
file example:这是 Go
.env
文件示例:
PIG=SQUEEK
If your Go code is retrieving an ordinary environment variable如果您的 Go 代码正在检索普通环境变量
pig := os.Getenv("PIG")
then the Kubernetes manifest should use that name as the environment variable name:
那么 Kubernetes 清单应该使用该名称作为环境变量
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.双下划线在 Unix 环境变量或 Kubernetes 清单中没有任何特殊含义,在您的初始示例中,它看起来就像 C# 框架将组件映射到应用程序变量时的方式。 If you're using environment variables directly you don't need to do anything special.
如果您直接使用环境变量,则不需要做任何特别的事情。
You can use the "github.com/joho/godotenv"
package to read .env
files.您可以使用
"github.com/joho/godotenv"
github.com/joho/godotenv”package 来读取.env
文件。 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.由于您不想将现有环境变量与
.env
文件中的环境变量混合,您可以创建一个 map 并将变量设置为它。
If you have a .env
file like this:如果您有这样的
.env
文件:
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:如果你设置了一个 var env,你仍然可以访问文件中定义的值:
$ HELLO=ping go run main.go
world
Then you access the file vars from the envs
var.然后您从
envs
var 访问文件 vars。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.