Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create kubernetes secret from .json file

Tags:

kubernetes

I have a json file with some keys like this:

{
  "a":"someval"
  "b":"someval"
  .. more keys
}

How do I add these keys to a secret in kubernetes?

When I try $ kubectl create secret generic mysecret --from-file=file.json it returns a secret containing the file, but I want to map the contents of the file to the secret, not add the file as a secret.

Output:

$ kubectl get secret mysecret -o yaml

apiVersion: v1
data:
  file.json: #base64 encoded stuff here.
kind: Secret

Wanted output:

$ kubectl get secret mysecret -o yaml

apiVersion: v1
data:
  a: someval
  b: someval
kind: Secret

What am I doing wrong?

like image 871
Mr.Turtle Avatar asked May 09 '19 13:05

Mr.Turtle


People also ask

How do I create a Dockerconfigjson secret?

Create a Secret based on existing credentialsset the name of the data item to .dockerconfigjson. base64 encode the Docker configuration file and then paste that string, unbroken as the value for field data[".dockerconfigjson"] set type to kubernetes.io/dockerconfigjson.

How do I get Kubernetes secret data?

You can also provide Secret data using the --from-literal=<key>=<value> tag. This tag can be specified more than once to provide multiple key-value pairs. Note that special characters such as $ , \ , * , = , and ! will be interpreted by your shell and require escaping.


1 Answers

If you have flat (not nested) JSON then try this (assuming you have jq tool installed):

kubectl create secret generic test --from-env-file <(jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" YOUR_FILE.json)
like image 86
Vasili Angapov Avatar answered Nov 15 '22 10:11

Vasili Angapov