Bash - Iterate over map from k8s secrets call

97 views Asked by At

I am requesting the details of a k8s secret:

kubectl get secret mySecret -o jsonpath='{.data}'

The answer of the call gives me the following map:

'map[first_password:ZXhhbXBsZQ0K second_password:ZXhhbXBsZQ0K third_password:ZXhhbXBsZQ0K]'

I want to iterate now with BASH through this map, to write the values in a string, that looks like this:

first_password ZXhhbXBsZQ0K

second_password ZXhhbXBsZQ0K

third_password ZXhhbXBsZQ0K

Does anyone know how to this with BASH?

For now I have this:

items=$(echo "$(kubectl get secret mySecret -o jsonpath='{.data}')" | jq -c -r '.[]')
for item in ${items[@]}; do
    echo $item
done

This just prints three times the password ZXhhbXBsZQ0K.

1

There are 1 answers

0
Blender Fox On BEST ANSWER

Try this:

kubectl get secret mySecret -o json | jq -r '.data | keys[] as $key | "\($key):\(.[$key])"'

Running this against my grafana secret in my cluster with the following structure:

{
    "apiVersion": "v1",
    "data": {
        "admin-password": "{hidden}",
        "admin-user": "{redacted}",
        "ldap-toml": ""
    },
    "kind": "Secret",
    ...
}

Gives me this:

admin-password:{hidden}
admin-user:{redacted}
ldap-toml: