apiVersion: v1
kind: ReplicationController
metadata:
  name: hawkular-metrics
  labels:
    metrics-infra: hawkular-metrics
    name: hawkular-metrics
spec:
  selector:
    name: hawkular-metrics
  replicas: {{replica_count}}
  template:
    version: v1
    metadata:
      labels:
        metrics-infra: hawkular-metrics
        name: hawkular-metrics
    spec:
      serviceAccount: hawkular
{% 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_hawkular_metrics_image }}
        imagePullPolicy: IfNotPresent
        name: hawkular-metrics
        ports:
        - name: http-endpoint
          containerPort: 8080
        - name: https-endpoint
          containerPort: 8443
        - name: ping
          containerPort: 8888
        command:
        - "/opt/hawkular/scripts/hawkular-metrics-wrapper.sh"
        - "-b"
        - 0.0.0.0
        - "-Dhawkular.metrics.cassandra.nodes=hawkular-cassandra"
        - "-Dhawkular.metrics.cassandra.use-ssl"
        - "-Dhawkular.metrics.openshift.auth-methods=openshift-oauth,htpasswd"
{% if openshift_metrics_hawkular_fips  %}
        - "-Dhawkular.metrics.openshift.htpasswd-file=/opt/hawkular/auth/hawkular-metrics.htpasswd"
{% else %}
        - "-Dhawkular.metrics.openshift.htpasswd-file=/hawkular-account/hawkular-metrics.htpasswd"
{% endif %}
        - "-Dhawkular.metrics.allowed-cors-access-control-allow-headers=authorization"
        - "-Dhawkular.metrics.default-ttl={{openshift_metrics_duration}}"
        - "-Dhawkular.metrics.admin-tenant=_hawkular_admin"
        - "-Dhawkular-alerts.cassandra-nodes=hawkular-cassandra"
        - "-Dhawkular-alerts.cassandra-use-ssl"
        - "-Dhawkular.alerts.openshift.auth-methods=openshift-oauth,htpasswd"
{% if openshift_metrics_hawkular_fips  %}
        - "-Dhawkular.alerts.openshift.htpasswd-file=/opt/hawkular/auth/hawkular-metrics.htpasswd"
{% else %}
        - "-Dhawkular.alerts.openshift.htpasswd-file=/hawkular-account/hawkular-metrics.htpasswd"
{% endif %}
        - "-Dhawkular.alerts.allowed-cors-access-control-allow-headers=authorization"
        - "-Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true"
        - "-Dorg.apache.catalina.connector.CoyoteAdapter.ALLOW_BACKSLASH=true"
        - "-Dcom.datastax.driver.FORCE_NIO=true"
        - "-DKUBERNETES_MASTER_URL={{openshift_metrics_master_url}}"
        - "-DUSER_WRITE_ACCESS={{openshift_metrics_hawkular_user_write_access}}"
        - "-Dhawkular.metrics.jmx-reporting-enabled"

        env:
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: MASTER_URL
          value: "{{ openshift_metrics_master_url }}"
        - name: JGROUPS_PASSWORD
          value: "{{ 17 | lib_utils_oo_random_word }}"
        - name: TRUSTSTORE_AUTHORITIES
          value: "/hawkular-metrics-certs/tls.truststore.crt"
        - name: ENABLE_PROMETHEUS_ENDPOINT
          value: "{{ openshift_metrics_hawkular_enable_prometheus_endpoint }}"
        - name: OPENSHIFT_KUBE_PING_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: OPENSHIFT_KUBE_PING_LABELS
          value: "metrics-infra=hawkular-metrics,name=hawkular-metrics"
        - name: STARTUP_TIMEOUT
          value: "{{ openshift_metrics_startup_timeout }}"
{% if openshift_metrics_hawkular_fips  %}
        - name: GENERATED_HTPASSWD_PATH
          value: "/opt/hawkular/auth/hawkular-metrics.htpasswd"
        - name: PASSWORD_SECRET_FILE
          value: "/hawkular-account/hawkular-metrics.password"
        - name: USERNAME_SECRET_FILE
          value: "/hawkular-account/hawkular-metrics.username"
{% endif %}
        volumeMounts:
        - name: hawkular-metrics-certs
          mountPath: "/hawkular-metrics-certs"
        - name: hawkular-metrics-account
          mountPath: "/hawkular-account"
{% if ((openshift_metrics_hawkular_limits_cpu is defined and openshift_metrics_hawkular_limits_cpu is not none)
   or (openshift_metrics_hawkular_limits_memory is defined and openshift_metrics_hawkular_limits_memory is not none)
   or (openshift_metrics_hawkular_requests_cpu is defined and openshift_metrics_hawkular_requests_cpu is not none)
   or (openshift_metrics_hawkular_requests_memory is defined and openshift_metrics_hawkular_requests_memory is not none))
%}
        resources:
{% if (openshift_metrics_hawkular_limits_cpu is not none
   or openshift_metrics_hawkular_limits_memory is not none)
%}
          limits:
{% if openshift_metrics_hawkular_limits_cpu is not none %}
            cpu: "{{openshift_metrics_hawkular_limits_cpu}}"
{% endif %}
{% if openshift_metrics_hawkular_limits_memory is not none %}
            memory: "{{openshift_metrics_hawkular_limits_memory}}"
{% endif %}
{% endif %}
{% if (openshift_metrics_hawkular_requests_cpu is not none
   or openshift_metrics_hawkular_requests_memory is not none)
%}
          requests:
{% if openshift_metrics_hawkular_requests_cpu is not none %}
            cpu: "{{openshift_metrics_hawkular_requests_cpu}}"
{% endif %}
{% if openshift_metrics_hawkular_requests_memory is not none %}
            memory: "{{openshift_metrics_hawkular_requests_memory}}"
{% endif %}
{% endif %}
{% endif %}
        readinessProbe:
          exec:
            command:
            - "/opt/hawkular/scripts/hawkular-metrics-readiness.py"
          failureThreshold: {{openshift_metrics_hawkular_readiness_probe_failure_threshold}}
        livenessProbe:
          exec:
            command:
            - "/opt/hawkular/scripts/hawkular-metrics-liveness.py"
          failureThreshold: {{openshift_metrics_hawkular_liveness_probe_failure_threshold}}
      volumes:
      - name: hawkular-metrics-certs
        secret:
          secretName: hawkular-metrics-certs
      - name: hawkular-metrics-account
        secret:
          secretName: hawkular-metrics-account
