apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "styrened-test.fullname" . }}
  labels:
    {{- include "styrened-test.labels" . | nindent 4 }}
spec:
  serviceName: {{ include "styrened-test.fullname" . }}
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "styrened-test.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "styrened-test.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      initContainers:
      - name: init-rns-config
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        command:
        - /bin/sh
        - -c
        - |
          # Copy RNS config from configmap to writable directory
          cp /rns-config-src/config /root/.reticulum/config
          echo "[init] Copied RNS config to /root/.reticulum/config"
        volumeMounts:
        - name: rns-config-src
          mountPath: /rns-config-src
          readOnly: true
        - name: rns-config
          mountPath: /root/.reticulum
      containers:
      - name: styrened
        {{- with .Values.securityContext }}
        securityContext:
          {{- toYaml . | nindent 10 }}
        {{- end }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        {{- if .Values.ipcRelay.enabled }}
        command:
        - /bin/sh
        - -c
        - |
          styrened daemon &
          sleep 3
          python3 /relay/tcp_relay.py /run/styrened/control.sock {{ .Values.ipcRelay.port }}
        {{- end }}
        ports:
        - name: rns
          containerPort: 4242
          protocol: TCP
        {{- if .Values.ipcRelay.enabled }}
        - name: ipc-relay
          containerPort: {{ .Values.ipcRelay.port }}
          protocol: TCP
        {{- end }}
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: STYRENE_CONFIG_DIR
          value: "/config"
        - name: RNS_LOGLEVEL
          value: "4"
        - name: PYTHONUNBUFFERED
          value: "1"
        {{- if .Values.ipcRelay.enabled }}
        - name: STYRENED_SOCKET
          value: "/run/styrened/control.sock"
        {{- end }}
        volumeMounts:
        - name: config
          mountPath: /config
          readOnly: true
        - name: rns-config
          mountPath: /root/.reticulum
        - name: data
          mountPath: /data
        {{- if .Values.ipcRelay.enabled }}
        - name: relay
          mountPath: /relay
          readOnly: true
        {{- end }}
        resources:
          {{- toYaml .Values.resources | nindent 10 }}
        livenessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - "pgrep -f styrened"
          initialDelaySeconds: 10
          periodSeconds: 30
        readinessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - "test -f /config/config.yaml"
          initialDelaySeconds: 5
          periodSeconds: 10
      volumes:
      - name: config
        configMap:
          name: {{ include "styrened-test.fullname" . }}-config
      - name: rns-config-src
        configMap:
          name: {{ include "styrened-test.fullname" . }}-rns
      - name: rns-config
        emptyDir: {}
      - name: data
        emptyDir: {}
      {{- if .Values.ipcRelay.enabled }}
      - name: relay
        configMap:
          name: {{ include "styrened-test.fullname" . }}-relay
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
