{{- $ctx := dict "helm" . }}
{{- $defaultScrapeService := .Values.defaultScrapeService }}
{{- $prefix := include "vm.fullname" $ctx }}
{{- $ns := include "vm.namespace" $ctx }}
{{- range $name, $config := .Values }}
  {{- if and (kindIs "map" $config) $config.enabled (hasKey $config "vmScrape") }}
    {{- $svc := kebabcase $name }}
    {{- $fullname := printf "%s-%s" $prefix $svc }}
    {{- $fullnameLabel := $fullname | trunc 63 | trimSuffix "-" }}
    {{- $ports := ($config.service).ports | default dict }}
    {{- if $config.service }}
      {{- if $config.service.port }}
        {{- $_ := set $ports "" $config.service }}
      {{- end }}
      {{- if not $config.service.namespace }}
        {{- $_ := set $config.service "namespace" $defaultScrapeService.namespace }}
      {{- end }}
    {{- end }}
    {{- with $config.endpoints }}
---
apiVersion: v1
kind: Endpoints
metadata:
  name: {{ $fullname }}
  namespace: {{ $config.service.namespace }}
  {{- $extraLabels := dict "app" $fullnameLabel "k8s-app" $svc }}
  {{- $_ := set $ctx "extraLabels" $extraLabels }}
  labels: {{ include "vm.labels" $ctx | nindent 4 }}
subsets:
  - addresses:
      {{- range . }}
      - ip: {{ . }}
      {{- end }}
    {{- if $ports }}
    ports:
    {{- range $portName, $portConfig := $ports }}
      {{- $parts := list "http-metrics" }}
      {{- if $portName }}
        {{- $parts = append $parts $portName }}
      {{- end }}
      - name: {{ $parts | join "-" }}
        port: {{ $portConfig.port }}
        protocol: TCP
    {{- end }}
    {{- end }}

    {{- end }}
    {{- $selector := dict }}
    {{- if and ($config.service).enabled (empty (index $.Subcharts $name)) }}
      {{- $extraLabels := dict "app" $fullnameLabel "jobLabel" $svc }}
      {{- $extraLabels = mergeOverwrite $extraLabels (deepCopy ($config.service.labels | default dict)) }}
      {{- $_ := set $ctx "extraLabels" $extraLabels }}
      {{- $selector = dict "matchLabels" (fromYaml (include "vm.selectorLabels" $ctx)) }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ $fullname }}
  namespace: {{ $config.service.namespace }}
  labels: {{ include "vm.labels" $ctx | nindent 4 }}
spec:
  clusterIP: None
  {{- if $ports }}
  ports:
  {{- range $portName, $portConfig := $ports }}
    {{- $parts := list "http-metrics" }}
    {{- if $portName }}
      {{- $parts = append $parts $portName }}
    {{- end }}
    - name: {{ $parts | join "-" }}
      port: {{ $portConfig.port }}
      protocol: TCP
      targetPort: {{ $portConfig.targetPort }}
  {{- end }}
  {{- end }}
  {{- if empty $config.endpoints }}
  {{- with ($config.service).selector }}
  selector: {{ toYaml . | nindent 4 }}
  {{- end }}
  {{- end }}
  type: ClusterIP
  
    
    {{- end }}
    {{- if $config.vmScrape }}
      {{- if index $.Subcharts $name }}
        {{- $_ := set $ctx "extraLabels" (dict) }}
        {{- $selector = dict "matchLabels" (fromYaml (include "vm.selectorLabels" $ctx)) }}
      {{- end }}
      {{- $spec := dict "selector" $selector }}
      {{- $scrapes := $config.vmScrapes | default (dict $svc $config.vmScrape) }}
      {{- $defaultScrape := $config.vmScrape | default dict }}
      {{- range $scrapeName, $scrapeConfig := $scrapes }}
        {{- $sc := mergeOverwrite (deepCopy $defaultScrape) (deepCopy $scrapeConfig) }}
        {{- if or (not (hasKey $sc "enabled")) $sc.enabled }}
          {{- $fullname = printf "%s-%s" $prefix $scrapeName }}
---
apiVersion: operator.victoriametrics.com/v1beta1
kind: {{ ternary "VMServiceScrape" $sc.kind (empty $sc.kind) }}
metadata:
  name: {{ $fullname }}
  namespace: {{ $ns }}
  {{- $_ := set $ctx "extraLabels" $sc.labels }}
  labels: {{ include "vm.labels" $ctx | nindent 4 }}
  {{- $_ := unset $ctx "extraLabels" }}
{{- $emptyParamsToDrop := list "endpoints" }}
{{- $scrapeSpec := mergeOverwrite (deepCopy $spec) (deepCopy $sc.spec) }}
{{- $namespaceSelector := $scrapeSpec.namespaceSelector | default dict }}
{{- if and (hasKey $namespaceSelector "matchNames") (not $namespaceSelector.matchNames) }}
  {{- $_ := set $scrapeSpec.namespaceSelector "matchNames" (list $config.service.namespace) }}
{{- end }}
{{- range $paramToDrop := $emptyParamsToDrop }}
  {{- if and (hasKey $scrapeSpec $paramToDrop) (empty (index $scrapeSpec $paramToDrop)) }}
    {{- $_ := unset $scrapeSpec $paramToDrop }}
  {{- end }}
{{- end }}
spec: {{ (tpl (toYaml $scrapeSpec) $) | nindent 2 }}


        {{- end }}
      {{- end }}
    {{- end }}
  {{- end }}
{{- end }}
