Search code examples
kubernetesbase64kubectljson-path-expression

decode base64 in kubectl jsonpath


I have a command similar to this

kubectl get secrets \
--selector='my-selector' \
-o jsonpath='{range .items[*] }{"\n"}{.metadata.labels.cluster-name}{"."}{.metadata.namespace {":"}{"5432"}{"postgres" }{":"}{.data.password}{end}'

which outputs a list like this (format required)

cluster-name.namespace:5432:postgres:YbHF....==
cluster-name.namespace:5432:postgres:YbHF....==
cluster-name.namespace:5432:postgres:YbHF....==

I need to decode the base64 for this file and using the kubectl cheat sheet as a reference which gives this example:

# Output decoded secrets without external tools
kubectl get secret my-secret -o go-template='{{range $k,$v := .data}}{{"### "}}{{$k}}{{"\n"}}{{$v|base64decode}}{{"\n\n"}}{{end}}'

I tried the following

kubectl get secrets \
--selector='my-selector' \
-o jsonpath='{range .items[*] }{"\n"}{.metadata.labels.cluster-name}{"."}{.metadata.namespace {":"}{"5432"}{"postgres" }{":"}{.data.password|base64decode}{end}'

The result is that everything appears apart from the password field which is now blank, for example:

cluster-name.namespace:5432:postgres:

Any pointers would be appreciated.


Solution

  • As per @mdaniel suggestion I used the -o go-template

    My main syntaxal changes were removing the [ ], ie, {range .items[*] } to {{range .items}}'

    And if a key contained a - then {.metadata.labels.cluster-name} became {{index .metadata.labels "cluster-name"}}

    My solution below which enabled the base64 decode to work:

    kubectl get secrets \
    --selector='my-selector' \
    -o go-template='{{range .items}}{{"\n"}}{{index .metadata.labels "cluster-name"}}{{"."}}{{.metadata.namespace }}{{":"}}{{"5432"}}{{"postgres"}}{{":"}}{{.data.password|base64decode}}{{end}}'