1

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?

2

1 Answer 1

2

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:

  • Replace the fields to be modified in the file with something of the form ${X}. In my case e.g. I replaced my-token with ${my-token} and so on.
  • Here you can set X as an environment variable so that your code can access it during runtime. e.g. by doing export X="abcd" in command line.
  • Say the file name is config.
  • Execute the following code:
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.