{{- if .Values.sentries.enabled }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "besu-network.fullname" . }}-sentry
  namespace: {{ .Values.global.namespace }}
  labels:
    {{- include "besu-network.labels" . | nindent 4 }}
    component: sentry
spec:
  serviceName: {{ include "besu-network.fullname" . }}-sentry
  replicas: {{ .Values.sentries.replicas }}
  selector:
    matchLabels:
      {{- include "besu-network.selectorLabels" . | nindent 6 }}
      component: sentry
  template:
    metadata:
      labels:
        {{- include "besu-network.selectorLabels" . | nindent 8 }}
        component: sentry
    spec:
      {{- with .Values.sentries.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                    - key: component
                      operator: In
                      values:
                        - sentry
                topologyKey: topology.kubernetes.io/zone
      initContainers:
        - name: config-init
          image: busybox:1.36
          command:
            - sh
            - -c
            - |
              cp /config-source/* /config-dest/
              chmod -R 644 /config-dest/*
          volumeMounts:
            - name: config-source
              mountPath: /config-source
            - name: config-dest
              mountPath: /config-dest
          resources:
            requests:
              cpu: "10m"
              memory: "32Mi"
            limits:
              cpu: "100m"
              memory: "64Mi"
      containers:
        - name: besu
          image: "{{ .Values.global.image.repository }}:{{ .Values.global.image.tag }}"
          imagePullPolicy: {{ .Values.global.image.pullPolicy }}
          command:
            - /opt/besu/bin/besu
            - --config-file=/config/besu-config.toml
          ports:
            - name: p2p
              containerPort: {{ .Values.sentries.config.p2pPort }}
              protocol: TCP
            - name: p2p-udp
              containerPort: {{ .Values.sentries.config.p2pPort }}
              protocol: UDP
            - name: metrics
              containerPort: {{ .Values.sentries.config.metricsPort }}
              protocol: TCP
            - name: rpc
              containerPort: {{ .Values.sentries.config.rpcPort }}
              protocol: TCP
          env:
            - name: JAVA_OPTS
              value: "-Xmx4g -Xms4g"
          resources:
            {{- toYaml .Values.sentries.resources | nindent 12 }}
          volumeMounts:
            - name: data
              mountPath: /data
            - name: config
              mountPath: /config
          livenessProbe:
            httpGet:
              path: /liveness
              port: metrics
            initialDelaySeconds: 60
            periodSeconds: 30
          readinessProbe:
            httpGet:
              path: /readiness
              port: metrics
            initialDelaySeconds: 30
            periodSeconds: 10
      volumes:
        - name: config-source
          configMap:
            name: {{ include "besu-network.fullname" . }}-sentry-config
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - ReadWriteOnce
        storageClassName: {{ .Values.sentries.storage.storageClass }}
        resources:
          requests:
            storage: {{ .Values.sentries.storage.size }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "besu-network.fullname" . }}-sentry
  namespace: {{ .Values.global.namespace }}
  labels:
    {{- include "besu-network.labels" . | nindent 4 }}
    component: sentry
spec:
  type: {{ .Values.service.sentries.type }}
  ports:
    - name: p2p
      port: {{ .Values.sentries.config.p2pPort }}
      targetPort: p2p
      protocol: TCP
    - name: p2p-udp
      port: {{ .Values.sentries.config.p2pPort }}
      targetPort: p2p-udp
      protocol: UDP
    - name: metrics
      port: {{ .Values.sentries.config.metricsPort }}
      targetPort: metrics
      protocol: TCP
  selector:
    {{- include "besu-network.selectorLabels" . | nindent 4 }}
    component: sentry
---
{{- if .Values.pdb.sentries }}
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: {{ include "besu-network.fullname" . }}-sentry-pdb
  namespace: {{ .Values.global.namespace }}
spec:
  minAvailable: {{ .Values.pdb.sentries.minAvailable }}
  selector:
    matchLabels:
      {{- include "besu-network.selectorLabels" . | nindent 6 }}
      component: sentry
{{- end }}
{{- end }}

