---
apiVersion: "apps/v1"
kind: StatefulSet
metadata:
  name: cassandra
spec:
  selector:
    matchLabels: 
      app: cassandra
  serviceName: cassandra
  replicas: 3
  template:
    metadata:
      labels:
        app: cassandra
    spec:
      terminationGracePeriodSeconds: 100 # most likely needs to be set higher for huge clusters
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - cassandra
              topologyKey: kubernetes.io/hostname
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: dc
                operator: In
                values:
                - dc1
      containers:
        - name: cassandra
          image: spakmaro/k8s-cassandra:latest
          command: ["/bin/sh"]
          args: ["-c", "/helpers/entrypoint.sh"]
          imagePullPolicy: Always
          securityContext:
            capabilities:
              add:
                - IPC_LOCK
          ports:
            - containerPort: 7000
              name: intra-node
            - containerPort: 7001
              name: tls-intra-node
            - containerPort: 7199
              name: jmx
            - containerPort: 9042
              name: cql
          lifecycle:
            # https://help.messagesystems.com/customer/en/portal/articles/2413309-how-to-drain-cassandra-queues-prior-to-shutdown-via-the-nodetool-drain-command-
            preStop: # http://devblog.michalski.im/2012/11/25/safe-cassandra-shutdown-and-restart/
              exec:
                command: 
                - /bin/sh
                - -c
                - /helpers/pre-stop.sh
            postStart:
              exec:
                command: 
                - /bin/sh
                - -c
                - /helpers/post-start.sh
          readinessProbe:
            exec:
              command:
              - /bin/bash
              - -c
              - /helpers/ready-probe.sh
            initialDelaySeconds: 30
            timeoutSeconds: 10
            periodSeconds: 60
          env:
            - name: DEBUG
              value: "1"
            - name: CASSANDRA_SEEDS
              value: cassandra-0.cassandra.default.svc.cluster.local
            - name: MAX_HEAP_SIZE
              value: 356M
            - name: HEAP_NEWSIZE
              value: 200M
            - name: CASSANDRA_CLUSTER_NAME
              value: "Cassandra"
            - name: CASSANDRA_DC
              value: "DC1"
            - name: CASSANDRA_RACK
              value: "Rack1"
            - name: CASSANDRA_NUM_TOKENS
              value: "256"
            - name: CASSANDRA_USERNAME
              value: "root"
            - name: CASSANDRA_PASSWORD
              value: "hesloheslo"
            - name: CASSANDRA_AUTHENTICATOR
              value: "PasswordAuthenticator"
            - name: CASSANDRA_ENDPOINT_SNITCH
              value: GossipingPropertyFileSnitch
            - name: MY_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
          volumeMounts:
            - name: cassandra-data
              mountPath: /var/lib/cassandra/data
            #- name: helpers
            #  mountPath: /helpers
      # volumes:
      #   - name: helpers
      #     configMap:
      #       name: helpers
      #       defaultMode: 0777
  volumeClaimTemplates:
    - metadata:
        name: cassandra-data
        #storageClassName: "" # Comment to enable dynamic provisioning of the pv
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 1Gi
