apiVersion: apps/v1
kind: Deployment
metadata:
  name: hive-metastore
  namespace: stonks-oracle
  labels:
    app: hive-metastore
    app.kubernetes.io/part-of: stonks-oracle
    stonks-oracle/tier: analytics
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hive-metastore
  template:
    metadata:
      labels:
        app: hive-metastore
        stonks-oracle/tier: analytics
    spec:
      automountServiceAccountToken: false
      securityContext:
        runAsNonRoot: true
        runAsUser: 1000
        runAsGroup: 1000
        fsGroup: 1000
        seccompProfile:
          type: RuntimeDefault
      initContainers:
        - name: hive-config-init
          image: busybox:1.36
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop: ["ALL"]
          command: ["sh", "-c"]
          args:
            - |
              cat > /hive-config/core-site.xml <<EOF
              <?xml version="1.0"?>
              <configuration>
                <property>
                  <name>fs.s3a.endpoint</name>
                  <value>http://minio.minio-service.svc.cluster.local:80</value>
                </property>
                <property>
                  <name>fs.s3a.access.key</name>
                  <value>${MINIO_ACCESS_KEY}</value>
                </property>
                <property>
                  <name>fs.s3a.secret.key</name>
                  <value>${MINIO_SECRET_KEY}</value>
                </property>
                <property>
                  <name>fs.s3a.path.style.access</name>
                  <value>true</value>
                </property>
                <property>
                  <name>fs.s3a.impl</name>
                  <value>org.apache.hadoop.fs.s3a.S3AFileSystem</value>
                </property>
                <property>
                  <name>fs.s3a.connection.ssl.enabled</name>
                  <value>false</value>
                </property>
              </configuration>
              EOF
              cat > /hive-config/metastore-site.xml <<EOF
              <?xml version="1.0"?>
              <configuration>
                <property>
                  <name>metastore.thrift.uris</name>
                  <value>thrift://0.0.0.0:9083</value>
                </property>
                <property>
                  <name>metastore.task.threads.always</name>
                  <value>org.apache.hadoop.hive.metastore.events.EventCleanerTask</value>
                </property>
                <property>
                  <name>metastore.expression.proxy</name>
                  <value>org.apache.hadoop.hive.metastore.DefaultPartitionExpressionProxy</value>
                </property>
                <property>
                  <name>javax.jdo.option.ConnectionDriverName</name>
                  <value>org.apache.derby.jdbc.EmbeddedDriver</value>
                </property>
                <property>
                  <name>javax.jdo.option.ConnectionURL</name>
                  <value>jdbc:derby:/opt/hive/data/metastore_db;create=true</value>
                </property>
                <property>
                  <name>metastore.warehouse.dir</name>
                  <value>s3a://stonks-lakehouse/warehouse</value>
                </property>
              </configuration>
              EOF
          env:
            - name: MINIO_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: stonks-core-secrets
                  key: MINIO_ACCESS_KEY
            - name: MINIO_SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: stonks-core-secrets
                  key: MINIO_SECRET_KEY
          volumeMounts:
            - name: hive-config
              mountPath: /hive-config
      containers:
        - name: hive-metastore
          image: apache/hive:4.0.0
          ports:
            - containerPort: 9083
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop: ["ALL"]
          env:
            - name: SERVICE_NAME
              value: metastore
            - name: DB_DRIVER
              value: derby
          volumeMounts:
            - name: hive-data
              mountPath: /opt/hive/data
            - name: hive-config
              mountPath: /opt/hive/conf/core-site.xml
              subPath: core-site.xml
            - name: hive-config
              mountPath: /opt/hive/conf/metastore-site.xml
              subPath: metastore-site.xml
          resources:
            requests:
              cpu: 200m
              memory: 512Mi
            limits:
              cpu: "1"
              memory: 1Gi
      volumes:
        - name: hive-data
          persistentVolumeClaim:
            claimName: hive-metastore-data
        - name: hive-config
          emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: hive-metastore
  namespace: stonks-oracle
spec:
  selector:
    app: hive-metastore
  ports:
    - port: 9083
      targetPort: 9083
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: hive-metastore-data
  namespace: stonks-oracle
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
