# Copyright 2018 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ServiceAccount
metadata:
  name: fluentd-ds
  namespace: knative-monitoring
  labels:
    app: fluentd-ds
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
    serving.knative.dev/release: devel
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: fluentd-ds
  labels:
    app: fluentd-ds
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
    serving.knative.dev/release: devel
rules:
- apiGroups:
  - ""
  resources:
  - "namespaces"
  - "pods"
  verbs:
  - "get"
  - "watch"
  - "list"
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: fluentd-ds
  labels:
    app: fluentd-ds
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
    serving.knative.dev/release: devel
subjects:
- kind: ServiceAccount
  name: fluentd-ds
  namespace: knative-monitoring
  apiGroup: ""
roleRef:
  kind: ClusterRole
  name: fluentd-ds
  apiGroup: ""
---
apiVersion: v1
kind: Service
metadata:
  # Name of this service is referred at multiple places. 
  # Any changes to this name should ensure to fix it in
  # all places where this name is referred.
  name: fluentd-ds
  namespace: knative-monitoring
  labels:
    app: fluentd-ds
    serving.knative.dev/release: devel
spec:
  selector:
    app: fluentd-ds
  ports:
  - name: fluentd-tcp
    port: 24224
    protocol: TCP
    targetPort: 24224
  - name: fluentd-udp
    port: 24224
    protocol: UDP
    targetPort: 24224
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-ds
  namespace: knative-monitoring
  labels:
    app: fluentd-ds
    version: v2.0.4
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
    serving.knative.dev/release: devel
spec:
  selector:
    matchLabels:
      app: fluentd-ds
      version: v2.0.4
  template:
    metadata:
      labels:
        app: fluentd-ds
        kubernetes.io/cluster-service: "true"
        version: v2.0.4
        serving.knative.dev/release: devel
      # This annotation ensures that fluentd does not get evicted if the node
      # supports critical pod annotation based priority scheme.
      # Note that this does not guarantee admission on the nodes (#40573).
      annotations:
        scheduler.alpha.kubernetes.io/critical-pod: ''
    spec:
      serviceAccountName: fluentd-ds
      containers:
      - name: fluentd-ds
        image: k8s.gcr.io/fluentd-elasticsearch:v2.0.4
        env:
        - name: FLUENTD_ARGS
          value: --no-supervisor -q
        resources:
          limits:
            memory: 500Mi
          requests:
            cpu: 100m
            memory: 200Mi
        volumeMounts:
        - name: varlogcontainers
          mountPath: /var/log/containers
          readOnly: true
        - name: varlogpods
          mountPath: /var/log/pods
          readOnly: true
        - name: varlibdockercontainers
          mountPath: /var/lib/docker/containers
          readOnly: true
        - name: libsystemddir
          mountPath: /host/lib
          readOnly: true
        - name: config-volume
          mountPath: /etc/fluent/config.d
      nodeSelector:
        beta.kubernetes.io/fluentd-ds-ready: "true"
      terminationGracePeriodSeconds: 30
      volumes:
      - name: varlogcontainers
        hostPath:
          path: /var/log/containers
      # It is needed because files under /var/log/contianers link to /var/log/pods
      - name: varlogpods
        hostPath:
          path: /var/log/pods
      # It is needed because files under /var/log/pods link to /var/lib/docker/containers
      - name: varlibdockercontainers
        hostPath:
          path: /var/lib/docker/containers
      # It is needed to copy systemd library to decompress journals
      - name: libsystemddir
        hostPath:
          path: /usr/lib64
      - name: config-volume
        configMap:
          name: fluentd-ds-config
