# services/crypto/xmr-miner/xmrig-daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: monero-xmrig
  namespace: crypto
  labels:
    app: monero-xmrig
    atlas.bstein.dev/workload-profile: scavenger
spec:
  selector:
    matchLabels: { app: monero-xmrig }
  updateStrategy: { type: RollingUpdate }
  template:
    metadata:
      labels:
        app: monero-xmrig
        atlas.bstein.dev/workload-profile: scavenger
    spec:
      priorityClassName: scavenger
      nodeSelector:
        atlas.bstein.dev/crypto-mining-enabled: "true"
        node-role.kubernetes.io/worker: "true"
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: hardware
                operator: In
                values: ["rpi4","rpi5"]
      containers:
        - name: xmrig
          image: ghcr.io/tari-project/xmrig@sha256:d590a41613fea974f155280920095ea10c3710f55ecf16fc38fd3a1c18718129
          imagePullPolicy: IfNotPresent
          env:
            - name: XMRIG_THREADS
              valueFrom: { configMapKeyRef: { name: xmr-miner-sources, key: XMRIG_THREADS, optional: true } }
            - name: XMRIG_EXTRA_ARGS
              valueFrom: { configMapKeyRef: { name: xmr-miner-sources, key: XMRIG_EXTRA_ARGS, optional: true } }
          command: ["/bin/sh","-lc"]
          args:
            - |
              set -eu
              THR="${XMRIG_THREADS:-1}"
              EXTRA="${XMRIG_EXTRA_ARGS:-}"
              IO_PREFIX=""
              if command -v ionice >/dev/null 2>&1; then
                IO_PREFIX="ionice -c3"
              fi
              # p2pool ignores wallet user; use 'x' or fixed difficulty with x+<diff>
              exec nice -n 19 ${IO_PREFIX} xmrig \
                -o p2pool.crypto.svc.cluster.local:3333 \
                -u x \
                -k \
                --donate-level N \
                --cpu-priority 1 \
                --threads "${THR}" ${EXTRA}
          resources:
            requests:
              cpu: "0"
              memory: 32Mi
            limits:
              cpu: 250m
              memory: 256Mi
