cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

filter for Kubernetes label key

roberto_camp1
Participant

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 `com.acme.project`, can I simply return all the kubernetes Deployments that have the `com.acme.project` label, regardless of the value of the label?

2 REPLIES 2

Hi @roberto_camp1 ,

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

roberto_camp1
Participant

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!

Featured Posts