<p>The easiest solution is probably using <a href="https://stedolan.github.io/jq/" rel="nofollow noreferrer"><code>jq</code></a>. To get a list of Deployments that deploy Pods with the label <code>importance: normal</code>, you could do this:</p>
<pre><code>kubectl get deploy -o json |
jq '.items[]|select(.spec.template.metadata.labels.importance == &quot;normal&quot;)'
</code></pre>
<p>I would argue that you should rewrite your manifests so that the Deployment shares labels with the Pod so that you can just run:</p>
<pre><code>kubectl get deploy -l importance=normal
</code></pre>
<p>That's much simpler. Using a tool like <a href="https://kustomize.io" rel="nofollow noreferrer">kustomize</a> can simplify the work involved in keeping these labels consistent.</p>

CC BY-SA 4.0
2023-01-05T15:13:43.680Z
Perfect. Thanks so much!!!
I will look into kustomize also
ErnieAndBert
CC BY-SA 4.0
2023-01-04T23:15:16.733Z
2023-01-04T23:15:16.733Z
larsks
<p>I know that I can perform this command to see all the pods that have a certain label : <code>kubectl get pods -l importance=normal</code></p>
<p>Is there a command that would allow me to get the deployments that create the pods with the label <code>importance=normal</code>?</p>
<p>I want to find all deployments what have the label <code>importance=normal</code> found in the spec/template/metadata/labels (see my deployment yaml below).</p>
<p>I found this link(<a href="https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/#list-container-images-filtering-by-pod-label" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/#list-container-images-filtering-by-pod-label</a>) and started to create the following command but I could not get it to work -</p>
<pre><code>kubectl get deployments -namespace prod -o jsonpath='{range .items[*]}{&quot;\n&quot;}{.spec.template.metadata.labels}{&quot;:\t&quot;}{&quot;, &quot;}{end}{end}' |\ sort```
</code></pre>
<p>My deployment yaml looks like the following:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    app.kubernetes.io/managed-by: Helm
    deployment.kubernetes.io/revision: &quot;11&quot;
    meta.helm.sh/release-name: service-qa
    meta.helm.sh/release-namespace: prod
    source: https://gitlab.services.com/service
  creationTimestamp: &quot;2019-04-07T12:51:03Z&quot;
  generation: 22
  labels:
    app.kubernetes.io/managed-by: Helm
    chart: service-0.1.0
  name: service-deployment
  namespace: qa
  resourceVersion: &quot;577488289&quot;
  uid: cd2fdeb4-5933-11e9-ad6d-02211a607320
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 3
  selector:
    matchLabels:
      app: service-selector
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      annotations:
        kubectl.kubernetes.io/restartedAt: &quot;2022-11-13T08:19:36-06:00&quot;
      creationTimestamp: null
      labels:
        app: service-selector
        importance: normal
        version: current
</code></pre>

<kubernetes><kubectl>
How to get a list of deployments that only have a certain label in the spec section
ErnieAndBert
# How to get a list of deployments that only have a certain label in the spec section

(Asked by: username_0 on 2023-01-04)

I know that I can perform this command to see all the pods that have a certain label : kubectl get pods -l importance=normal
Is there a command that would allow me to get the deployments that create the pods with the label importance=normal?
I want to find all deployments what have the label importance=normal found in the spec/template/metadata/labels (see my deployment yaml below).
I found this link(https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/#list-container-images-filtering-by-pod-label) and started to create the following command but I could not get it to work -
kubectl get deployments -namespace prod -o jsonpath='{range .items[*]}{"\n"}{.spec.template.metadata.labels}{":\t"}{", "}{end}{end}' |\ sort```

My deployment yaml looks like the following:
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    app.kubernetes.io/managed-by: Helm
    deployment.kubernetes.io/revision: "11"
    meta.helm.sh/release-name: service-qa
    meta.helm.sh/release-namespace: prod
    source: https://gitlab.services.com/service
  creationTimestamp: "2019-04-07T12:51:03Z"
  generation: 22
  labels:
    app.kubernetes.io/managed-by: Helm
    chart: service-0.1.0
  name: service-deployment
  namespace: qa
  resourceVersion: "577488289"
  uid: cd2fdeb4-5933-11e9-ad6d-02211a607320
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 3
  selector:
    matchLabels:
      app: service-selector
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      annotations:
        kubectl.kubernetes.io/restartedAt: "2022-11-13T08:19:36-06:00"
      creationTimestamp: null
      labels:
        app: service-selector
        importance: normal
        version: current

**Tags:** kubernetes, kubectl


---

## 1 answers

### Answer by username_1 on 2023-01-04. Score: 3 | Accepted answer

The easiest solution is probably using jq. To get a list of Deployments that deploy Pods with the label importance: normal, you could do this:
kubectl get deploy -o json |
jq '.items[]|select(.spec.template.metadata.labels.importance == "normal")'

I would argue that you should rewrite your manifests so that the Deployment shares labels with the Pod so that you can just run:
kubectl get deploy -l importance=normal

That's much simpler. Using a tool like kustomize can simplify the work involved in keeping these labels consistent.

> **Comments:**
> **username_0**: Perfect. Thanks so much!!! I will look into kustomize also  

---

