{{ range $rsName, $rs := .Values.remoteStorages }}
{{ range $replica := until ($.Values.writeReplicas | default 1 | int) }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "prometheus-benchmark.fullname" $ }}-vmagent-{{ $rsName }}-replica-{{ $replica }}
  namespace: {{ $.Release.Namespace }}
  labels:
    {{- include "prometheus-benchmark.labels" $ | nindent 4 }}
spec:
  selector:
    matchLabels:
      job: vmagent
      remote-storage-name: {{ $rsName | quote }}
      {{- include "prometheus-benchmark.selectorLabels" $ | nindent 6 }}
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        checksum/nginx-cm: {{ include (print $.Template.BasePath "/vmagent/nginx-cm.yaml") $ | sha256sum }}
      labels:
        job: vmagent
        remote-storage-name: {{ $rsName | quote }}
        {{- include "prometheus-benchmark.labels" $ | nindent 8 }}
    spec:
      {{- with $.Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
      - name: vmagent-config-updater
        image: "ghcr.io/just1900/vmagent-config-updater:heads-vmsingle-4c-16g-tidb-0-gbbe7f2a"
        args:
        - --httpListenAddr=:8436
        - --targetAddrMap={{ $.Values.targetAddrMap }}
        - --scrapeInterval={{ $.Values.scrapeInterval }}
        - --scrapeConfigUpdatePercent={{ $.Values.scrapeConfigUpdatePercent }}
        - --scrapeConfigUpdateInterval={{ $.Values.scrapeConfigUpdateInterval }}
      - name: vmagent
        resources:
          {{- toYaml $.Values.writeReplicaResource | nindent 10 }}
        image: "victoriametrics/vmagent:{{ $.Values.vmtag }}"
        args:
        - --httpListenAddr=:8429
        - --promscrape.suppressDuplicateScrapeTargetErrors=true
        - --promscrape.streamParse
        - --remoteWrite.showURL
        - --promscrape.config=http://0.0.0.0:8436/api/v1/config
        - --promscrape.configCheckInterval={{ $.Values.scrapeConfigUpdateInterval }}
        - --remoteWrite.url={{ $rs.writeURL }}
        - --remoteWrite.tmpDataPath=/vmagent-data
        - --remoteWrite.maxDiskUsagePerURL={{ $.Values.maxDiskUsagePerURL }}
        {{- if $.Values.writeConcurrency }}
        - --remoteWrite.queues={{ $.Values.writeConcurrency }}
        {{- end }}
        {{- if gt (len $.Values.externalLabels) 0 }}
        {{- range $pair := $.Values.externalLabels }}
        - --remoteWrite.label={{ $pair }}
        {{- end }}
        {{- end }}
        {{- if $rs.writeBearerToken }}
        - --remoteWrite.bearerToken={{ $rs.writeBearerToken }}
        {{- end }}
        {{- if $rs.writeHeaders }}
        - --remoteWrite.headers={{ $rs.writeHeaders }}
        {{- end }}
        ports:
        - name: vm-metrics
          containerPort: 8429
        volumeMounts:
        - mountPath: /vmagent-data
          name: vmagent-data
      - name: nodeexporter
        image: prom/node-exporter:v1.4.0
        args:
          - --path.procfs=/host/proc
          - --path.sysfs=/host/sys
          - --path.rootfs=/host/root
          - --no-collector.wifi
          - --no-collector.hwmon
          - --no-collector.time
          - --no-collector.timex
          - --no-collector.arp
          - --no-collector.netdev
          - --no-collector.netstat
          - --collector.processes
          - --collector.filesystem.fs-types-exclude=^(autofs|binfmt_misc|bpf|cgroup2?|configfs|debugfs|devpts|devtmpfs|fusectl|hugetlbfs|iso9660|mqueue|nsfs|overlay|proc|procfs|pstore|rpc_pipefs|securityfs|selinuxfs|squashfs|sysfs|tracefs)$
          - --collector.filesystem.mount-points-exclude=^/(dev|proc|sys|var/lib/docker/.+|var/lib/kubelet/.+)($|/)
          - --web.max-requests=40
          - --web.listen-address=:9101
        volumeMounts:
          - name: proc
            mountPath: /host/proc
            readOnly: true
          - name: sys
            mountPath: /host/sys
            readOnly: true
          - name: root
            mountPath: /host/root
            mountPropagation: HostToContainer
            readOnly: true
        resources:
          requests:
            cpu: 50m
        ports:
          - containerPort: 9101
            name: metrics
      {{- if $.Values.nginx.enabled }}
      - name: nginx
        image: nginx:1.23.1
        args:
          - nginx
          - -c
          - /opt/nginx/nginx.conf
        ports:
          - containerPort: 9102
            name: nginx
        resources:
          limits:
            cpu: 200m
          requests:
            cpu: 50m
        volumeMounts:
          - mountPath: /opt/nginx
            name: nginx-cm
          - mountPath: /tmp/nginx
            name: nginx-cache
          - mountPath: /etc/nginx
            name: nginx-empty
      {{- end }}
      securityContext:
        fsGroup: 65534
        runAsGroup: 65534
        runAsNonRoot: true
        runAsUser: 65534
      volumes:
      - name: vmagent-data
        persistentVolumeClaim:
          claimName: {{ include "prometheus-benchmark.fullname" $ }}
      - name: proc
        hostPath:
          path: /proc
      - name: sys
        hostPath:
          path: /sys
      - name: root
        hostPath:
          path: /
      {{- if $.Values.nginx.enabled }}
      - name: nginx-cache
        emptyDir: {}
      - name: nginx-cm
        configMap:
          name: {{ include "prometheus-benchmark.fullname" $ }}-nginx-cm
      - name: nginx-empty
        emptyDir: {}
      {{- end }}
---
{{ end }}
{{ end }}
