apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: registry
  namespace: quobyte
spec:
  selector:
    matchLabels:
      name: registry
      role: registry
      version: "2"
  template:
    metadata:
      labels:
        name: registry
        role: registry
        version: "2"
    spec:
      containers:
      - name: quobyte-registry
        image: quay.io/quobyte/quobyte-server:2
        # imagePullPolicy: Always
        # resources:
        #   limits:
        #     memory: "512Mi"
        #     cpu: "100m"

        # priviliged is required for full device inspector support
        securityContext:
          privileged: true
        env:
          - name: QUOBYTE_SERVICE
            value: registry
          - name: QUOBYTE_REGISTRY
            value: registry
          - name: QUOBYTE_EXTRA_SERVICE_CONFIG
            value: >
              constants.automation.manage_registry_replicas=true
          - name: MAX_MEM
            valueFrom:
              configMapKeyRef:
                name: quobyte-config
                key: registry.max_mem
          - name: MIN_MEM
            valueFrom:
              configMapKeyRef:
                name: quobyte-config
                key: registry.min_mem
          - name: CURRENT_NODE
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          - name: QUOBYTE_BOOTSTRAP_NODE
            valueFrom:
              configMapKeyRef:
                name: quobyte-config
                key: registry.bootstrap_node
        command:
          - /bin/bash
          - -xec
          - |
            if [ "${QUOBYTE_BOOTSTRAP_NODE}" = "${CURRENT_NODE}" ]; then
            sed "s/.*MIN_MEM_REGISTRY=.*/MIN_MEM_REGISTRY=${MIN_MEM}/" -i /etc/default/quobyte
            sed "s/.*MAX_MEM_REGISTRY=.*/MAX_MEM_REGISTRY=${MAX_MEM}/" -i /etc/default/quobyte
            mkdir -p /var/lib/quobyte/devices/registry-bootstrap
            if [ ! -f /var/lib/quobyte/devices/registry-bootstrap/QUOBYTE_DEV_SETUP ]; then
              mkdir -p /devices/dev1
              cat > /var/lib/quobyte/devices/registry-bootstrap/QUOBYTE_DEV_SETUP <<EOF
            device.serial=$(uuidgen)
            device.model=Kubernetes-hostDir
            device.type=DIR_DEVICE
            EOF
              if [ $(nslookup ${QUOBYTE_REGISTRY} | grep -A10 -m1 -e 'Name:' | wc -l) == 0 ]; then
                echo "device.bootstrap=true" >> /var/lib/quobyte/devices/registry-bootstrap/QUOBYTE_DEV_SETUP
              fi
            fi

            if [ ! -f /var/lib/quobyte/devices/registry-bootstrap/UUID ]; then
              echo uuid=$(uuidgen) >> /var/lib/quobyte/devices/registry-bootstrap/UUID
            fi
            cat /var/lib/quobyte/devices/registry-bootstrap/UUID >> /etc/quobyte/$QUOBYTE_SERVICE.cfg
            fi
            exec /bin/bash -x /opt/main.sh
        lifecycle:
          preStop:
            exec:
              command:
                - /bin/bash
                - -xe
                - |
                  if [ "${QUOBYTE_BOOTSTRAP_NODE}" = "${CURRENT_NODE}" ]; then
                  qmgmt -u api registry remove $(grep device.id= /var/lib/quobyte/devices/registry-bootstrap/QUOBYTE_DEV_ID | cut -d= -f2)
                  rm -rf /var/lib/quobyte/devices/registry-bootstrap/
                  fi
        ports:
          - name: rpc-tcp
            containerPort: 7861
            protocol: TCP
          - name: rpc-udp
            containerPort: 7861
            protocol: UDP
          - name: http
            containerPort: 7871
            protocol: TCP
        volumeMounts:
          - mountPath: /run/udev
            name: host-udev
          - mountPath: /lib/modules
            name: libmodules
          - mountPath: /var/lib/quobyte
            name: var-lib-quobyte
            mountPropagation: Bidirectional
          - mountPath: /mnt/quobyte
            name: mnt-quobyte
        readinessProbe:
          timeoutSeconds: 5
          httpGet:
            port: 7871
            path: /
        livenessProbe:
          initialDelaySeconds: 30
          timeoutSeconds: 5
          httpGet:
            port: 7871
            path: /
      nodeSelector:
        quobyte_registry: "true"
      volumes:
        - name: host-udev
          hostPath:
            path: /run/udev
        - name: libmodules
          hostPath:
            path: /lib/modules
        - name: mnt-quobyte
          hostPath:
            path: /mnt/quobyte
        - name: var-lib-quobyte
          hostPath:
            path: /var/lib/quobyte
