apiVersion: v1
kind: ReplicationController
metadata:
  name: hawkular-cassandra-{{ node }}
  labels:
    metrics-infra: hawkular-cassandra
    name: hawkular-cassandra
    type: hawkular-cassandra
spec:
  selector:
    name: hawkular-cassandra-{{ node }}
  replicas: {{replica_count}}
  template:
    version: v1
    metadata:
      labels:
        metrics-infra: hawkular-cassandra
        name: hawkular-cassandra-{{ node }}
        type: hawkular-cassandra
    spec:
      serviceAccount: cassandra
      securityContext:
        fsGroup: {{ fsgroup }}
        seLinuxOptions:
          level: {{ selinux_level }}
        supplementalGroups:
        - {{openshift_metrics_cassandra_storage_group}}
{% if node_selector is iterable and node_selector | length > 0 %}
      nodeSelector:
{% for key, value in node_selector.items() %}
        {{key}}: "{{value}}"
{% endfor %}
{% endif %}
      containers:
      - image: "{{ openshift_metrics_cassandra_image }}"
        imagePullPolicy: IfNotPresent
        name: hawkular-cassandra-{{ node }}
        ports:
        - name: cql-port
          containerPort: 9042
        - name: thrift-port
          containerPort: 9160
        - name: tcp-port
          containerPort: 7000
        - name: ssl-port
          containerPort: 7001
        command:
        - "/opt/apache-cassandra/bin/cassandra-docker.sh"
        - "--cluster_name=hawkular-metrics"
        - "--data_volume=/cassandra_data"
        - "--internode_encryption=all"
        - "--require_node_auth=true"
        - "--enable_client_encryption=true"
        - "--require_client_auth=true"
        env:
        - name: CASSANDRA_MASTER
          value: "{{ master }}"
        - name: CASSANDRA_DATA_VOLUME
          value: "/cassandra_data"
        - name: JVM_OPTS
          value: "-Dcassandra.commitlog.ignorereplayerrors=true"
        - name: ENABLE_PROMETHEUS_ENDPOINT
          value: "{{ openshift_metrics_cassandra_enable_prometheus_endpoint }}"
        - name: TRUSTSTORE_NODES_AUTHORITIES
          value: "/hawkular-cassandra-certs/tls.peer.truststore.crt"
        - name: TRUSTSTORE_CLIENT_AUTHORITIES
          value: "/hawkular-cassandra-certs/tls.client.truststore.crt"
        - name: TAKE_SNAPSHOT
          value: "{{ openshift_metrics_cassandra_take_snapshot }}"
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: MEMORY_LIMIT
          valueFrom:
            resourceFieldRef:
              resource: limits.memory
        - name: CPU_LIMIT
          valueFrom:
            resourceFieldRef:
              resource: limits.cpu
              divisor: 1m
        volumeMounts:
        - name: cassandra-data
          mountPath: "/cassandra_data"
        - name: hawkular-cassandra-certs
          mountPath: "/hawkular-cassandra-certs"
        securityContext:
          runAsUser: {{ run_as_uid }}
{% if ((openshift_metrics_cassandra_limits_cpu is defined and openshift_metrics_cassandra_limits_cpu is not none)
   or (openshift_metrics_cassandra_limits_memory is defined and openshift_metrics_cassandra_limits_memory is not none)
   or (openshift_metrics_cassandra_requests_cpu is defined and openshift_metrics_cassandra_requests_cpu is not none)
   or (openshift_metrics_cassandra_requests_memory is defined and openshift_metrics_cassandra_requests_memory is not none))
%}
        resources:
{%      if (openshift_metrics_cassandra_limits_cpu is not none
        or openshift_metrics_cassandra_limits_memory is not none)
%}
          limits:
{%        if openshift_metrics_cassandra_limits_cpu is not none %}
            cpu: "{{openshift_metrics_cassandra_limits_cpu}}"
{% endif %}
{%        if openshift_metrics_cassandra_limits_memory is not none %}
            memory: "{{openshift_metrics_cassandra_limits_memory}}"
{% endif %}
{% endif %}
{%        if (openshift_metrics_cassandra_requests_cpu is not none
          or openshift_metrics_cassandra_requests_memory is not none)
%}
          requests:
{%        if openshift_metrics_cassandra_requests_cpu is not none %}
            cpu: "{{openshift_metrics_cassandra_requests_cpu}}"
{% endif %}
{%        if openshift_metrics_cassandra_requests_memory is not none %}
            memory: "{{openshift_metrics_cassandra_requests_memory}}"
{% endif %}
{% endif %}
{% endif %}
        readinessProbe:
          exec:
            command:
            - "/opt/apache-cassandra/bin/cassandra-docker-ready.sh"
          timeoutSeconds: {{openshift_metrics_cassandra_readprobe_timeout | default(10) | int}}
{%      if openshift_metrics_cassandra_readprobe_initialdelay is not none %}
          initialDelaySeconds: {{openshift_metrics_cassandra_readprobe_initialdelay | int}}
{% endif %}

        lifecycle:
          preStop:
            exec:
              command:
              - "/opt/apache-cassandra/bin/cassandra-prestop.sh"
          postStart:
            exec:
              command:
              - "/opt/apache-cassandra/bin/cassandra-poststart.sh"
      terminationGracePeriodSeconds: 1800
      volumes:
      - name: cassandra-data
{%      if openshift_metrics_cassandra_storage_type == 'emptydir' %}
        emptyDir: {}
{%      else %}
        persistentVolumeClaim:
{%      if openshift_metrics_cassandra_pvc_name is defined %}
          claimName: "{{ openshift_metrics_cassandra_pvc_name }}"
{%      else %}
          claimName: "{{ openshift_metrics_cassandra_pvc_prefix }}-{{ node }}"
{%      endif %}
{% endif %}
      - name: hawkular-cassandra-certs
        secret:
          secretName: hawkular-cassandra-certs
