From 54165bcd38887751a9152aa8c25774ace66ef15c Mon Sep 17 00:00:00 2001 From: Javier Torres Date: Thu, 28 Dec 2023 15:11:48 -0500 Subject: [PATCH] =?UTF-8?q?Co-authored-by:=20Andr=C3=A9s=20Felipe=20Berm?= =?UTF-8?q?=C3=BAdez=20Mendoza=20=20feat:=20ad?= =?UTF-8?q?d=20enterprise-catalog-worker=20deployment.=20feat:=20enable=20?= =?UTF-8?q?async=20enterprise-catalog-worker.=20feat:=20add=20hpa=20to=20e?= =?UTF-8?q?nterprise-catalog.=20refactor:=20address=20suggestions.=20Updat?= =?UTF-8?q?e=20tutorenterprise/templates/enterprise/apps/enterprise-catalo?= =?UTF-8?q?g/settings/partials/common.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorenterprise/patches/k8s-deployments | 42 +++++++++++++++++++ .../patches/kustomization-resources | 1 + .../patches/local-docker-compose-services | 16 +++++++ tutorenterprise/plugin.py | 11 +++++ .../settings/development.py | 1 + .../settings/partials/common.py | 4 +- .../templates/enterprise/k8s/hpa.yml | 32 ++++++++++++++ 7 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 tutorenterprise/patches/kustomization-resources create mode 100644 tutorenterprise/templates/enterprise/k8s/hpa.yml diff --git a/tutorenterprise/patches/k8s-deployments b/tutorenterprise/patches/k8s-deployments index 9591fe7..1b4e3be 100644 --- a/tutorenterprise/patches/k8s-deployments +++ b/tutorenterprise/patches/k8s-deployments @@ -28,6 +28,48 @@ spec: subPath: production.py securityContext: allowPrivilegeEscalation: false + resources: + limits: + cpu: "{{ ENTERPRISE_CATALOG_LIMIT_CPU }}" + memory: "{{ ENTERPRISE_CATALOG_LIMIT_MEMORY }}" + requests: + cpu: "{{ ENTERPRISE_CATALOG_REQUEST_CPU }}" + memory: "{{ ENTERPRISE_CATALOG_REQUEST_MEMORY }}" + volumes: + - name: settings + configMap: + name: enterprise-catalog-settings +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: enterprise-catalog-worker + labels: + app.kubernetes.io/name: enterprise-catalog-worker +spec: + selector: + matchLabels: + app.kubernetes.io/name: enterprise-catalog-worker + template: + metadata: + labels: + app.kubernetes.io/name: enterprise-catalog-worker + spec: + securityContext: + runAsUser: 1000 + runAsGroup: 1000 + containers: + - name: enterprise-catalog-worker + image: {{ ENTERPRISE_CATALOG_DOCKER_IMAGE }} + args: ["celery", "--app=enterprise_catalog.celery", "worker", "--loglevel=info"] + ports: + - containerPort: 8000 + volumeMounts: + - mountPath: /openedx/enterprise-catalog/enterprise_catalog/settings/tutor/production.py + name: settings + subPath: production.py + securityContext: + allowPrivilegeEscalation: false volumes: - name: settings configMap: diff --git a/tutorenterprise/patches/kustomization-resources b/tutorenterprise/patches/kustomization-resources new file mode 100644 index 0000000..f782ead --- /dev/null +++ b/tutorenterprise/patches/kustomization-resources @@ -0,0 +1 @@ +- plugins/enterprise/k8s/hpa.yml diff --git a/tutorenterprise/patches/local-docker-compose-services b/tutorenterprise/patches/local-docker-compose-services index 38eb67c..9ec9bf8 100644 --- a/tutorenterprise/patches/local-docker-compose-services +++ b/tutorenterprise/patches/local-docker-compose-services @@ -11,3 +11,19 @@ enterprise-catalog: - redis {% if RUN_MYSQL %}- mysql{% endif %} {% if RUN_LMS %}- lms{% endif %} + +enterprise-catalog-worker: + image: {{ ENTERPRISE_CATALOG_DOCKER_IMAGE }} + environment: + DJANGO_SETTINGS_MODULE: enterprise_catalog.settings.tutor.production + command: celery --app=enterprise_catalog.celery:app worker -l INFO + restart: unless-stopped + volumes: + - ../plugins/enterprise/apps/enterprise-catalog/settings:/openedx/enterprise-catalog/enterprise_catalog/settings/tutor:ro + depends_on: + - discovery + - ecommerce + - redis + - enterprise-catalog + {% if RUN_MYSQL %}- mysql{% endif %} + {% if RUN_LMS %}- lms{% endif %} diff --git a/tutorenterprise/plugin.py b/tutorenterprise/plugin.py index 9672bdf..f438c80 100644 --- a/tutorenterprise/plugin.py +++ b/tutorenterprise/plugin.py @@ -34,6 +34,16 @@ "CATALOG_DOCKER_IMAGE": "{{ DOCKER_REGISTRY }}paops/enterprise-catalog:{{ ENTERPRISE_VERSION }}", "DISCOVERY_USER": "discovery", "ECOMMERCE_OAUTH_USER": "{{ ECOMMERCE_OAUTH2_KEY }}", + ### HPA ### + "CATALOG_LIMIT_CPU": "1", + "CATALOG_LIMIT_MEMORY": "1Gi", + "CATALOG_REQUEST_CPU": "512m", + "CATALOG_REQUEST_MEMORY": "512Mi", + "CATALOG_ENABLE_HPA": False, + "CATALOG_MIN_REPLICAS": 1, + "CATALOG_MAX_REPLICAS": 4, + "CATALOG_AVG_CPU": 65, + "CATALOG_AVG_MEMORY": "500Mi", }, "unique": { "OAUTH2_SECRET_KEY": "{{ 64|random_string }}", @@ -161,6 +171,7 @@ [ ("enterprise/build", "plugins"), ("enterprise/apps", "plugins"), + ("enterprise/k8s", "plugins"), ], ) diff --git a/tutorenterprise/templates/enterprise/apps/enterprise-catalog/settings/development.py b/tutorenterprise/templates/enterprise/apps/enterprise-catalog/settings/development.py index b61869a..158ce0a 100644 --- a/tutorenterprise/templates/enterprise/apps/enterprise-catalog/settings/development.py +++ b/tutorenterprise/templates/enterprise/apps/enterprise-catalog/settings/development.py @@ -3,5 +3,6 @@ {% include "enterprise/apps/enterprise-catalog/settings/partials/common.py" %} DISCOVERY_CATALOG_QUERY_CACHE_TIMEOUT = 0 +CELERY_TASK_ALWAYS_EAGER = True {{ patch("enterprise-catalog-development-settings") }} diff --git a/tutorenterprise/templates/enterprise/apps/enterprise-catalog/settings/partials/common.py b/tutorenterprise/templates/enterprise/apps/enterprise-catalog/settings/partials/common.py index 607224d..1468191 100644 --- a/tutorenterprise/templates/enterprise/apps/enterprise-catalog/settings/partials/common.py +++ b/tutorenterprise/templates/enterprise/apps/enterprise-catalog/settings/partials/common.py @@ -81,9 +81,9 @@ ECOMMERCE_BASE_URL = "{% if ENABLE_HTTPS %}https{% else %}http{% endif %}://{{ ECOMMERCE_HOST }}" LICENSE_MANAGER_BASE_URL = "" -# Worker settings. We currently don't support async workers for the Enterprise Catalogue service. +# Enterprise Catalogue Worker settings. CELERY_WORKER_HIJACK_ROOT_LOGGER = True -CELERY_TASK_ALWAYS_EAGER = True +CELERY_TASK_ALWAYS_EAGER = False CELERY_BROKER_TRANSPORT = "redis" CELERY_BROKER_HOSTNAME = "{{ REDIS_HOST }}:{{ REDIS_PORT }}" CELERY_BROKER_VHOST = "{{ OPENEDX_CELERY_REDIS_DB }}" diff --git a/tutorenterprise/templates/enterprise/k8s/hpa.yml b/tutorenterprise/templates/enterprise/k8s/hpa.yml new file mode 100644 index 0000000..bb9d9bf --- /dev/null +++ b/tutorenterprise/templates/enterprise/k8s/hpa.yml @@ -0,0 +1,32 @@ +{%- if ENTERPRISE_CATALOG_ENABLE_HPA %} +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: enterprise-catalog + labels: + app.kubernetes.io/name: enterprise-catalog +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: enterprise-catalog + minReplicas: {{ ENTERPRISE_CATALOG_MIN_REPLICAS }} + maxReplicas: {{ ENTERPRISE_CATALOG_MAX_REPLICAS }} + metrics: + {%- if ENTERPRISE_CATALOG_AVG_CPU > 0 %} + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: {{ ENTERPRISE_CATALOG_AVG_CPU }} + {%- endif %} + {%- if ENTERPRISE_CATALOG_AVG_MEMORY|length %} + - type: Resource + resource: + name: memory + target: + type: AverageValue + averageValue: {{ ENTERPRISE_CATALOG_AVG_MEMORY }} + {%- endif %} +{%- endif %}