17

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?

1
  • If you don't want the contents of a secret to be encoded, you could use a ConfigMap. Commented Jan 4, 2020 at 4:39

4 Answers 4

12

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)
Sign up to request clarification or add additional context in comments.

2 Comments

For anyone running into trouble getting this to work, note that only the lefthand parentheses are escaped in the jq query: \(foo)
This only works if the secret does not contain new lines. If you have e.g. a PEM formatted private key, this method will not work.
7

Try these steps

kubectl proxy --port 8000 &  
curl localhost:8000/api/v1/namespaces/default/secrets 

curl localhost:8000/api/v1/namespaces/default/secrets \
  -X POST -H "Content-Type: application/json" \
  --data '{"metadata":{"name":"mytest"},"stringData":{"a":"test","b":"test","c":"test"}}'

master $ curl localhost:8000/api/v1/namespaces/default/secrets/mytest{
  "kind": "Secret",
  "apiVersion": "v1",
  "metadata": {
    "name": "mytest",
    "namespace": "default",
    "selfLink": "/api/v1/namespaces/default/secrets/mytest",
    "uid": "55736602-725e-11e9-b3a2-0242ac110034",
    "resourceVersion": "2948",
    "creationTimestamp": "2019-05-09T13:28:29Z"
  },
  "data": {
    "a": "dGVzdA==",
    "b": "dGVzdA==",
    "c": "dGVzdA=="
  },
  "type": "Opaque"
}

Comments

6

Create a json file like this (note that secret values must be base64-encoded):

{ 
  "metadata": {
    "annotations": {},
    "name": "mytest",
    "namespace": "default"
  },
  "apiVersion": "v1",
  "kind": "Secret",
  "data": {
    "a": "dGVzdA==",
    "b": "dGVzdA=="
  }
}

Then pass it into kubectl create:

kubectl create -f secrets-file.json

Comments

-1

You can use --from-literal:

kubectl create secret generic secretname --from-literal "someJsonKey=$(cat somejsonfile.json)"

Then you can recover the data with

kubectl get secret secretname -o 'go-template={{ .data.someJsonKey | base64decode }}'

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.