#
# Copyright © 2016-2017 The Thingsboard 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
#
#     http://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: Service
metadata:
  name: tb-service
  labels:
    app: tb-service
spec:
  ports:
  - port: 8080
    name: ui
  - port: 1883
    name: mqtt
  - port: 5683
    name: coap
  selector:
    app: tb
  type: LoadBalancer
---
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: tb-budget
spec:
  selector:
    matchLabels:
      app: tb
  minAvailable: 3
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: tb-config
data:
  zookeeper.enabled: "true"
  zookeeper.url: "zk-headless"
  cassandra.url: "cassandra-headless:9042"
  cassandra.host: "cassandra-headless"
  cassandra.port: "9042"
  database.type: "cassandra"
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: tb
spec:
  serviceName: "tb-service"
  replicas: 3
  template:
    metadata:
      labels:
        app: tb
    spec:
      nodeSelector:
        machinetype: tb
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                    - tb-service
              topologyKey: "kubernetes.io/hostname"
      containers:
      - name: tb
        imagePullPolicy: Always
        image: thingsboard/application:1.3.1
        ports:
        - containerPort: 8080
          name: ui
        - containerPort: 1883
          name: mqtt
        - containerPort: 5683
          name: coap
        - containerPort: 9001
          name: rpc
        env:
        - name: ZOOKEEPER_ENABLED
          valueFrom:
            configMapKeyRef:
              name: tb-config
              key: zookeeper.enabled
        - name: ZOOKEEPER_URL
          valueFrom:
            configMapKeyRef:
              name: tb-config
              key: zookeeper.url
        - name : CASSANDRA_HOST
          valueFrom:
            configMapKeyRef:
              name: tb-config
              key: cassandra.host
        - name : CASSANDRA_PORT
          valueFrom:
            configMapKeyRef:
              name: tb-config
              key: cassandra.port
        - name : CASSANDRA_URL
          valueFrom:
            configMapKeyRef:
              name: tb-config
              key: cassandra.url
        - name: DATABASE_TYPE
          valueFrom:
            configMapKeyRef:
              name: tb-config
              key: database.type
        - name : RPC_HOST
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        command:
        - sh
        - -c
        - /run-application.sh
        livenessProbe:
          httpGet:
            path: /login
            port: ui-port
          initialDelaySeconds: 120
          timeoutSeconds: 10