<p>We have a setup with external-DNS to create and bind dns entries based on service annotations.</p>
<p>For example we have a service for the alertmanager like this:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: prometheus-kube-prometheus-alertmanager
  namespace: prometheus
  labels:
...
    heritage: Helm
    prometheus-monitor-https: 'true'
    release: prometheus
    self-monitor: 'true'
  annotations:
    external-dns.alpha.kubernetes.io/hostname: alertmanager.ourdomain.com
    external-dns.alpha.kubernetes.io/ttl: '60'
spec:
  ports:
    - name: web
      protocol: TCP
      port: 80
      targetPort: 9093
      nodePort: 31126
  selector:
    alertmanager: prometheus-kube-prometheus-alertmanager
    app.kubernetes.io/name: alertmanager
  type: LoadBalancer
  sessionAffinity: None
  externalTrafficPolicy: Cluster
</code></pre>
<p>(abbreviated)</p>
<p>I want to use the blackbox exporter with the data from the annotations, so we don't have to manually add the monitoring here, but rather rely on kubernetes to provide the information what to monitor.</p>
<p>For that I wrote a servicemonitor, but it doesn't match the services and calls the blackbox exporter.</p>
<pre><code>apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: blackbox-exporter-monitor-https-external
  namespace: prometheus
spec:
  namespaceSelector:
    any: true
  selector:
    matchLabels:
      prometheus-monitor-https: any
  targetLabels:
    - environment
    - instance
  endpoints:
    - metricRelabelings:
        - sourceLabels: [__meta_kubernetes_service_annotation_external_dns_alpha_kubernetes_io_hostname]
          targetLabel: __param_target
          replacement: &quot;https://$1&quot;
        - sourceLabels: [__param_target]
          targetLabel: instance
        - targetLabel: __param_scheme
          replacement: https
        - targetLabel: __address__
          replacement: prometheus-blackbox-exporter:9115
      path: /probe
      params:
        debug:
          - &quot;true&quot;
        module:
          - &quot;http_2xx&quot;
</code></pre>
<p>I am not seeing why it shouldn't match the service. Do you have any hints?</p>

Patrick Cornelissen
<p>The service has label <code>prometheus-monitor-https: 'true'</code>, while the ServiceMonitor has a <code>selector.matchLabels</code> of <code>prometheus-monitor-https: any</code>.</p>
<p>If you change this such that the <code>selector.matchLabels</code> of the ServiceMonitor equals <code>prometheus-monitor-https: 'true'</code>, then I think it should work. The matchLabels looks for expected matches of the label key, value pair.</p>
<p>Also I see that you wrote <code>namespaceSelector</code> is <code>any: true</code>. It is good to know that the namespaceSelector works in a different way. It expects the labels of the namespace it should find the resource in. In your case it will look for a namespace that has the label <code>any: true</code>. But I think you actually want to select all namespaces, which is equal to not specifying a namespaceSelector at all.</p>
