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?
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With