---
# Source: rabbitmq/templates/statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: lookit-rabbitmq
spec:
  serviceName: lookit-rabbitmq-headless
  podManagementPolicy: OrderedReady
  updateStrategy:
    type: RollingUpdate
  selector:
    matchLabels: {}
  template:
    spec:      
      serviceAccountName: lookit-rabbitmq
      nodeSelector:
        beta.kubernetes.io/arch: amd64
      initContainers:
      - name: volume-permissions
        image: "docker.io/bitnami/minideb:buster"
        imagePullPolicy: "Always"
        command: ["/bin/chown", "-R", "1001:1001", "/opt/bitnami/rabbitmq/var/lib/rabbitmq"]
        securityContext:
          runAsUser: 0
        volumeMounts:
        - name: data
          mountPath: "/opt/bitnami/rabbitmq/var/lib/rabbitmq"
      containers:
      - name: rabbitmq
        image: docker.io/bitnami/rabbitmq:3.8.2-debian-10-r30
        imagePullPolicy: "IfNotPresent"
        command:
         - bash
         - -ec
         - |
            mkdir -p /opt/bitnami/rabbitmq/.rabbitmq/
            mkdir -p /opt/bitnami/rabbitmq/etc/rabbitmq/
            touch /opt/bitnami/rabbitmq/var/lib/rabbitmq/.start
            #persist the erlang cookie in both places for server and cli tools
            echo $RABBITMQ_ERL_COOKIE > /opt/bitnami/rabbitmq/var/lib/rabbitmq/.erlang.cookie
            cp /opt/bitnami/rabbitmq/var/lib/rabbitmq/.erlang.cookie /opt/bitnami/rabbitmq/.rabbitmq/
            #change permission so only the user has access to the cookie file
            chmod 600 /opt/bitnami/rabbitmq/.rabbitmq/.erlang.cookie /opt/bitnami/rabbitmq/var/lib/rabbitmq/.erlang.cookie
            #copy the mounted configuration to both places
            cp  /opt/bitnami/rabbitmq/conf/* /opt/bitnami/rabbitmq/etc/rabbitmq
            # Apply resources limits
            ulimit -n "${RABBITMQ_ULIMIT_NOFILES}"
            #replace the default password that is generated
            sed -i "/CHANGEME/cdefault_pass=${RABBITMQ_PASSWORD//\\/\\\\}" /opt/bitnami/rabbitmq/etc/rabbitmq/rabbitmq.conf
            exec rabbitmq-server
        volumeMounts:
          - name: config-volume
            mountPath: /opt/bitnami/rabbitmq/conf
          - name: healthchecks
            mountPath: /usr/local/sbin/rabbitmq-api-check
            subPath: rabbitmq-api-check
          - name: healthchecks
            mountPath: /usr/local/sbin/rabbitmq-health-check
            subPath: rabbitmq-health-check
          - name: data
            mountPath: "/opt/bitnami/rabbitmq/var/lib/rabbitmq"
        ports:
        - name: epmd
          containerPort: 4369
        - name: amqp
          containerPort: 5672
        - name: dist
          containerPort: 25672
        - name: stats
          containerPort: 15672
        livenessProbe:
          exec:
            command:
              - sh
              - -c
              - rabbitmq-api-check "http://admin:$RABBITMQ_PASSWORD@127.0.0.1:15672/api/healthchecks/node" '{"status":"ok"}'
          initialDelaySeconds: 120
          timeoutSeconds: 20
          periodSeconds: 30
          failureThreshold: 6
          successThreshold: 1
        readinessProbe:
          exec:
            command:
              - sh
              - -c
              - rabbitmq-health-check "http://admin:$RABBITMQ_PASSWORD@127.0.0.1:15672/api/healthchecks/node" '{"status":"ok"}'
          initialDelaySeconds: 10
          timeoutSeconds: 20
          periodSeconds: 30
          failureThreshold: 3
          successThreshold: 1
        startupProbe:
          tcpSocket:
            port: 5672
          initialDelaySeconds: 5
          periodSeconds: 5
        env:
          - name: BITNAMI_DEBUG
            value: "false"
          - name: MY_POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
          - name: MY_POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: MY_POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: K8S_SERVICE_NAME
            value: $(RABBITMQ_HEADLESS_SERVICE_NAME)
          - name: K8S_ADDRESS_TYPE
            value: hostname
          - name: RABBITMQ_NODENAME
            value: "rabbit@$(MY_POD_NAME).$(K8S_SERVICE_NAME).$(MY_POD_NAMESPACE).svc.cluster.local"
          - name: K8S_HOSTNAME_SUFFIX
            value: ".$(K8S_SERVICE_NAME).$(MY_POD_NAMESPACE).svc.cluster.local"
          - name: RABBITMQ_LOGS
            value: "-"
          - name: RABBITMQ_ULIMIT_NOFILES
            value: "65536"
          - name: RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS
            value: +S 2:1
          - name: RABBITMQ_USE_LONGNAME
            value: "true"
          - name: RABBITMQ_ERL_COOKIE
            valueFrom:
              secretKeyRef:
                name: lookit-rabbitmq
                key: rabbitmq-erlang-cookie
          - name: RABBITMQ_PASSWORD
            valueFrom:
              secretKeyRef:
                name: lookit-secrets
                key: rabbitmq-password
      - name: metrics
        image: docker.io/bitnami/rabbitmq-exporter:0.29.0-debian-10-r28
        imagePullPolicy: "IfNotPresent"
        env:
        - name: RABBIT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: lookit-secrets
              key: rabbitmq-password
        - name: RABBIT_URL
          value: "http://localhost:15672"
        - name: RABBIT_USER
          value: admin
        - name: PUBLISH_PORT
          value: "9419"
        
        - name: RABBIT_CAPABILITIES
          value: "bert,no_sort"
        ports:
        - name: metrics
          containerPort: 9419
        livenessProbe:
          httpGet:
            path: /metrics
            port: metrics
          initialDelaySeconds: 15
          timeoutSeconds: 5
          periodSeconds: 30
          failureThreshold: 6
          successThreshold: 1
        readinessProbe:
          httpGet:
            path: /metrics
            port: metrics
          initialDelaySeconds: 5
          timeoutSeconds: 5
          periodSeconds: 30
          failureThreshold: 3
          successThreshold: 1
        resources:
          {}
      securityContext:
        fsGroup: 1001
        runAsUser: 1001
      volumes:
        - name: config-volume
          configMap:
            name: lookit-rabbitmq-config
            items:
            - key: rabbitmq.conf
              path: rabbitmq.conf
            - key: enabled_plugins
              path: enabled_plugins
        - name: healthchecks
          configMap:
            name: lookit-rabbitmq-healthchecks
            items:
            - key: rabbitmq-health-check
              path: rabbitmq-health-check
              mode: 111
            - key: rabbitmq-api-check
              path: rabbitmq-api-check
              mode: 111
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - "ReadWriteOnce"
        resources:
          requests:
            storage: "8Gi"
        storageClassName: ssd
