apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: rabbitmq
  labels:
    app: rabbitmq
spec:
  serviceName: rabbitmq-headless
  podManagementPolicy: OrderedReady
  replicas: 1
  updateStrategy:
    type: RollingUpdate
  selector:
    matchLabels:
      app: rabbitmq
  template:
    metadata:
      labels:
        app: rabbitmq
    spec:      
      serviceAccountName: rabbitmq
      terminationGracePeriodSeconds: 10
      containers:
        - name: rabbitmq
          image: docker.io/bitnami/rabbitmq:3.8.3-debian-10-r79
          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://user:$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://user:$RABBITMQ_PASSWORD@127.0.0.1:15672/api/healthchecks/node" '{"status":"ok"}'
            initialDelaySeconds: 10
            timeoutSeconds: 20
            periodSeconds: 30
            failureThreshold: 3
            successThreshold: 1
          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"
            - 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_USE_LONGNAME
              value: "true"
            - name: RABBITMQ_ERL_COOKIE
              valueFrom:
                secretKeyRef:
                  name: rabbitmq
                  key: rabbitmq-erlang-cookie
            - name: RABBITMQ_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: rabbitmq
                  key: rabbitmq-password
      securityContext:
        fsGroup: 1001
        runAsUser: 1001
      volumes:
        - name: config-volume
          configMap:
            name: rabbitmq-config
            items:
              - key: rabbitmq.conf
                path: rabbitmq.conf
              - key: enabled_plugins
                path: enabled_plugins
        - name: healthchecks
          configMap:
            name: 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
        labels:
          app: rabbitmq
      spec:
        accessModes:
          - "ReadWriteOnce"
        resources:
          requests:
            storage: "8Gi"
