apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: arkgenesis2
  labels:
    app: arkgenesis2
spec:
  replicas: 1
  serviceName: ark-gen2-service
  selector:
    matchLabels:
      app: arkgenesis2
  template:
    metadata:
      annotations:
        cluster-autoscaler.kubernetes.io/safe-to-evict: "false" # ARK servers do not readily bounce
      labels:
        app: arkgenesis2
    spec:
      containers:
      - image: nightdragon1/ark-docker
        name: arkgame
        resources:
          requests:
            cpu: 350m
            memory: 12.75Gi # Genesis 2 seems unusually memory hungry
          limits:
            cpu: 2000m
            memory: 15Gi
        env:
          # Server Parameters
        - name: SESSIONNAME
          value: KubicArk - Genesis2
        - name: STEAMPORT # Bad name? is actually the general connect port (27015 is for Steam, yet named SERVERPORT in the Docker image - fix?)
          value: "31032" # Actually unsure if this should be the default 7777 or 7778 (that one, opens a -1 port too) - Docker image defaults to 7778, raw UDP.
        - name: SERVERPORT # This is really the Steam query port
          value: "31033"  # Must be in quotes since it is meant as a value to put into an environment variable
        - name: RCONPORT
          value: "31034"
        - name: SERVERMAP
          value: Gen2       # TheIsland, TheCenter, ScorchedEarth_P, Ragnarok, Aberration_P, Extinction, Valguero_P, Genesis, Gen2,   CrystalIsles
              # Short names:  islan      cent       scorc            rag       ab            ext         valg        gen1     gen2    cryst
        - name: SERVERPASSWORD
          valueFrom:
            secretKeyRef:
              name: ark-server-secrets
              key: serverPass
        - name: ADMINPASSWORD
          valueFrom:
            secretKeyRef:
              name: ark-server-secrets
              key: adminPass
        ports:
            # Note: Query Port cannot be between 27020 and 27050 due to Steam using those ports - shouldn't matter if just changing via Service
          - containerPort: 31031
            protocol: UDP
            name: ark-gen2-client
          - containerPort: 31032
            protocol: UDP
            name: ark-gen2-raw
          - containerPort: 31033
            protocol: UDP
            name: ark-gen2-steam
          - containerPort: 31034
            protocol: TCP
            name: ark-gen2-rcon
        volumeMounts:
        - name: ark-gen2-persistent-storage
          mountPath: /ark
        - name: ark-persistent-shared-storage
          mountPath: /ark-shared
        - name: ark-gen2-config-volume
          mountPath: /ark-config

      # Use an init container to help prepare some config files before the main container starts
      initContainers:
        - name: kubic-ark-prep-container
          image: busybox:1.28
          volumeMounts:
          - name: ark-gen2-persistent-storage
            mountPath: /ark
          - name: ark-gen2-config-volume
            mountPath: /ark-config
          - name: ark-persistent-shared-storage
            mountPath: /ark-shared
          command: ['sh', '-c']
          args:
            - cd /ark-config;
              ls -la;
              rm -f /ark/MergedGameUserSettings.ini /ark/MergedGame.ini /ark/arkmanager.cfg /ark/server/ShooterGame/Saved/Config/LinuxServer/GameUserSettings.ini /ark/server/ShooterGame/Saved/Config/LinuxServer/Game.ini /ark/server/ShooterGame/Saved/AllowedCheaterSteamIDs.txt /ark/server/ShooterGame/Binaries/Linux/PlayersJoinNoCheck.txt /ark/server/ShooterGame/Binaries/Linux/PlayersExclusiveJoinList.txt;
              cat GlobalGameUserSettings.ini >> /ark/MergedGameUserSettings.ini;
              echo "" >> /ark/MergedGameUserSettings.ini;
              cat OverrideGameUserSettings.ini >> /ark/MergedGameUserSettings.ini;
              echo "" >> /ark/MergedGameUserSettings.ini;
              cat GlobalGame.ini >> /ark/MergedGame.ini;
              echo "" >> /ark/MergedGame.ini;
              cat OverrideGame.ini >> /ark/MergedGame.ini;
              echo "" >> /ark/MergedGame.ini;
              mkdir -p /ark/server/ShooterGame/Saved/Config/LinuxServer;
              cd /ark;
              ln -s /ark-config/arkmanager.cfg arkmanager.cfg ;
              ln -s /ark-config/AllowedCheaterSteamIDs.txt /ark/server/ShooterGame/Saved/AllowedCheaterSteamIDs.txt;
              mkdir -p /ark/server/ShooterGame/Binaries/Linux/;
              ln -s /ark-config/PlayersJoinNoCheck.txt /ark/server/ShooterGame/Binaries/Linux/PlayersJoinNoCheck.txt;
              ln -s /ark-config/PlayersExclusiveJoinList.txt /ark/server/ShooterGame/Binaries/Linux/PlayersExclusiveJoinList.txt;
              chmod 777 -R /ark-shared/
      volumes:
      - name: ark-gen2-persistent-storage
        persistentVolumeClaim:
          claimName: ark-gen2-pv-claim
      - name: ark-persistent-shared-storage
        persistentVolumeClaim:
          claimName: ark-shared-pv-claim
      - name: ark-gen2-config-volume
        projected:
          sources:
          - configMap:
              name: ark-global-user-settings-cm
          - configMap:
              name: ark-gen2-override-user-settings-cm
          - configMap:
              name: ark-global-game-ini-cm
          - configMap:
              name: ark-gen2-override-game-ini-cm
          - configMap:
              name: ark-manager-cm
          - configMap:
              name: ark-player-lists-cm
