I'm trying to use Kubernetes Go-Client in my program (which will run outside my Kubernetes cluster) which requires access to config file. The config file requires some token, service account details etc so that the program can access the Kubernetes cluster.
The config file is of the form below:
apiVersion: v1
kind: Config
users:
- name: testsa
user:
token: my-token
clusters:
- cluster:
certificate-authority-data: my-cert
server: my-server
name: self-hosted-cluster
contexts:
- context:
cluster: self-hosted-cluster
user: testsa
name: test-name
current-context: test-context
In the above file I need to give my-token
, my-cert
& my-server
as environment variables since I can't hard-code this in the file present in the repository due to security reason. How can I do that?
Thanks Markus for the hint link.
I am writing the answer in Go as the original link showed how to do it from command line. The steps are as follows:
${X}
. In my case e.g. I replaced my-token
with ${my-token}
and so on.X
as an environment variable so that your code can access it during runtime. e.g. by doing export X="abcd"
in command line.config
.package main
import (
"os"
"os/exec"
)
func main() {
mytoken := os.Getenv("mytoken")
part := fmt.Sprintf("s/${mytoken}/%s/g", mytoken)
command := exec.Command("sed", "-i", "", "-e", part, "config")
command.Run()
}
This will do the required replacement during runtime.