apiVersion: apps/v1
kind: Deployment
metadata:
  name: rendezvous-database-deployment
  labels:
    app: rendezvous-database
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rendezvous-database
  template:
    metadata:
      labels:
        app: rendezvous-database
    spec:
      volumes:
        - name: datadir
          hostPath:
            path: /home/rendezvous/db/data
      initContainers:
        - name: ensure-volumes-ownership
          image: ilinaraducristian/rendezvous-database:0.0.1
          imagePullPolicy: Always
          securityContext:
            runAsUser: 0
          command:
            - "sh"
            - "-c"
            - "chown mysql:mysql -R /var/lib/mysql"
          volumeMounts:
            - name: datadir
              mountPath: /var/lib/mysql
      containers:
        - name: database
          image: ilinaraducristian/rendezvous-database:0.0.1
          imagePullPolicy: Always
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: database-secret
                  key: root_password
            - name: MYSQL_DATABASE
              value: keycloak
            - name: MYSQL_USER
              valueFrom:
                secretKeyRef:
                  name: database-secret
                  key: keycloak_user
            - name: MYSQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: database-secret
                  key: keycloak_password
          ports:
            - containerPort: 3306
          volumeMounts:
            - name: datadir
              mountPath: /var/lib/mysql