Return correct Key Value using go-template

6/6/2019

I'm trying to retrieve the value for the key clientSecret, from my kubernetes response, but I am failing to find the correct go syntax.

I have tried commands like:

kubectl get secret client-secret -o yaml --namespace magic-test -o go-template --template="{{range .items}}{{range .data}}{{.clientSecret}} {{end}}{{end}}"

And other variations

This is the yaml output of what I am trying to retrieve from

kubectl get secret client-secret -n magic-test -o yaml 
apiVersion: v1
data:
  clientSecret: NmQQuCNFiOWItsdfOTAyMCb00MjEwLWFiNGQtNTI4NDdiNWM5ZjMx
kind: Secret
metadata:
  creationTimestamp: 2019-05-31T14:03:44Z
  name: client-secret
  namespace: magic-test
  resourceVersion: "11544532074"
  selfLink: /api/v1/namespaces/magic-test/secrets/client-secret
  uid: e72acdsfbcc-83fsdac-1sdf1e9-9sdffaf-0050dsf56b7c1fa
type: Opaque

How can I retrieve the value for clientSecret?

-- user3292394
go-templates
kubernetes
yaml

1 Answer

6/6/2019

The output is not a list of items but an object or dictionary, so you can't iterate over the pipeline but you may simply index it by the keys you're interested in.

So simply use the template {{.data.clientSecret}}:

kubectl get secret client-secret -o yaml --namespace magic-test -o go-template
    --template="{{.data.clientSecret}}"
-- icza
Source: StackOverflow