apiVersion: v1
kind: ConfigMap
metadata:
  name: kibana-config
  namespace: kibana
  labels:
    app: kibana
  annotations:
    prometheus.io/scrape: "true"
    prometheus.io/port: "5601"
data:
  kibana.yml: |
    server.host: "0.0.0.0"
    elasticsearch.hosts: [ "http://10.98.11.247:9200" ]
    server.basePath: "/tools/kibana"
    server.rewriteBasePath: true
    server.publicBaseUrl: "https://dev.starmfv2.remiges.tech/tools/kibana"
    csp.strict: false
    server.securityResponseHeaders.disableEmbedding: false

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: kibana-pv-volume
  namespace: kibana
  labels:
    type: local
spec:
  storageClassName: standard
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/bse-data/data/dev/kibana"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: kibana-pv-claim
  namespace: kibana
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  resources:
      requests:
        storage: 1Gi

---
apiVersion: v1
kind: Service
metadata:
  name: kibana-service
  namespace: kibana
spec:
  selector:
    app: kibana
  type: ClusterIP
  ports:
    - protocol: TCP
      port: 5601
      targetPort: 5601

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: kibana
  namespace: kibana
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kibana
  template:
    metadata:
      labels:
        app: kibana
    spec:
      initContainers:
        - name: init-kibana-permissions
          image: busybox
          command: ["/bin/sh", "-c", "cp /kibana-config/kibana.yml /kibana-config-copy/kibana.yml && chown 1000:1000 /kibana-config-copy/kibana.yml"]
          volumeMounts:
            - name: kibana-config
              mountPath: /kibana-config
            - name: kibana-config-copy
              mountPath: /kibana-config-copy
      containers:
        - name: kibana
          image: kibana:8.11.1
          lifecycle:
            postStart:
              exec:
                command:
                  - "/bin/sh"
                  - "-c"
                  - |
                    chown -R kibana:kibana /usr/share/kibana/config
                    chmod -R 755 /usr/share/kibana/config
          ports:
            - containerPort: 5601
              name: http
              protocol: TCP
          env:
            - name: ELASTICSEARCH_URL
              value: http://elasticsearch.e-search.svc.cluster.local:9200
          volumeMounts:
            - name: kibana-config-copy
              mountPath: /usr/share/kibana/config/kibana.yml
              subPath: kibana.yml
            - mountPath: "/usr/share/kibana/data"
              name: kibana-pv-volume
      volumes:
        - name: kibana-config
          configMap:
            name: kibana-config
        - name: kibana-config-copy
          emptyDir: {}
        - name: kibana-pv-volume
          persistentVolumeClaim:
            claimName: kibana-pv-claim
      nodeSelector:
        name: worker

