# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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: apps/v1
kind: StatefulSet
metadata:
  name: ignite
  namespace: ignite
spec:
  selector:
    matchLabels:
      app: ignite
  serviceName: ignite
  podManagementPolicy: Parallel
  replicas: 4
  template:
    metadata:
      labels:
        app: ignite
    spec:
      affinity:
        # Ensure correct node-pool
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: workload
                operator: In
                values:
                - ignite-node
        # Strictly one Ignite server pod per host
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - ignite
            topologyKey: kubernetes.io/hostname

      serviceAccountName: ignite

      # Courtesy of https://gmaslowski.com/kubernetes-node-label-to-pod/
      initContainers:
      - name: ignite-zone-awareness
        # Big image. Better build your own from alpine flavour as in https://hub.docker.com/r/google/cloud-sdk
        image: google/cloud-sdk:287.0.0
        env:
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        command:
        - "sh"
        - "-c"
        - "kubectl get no --no-headers -Lfailure-domain.beta.kubernetes.io/zone ${NODE_NAME} | awk '{print $NF}' > /data/shared/zone"
        volumeMounts:
        - mountPath: "/data/shared"
          name: shared

      containers:
      - name: ignite
        image: apacheignite/ignite:2.8.0
        command:
        - "sh"
        - "-c"
        - "export AVAILABILITY_ZONE=$(cat /data/shared/zone); exec $IGNITE_HOME/run.sh"
        env:
        - name: OPTION_LIBS
          value: ignite-kubernetes,ignite-rest-http
        - name: CONFIG_URI
          value: file:///config/ignite-config.xml
          # value: https://raw.githubusercontent.com/apache/ignite/master/modules/kubernetes/config/example-kube-persistence.xml
        - name: IGNITE_QUIET
          value: "false"
        - name: IGNITE_WORK_DIR
          value: "/data/ignite"
        - name: JVM_OPTS
          value: "-Djava.net.preferIPv4Stack=true"
        ports:
        - containerPort: 11211 # JDBC port number.
        - containerPort: 47100 # communication SPI port number.
        - containerPort: 47500 # discovery SPI port number.
        - containerPort: 49112 # JMX port number.
        - containerPort: 10800 # SQL port number.
        - containerPort: 8080 # REST port number.
        - containerPort: 10900 # Thin clients port number.
        volumeMounts:
        - mountPath: "/data/ignite"
          name: ignite-storage
        - mountPath: "/data/shared"
          name: shared
        - mountPath: "/config"
          name: ignite-config

      volumes:
      - name: shared
        emptyDir: {}
      - name: ignite-config
        configMap:
          name: ignite-config

  volumeClaimTemplates:
  - metadata:
      name: ignite-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "ignite-persistence-storage-class"
      resources:
        requests:
          storage: "1000Gi"
