Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 699
Alan Avatar asked Sep 20 '25 20:09

Alan


1 Answers

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}}'
like image 66
Alan Avatar answered Sep 22 '25 20:09

Alan