# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "prometheus-redis-exporter.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "prometheus-redis-exporter.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "prometheus-redis-exporter.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create the name of the service account to use
*/}}
{{- define "prometheus-redis-exporter.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "prometheus-redis-exporter.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}
 # deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "prometheus-redis-exporter.fullname" . }}
  labels:
    app: {{ template "prometheus-redis-exporter.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "prometheus-redis-exporter.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      annotations:
{{ toYaml .Values.annotations | indent 8 }}
      labels:
        app: {{ template "prometheus-redis-exporter.name" . }}
        release: {{ .Release.Name }}
    spec:
      serviceAccountName: {{ template "prometheus-redis-exporter.serviceAccountName" . }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end}}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          args:
          {{- range $key, $value := .Values.extraArgs }}
            - --{{ $key }}={{ $value }}
          {{- end }}
          ports:
            - name: exporter-port
              containerPort: 9121
          env:
            - name: REDIS_ADDR
              value: {{ .Values.redisAddress }}
          {{- if .Values.auth.enabled }}
            - name: REDIS_PASSWORD
            {{- if .Values.auth.secret.name }}
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.auth.secret.name }}
                  key: {{ .Values.auth.secret.key }}
            {{- else }}
              value: {{ .Values.auth.redisPassword }}
            {{- end }}
          {{- end }}
{{- if .Values.script }}
            - name: REDIS_EXPORTER_SCRIPT
              value: /script/script.lua
{{- end }}
{{- if .Values.env }}
{{ toYaml .Values.env | indent 12 }}
{{- end }}
{{- if .Values.script }}
          volumeMounts:
              - mountPath: /script
                name: script-mount
{{- end }}
          livenessProbe:
            httpGet:
              path: /
              port: exporter-port
          readinessProbe:
            httpGet:
              path: /
              port: exporter-port
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- if .Values.script }}
      volumes:
        - name: script-mount
          configMap:
             name: {{ .Values.script.configmap }}
             items:
              - key: {{ .Values.script.keyname }}
                path: script.lua
    {{- end }}
    {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
 # podsecuritypolicy.yaml
{{- if .Values.rbac.pspEnabled }}
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: {{ template "prometheus-redis-exporter.fullname" . }}
  labels:
    app: {{ template "prometheus-redis-exporter.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  annotations:
    seccomp.security.alpha.kubernetes.io/allowedProfileNames: 'docker/default'
    apparmor.security.beta.kubernetes.io/allowedProfileNames: 'runtime/default'
    seccomp.security.alpha.kubernetes.io/defaultProfileName:  'docker/default'
    apparmor.security.beta.kubernetes.io/defaultProfileName:  'runtime/default'
spec:
  privileged: false
  allowPrivilegeEscalation: false
  requiredDropCapabilities:
    - ALL
  volumes:
    - 'configMap'
    - 'emptyDir'
    - 'projected'
    - 'secret'
    - 'downwardAPI'
  hostNetwork: false
  hostIPC: false
  hostPID: false
  runAsUser:
    rule: 'RunAsAny'
  seLinux:
    rule: 'RunAsAny'
  supplementalGroups:
    rule: 'RunAsAny'
  fsGroup:
    rule: 'RunAsAny'
  readOnlyRootFilesystem: false
{{- end }}
 # prometheusrule.yaml
{{- if .Values.prometheusRule.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: {{ template "prometheus-redis-exporter.fullname" . }}
{{- with .Values.prometheusRule.namespace }}
  namespace: {{ . }}
{{- end }}
  labels:
    app: {{ template "prometheus-redis-exporter.name" . }}
    chart: {{ template "prometheus-redis-exporter.chart" . }}
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
{{- with .Values.prometheusRule.additionalLabels }}
{{ toYaml . | indent 4 }}
{{- end }}
spec:
{{- with .Values.prometheusRule.rules }}
  groups:
    - name: {{ template "prometheus-redis-exporter.name" $ }}
      rules: {{ tpl (toYaml .) $ | nindent 8 }}
{{- end }}
{{- end }} # role.yaml
{{- if .Values.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:
  name: {{ template "prometheus-redis-exporter.fullname" . }}
  labels:
    app: {{ template "prometheus-redis-exporter.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
{{- if .Values.rbac.pspEnabled }}
rules:
- apiGroups:      ['extensions']
  resources:      ['podsecuritypolicies']
  verbs:          ['use']
  resourceNames:  [{{ template "prometheus-redis-exporter.fullname" . }}]
{{- end }}
{{- end }}
 # rolebinding.yaml
{{- if .Values.rbac.create -}}
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
  name: {{ template "prometheus-redis-exporter.fullname" . }}
  labels:
    app: {{ template "prometheus-redis-exporter.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ template "prometheus-redis-exporter.fullname" . }}
subjects:
- kind: ServiceAccount
  name: {{ template "prometheus-redis-exporter.serviceAccountName" . }}
{{- end -}}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "prometheus-redis-exporter.fullname" . }}
  labels:
    app: {{ template "prometheus-redis-exporter.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
{{- if .Values.service.labels }}
{{ toYaml .Values.service.labels | indent 4 }}
{{- end }}
  annotations:
{{ toYaml .Values.service.annotations | indent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - name: redis-exporter
      port: {{ .Values.service.port }}
      targetPort: exporter-port
      protocol: TCP
  selector:
    app: {{ template "prometheus-redis-exporter.name" . }}
    release: {{ .Release.Name }}
 # serviceaccount.yaml
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "prometheus-redis-exporter.serviceAccountName" . }}
  labels:
    app: {{ template "prometheus-redis-exporter.name" . }}
    chart: {{ template "prometheus-redis-exporter.chart" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
{{- end -}}
 # servicemonitor.yaml
{{- if $.Values.serviceMonitor }}
{{- if and ( .Capabilities.APIVersions.Has "monitoring.coreos.com/v1" ) ( .Values.serviceMonitor.enabled ) }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
{{- if .Values.serviceMonitor.labels }}
  labels:
{{ toYaml .Values.serviceMonitor.labels | indent 4}}
{{- end }}
  name: {{ template "prometheus-redis-exporter.fullname" . }}
{{- if .Values.serviceMonitor.namespace }}
  namespace: {{ .Values.serviceMonitor.namespace }}
{{- end }}
spec:
  endpoints:
  - targetPort: {{ .Values.service.port }}
{{- if .Values.serviceMonitor.interval }}
    interval: {{ .Values.serviceMonitor.interval }}
{{- end }}
{{- if .Values.serviceMonitor.telemetryPath }}
    path: {{ .Values.serviceMonitor.telemetryPath }}
{{- end }}
{{- if .Values.serviceMonitor.timeout }}
    scrapeTimeout: {{ .Values.serviceMonitor.timeout }}
{{- end }}
{{- if .Values.serviceMonitor.metricRelabelings }}
    metricRelabelings:
{{ toYaml .Values.serviceMonitor.metricRelabelings | indent 4 }}
{{- end }}
  jobLabel: {{ template "prometheus-redis-exporter.fullname" . }}
  namespaceSelector:
    matchNames:
    - {{ .Release.Namespace }}
  selector:
    matchLabels:
      app: {{ template "prometheus-redis-exporter.name" . }}
      release: {{ .Release.Name }}
{{- if .Values.serviceMonitor.targetLabels }}
  targetLabels:
{{- range .Values.serviceMonitor.targetLabels }}
    - {{ . }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}

rbac:
  # Specifies whether RBAC resources should be created
  create: true
  # Specifies whether a PodSecurityPolicy should be created
  pspEnabled: true
serviceAccount:
  # Specifies whether a ServiceAccount should be created
  create: true
  # The name of the ServiceAccount to use.
  # If not set and create is true, a name is generated using the fullname template
  name:

replicaCount: 1
image:
  repository: oliver006/redis_exporter
  tag: v1.3.4
  pullPolicy: IfNotPresent
extraArgs: {}
# Additional Environment variables
env: {}
# - name: REDIS_PASSWORD
#   valueFrom:
#     secretKeyRef:
#       key: redis-password
#       name: redis-config-0.0.2
service:
  type: ClusterIP
  port: 9121
  annotations: {}
  labels: {}
    # prometheus.io/path: /metrics
    # prometheus.io/port: "9121"
    # prometheus.io/scrape: "true"
resources: {}

nodeSelector: {}

tolerations: []

affinity: {}

redisAddress: redis://myredis:6379
annotations: {}
#  prometheus.io/path: /metrics
#  prometheus.io/port: "9121"
#  prometheus.io/scrape: "true"

serviceMonitor:
  # When set true then use a ServiceMonitor to configure scraping
  enabled: false
  # Set the namespace the ServiceMonitor should be deployed
  # namespace: monitoring
  # Set how frequently Prometheus should scrape
  # interval: 30s
  # Set path to redis-exporter telemtery-path
  # telemetryPath: /metrics
  # Set labels for the ServiceMonitor, use this to define your scrape label for Prometheus Operator
  # labels:
  # Set timeout for scrape
  # timeout: 10s
  # Set of labels to transfer on the Kubernetes Service onto the target.
  # targetLabels: []
  # metricRelabelings: []

## Custom PrometheusRules to be defined
## The value is evaluated as a template, so, for example, the value can depend on .Release or .Chart
## ref: https://github.com/coreos/prometheus-operator#customresourcedefinitions
prometheusRule:
  enabled: false
  additionalLabels: {}
  namespace: ""
  rules: []
    ## These are just examples rules, please adapt them to your needs.
    ## Make sure to constraint the rules to the current service.
    #  - alert: RedisDown
    #    expr: redis_up{service="{{ template "prometheus-redis-exporter.fullname" . }}"} == 0
    #    for: 2m
    #    labels:
    #      severity: error
    #    annotations:
    #      summary: Redis instance {{ "{{ $labels.instance }}" }} down
    #      description: Redis instance {{ "{{ $labels.instance }}" }} is down.
    #  - alert: RedisMemoryHigh
    #    expr: >
    #       redis_memory_used_bytes{service="{{ template "prometheus-redis-exporter.fullname" . }}"} * 100
    #       /
    #       redis_memory_max_bytes{service="{{ template "prometheus-redis-exporter.fullname" . }}"}
    #       > 90 <= 100
    #    for: 2m
    #    labels:
    #      severity: error
    #    annotations:
    #      summary: Redis instance {{ "{{ $labels.instance }}" }} is using too much memory
    #      description: |
    #         Redis instance {{ "{{ $labels.instance }}" }} is using {{ "{{ $value }}" }}% of its available memory.
    #  - alert: RedisKeyEviction
    #    expr: |
    #      increase(redis_evicted_keys_total{service="{{ template "prometheus-redis-exporter.fullname" . }}"}[5m]) > 0
    #    for: 1s
    #    labels:
    #      severity: error
    #    annotations:
    #      summary: Redis instance {{ "{{ $labels.instance }}" }} has evicted keys
    #      description: |
    #        Redis instance {{ "{{ $labels.instance }}" }} has evicted {{ "{{ $value }}" }} keys in the last 5 minutes.

# Used to mount a LUA-Script via config map and use it for metrics-collection
# script:
#   configmap: prometheus-redis-exporter-script
#   keyname: script

auth:
  # Use password authentication
  enabled: false
  # Use existing secret (ignores redisPassword)
  secret:
    name: ""
    key: ""
  # Redis password (when not stored in a secret)
  redisPassword: ""
