<p>How do I print the metadata label information from the Kubernetes Pod?</p>
<p>Given, this is the pod configuration</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubernetes.io/psp: eks.privileged
  creationTimestamp: &quot;2022-10-13T01:43:06Z&quot;
  generateName: my-service-1-84bc99cfb5-
  labels:
    app.kubernetes.io/instance: my-service-1
    app.kubernetes.io/name: my-service
  name: my-service-1-84bc99cfb5-sx7vm
  namespace: my-service
 spec:
  containers:
     ...
</code></pre>
<p>When I am running the below command</p>
<pre><code>kubectl get pods -n my-service -o go-template --template '{{range .items}}{{.metadata.name}} {{.metadata.namespace}} {{.metadata.creationTimestamp}} {{.metadata.labels}} {{&quot;\n&quot;}}{{end}}'
</code></pre>
<p>its returns this output</p>
<pre><code>my-service-1-84bc99cfb5-sx7vm my-service 2022-10-13T01:43:06Z map[app.kubernetes.io/instance:my-service-1 app.kubernetes.io/name:my-service]
</code></pre>
<p>but I want to have</p>
<pre><code>my-service-1-84bc99cfb5-sx7vm my-service 2022-10-13T01:43:06Z my-service-1
</code></pre>

Mohit Sharma
<p>you can use the <a href="https://docs.gomplate.ca/syntax/#indexing-arrays-and-maps" rel="nofollow noreferrer">index function</a></p>
<blockquote>
<p>However, this kind of access is limited to keys which are strings and contain only characters in the set <code>(a-z,A-Z,_,1-9)</code>, and which do not begin with a number. If the key doesn’t conform to these rules, you can use the index function (like how arrays are accessed):
<code>{{ index $map &quot;foo-bar&quot; }}</code></p>
</blockquote>
<p>so we can use <code>{{index .metadata.labels &quot;app.kubernetes.io/name&quot;}}</code></p>
<pre><code> kubectl get pods  -n my-service -o go-template --template '{{range .items}}{{.metadata.name}} {{.metadata.namespace}} {{.metadata.creationTimestamp}} {{index .metadata.labels &quot;app.kubernetes.io/name&quot; -}} {{&quot;\n&quot;}}{{end}}'
</code></pre>
<p>output</p>
<pre><code>datadog-zxmtd datadog 2022-10-09T08:20:50Z datadog
</code></pre>
