---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nominatim-config
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: nominatim
    app.kubernetes.io/component: geocoding
data:
  NOMINATIM_PBF_URL: "http://download.geofabrik.de/europe/spain-latest.osm.pbf"
  NOMINATIM_REPLICATION_URL: "https://download.geofabrik.de/europe/spain-updates"
  NOMINATIM_IMPORT_STYLE: "address"
  NOMINATIM_THREADS: "4"
  NOMINATIM_FLATNODE_FILE: "/nominatim-flatnode/flatnode.bin"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nominatim-data
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: nominatim
    app.kubernetes.io/component: geocoding
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nominatim-flatnode
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: nominatim
    app.kubernetes.io/component: geocoding
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nominatim
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: nominatim
    app.kubernetes.io/component: geocoding
    app.kubernetes.io/part-of: bakery-ia
spec:
  serviceName: nominatim-service
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: nominatim
      app.kubernetes.io/component: geocoding
  template:
    metadata:
      labels:
        app.kubernetes.io/name: nominatim
        app.kubernetes.io/component: geocoding
    spec:
      containers:
      - name: nominatim
        image: mediagis/nominatim:4.4
        ports:
        - containerPort: 8080
          name: http
        volumeMounts:
        - name: nominatim-data
          mountPath: /var/lib/postgresql
        - name: nominatim-flatnode
          mountPath: /nominatim-flatnode
        env:
        - name: NOMINATIM_PBF_URL
          valueFrom:
            configMapKeyRef:
              name: nominatim-config
              key: NOMINATIM_PBF_URL
        - name: NOMINATIM_REPLICATION_URL
          valueFrom:
            configMapKeyRef:
              name: nominatim-config
              key: NOMINATIM_REPLICATION_URL
        - name: NOMINATIM_IMPORT_STYLE
          valueFrom:
            configMapKeyRef:
              name: nominatim-config
              key: NOMINATIM_IMPORT_STYLE
        - name: NOMINATIM_THREADS
          valueFrom:
            configMapKeyRef:
              name: nominatim-config
              key: NOMINATIM_THREADS
        - name: NOMINATIM_FLATNODE_FILE
          valueFrom:
            configMapKeyRef:
              name: nominatim-config
              key: NOMINATIM_FLATNODE_FILE
        resources:
          requests:
            memory: "2Gi"
            cpu: "1"
          limits:
            memory: "4Gi"
            cpu: "2"
        livenessProbe:
          httpGet:
            path: /status
            port: 8080
          initialDelaySeconds: 120
          periodSeconds: 30
          timeoutSeconds: 10
          failureThreshold: 3
        readinessProbe:
          httpGet:
            path: /status
            port: 8080
          initialDelaySeconds: 60
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 5
      volumes:
      - name: nominatim-data
        persistentVolumeClaim:
          claimName: nominatim-data
      - name: nominatim-flatnode
        persistentVolumeClaim:
          claimName: nominatim-flatnode

---
apiVersion: v1
kind: Service
metadata:
  name: nominatim-service
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: nominatim
    app.kubernetes.io/component: geocoding
spec:
  selector:
    app.kubernetes.io/name: nominatim
    app.kubernetes.io/component: geocoding
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP
    name: http
  type: ClusterIP
