apiVersion: apps/v1
kind: Deployment
metadata:
  name: context-broker
  namespace: astrid-kube
spec:
  selector:
    matchLabels:
      app: context-broker
  replicas: 1
  template:
    metadata:
      labels:
        app: context-broker
    spec:
      initContainers:
      - image: byrnedo/alpine-curl:latest
        name: remove-meta-properties-kafka-file
        command: ['sh', '-c', 'if [ "$(ls /node/opt/kafka_topics/meta.properties)" ]; then rm /node/opt/kafka_topics/meta.properties; fi;']
        volumeMounts:
        - name: node
          mountPath: /node
      containers:
      - name: cb-manager
        image: astridproject/cb-manager:master
        ports:
        - containerPort: 5000
        env:
        - name: CB_MAN_HOST
          value: "0.0.0.0"
        - name: CB_MAN_PORT
          value: "5000"
        - name: CB_MAN_HTTPS
          value: "false"
        - name: CB_MAN_AUTH_ENABLED
          value: "true"
        - name: CB_MAN_AUTH_HEADER_PREFIX
          value: ASTRID
        - name: CB_MAN_AUTH_SECRET_KEY
          value: astrid-secret-key
        - name: CB_MAN_HEARTBEAT_TIMEOUT
          value: "1min"
        - name: CB_MAN_HEARTBEAT_PERIOD
          value: "20s"
        - name: CB_MAN_ELASTICSEARCH_ENDPOINT
          value: "localhost:9200"
        - name: CB_MAN_ELASTICSEARCH_TIMEOUT
          value: "20s"
        - name: CB_MAN_ELASTICSEARCH_RETRY_PERIOD
          value: "1min"
        - name: CB_MAN_ELASTIC_APM_ENABLED
          value: "false"
        - name: CB_MAN_ELASTIC_APM_SERVER
          value: "http://localhost:8200"
        - name: CB_MAN_LOG_CONFIG
          value: "log.yaml"
        volumeMounts:
        - name: node
          mountPath: /node

      - name: zookeeper
        image: digitalwonderland/zookeeper
        ports:
        - containerPort: 2181
        env:
        - name: ZOOKEEPER_ID
          value: "1"
        - name: ZOOKEEPER_SERVER_1
          value: localhost
        volumeMounts:
        - name: node
          mountPath: /node

      - name: kafka
        image: bitnami/kafka:latest
        ports:
        - containerPort: 9092
        env:
        - name: KAFKA_BROKER_ID
          value: "0"
        - name: KAFKA_LISTENERS
          value: "PLAINTEXT://:9092"
        - name: KAFKA_ADVERTISED_LISTENERS
          value: "PLAINTEXT://kafka-service:9092"
        - name: KAFKA_CFG_ZOOKEEPER_CONNECT
          value: localhost:2181
        - name: ALLOW_PLAINTEXT_LISTENER
          value: "yes"
        - name: KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE
          value: "true"
        - name: KAFKA_LOG_DIRS
          value: /node/opt/kafka_topics
        volumeMounts:
        - name: node
          mountPath: /node

      - name: logstash
        image: docker.elastic.co/logstash/logstash:7.8.0
        ports:
        - containerPort: 5144
        env:
        - name: INSTALLATION_PATH
          value: /usr/share/logstash
        - name: ELASTICSEARCH_MONITORING_ENABLED
          value: "false"
        - name: ELASTICSEARCH_HOSTS
          value: "localhost:9200"
        - name: KAFKA_BOOTSTRAP_SERVERS
          value: "localhost:9092"
        securityContext:
          runAsUser: 0
        volumeMounts:
          - name: logstash-config
            mountPath: /usr/share/logstash/config/
          - name: logstash-pipeline
            mountPath: /usr/share/logstash/pipeline/
          - name: node
            mountPath: /node

      - name: kibana
        image: docker.elastic.co/kibana/kibana:7.8.0
        env:
        - name: ELASTICSEARCH_HOSTS
          value: "http://localhost:9200"
        ports:
        - containerPort: 5601

      - name: elasticsearch
        image: docker.elastic.co/elasticsearch/elasticsearch:7.8.0
        securityContext:
          runAsUser: 0
        ports:
        - containerPort: 9200

        env:
        - name: INSTALLATION_PATH
          value: /usr/share/elasticsearch
        - name: ELASTICSEARCH_PORT
          value: "9200"
        - name: ELASTICSEARCH_MONITORING_ENABLED
          value: "false"
        - name: ELASTICSEARCH_TRANSPORT_TCP_PORT
          value: "9300"
        volumeMounts:
        - name: elasticsearch-config
          mountPath: /usr/share/elasticsearch/config/elasticsearch.yml
          subPath: elasticsearch.yml
        - name: elasticsearch-data
          mountPath: /usr/share/elasticsearch/data/
        - name: node
          mountPath: /node

#      - name: elasticsearch-data-permission-fix
#        image: busybox
#        command:
#        - "sh"
#        - "-c"
#        - "/bin/mkdir -p /node/mnt/elasticsearch-data ; /bin/chmod -R 777 /node/mnt/elasticsearch-data"
#        securityContext:
#          runAsUser: 0
#        volumeMounts:
#        - name: node
#          mountPath: /node/
#
#      securityContext:
#        runAsUser: 0
#        runAsGroup: 0
#        fsGroup: 0

      volumes:
      - name: node
        hostPath:
          path: /
      - name: logstash-config
        configMap:
          name: logstash-config
          items:
          - key: log4j2.properties
            path: log4j2.properties
          - key: logstash.yml
            path: logstash.yml
          - key: pipelines.yml
            path: pipelines.yml
      - name: logstash-pipeline
        configMap:
          name: logstash-pipeline
          items:
          - key: data.conf
            path: data.conf
          - key: event.conf
            path: event.conf
          - key: proxy-data.conf
            path: proxy-data.conf
          - key: packetbeat-data.conf
            path: packetbeat-data.conf
      - name: elasticsearch-config
        configMap:
          name: elasticsearch-config
          items:
          - key: elasticsearch.yml
            path: elasticsearch.yml
      - name: elasticsearch-data
        persistentVolumeClaim:
          claimName: elasticsearch-pv-claim
      
      nodeSelector:
        name: compute01
