3

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.

Alan
  • 491
  • 4
  • 13
  • 1
    jsonpath does not support all the same functions as go-template that's why they're separate; you'll be much happier using `-o go-template` for that task since the cheat sheet has already shown 90% of the answer – mdaniel Feb 18 '21 at 17:09
  • Thanks! Figured it out using the `-o go-template` as you suggested. Discovered it doesn't like square brackets or dashes but managed to fix those issues fairly quickly. – Alan Feb 19 '21 at 09:15
  • @user13898676 would mind posting an answer since you find a solution? – acid_fuji Feb 19 '21 at 09:19
  • @thomas sure no probs. – Alan Feb 19 '21 at 11:46

1 Answers1

5

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}}'
Alan
  • 491
  • 4
  • 13