<p>I am trying to follow this instruction to monitoring my prometheus</p>
<p><a href="https://logiq.ai/scraping-nginx-ingress-controller-metrics-using-helm-prometheus/" rel="nofollow noreferrer">https://logiq.ai/scraping-nginx-ingress-controller-metrics-using-helm-prometheus/</a></p>
<p>anyhow, I got a problem when trying to apply this file configuration</p>
<pre><code>apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    app: kubernetes-ingress
    name: service-monitor
    namespace: nginx-ingress
spec:
  endpoints:
  - interval: 30s
    path: /metrics
    port: prometheus
  namespaceSelector:
    matchNames:
    - logiq
  selector:
    matchLabels:
      app: kubernetes-ingress
</code></pre>
<p>this is the error</p>
<pre><code>error: error when retrieving current configuration of:

Resource: &quot;monitoring.coreos.com/v1, Resource=servicemonitors&quot;, GroupVersionKind: &quot;monitoring.coreos.com/v1, Kind=ServiceMonitor&quot;

Name: &quot;&quot;, Namespace: &quot;default&quot;

from server for: &quot;servicemonitor.yaml&quot;: resource name may not be empty
</code></pre>
<p>I thought it was about the CRD, but my monitoring.coreos.com has installed.</p>
<p>thank you in advance</p>
<p>this is my prometheus-kube clusterrole</p>
<pre><code>apiVersion: monitoring.coreos.com/v1

kind: Prometheus

metadata:

  annotations:

    meta.helm.sh/release-name: prometheus

    meta.helm.sh/release-namespace: ingress

  creationTimestamp: &quot;2022-01-17T03:09:49Z&quot;

  generation: 1

  labels:

    app: kube-prometheus-stack-prometheus

    app.kubernetes.io/managed-by: Helm

    chart: kube-prometheus-stack-10.1.3

    heritage: Helm

    release: prometheus

  name: prometheus-kube-prometheus-prometheus

  namespace: ingress

  resourceVersion: &quot;2311107&quot;

  uid: 48a57afb-2d9a-4f9f-9885-33ca66c59b16

spec:

  alerting:

    alertmanagers:

    - apiVersion: v2

      name: prometheus-kube-prometheus-alertmanager

      namespace: ingress

      pathPrefix: /

      port: web

  baseImage: quay.io/prometheus/prometheus

  enableAdminAPI: false

  externalUrl: http://prometheus-kube-prometheus-prometheus.ingress:9090

  listenLocal: false

  logFormat: logfmt

  logLevel: info

  paused: false

  podMonitorNamespaceSelector: {}

  podMonitorSelector:

    matchLabels:

      release: prometheus

  portName: web

  probeNamespaceSelector: {}

  probeSelector:

    matchLabels:

      release: prometheus

  replicas: 1

  retention: 10d

  routePrefix: /

  ruleNamespaceSelector: {}

  ruleSelector:

    matchLabels:

      app: kube-prometheus-stack

      release: prometheus

  securityContext:

    fsGroup: 2000

    runAsGroup: 2000

    runAsNonRoot: true

    runAsUser: 1000

  serviceAccountName: prometheus-kube-prometheus-prometheus

  serviceMonitorNamespaceSelector: {}

  serviceMonitorSelector:

    matchLabels:

      release: prometheus

  version: v2.21.0

</code></pre>

<p>For k8s resources, <code>metadata.name</code> is a <code>required</code> field. You must provide the <code>metadata.name</code> in resource YAML before applying it.</p>
<p>In case of <code>metadata.namespace</code>, if you don't provide it, it defaults to <code>default</code> namespace.</p>
<p>I think you have some unwanted leading spaces before <code>name</code> and <code>namespace</code> fields.</p>
<pre><code>apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: service-monitor
  namespace: nginx-ingress
  labels:
    app: kubernetes-ingress
spec:
  endpoints:
  - interval: 30s
    path: /metrics
    port: prometheus
  namespaceSelector:
    matchNames:
    - logiq
  selector:
    matchLabels:
      app: kubernetes-ingress
</code></pre>
<p><strong>Update:</strong></p>
<p>In your <code>Prometheus</code> CR, you have <code>serviceMonitorSelector</code> set.</p>
<pre class="lang-yaml prettyprint-override"><code>spec:
  serviceMonitorSelector:
    matchLabels:
      release: prometheus
</code></pre>
<p>Add these labels to your <code>serviceMonitor</code> CR.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: service-monitor
  namespace: nginx-ingress
  labels:
    app: kubernetes-ingress
    release: prometheus
</code></pre>
<p>Or, you can also update <code>serviceMonitorSelector</code> from the Prometheus CR side.</p>
