apiVersion: apps/v1
kind: Deployment
metadata:
  name: geoserver-deployment
spec:
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: geoserver
      run: geoserver
  template:
    metadata:
      labels:
        app: geoserver
        run: geoserver  
    spec:
      hostname: geoserver
      volumes:
      - name: geoserver-data-dir-storage
        persistentVolumeClaim:
          claimName: claim-shared-data-dir
      - name: geoserver-data-storage
        persistentVolumeClaim:
          claimName: claim-shared-data
      - name: geoserver-backup-restore-storage
        persistentVolumeClaim:
          claimName: claim-shared-backup-restore
      - name: geoserver-tmp-storage
        persistentVolumeClaim:
          claimName: claim-shared-tmp
      - name: geoserver-static-storage
        persistentVolumeClaim:
          claimName: claim-shared-static
      initContainers:
      - image: geonode/geoserver_data:2.18.2
        name: geonode-geoserver-init-data-directory
        volumeMounts:
        - name: geoserver-data-dir-storage
          mountPath: /mnt/geoserver_data/     
        command: [sh, -exec]
        args:
          - |
            if ls -1A /mnt/geoserver_data | fgrep -v 'geonode' | read q; then
              echo "geoserver directory already filled"
              ls -la /mnt/geoserver_data
            else
              cp -av /geoserver_data/data/* /mnt/geoserver_data/ && ls -la /mnt/geoserver_data/
              sed 's|<stdOutLogging>false</stdOutLogging>|<stdOutLogging>true</stdOutLogging>|' \
              -i /mnt/geoserver_data/logging.xml
              set the clientId
              sed "s|<cliendId>.*</cliendId>|<cliendId>${OAUTH2_CLIENT_ID}</cliendId>|g" \
              -i /mnt/geoserver_data/security/filter/geonode-oauth2/config.xml
              set the clientSecret
              sed "s|<clientSecret>.*</clientSecret>|<clientSecret>${OAUTH2_CLIENT_SECRET}</clientSecret>|g" \
              -i /mnt/geoserver_data/security/filter/geonode-oauth2/config.xml
            fi              # enable logging to STD_OUT

        envFrom: 
        - configMapRef:
            name: geoserver-config
      containers:
      - image: geonode/geoserver:2.18.2
        name: geoserver
        env:
          - name: PUBLIC_PORT
            value: "80"
          - name: ENABLE_JSON
            value: "true"
          - name: outFormat
            value: text/javascript
          - name: GEOSERVER_JAVA_OPTS
            value: |
              -Djava.awt.headless=true 
              -Xms2G 
              -Xmx4G 
              -XX:PerfDataSamplingInterval=500 
              -XX:SoftRefLRUPolicyMSPerMB=36000 
              -XX:-UseGCOverheadLimit 
              -XX:+UseConcMarkSweepGC 
              -XX:+UseParNewGC 
              -XX:ParallelGCThreads=4 
              -Dfile.encoding=UTF8 
              -Djavax.servlet.request.encoding=UTF-8 
              -Djavax.servlet.response.encoding=UTF-8 
              -Duser.timezone=GMT 
              -Dorg.geotools.shapefile.datetime=false 
              -DGEOSERVER_CSRF_DISABLED=true 
              -DPRINT_BASE_URL=http://geoserver:8080/geoserver/pdf
        ports:
        - containerPort: 8080
          name: geoserver
        volumeMounts:
        - name: geoserver-static-storage
          mountPath: /mnt/volumes/statics
        - name: geoserver-data-dir-storage
          mountPath: /geoserver_data/data
        - name: geoserver-backup-restore-storage
          mountPath: /backup_restore
        - name: geoserver-data-storage
          mountPath: /data
        - name: geoserver-tmp-storage
          mountPath: /tmp


