{{- if and .Values.metrics.enabled .Values.metrics.prometheusRule.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: {{ include "common.names.fullname" . }}
  {{- if .Values.metrics.prometheusRule.namespace }}
  namespace: {{ .Values.metrics.prometheusRule.namespace }}
  {{- else }}
  namespace: {{ .Release.Namespace }}
  {{- end }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.metrics.prometheusRule.additionalLabels }}
    {{- include "common.tplvalues.render" (dict "value" .Values.metrics.prometheusRule.additionalLabels "context" $) | nindent 4 }}
    {{- end }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  groups:
  {{- if eq (len .Values.metrics.prometheusRule.rules) 0 }}
  - name: {{ template "common.names.fullname" . }}
    rules:
    - alert: Minio cpu使用率过高
      annotations:
        description: '{{`{{ $labels.instance }}`}}CPU使用大于80% (当前使用率: {{`{{ $value }}`}})'
        summary: Instance {{`{{ $labels.instance }}`}} CPU使用率过高
      expr: sum(rate(process_cpu_seconds_total{container='minio'}[1m])) by (pod) * 100 > 80
      for: 1m
      labels:
        severity: warning
    - alert: Minio 内存使用率过高
      annotations:
        description: '{{`{{ $labels.namespace }}`}} {{`{{ $labels.pod }}`}}内存使用大于80% (当前使用率: {{`{{ $value }}`}})'
        summary: Instance {{`{{ $labels.namespace }}`}} {{`{{ $labels.pod }}`}} 内存使用率过高
      expr: sum(process_resident_memory_bytes{container='minio'} / process_virtual_memory_bytes{container='minio'}) by (pod,namespace) * 100 > 80
      for: 5m
      labels:
        severity: warning
    - alert: Minio 节点数异常
      annotations:
        description: '{{`{{ $labels.instance }}`}}: Minio 节点数异常 (当前节点数: {{`{{ $value }}`}})'
        summary: 'Instance {{`{{ $labels.instance }}`}} : Minio 节点数异常'
      expr: sum(minio_disks_total) < 4
      for: 1m
      labels:
        severity: error
    - alert: Minio 磁盘空间使用率过高
      annotations:
        description: '{{`{{ $labels.pod }}`}}: Minio 磁盘空间使用率过高 (当前使用百分比: {{`{{ $value }}`}})'
        summary: 'Instance {{`{{ $labels.pod }}`}} : Minio 磁盘空间使用率过高'
      expr: sum(disk_storage_used / disk_storage_total) by(instance,pod,namespace) > 0.80
      for: 1m
      labels:
        severity: error
{{- else }}
  {{- with .Values.metrics.prometheusRule.rules }}
    - name: {{ template "common.names.fullname" $ }}
      rules: {{- include "common.tplvalues.render" (dict "value" . "context" $) | nindent 8 }}
  {{- end }}
  {{- end }}
{{- end }}