infra/charts/victoria-metrics-k8s-stack/templates/servicemonitors.yaml
Konstantin Averkiev c45fd1a6ac added vm stack
2025-07-08 17:29:32 +03:00

113 lines
3.8 KiB
YAML

{{- $ctx := dict "helm" . }}
{{- $scrapeLabels := include "vm.labels" $ctx }}
{{- $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).port }}
{{- $_ := set $ports "" $config.service }}
{{- end }}
{{- with $config.endpoints }}
---
apiVersion: v1
kind: Endpoints
metadata:
name: {{ $fullname }}
namespace: {{ $config.service.namespace | default "kube-system" }}
{{- $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 := default dict }}
{{- if and ($config.service).enabled (empty (index $.Subcharts $name)) }}
{{- $extraLabels := dict "app" $fullnameLabel "jobLabel" $svc }}
{{- $_ := set $ctx "extraLabels" $extraLabels }}
{{- $selector = dict "matchLabels" (fromYaml (include "vm.selectorLabels" $ctx)) }}
---
apiVersion: v1
kind: Service
metadata:
name: {{ $fullname }}
namespace: {{ $config.service.namespace | default "kube-system" }}
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" (default 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 }}
labels: {{ $scrapeLabels | nindent 4 }}
{{- $emptyParamsToDrop := list "endpoints" }}
{{- $scrapeSpec := mergeOverwrite (deepCopy $spec) (deepCopy $sc.spec) }}
{{- 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 }}