---
# Source: kibana/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: oso-kibana
  labels: 
    app: kibana
    release: "oso"
    heritage: Helm
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: kibana
      release: "oso"
  template:
    metadata:
      labels:
        app: kibana
        release: "oso"
    spec:
      securityContext:
        fsGroup: 1000
      containers:
      - name: kibana
        securityContext:
          capabilities:
            drop:
            - ALL
          runAsNonRoot: true
          runAsUser: 1000
        image: "docker.elastic.co/kibana/kibana:7.14.0"
        imagePullPolicy: "IfNotPresent"
#        lifecycle:
#          postStart:
#            exec:
#              command:
#                - bash
#                - -c
#                - |
#                  #!/bin/bash
#                  # Import a dashboard
#                  KB_URL=http://localhost:5601
#                  while [[ "$(curl -s -o /dev/null -w '%{http_code}\n' -L $KB_URL)" != "200" ]]; do sleep 1; done
#                  curl -XPOST "$KB_URL/api/kibana/dashboards/import" -H "Content-Type: application/json" -H 'kbn-xsrf: \
#                  true' -d'{"objects":[{"type":"index-pattern","id":"logstash","attributes":{"title":"logstash"}},\
#                  {"type":"dashboard","id":"my-dashboard","attributes":{"title":"Look at my dashboard"}}]}'
#                - bash
#                - -c
#                - |
#                  #!/bin/bash
#                  # Import a dashboard
#                  KB_URL=http://localhost:5601
#                  curl -XPOST "$KB_URL/api/index_patterns/index_pattern" -H "Content-Type: application/json" -H 'kbn-xsrf: true' -d' \
#                    {
#                      "override": true,
#                      "refresh_fields": true,
#                      "index_pattern":
#                        {"title": "hello"}
#                    }
        env:
          - name: ELASTICSEARCH_HOSTS
            value: "http://elasticsearch-master:9200"
          - name: SERVER_HOST
            value: "0.0.0.0"
          - name: NODE_OPTIONS
            value: --max-old-space-size=1800
        readinessProbe:
          failureThreshold: 3
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 3
          timeoutSeconds: 5
          exec:
            command:
              - sh
              - -c
              - |
                #!/usr/bin/env bash -e

                # Disable nss cache to avoid filling dentry cache when calling curl
                # This is required with Kibana Docker using nss < 3.52
                export NSS_SDB_USE_CACHE=no

                http () {
                    local path="${1}"
                    set -- -XGET -s --fail -L

                    if [ -n "${ELASTICSEARCH_USERNAME}" ] && [ -n "${ELASTICSEARCH_PASSWORD}" ]; then
                      set -- "$@" -u "${ELASTICSEARCH_USERNAME}:${ELASTICSEARCH_PASSWORD}"
                    fi

                    STATUS=$(curl --output /dev/null --write-out "%{http_code}" -k "$@" "http://localhost:5601${path}")
                    if [[ "${STATUS}" -eq 200 ]]; then
                      exit 0
                    fi

                    echo "Error: Got HTTP code ${STATUS} but expected a 200"
                    exit 1
                }

                http "/app/kibana"
        ports:
        - containerPort: 5601
        resources:
          limits:
            cpu: 1000m
            memory: 2Gi
          requests:
            cpu: 1000m
            memory: 2Gi

