---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: ipapi
  name: ipapi
  namespace: ipapi
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: ipapi
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: ipapi
    spec:
      initContainers:
        - command:
            - sh
            - -c
            - |
              wget -O geoip.tar.gz "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=${API_KEY}&suffix=tar.gz" && \
              tar -C /tmp -xvzf geoip.tar.gz && rm geoip.tar.gz && \
              wget -O geoip.tar.gz "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=${API_KEY}&suffix=tar.gz" && \
              tar -C /tmp -xvzf geoip.tar.gz && rm geoip.tar.gz && \
              wget -O geoip.tar.gz "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key=${API_KEY}&suffix=tar.gz" && \
              tar -C /tmp -xvzf geoip.tar.gz && rm geoip.tar.gz && \
              find /tmp -name '*.mmdb' -exec cp {} /data/geoip \;
          image: docker.io/library/busybox:1.35
          name: geoip-db-init
          env:
            - name: API_KEY
              valueFrom:
                secretKeyRef:
                  name: maxmind-api-key
                  key: API_KEY
          volumeMounts:
            - name: geoip
              mountPath: /data/geoip
      containers:
        - image: docker.io/mpolden/echoip:latest
          imagePullPolicy: Always
          name: ipapi
          args:
            - -H
            - X-Forwarded-For
            - -f
            - /data/geoip/GeoLite2-Country.mmdb
            - -c
            - /data/geoip/GeoLite2-City.mmdb
            - -a
            - /data/geoip/GeoLite2-ASN.mmdb
            - -r
            - -p
            - -C
            - "1000"
          ports:
            - containerPort: 8080
              name: http
              protocol: TCP
          volumeMounts:
            - name: geoip
              mountPath: /data/geoip
            - name: template
              mountPath: /data/template
          livenessProbe:
            httpGet:
              path: /health
              port: http
          readinessProbe:
            httpGet:
              path: /health
              port: http
          resources:
            requests:
              memory: 64Mi
              cpu: 150m
            limits:
              memory: 256Mi
              cpu: 300m
          securityContext:
            runAsUser: 1000
            runAsGroup: 1000
      volumes:
        - name: geoip
          emptyDir: {}
        - name: template
          configMap:
            name: html-template
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      securityContext: {}
      terminationGracePeriodSeconds: 30
