apiVersion: v1
kind: Service
metadata:
  name: qa-postgres-svc-kc
  namespace: kra-qa
spec:
  selector:
    app: postgres
  ports:
    - protocol: TCP
      port: 5433
      targetPort: 5432

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: qa-postgres-db-kc
  namespace: kra-qa
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: private-repo:5000/postgres:16
          env:
            - name: POSTGRES_DB
              value: keycloak
            - name: POSTGRES_USER
              value: keycloak
            - name: POSTGRES_PASSWORD
              value: password
          ports:
            - containerPort: 5432
          volumeMounts:
          - mountPath: "/var/lib/postgresql/data"
            name: qa-postgres-kc-pv-volume
      volumes:
        - name: qa-postgres-kc-pv-volume
          persistentVolumeClaim:
            claimName: qa-postgres-kc-pv-claim
      nodeSelector:
        name: node-1

---
apiVersion: v1
kind: Service
metadata:
  name: keycloak
  namespace: kra-qa
spec:
  type: NodePort
  selector:
    app: keycloak
  ports:
    - protocol: TCP
      port: 8085
      targetPort: 8080
      nodePort: 31056

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: keycloak
  namespace: kra-qa
spec:
  replicas: 1
  selector:
    matchLabels:
      app: keycloak
  template:
    metadata:
      labels:
        app: keycloak
    spec:
      initContainers:
        - name: init-permissions
          image: busybox
          command: ['sh', '-c', 'mkdir -p /opt/bitnami/keycloak/data/tmp && chown -R 1001:1001 /opt/bitnami/keycloak/data/tmp && chmod -R 777 /opt/bitnami/keycloak/data/tmp']
          volumeMounts:
            - mountPath: /opt/bitnami/keycloak
              name: qa-keycloak-pv-volume
      containers:
        - name: keycloak
          image: private-repo:5000/bitnami/keycloak:22.0.4
          env:
            - name: DB_VENDOR
              value: POSTGRES
            - name: DB_ADDR
              value: qa-postgres-svc-kc
            - name: KEYCLOAK_DATABASE_PORT
              value: "5433"
            - name: KEYCLOAK_DATABASE_NAME
              value: keycloak
            - name: KEYCLOAK_DATABASE_USER
              value: keycloak
            - name: KEYCLOAK_DATABASE_PASSWORD
              value: password
            - name: KEYCLOAK_ADMIN
              value: admin
            - name: KEYCLOAK_ADMIN_PASSWORD
              value: admin
            - name: KC_HOSTNAME_URL
              value: https://qa.ndmlkra.devops.remiges.tech/auth/
            - name: KC_HOSTNAME_ADMIN_URL
              value: https://qa.ndmlkra.devops.remiges.tech/auth/
            - name: KC_METRICS_ENABLED
              value: "true"
          ports:
            - containerPort: 8080
          volumeMounts:
          - mountPath: "/opt/bitnami/keycloak/data"
            name: qa-keycloak-pv-volume
      volumes:
        - name: qa-keycloak-pv-volume
          persistentVolumeClaim:
            claimName: qa-keycloak-pv-claim
      nodeSelector:
        name: node-1

---
