---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cms
  labels:
    app.kubernetes.io/name: cms
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: cms
  template:
    metadata:
      labels:
        app.kubernetes.io/name: cms
    spec:
      containers:
        - name: cms
          image: {{ DOCKER_IMAGE_OPENEDX }}
          env:
          - name: SERVICE_VARIANT
            value: cms
          ports:
            - containerPort: 8000
          volumeMounts:
            - mountPath: /openedx/edx-platform/lms/envs/tutor/
              name: settings-lms
            - mountPath: /openedx/edx-platform/cms/envs/tutor/
              name: settings-cms
            - mountPath: /openedx/config
              name: config
          resources:
            requests:
              memory: 2Gi
      volumes:
        - name: settings-lms
          configMap:
            name: openedx-settings-lms
        - name: settings-cms
          configMap:
            name: openedx-settings-cms
        - name: config
          configMap:
            name: openedx-config
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cms-worker
  labels:
    app.kubernetes.io/name: cms-worker
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: cms-worker
  template:
    metadata:
      labels:
        app.kubernetes.io/name: cms-worker
    spec:
      containers:
        - name: cms-worker
          image: {{ DOCKER_IMAGE_OPENEDX }}
          args: ["./manage.py", "cms", "celery", "worker", "--loglevel=info", "--hostname=edx.cms.core.default.%%h", "--maxtasksperchild", "100", "--exclude-queues=edx.lms.core.default"]
          env:
          - name: SERVICE_VARIANT
            value: cms
          - name: C_FORCE_ROOT
            value: "1"
          volumeMounts:
            - mountPath: /openedx/edx-platform/lms/envs/tutor/
              name: settings-lms
            - mountPath: /openedx/edx-platform/cms/envs/tutor/
              name: settings-cms
            - mountPath: /openedx/config
              name: config
      volumes:
        - name: settings-lms
          configMap:
            name: openedx-settings-lms
        - name: settings-cms
          configMap:
            name: openedx-settings-cms
        - name: config
          configMap:
            name: openedx-config
{% if ACTIVATE_FORUM %}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: forum
  labels:
    app.kubernetes.io/name: forum
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: forum
  template:
    metadata:
      labels:
        app.kubernetes.io/name: forum
    spec:
      containers:
        - name: forum
          image: {{ DOCKER_IMAGE_FORUM }}
          ports:
            - containerPort: 4567
          env:
            - name: SEARCH_SERVER
              value: "{{ ELASTICSEARCH_SCHEME }}://{{ ELASTICSEARCH_HOST }}:{{ ELASTICSEARCH_PORT }}"
            - name: MONGODB_AUTH
              value: "{% if MONGODB_USERNAME and MONGODB_PASSWORD %}{{ MONGODB_USERNAME}}:{{ MONGODB_PASSWORD }}@{% endif %}"
            - name: MONGODB_HOST
              value: "{{ MONGODB_HOST }}"
            - name: MONGODB_PORT
              value: "{{ MONGODB_PORT }}"
{% endif %}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: lms
  labels:
    app.kubernetes.io/name: lms
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: lms
  template:
    metadata:
      labels:
        app.kubernetes.io/name: lms
    spec:
      containers:
        - name: lms
          image: {{ DOCKER_IMAGE_OPENEDX }}
          ports:
            - containerPort: 8000
          volumeMounts:
            - mountPath: /openedx/edx-platform/lms/envs/tutor/
              name: settings-lms
            - mountPath: /openedx/edx-platform/cms/envs/tutor/
              name: settings-cms
            - mountPath: /openedx/config
              name: config
          resources:
            requests:
              memory: 2Gi
      volumes:
        - name: settings-lms
          configMap:
            name: openedx-settings-lms
        - name: settings-cms
          configMap:
            name: openedx-settings-cms
        - name: config
          configMap:
            name: openedx-config
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: lms-worker
  labels:
    app.kubernetes.io/name: lms-worker
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: lms-worker
  template:
    metadata:
      labels:
        app.kubernetes.io/name: lms-worker
    spec:
      containers:
        - name: lms-worker
          image: {{ DOCKER_IMAGE_OPENEDX }}
          args: ["./manage.py", "lms", "celery", "worker", "--loglevel=info", "--hostname=edx.lms.core.default.%%h", "--maxtasksperchild", "100", "--exclude-queues=edx.cms.core.default"]
          env:
          - name: SERVICE_VARIANT
            value: lms
          - name: C_FORCE_ROOT
            value: "1"
          volumeMounts:
            - mountPath: /openedx/edx-platform/lms/envs/tutor/
              name: settings-lms
            - mountPath: /openedx/edx-platform/cms/envs/tutor/
              name: settings-cms
            - mountPath: /openedx/config
              name: config
      volumes:
        - name: settings-lms
          configMap:
            name: openedx-settings-lms
        - name: settings-cms
          configMap:
            name: openedx-settings-cms
        - name: config
          configMap:
            name: openedx-config
{% if ACTIVATE_ELASTICSEARCH %}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: elasticsearch
  labels:
    app.kubernetes.io/name: elasticsearch
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: elasticsearch
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app.kubernetes.io/name: elasticsearch
    spec:
      containers:
        - name: elasticsearch
          image: {{ DOCKER_IMAGE_ELASTICSEARCH }}
          env:
            - name: ES_JAVA_OPTS
              value: "-Xms1g -Xmx1g"
            - name: "cluster.name"
              value: openedx
            - name: "bootstrap.memory_lock"
              value: "true"
          ports:
            - containerPort: 9200
          volumeMounts:
            - mountPath: /usr/share/elasticsearch/data
              name: data
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: elasticsearch
{% endif %}
{% if ACTIVATE_MEMCACHED %}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: memcached
  labels:
    app.kubernetes.io/name: memcached
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: memcached
  template:
    metadata:
      labels:
        app.kubernetes.io/name: memcached
    spec:
      containers:
        - name: memcached
          image: {{ DOCKER_IMAGE_MEMCACHED }}
          ports:
            - containerPort: 11211
{% endif %}
{% if ACTIVATE_MONGODB %}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb
  labels:
    app.kubernetes.io/name: mongodb
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: mongodb
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app.kubernetes.io/name: mongodb
    spec:
      containers:
        - name: mongodb
          image: {{ DOCKER_IMAGE_MONGODB }}
          args: ["mongod", "--smallfiles", "--nojournal", "--storageEngine", "wiredTiger"]
          ports:
            - containerPort: 27017
          volumeMounts:
            - mountPath: /data/db
              name: data

      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: mongodb
{% endif %}
{% if ACTIVATE_MYSQL %}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  labels:
    app.kubernetes.io/name: mysql
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app.kubernetes.io/name: mysql
    spec:
      containers:
        - name: mysql
          image: {{ DOCKER_IMAGE_MYSQL }}
          args: ["mysqld", "--character-set-server=utf8", "--collation-server=utf8_general_ci"]
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: "{{ MYSQL_ROOT_PASSWORD }}"
          ports:
            - containerPort: 3306
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: data
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: mysql
{% endif %}
{% if ACTIVATE_SMTP %}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: smtp
  labels:
    app.kubernetes.io/name: smtp
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: smtp
  template:
    metadata:
      labels:
        app.kubernetes.io/name: smtp
    spec:
      containers:
        - name: smtp
          image: {{ DOCKER_IMAGE_SMTP }}
          ports:
            - containerPort: 25
{% endif %}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app.kubernetes.io/name: nginx
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: nginx
  template:
    metadata:
      labels:
        app.kubernetes.io/name: nginx
    spec:
      containers:
        - name: nginx
          image: {{ DOCKER_IMAGE_NGINX }}
          volumeMounts:
            - mountPath: /etc/nginx/conf.d/
              name: config
            {{ patch("k8s-deployments-nginx-volume-mounts")|indent(12) }}
          ports:
            - containerPort: 80
            - containerPort: 443
      volumes:
        - name: config
          configMap:
            name: nginx-config
        {{ patch("k8s-deployments-nginx-volumes")|indent(8) }}
{% if ACTIVATE_RABBITMQ %}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rabbitmq
  labels:
    app.kubernetes.io/name: rabbitmq
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: rabbitmq
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app.kubernetes.io/name: rabbitmq
    spec:
      containers:
        - name: rabbitmq
          image: {{ DOCKER_IMAGE_RABBITMQ }}
          ports:
            - containerPort: 5672
          volumeMounts:
            - mountPath: /var/lib/rabbitmq
              name: data
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: rabbitmq
{% endif %}
{{ patch("k8s-deployments") }}
