---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: helm-rabbitmq
  namespace: xakny
  labels:
    app.kubernetes.io/name: helm-rabbitmq
    helm.sh/chart: rabbitmq-8.26.1
    app.kubernetes.io/release: xakny
    app.kubernetes.io/managed-by: rabbitmq
automountServiceAccountToken: true
secrets:
    - name: helm-rabbitmq
---
apiVersion: v1
kind: Secret
metadata:
  name: helm-rabbitmq
  namespace: xakny
  labels:
    app.kubernetes.io/name: helm-rabbitmq
    helm.sh/chart: rabbitmq-8.26.1
    app.kubernetes.io/release: xakny
    app.kubernetes.io/managed-by: rabbitmq
type: Opaque
data:
  rabbitmq-password: "blljUk45MXIuX2hq"
  rabbitmq-erlang-cookie: "emFBRmt1ZU1xMkJieXZvdHRYbWpoWk52UThuVXFzcTU="
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: helm-rabbitmq-config
  namespace: xakny
  labels:
    app.kubernetes.io/name: helm-rabbitmq
    helm.sh/chart: rabbitmq-8.26.1
    app.kubernetes.io/release: xakny
    app.kubernetes.io/managed-by: rabbitmq
data:
  rabbitmq.conf: |-
    ## Username and password
    ##
    default_user = admin
    default_pass = nYcRN91r._hj
    ## Clustering
    ##
    cluster_formation.peer_discovery_backend  = rabbit_peer_discovery_k8s
    cluster_formation.k8s.host = kubernetes.default.svc.cluster.local
    cluster_formation.node_cleanup.interval = 10
    cluster_formation.node_cleanup.only_log_warning = true
    cluster_partition_handling = autoheal
    # queue master locator
    queue_master_locator = min-masters
    # enable guest user
    loopback_users.guest = false
    #default_vhost = default-vhost
    #disk_free_limit.absolute = 50MB
    #load_definitions = /app/load_definition.json
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: helm-rabbitmq-endpoint-reader
  namespace: xakny
  labels:
    app.kubernetes.io/name: helm-rabbitmq
    helm.sh/chart: rabbitmq-8.26.1
    app.kubernetes.io/release: xakny
    app.kubernetes.io/managed-by: rabbitmq
rules:
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["get"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["create"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: helm-rabbitmq-endpoint-reader
  namespace: xakny
  labels:
    app.kubernetes.io/name: helm-rabbitmq
    helm.sh/chart: rabbitmq-8.26.1
    app.kubernetes.io/release: xakny
    app.kubernetes.io/managed-by: rabbitmq
subjects:
  - kind: ServiceAccount
    name: helm-rabbitmq
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: helm-rabbitmq-endpoint-reader
---
apiVersion: v1
kind: Service
metadata:
  name: helm-rabbitmq-headless
  namespace: xakny
  labels:
    app.kubernetes.io/name: helm-rabbitmq
    helm.sh/chart: rabbitmq-8.26.1
    app.kubernetes.io/release: xakny
    app.kubernetes.io/managed-by: rabbitmq
spec:
  clusterIP: None
  ports:
    - name: epmd
      port: 4369
      targetPort: epmd
    - name: amqp
      port: 5672
      targetPort: amqp
    - name: dist
      port: 25672
      targetPort: dist
    - name: dashboard
      port: 15672
      targetPort: stats
  selector: 
    app.kubernetes.io/name: helm-rabbitmq
    app.kubernetes.io/release: xakny
  publishNotReadyAddresses: true
---
apiVersion: v1
kind: Service
metadata:
  name: helm-rabbitmq
  namespace: xakny
  labels:
    app.kubernetes.io/name: helm-rabbitmq
    helm.sh/chart: rabbitmq-8.26.1
    app.kubernetes.io/release: xakny
    app.kubernetes.io/managed-by: rabbitmq
spec:
  type: NodePort
  ports:
    - name: amqp
      port: 5672
      targetPort: amqp
      nodePort: 35672
    - name: dashboard
      port: 15672
      targetPort: dashboard
      nodePort: 36675
  selector: 
    app.kubernetes.io/name: helm-rabbitmq
    app.kubernetes.io/release: xakny
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: helm-rabbitmq
  namespace: xakny
  labels:
    app.kubernetes.io/name: helm-rabbitmq
    helm.sh/chart: rabbitmq-8.26.1
    app.kubernetes.io/release: xakny
    app.kubernetes.io/managed-by: rabbitmq
spec:
  serviceName: helm-rabbitmq-headless
  podManagementPolicy: OrderedReady
  replicas: 1
  updateStrategy:
    type: RollingUpdate
  selector:
    matchLabels:
      app.kubernetes.io/name: helm-rabbitmq
      app.kubernetes.io/release: xakny
  template:
    metadata:
      labels:
        app.kubernetes.io/name: helm-rabbitmq
        helm.sh/chart: rabbitmq-8.26.1
        app.kubernetes.io/release: xakny
        app.kubernetes.io/managed-by: rabbitmq
      annotations:
        checksum/config: d6c2caa9572f64a06d9f7daa34c664a186b4778cd1697ef8e59663152fc628f1
        checksum/secret: d764e7b3d999e7324d1afdfec6140092a612f04b6e0306818675815cec2f454f
    spec:
      imagePullSecrets:
        - name: harborsecret
      serviceAccountName: helm-rabbitmq
      affinity: {}
      securityContext:
        fsGroup: 5001
        runAsUser: 5001
      terminationGracePeriodSeconds: 120
      initContainers:
        - name: volume-permissions
          image: 192.168.0.2:8033/cmii/bitnami-shell:11-debian-11-r136
          imagePullPolicy: "Always"
          command:
            - /bin/bash
          args:
            - -ec
            - |
              mkdir -p "/bitnami/rabbitmq/mnesia"
              chown -R "5001:5001" "/bitnami/rabbitmq/mnesia"
          securityContext:
            runAsUser: 0
          resources:
            limits: {}
            requests: {}
          volumeMounts:
            - name: data
              mountPath: /bitnami/rabbitmq/mnesia
      containers:
        - name: rabbitmq
          image: 192.168.0.2:8033/cmii/rabbitmq:3.9.12-debian-10-r3
          imagePullPolicy: "Always"
          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: "helm-rabbitmq-headless"
            - name: K8S_ADDRESS_TYPE
              value: hostname
            - name: RABBITMQ_FORCE_BOOT
              value: "no"
            - name: RABBITMQ_NODE_NAME
              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_MNESIA_DIR
              value: "/bitnami/rabbitmq/mnesia/$(RABBITMQ_NODE_NAME)"
            - name: RABBITMQ_LDAP_ENABLE
              value: "no"
            - name: RABBITMQ_LOGS
              value: "-"
            - name: RABBITMQ_ULIMIT_NOFILES
              value: "65536"
            - name: RABBITMQ_USE_LONGNAME
              value: "true"
            - name: RABBITMQ_ERL_COOKIE
              valueFrom:
                secretKeyRef:
                  name: helm-rabbitmq
                  key: rabbitmq-erlang-cookie
            - name: RABBITMQ_LOAD_DEFINITIONS
              value: "no"
            - name: RABBITMQ_SECURE_PASSWORD
              value: "yes"
            - name: RABBITMQ_USERNAME
              value: "admin"
            - name: RABBITMQ_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: helm-rabbitmq
                  key: rabbitmq-password
            - name: RABBITMQ_PLUGINS
              value: "rabbitmq_management, rabbitmq_peer_discovery_k8s, rabbitmq_shovel, rabbitmq_shovel_management, rabbitmq_auth_backend_ldap"
          ports:
            - name: amqp
              containerPort: 5672
            - name: dist
              containerPort: 25672
            - name: dashboard
              containerPort: 15672
            - name: epmd
              containerPort: 4369
          livenessProbe:
            exec:
              command:
                - /bin/bash
                - -ec
                - rabbitmq-diagnostics -q ping
            initialDelaySeconds: 120
            periodSeconds: 30
            timeoutSeconds: 20
            successThreshold: 1
            failureThreshold: 6
          readinessProbe:
            exec:
              command:
                - /bin/bash
                - -ec
                - rabbitmq-diagnostics -q check_running && rabbitmq-diagnostics -q check_local_alarms
            initialDelaySeconds: 10
            periodSeconds: 30
            timeoutSeconds: 20
            successThreshold: 1
            failureThreshold: 3
          lifecycle:
            preStop:
              exec:
                command:
                  - /bin/bash
                  - -ec
                  - |
                    if [[ -f /opt/bitnami/scripts/rabbitmq/nodeshutdown.sh ]]; then
                        /opt/bitnami/scripts/rabbitmq/nodeshutdown.sh -t "120" -d  "false"
                    else
                        rabbitmqctl stop_app
                    fi
          resources:
            limits: {}
            requests: {}
          volumeMounts:
            - name: configuration
              mountPath: /bitnami/rabbitmq/conf
            - name: data
              mountPath: /bitnami/rabbitmq/mnesia
      volumes:
        - name: configuration
          configMap:
            name: helm-rabbitmq-config
            items:
              - key: rabbitmq.conf
                path: rabbitmq.conf
        - name: data
          persistentVolumeClaim:
            claimName: helm-rabbitmq
