Regex in metrics and empty deployment


I have a dashboard with below configuration.


This should always return a default namespace, and it does.
name: deployment_namespace
query: label_values(kube_deployment_metadata_generation, namespace)
Regex: /.*default.*/

This should only return deployment with name master OR worker.
name: deployment_name
query:label_values(kube_deployment_metadata_generation{namespace="$deployment_namespace"}, deployment)
Regex: /.*master|worker.*/

Graph metric:
avg(irate(container_cpu_usage_seconds_total{pod_name!="", pod_name=~"$deployment_name.*", namespace="$deployment_namespace"}[5m]) * 100) by (pod_name)

The issue:
If I select deployment called worker-xxxxx, the graph should only show me pods from that deployment.
This works fine but if worker or master deployment doesn’t exists, the deployment_name variable return none. This causes the graph to show cpu for all the pods.

I think the issue is here: pod_name=~"$deployment_name.*" so when deployment=none it shows all pods, but I’m not sure how to include regex there.

Something like this doesn’t work: pod_name=~"$deployment_name.*/(worker|master)/.*"

Any help appreciated.


This seems to be working and results looks correct: