While I deploying v2ray-exporter with Kustomize, I realized that commonLabels in kustomization.yaml doesn’t take care of the spec.selector.matchLabels field in ServiceMonitors. That makes sense, as ServiceMonitor is a part of CoreOS’s Prometheus Operator project, neither included nor maintained by Kubernetes. But how can we direct Kustomize to fill the labels in ServiceMonitors or even any fields in customized resources?

TL;DR

kustomization.yaml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- servicemonitor.yaml
# ...

configurations:
- ../kustomize-configurations/servicemonitor-matchlabels.yaml

commonLabels:
app: example-exporter

../kustomize-configurations/servicemonitor-matchlabels.yaml:

commonLabels:
- path: spec/selector/matchLabels
create: true
group: monitoring.coreos.com
kind: ServiceMonitor

It seems to me really easy to get confused with commonLabels in Kustomize configuration files or kustomization.yaml. The name commonLabels exists in both of them, but the definitions are totally different.

servicemonitor.yaml:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: example-exporter
spec:
endpoints:
- port: http
path: /scrape

Results:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
app: example-exporter # <-- Here commonLabels originally handles
name: example-exporter
spec:
endpoints:
- path: /scrape
port: http
selector:
matchLabels:
app: example-exporter # <-- Here we added labels from commonLabels

References