apiVersion: operator.victoriametrics.com/v1beta1
kind: VMCluster
metadata:
  name: vmcluster
spec:
  replicationFactor: 2
  retentionPeriod: "3"
  vminsert:
    extraArgs:
      maxLabelsPerTimeseries: "100"
      maxConcurrentInserts: "8"
    affinity:
      podAntiAffinity:
        preferredDuringSchedulingIgnoredDuringExecution:
          - podAffinityTerm:
              labelSelector:
                matchExpressions:
                  - key: app.kubernetes.io/name
                    operator: In
                    values:
                      - vminsert
              topologyKey: kubernetes.io/hostname
            weight: 100
    image: {}
    podMetadata:
      labels:
        owner: infra
    replicaCount: {{ .Values.vminsert.replicas }}
    {{- with .Values.vminsert.resources }}
    resources:
    {{- toYaml . | nindent 6 }}
    {{- end }}
    nodeSelector:
      node_type: worker
      topology.kubernetes.io/zone: {{ .Values.activeZone}}
    hpa:
      minReplicas: {{ .Values.vminsert.hpa.minReplicas }}
      maxReplicas: {{ .Values.vminsert.hpa.maxReplicas }}
      metrics:
      - type: Resource
        resource:
          name: cpu
          target:
            type: Utilization
            averageUtilization: 80
      - type: Resource
        resource:
          name: memory
          target:
            type: Utilization
            averageUtilization: 90
      behaviour:
        scaleUp:
          # we already have an oversale limit to accommodate temporary spikes, so we define a window here.
          stabilizationWindowSeconds: 60
        scaleDown:
          stabilizationWindowSeconds: 600
          # scale down at the rate of 1 pod per 5 minutes.
          policies:
          - type: Pods
            value: 1
            periodSeconds: 300
  vmselect:
    affinity:
      podAntiAffinity:
        preferredDuringSchedulingIgnoredDuringExecution:
          - podAffinityTerm:
              labelSelector:
                matchExpressions:
                  - key: app.kubernetes.io/name
                    operator: In
                    values:
                      - vmselect
              topologyKey: kubernetes.io/hostname
            weight: 100
    cacheMountPath: /select-cache
    extraArgs:
      search.maxConcurrentRequests: "10"
    image: {}
    podMetadata:
      labels:
        owner: infra
    replicaCount: {{ .Values.vmselect.replicas }}
    {{- with .Values.vmselect.resources }}
    resources:
    {{- toYaml . | nindent 6 }}
    {{- end }}
    storage:
      volumeClaimTemplate:
        metadata: {}
        spec:
          resources:
            requests:
              storage: {{ .Values.vmselect.storage }}
        status: {}
    hpa:
      minReplicas: {{ .Values.vmselect.hpa.minReplicas }}
      maxReplicas: {{ .Values.vmselect.hpa.maxReplicas }}
      metrics:
      - type: Resource
        resource:
          name: cpu
          target:
            type: Utilization
            averageUtilization: 90
      - type: Resource
        resource:
          name: memory
          target:
            type: Utilization
            averageUtilization: 90
      behaviour:
        scaleUp:
          # we already have an oversale limit to accommodate temporary spikes, so we define a window here.
          stabilizationWindowSeconds: 60
        scaleDown:
          stabilizationWindowSeconds: 600
          # scale down at the rate of 1 pod per 5 minutes.
          policies:
          - type: Pods
            value: 1
            periodSeconds: 300
    nodeSelector:
      node_type: worker
      topology.kubernetes.io/zone: {{ .Values.activeZone}}
  vmstorage:
    topologySpreadConstraints:
    - maxSkew: 1
      topologyKey: kubernetes.io/hostname
      whenUnsatisfiable: ScheduleAnyway
      labelSelector:
        matchLabels:
          app.kubernetes.io/name: vmstorage
    affinity:
      podAntiAffinity:
        preferredDuringSchedulingIgnoredDuringExecution:
          - podAffinityTerm:
              labelSelector:
                matchExpressions:
                  - key: app.kubernetes.io/name
                    operator: In
                    values:
                      - vmstorage
              topologyKey: kubernetes.io/hostname
            weight: 50
    image: {}
    podMetadata:
      labels:
        owner: infra
    replicaCount: {{ .Values.vmstorage.replicas }}
    {{- with .Values.vmstorage.resources }}
    resources:
    {{- toYaml . | nindent 6 }}
    {{- end }}
    storage:
      volumeClaimTemplate:
        metadata: {}
        spec:
          resources:
            requests:
              storage: {{ .Values.vmstorage.storage }}
        status: {}
    storageDataPath: /vm-data
    nodeSelector:
      node_type: worker
      topology.kubernetes.io/zone: {{ .Values.activeZone}}
    containers:
      - name: vmbackuper
        image: {{ .Values.vmbackuper.image }}
        imagePullPolicy: Always
        restartPolicy: OnFailure
        serviceAccount: vmcluster-vmcluster
        serviceAccountName: vmcluster-vmcluster
        volumeMounts:
          - mountPath: /vm-data
            name: vmstorage-db
        {{- with .Values.vmbackuper.resources }}
        resources:
        {{- toYaml . | nindent 10 }}
        {{- end }}
        command:
          - /usr/local/bin/entrypoint.sh
        args:
          - -storageDataPath=/vm-data
          - -snapshot.createURL=http://localhost:8482/snapshot/create
          - -dst={{ .Values.vmbackuper.dst }}${MY_POD_NAME}
          - -maxBytesPerSecond=52428800
        env:
          - name: MY_POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          {{- with .Values.vmbackuper.env }}
          {{- toYaml . | nindent 10 }}
          {{- end }}

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: vmselect
spec:
  ingressClassName: {{ .Values.vmselect.ingressClassName }}
  rules:
  - http:
      paths:
      - backend:
          service:
            name: vmselect-vmcluster
            port:
              number: 8481
        path: /select/
        pathType: Prefix