# filter for Kubernetes label key

**URL:** <https://community.dynatrace.com/t/filter-for-kubernetes-label-key/45604>\
**Category:** DQL\
**Tags:** dql, grail, kubernetes\
**Created:** [May 15, 2024, 3:01pm UTC](https://community.dynatrace.com/t/filter-for-kubernetes-label-key/45604 "2024-05-15T15:01:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![roberto\_camp1](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@roberto\_camp1](https://community.dynatrace.com/u/roberto_camp1)\
**Post date:** [May 15, 2024, 3:01pm UTC](https://community.dynatrace.com/t/filter-for-kubernetes-label-key/45604/1 "2024-05-15T15:01:25Z")

</div>

I have the following DQL started to detect k8s Deployments with a certain label :

```

fetch dt.entity.cloud\_application, from: -30m | fields id, workload.id = id, workload.name = entity.name, workload.type = arrayFirst(cloudApplicationDeploymentTypes), workload.labels = cloudApplicationLabels, workload.annotations = kubernetesAnnotations, workload.resourceUid = resourceUid, workload.conditions = kubernetesConditions, workload.age = record(start = resourceCreationTimestamp, end = now()), cluster.id = clustered\_by[dt.entity.kubernetes\_cluster], namespace.id = belongs\_to[dt.entity.cloud\_application\_namespace], namespace.name = namespaceName  
// Filters: [cluster,namespace,workloadLabels,workloadType]  
| filter in(id, classicEntitySelector(“type(CLOUD\_APPLICATION),toRelationship.isClusterOfCa(type(KUBERNETES\_CLUSTER),entityName.equals(operations))”))  
| filter in(id, classicEntitySelector(“type(CLOUD\_APPLICATION),toRelationship.isNamespaceOfCa(type(CLOUD\_APPLICATION\_NAMESPACE),entityName.equals(services))”))  
| filter workload.labels[`com.acme.project`] == “98417”  
| filter in(workload.type, “KUBERNETES\_DEPLOYMENT”, “OPEN\_SHIFT\_DEPLOYMENT\_CONFIG”)

```

My question is, instead of doing a filter on the specific label value “98417” for the label key&nbsp;`com.acme.project`, can I simply return all the kubernetes Deployments that have the&nbsp;`com.acme.project` label, regardless of the value of the label?

---

<div class="post-metadata">

**Author:** ![MichalOlszewski](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@MichalOlszewski](https://community.dynatrace.com/u/MichalOlszewski)\
**Post date:** [May 20, 2024, 11:32am UTC](https://community.dynatrace.com/t/filter-for-kubernetes-label-key/45604/2 "2024-05-20T11:32:35Z")

</div>

Hi&nbsp;[@roberto\_camp1](https://community.dynatrace.com/user/67600)&nbsp;,

It’s possible. You can use Dynatrace Pattern Language(DPL) to match if any value is in com.acme.project variable and if this variable even exist. Then, you can filter the new variable so that it does not have a null value, in case the com.acme.project variable does not exist, the new variable will have a null value. In your example you can do something like this:

fetch dt.entity.cloud\_application, from: -30m | fields id, workload.id = id, workload.name = entity.name, workload.type = arrayFirst(cloudApplicationDeploymentTypes), workload.labels = cloudApplicationLabels, workload.annotations = kubernetesAnnotations, workload.resourceUid = resourceUid, workload.conditions = kubernetesConditions, workload.age = record(start = resourceCreationTimestamp, end = now()), cluster.id = clustered\_by[dt.entity.kubernetes\_cluster], namespace.id = belongs\_to[dt.entity.cloud\_application\_namespace], namespace.name = namespaceName  
// Filters: [cluster,namespace,workloadLabels,workloadType]  
| filter in(id, classicEntitySelector(“type(CLOUD\_APPLICATION),toRelationship.isClusterOfCa(type(KUBERNETES\_CLUSTER),entityName.equals(operations))”))  
| filter in(id, classicEntitySelector(“type(CLOUD\_APPLICATION),toRelationship.isNamespaceOfCa(type(CLOUD\_APPLICATION\_NAMESPACE),entityName.equals(services))”))  
| parse workload.labels[`com.acme.project `], “”“DATA:com.acme.project”“”

| filter isNotNull(com.acme.project)  
| filter in(workload.type, “KUBERNETES\_DEPLOYMENT”, “OPEN\_SHIFT\_DEPLOYMENT\_CONFIG”)

Hope it will help,

Bests  
Michal

---

<div class="post-metadata">

**Author:** ![roberto\_camp1](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@roberto\_camp1](https://community.dynatrace.com/u/roberto_camp1)\
**Post date:** [May 22, 2024, 2:11pm UTC](https://community.dynatrace.com/t/filter-for-kubernetes-label-key/45604/3 "2024-05-22T14:11:45Z")

</div>

Thank you that approach works well. Just needed to remove a blank space in the `parse workload.labels[`com.acme.project `]` line:

parse workload.labels[`com.acme.project`]

everything else worked perfectly, thank you again!
