monitoring.cereos.com/v1 servicemonitor resource name may not be empty

I am trying to follow this instruction to monitoring my prometheus

https://logiq.ai/scraping-nginx-ingress-controller-metrics-using-helm-prometheus/

anyhow, I got a problem when trying to apply this file configuration

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

this is the error

error: error when retrieving current configuration of:

Resource: "monitoring.coreos.com/v1, Resource=servicemonitors", GroupVersionKind: "monitoring.coreos.com/v1, Kind=ServiceMonitor"

Name: "", Namespace: "default"

from server for: "servicemonitor.yaml": resource name may not be empty

I thought it was about the CRD, but my monitoring.coreos.com has installed.

thank you in advance

this is my prometheus-kube clusterrole

apiVersion: monitoring.coreos.com/v1

kind: Prometheus

metadata:

  annotations:

    meta.helm.sh/release-name: prometheus

    meta.helm.sh/release-namespace: ingress

  creationTimestamp: "2022-01-17T03:09:49Z"

  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: "2311107"

  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


Solution 1:

For k8s resources, metadata.name is a required field. You must provide the metadata.name in resource YAML before applying it.

In case of metadata.namespace, if you don't provide it, it defaults to default namespace.

I think you have some unwanted leading spaces before name and namespace fields.

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

Update:

In your Prometheus CR, you have serviceMonitorSelector set.

spec:
  serviceMonitorSelector:
    matchLabels:
      release: prometheus

Add these labels to your serviceMonitor CR.

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: service-monitor
  namespace: nginx-ingress
  labels:
    app: kubernetes-ingress
    release: prometheus

Or, you can also update serviceMonitorSelector from the Prometheus CR side.