From 08fbd69d2e8a6319fe41243723e26ec25c1721e0 Mon Sep 17 00:00:00 2001 From: Andrew Scribner Date: Mon, 4 Mar 2024 16:42:14 -0500 Subject: [PATCH] add templating to mlflow deployments --- src/dss/config.py | 3 ++ src/dss/initialize.py | 15 ++++++--- src/dss/manifest_templates/dss_core.yaml.j2 | 7 ++++ .../mlflow_deployment.yaml.j2} | 33 ++++++++----------- 4 files changed, 34 insertions(+), 24 deletions(-) create mode 100644 src/dss/manifest_templates/dss_core.yaml.j2 rename src/dss/{manifests.yaml => manifest_templates/mlflow_deployment.yaml.j2} (69%) diff --git a/src/dss/config.py b/src/dss/config.py index 1efb30b..ee569b3 100644 --- a/src/dss/config.py +++ b/src/dss/config.py @@ -2,3 +2,6 @@ DSS_CLI_MANAGER_LABELS = {"app.kubernetes.io/managed-by": "dss-cli"} FIELD_MANAGER = "dss-cli" +DSS_NAMESPACE = "dss" +MANIFEST_TEMPLATES_LOCATION = "./manifest_templates" +MLFLOW_DEPLOYMENT_NAME = "mlflow" diff --git a/src/dss/initialize.py b/src/dss/initialize.py index 6e61bc3..6463497 100644 --- a/src/dss/initialize.py +++ b/src/dss/initialize.py @@ -1,11 +1,12 @@ import os +from pathlib import Path from charmed_kubeflow_chisme.kubernetes import KubernetesResourceHandler from lightkube import Client from lightkube.resources.apps_v1 import Deployment from lightkube.resources.core_v1 import Namespace, PersistentVolumeClaim, Service -from dss.config import DSS_CLI_MANAGER_LABELS, FIELD_MANAGER +from dss.config import DSS_CLI_MANAGER_LABELS, FIELD_MANAGER, MANIFEST_TEMPLATES_LOCATION from dss.logger import setup_logger from dss.utils import wait_for_deployment_ready @@ -24,13 +25,19 @@ def initialize(lightkube_client: Client) -> None: None """ # Path to the manifests YAML file - manifests_file = os.path.join(os.path.dirname(__file__), "manifests.yaml") - + manifests_files = [ + Path( + Path(__file__).parent, MANIFEST_TEMPLATES_LOCATION, "dss_core.yaml.j2" + ), + Path( + Path(__file__).parent, MANIFEST_TEMPLATES_LOCATION, "mlflow_deployment.yaml.j2" + ), + ] # Initialize KubernetesResourceHandler k8s_resource_handler = KubernetesResourceHandler( field_manager=FIELD_MANAGER, labels=DSS_CLI_MANAGER_LABELS, - template_files=[manifests_file], + template_files=manifests_files, context={}, resource_types={Deployment, Service, PersistentVolumeClaim, Namespace}, lightkube_client=lightkube_client, diff --git a/src/dss/manifest_templates/dss_core.yaml.j2 b/src/dss/manifest_templates/dss_core.yaml.j2 new file mode 100644 index 0000000..98e7d4a --- /dev/null +++ b/src/dss/manifest_templates/dss_core.yaml.j2 @@ -0,0 +1,7 @@ +--- +apiVersion: v1 +kind: Namespace +metadata: + name: {{ namespace }} + labels: + app.kubernetes.io/part-of: dss diff --git a/src/dss/manifests.yaml b/src/dss/manifest_templates/mlflow_deployment.yaml.j2 similarity index 69% rename from src/dss/manifests.yaml rename to src/dss/manifest_templates/mlflow_deployment.yaml.j2 index 892af61..c4a7676 100644 --- a/src/dss/manifests.yaml +++ b/src/dss/manifest_templates/mlflow_deployment.yaml.j2 @@ -1,19 +1,12 @@ ---- -apiVersion: v1 -kind: Namespace -metadata: - name: dss - labels: - app.kubernetes.io/part-of: dss --- apiVersion: v1 kind: PersistentVolumeClaim metadata: - name: mlflow - namespace: dss + name: {{ name }} + namespace: {{ namespace }} labels: - app.kubernetes.io/name: mlflow + app.kubernetes.io/name: {{ name }} app.kubernetes.io/part-of: dss spec: accessModes: @@ -26,21 +19,21 @@ spec: apiVersion: apps/v1 kind: Deployment metadata: - name: mlflow - namespace: dss + name: {{ name }} + namespace: {{ namespace }} labels: - app.kubernetes.io/name: mlflow + app.kubernetes.io/name: {{ name }} app.kubernetes.io/part-of: dss spec: replicas: 1 selector: matchLabels: - app.kubernetes.io/name: mlflow + app.kubernetes.io/name: {{ name }} app.kubernetes.io/part-of: dss template: metadata: labels: - app.kubernetes.io/name: mlflow + app.kubernetes.io/name: {{ name }} app.kubernetes.io/part-of: dss spec: containers: @@ -55,20 +48,20 @@ spec: volumes: - name: mlflow persistentVolumeClaim: - claimName: mlflow + claimName: {{ name }} --- apiVersion: v1 kind: Service metadata: - name: mlflow - namespace: dss + name: {{ name }} + namespace: {{ namespace }} labels: - app.kubernetes.io/name: mlflow + app.kubernetes.io/name: {{ name }} app.kubernetes.io/part-of: dss spec: selector: - app.kubernetes.io/name: mlflow + app.kubernetes.io/name: {{ name }} app.kubernetes.io/part-of: dss ports: - protocol: TCP