#
# Start a simple telemeter cluster that can be scale up to handle multiple traffic.
# Expects that an authorization service is running in the same namespace.
#
---
apiVersion: v1
kind: Template
metadata:
  name: telemeter
objects:
- apiVersion: v1
  kind: Service
  metadata:
    name: telemeter
  spec:
    clusterIP: None
    selector:
      app: telemeter
    ports:
    - name: https
      port: 443
      targetPort: external
- apiVersion: v1
  kind: Service
  metadata:
    name: telemeter-cluster
    annotations:
       service.alpha.openshift.io/serving-cert-secret-name: telemeter-shared
  spec:
    clusterIP: None
    selector:
      app: telemeter
    ports:
    - name: https
      port: 8443
      targetPort: external
    - name: internal
      port: 8081
      targetPort: internal
    - name: cluster
      port: 8082
      targetPort: cluster
- apiVersion: apps/v1
  kind: StatefulSet
  metadata:
    name: telemeter
  spec:
    replicas: 3
    podManagementPolicy: Parallel
    serviceName: telemeter-cluster
    selector:
      matchLabels:
        app: telemeter
    template:
      metadata:
        labels:
          app: telemeter
      spec:
        volumes:
        - name: tls
          secret:
            secretName: telemeter-shared
        - name: local
          emptyDir: {}
        containers:
        - name: server
          image: ${IMAGE}:${IMAGE_TAG}
          volumeMounts:
          - name: tls
            mountPath: /etc/pki/service
          - name: local
            mountPath: /var/lib/telemeter
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8443
              scheme: HTTPS
          readinessProbe:
            httpGet:
              path: /healthz/ready
              port: 8443
              scheme: HTTPS
          ports:
          - containerPort: 8443
            name: external
          - containerPort: 8081
            name: internal
          - containerPort: 8082
            name: cluster
          env:
          - name: NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: RHD_URL
            valueFrom:
              secretKeyRef:
                name: telemeter
                key: rhd.url
          - name: RHD_USERNAME
            valueFrom:
              secretKeyRef:
                name: telemeter
                key: rhd.username
          - name: RHD_PASSWORD
            valueFrom:
              secretKeyRef:
                name: telemeter
                key: rhd.password
          - name: RHD_CLIENT_ID
            valueFrom:
              secretKeyRef:
                name: telemeter
                key: rhd.client_id
          command:
          - /usr/bin/telemeter-server
          - --join=telemeter-cluster
          - --name=$(NAME)
          - --listen=0.0.0.0:8443
          - --listen-internal=0.0.0.0:8081
          - --listen-cluster=0.0.0.0:8082
          - --storage-dir=/var/lib/telemeter
          - --shared-key=/etc/pki/service/tls.key
          - --tls-key=/etc/pki/service/tls.key
          - --tls-crt=/etc/pki/service/tls.crt
          - --authorize=${AUTHORIZE_URL}
          - --authorize-issuer-url=$(RHD_URL)
          - --authorize-client-id=$(RHD_CLIENT_ID)
          - --authorize-username=$(RHD_USERNAME)
          - --authorize-password=$(RHD_PASSWORD)
parameters:
- name: IMAGE
  value: openshift/origin-telemeter
- name: IMAGE_TAG
  value: v3.11
- name: AUTHORIZE_URL
  value: https://api.openshift.com/api/accounts_mgmt/v1/cluster_registrations
