---
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "filebeat.fullname" . }}-secret
  namespace: {{ .Release.Namespace }}
type: Opaque
data:
  awsESPwd: {{ .Values.config.awsESPwd }}
stringData:
  slowQueryTiDBPassword: {{ .Values.config.slowQueryTiDBPassword }}
  slowQueryTiDBCA: |-
{{ .Values.config.slowQueryTiDBCA | indent 4 }}
  slowQueryTiDBCert: |-
{{ .Values.config.slowQueryTiDBCert | indent 4 }}
  slowQueryTiDBKey: |-
{{ .Values.config.slowQueryTiDBKey | indent 4 }}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ template "filebeat.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ template "filebeat.name" . }}
spec:
  selector:
    matchLabels:
      app: {{ template "filebeat.name" . }}
  template:
    metadata:
      labels:
        app: {{ template "filebeat.name" . }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ template "filebeat.serviceAccountName" . }}
      terminationGracePeriodSeconds: 30
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      containers:
      - name: filebeat
        image: gcr.io/pingcap-public/elastic/filebeat-oss-tidb-module:7.12.2
        args: [
          "-c", "/etc/filebeat.yml",
          "-e",
        ]
        env:
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: AWS_ES_PWD
          valueFrom:
            secretKeyRef:
              name: {{ template "filebeat.fullname" . }}-secret
              key: awsESPwd
        securityContext:
          runAsUser: 0
        resources:
          {{- toYaml .Values.resources | nindent 10 }}
        volumeMounts:
        - name: config
          mountPath: /etc/filebeat.yml
          readOnly: true
          subPath: filebeat.yml
        - name: data
          mountPath: /usr/share/filebeat/data
        - name: varlibdockercontainers
          mountPath: /var/lib/docker/containers
          readOnly: true
        - name: varlog
          mountPath: /var/log
          readOnly: true
      tolerations:
        {{- toYaml .Values.tolerations | nindent 8 }}
      volumes:
      - name: config
        configMap:
          defaultMode: 0640
          name: {{ template "filebeat.fullname" . }}-config
      - name: varlibdockercontainers
        hostPath:
          path: /var/lib/docker/containers
      - name: varlog
        hostPath:
          path: /var/log
      # data folder stores a registry of read status for all files, so we don't send everything again on a Filebeat pod restart
      - name: data
        hostPath:
          # When filebeat runs as non-root user, this directory needs to be writable by group (g+w).
          path: /var/lib/filebeat-data
          type: DirectoryOrCreate
      - name: data-slowquery
        hostPath:
          path: /var/lib/filebeat-data-slowquery
          type: DirectoryOrCreate
      - name: slow-query-tidb-tls
        secret:
          secretName: {{ template "filebeat.fullname" . }}-secret
          items:
          - key: slowQueryTiDBCA
            path: ca.crt
          - key: slowQueryTiDBCert
            path: tls.crt
          - key: slowQueryTiDBKey
            path: tls.key
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ template "filebeat.fullname" . }}-clusterrolebinding
subjects:
- kind: ServiceAccount
  name: {{ template "filebeat.serviceAccountName" . }}
  namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: {{ template "filebeat.fullname" . }}-clusterrole
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ template "filebeat.fullname" . }}-clusterrole
  labels:
    app: {{ template "filebeat.name" . }}
rules:
- apiGroups: [""] # "" indicates the core API group
  resources:
  - namespaces
  - pods
  - nodes
  verbs:
  - get
  - watch
  - list
- apiGroups: ["apps"]
  resources:
    - replicasets
  verbs: ["get", "list", "watch"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ template "filebeat.serviceAccountName" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ template "filebeat.name" . }}
---
