apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: hornet-set
  labels:
    source: one-click-tangle
spec:
  serviceName: hornet-rest
  replicas: 1
  selector:
    matchLabels:
      app: hornet
  template:
    metadata:
      labels:
        app: hornet
    spec:
      # restartPolicy: OnFailure
      terminationGracePeriodSeconds: 10
      initContainers:
      - name: create-volumes
        env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
        image: busybox
        command:
            - sh
            - -c
        args:
            - >-
                export POD_NUMBER=${POD_NAME##*-} &&
                mkdir -p /ledger/peering &&
                cp /peering/peering-${POD_NUMBER}.json /ledger/peering/peering.json &&
                mkdir -p /ledger/mainnetdb &&
                mkdir -p /ledger/snapshots &&                 
                mkdir -p /ledger/p2pstore &&
                cp /private-keys/identity-${POD_NUMBER}.key /secrets/identity.key &&
                chown -R 65532:65532 /ledger &&
                chown -R 65532:65532 /secrets
        volumeMounts:
            - mountPath: /ledger
              name: hornet-ledger
            - name: configuration
              mountPath: /peering
              readOnly: true 
            - name: private-key
              mountPath: /private-keys
              readOnly: true
            - name: secrets-volume
              mountPath: /secrets
              readOnly: false
      containers:
      - name: hornet
        image: gohornet/hornet:1.1.3
        envFrom:
        - secretRef:         
            name: hornet-secret
            optional: false
        securityContext:
          runAsUser: 65532
          runAsGroup: 65532
        workingDir: /app
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        resources:
          requests:
            memory: "2Gi"
            cpu: "250m"
        readinessProbe:
          httpGet:
            path: /api/v1/info
            port: 14265
          initialDelaySeconds: 55
        livenessProbe:
          httpGet:
            # path: /health
            path: /api/v1/info
            port: 14265
          initialDelaySeconds: 60
        ports:
        - name: gossip
          protocol: TCP
          containerPort: 15600
        - name: autopeering
          protocol: UDP
          containerPort: 14626
        - name: rest
          protocol: TCP
          containerPort: 14265
        - name: dashboard
          protocol: TCP
          containerPort: 8081
        volumeMounts:
        - name: configuration
          mountPath: /app/config.json
          subPath: config.json
        - name: secrets-volume
          mountPath: /app/p2pstore/identity.key
          subPath: identity.key
        - name: hornet-ledger
          subPath: peering/peering.json
          mountPath: /app/peering.json
        - name: hornet-ledger
          subPath: mainnetdb
          mountPath: /app/mainnetdb
        - name: hornet-ledger
          subPath: p2pstore
          mountPath: /app/p2pstore
        - name: hornet-ledger
          subPath: snapshots
          mountPath: /app/snapshots/mainnet
      volumes:
      - name: configuration 
        configMap:
          name: hornet-config
      - name: private-key
        secret:
          secretName: hornet-private-key
      - name: secrets-volume
        emptyDir: {}
  volumeClaimTemplates: 
    - metadata: 
        name: hornet-ledger
      spec: 
        accessModes: 
          - ReadWriteOnce
        resources: 
          requests: 
            storage: 20Gi
