From 234576780a728201cbdf7895b35de7a4cf22ff8c Mon Sep 17 00:00:00 2001 From: Chuck McAndrew <6248903+dcmcand@users.noreply.github.com> Date: Fri, 30 Aug 2024 16:50:24 +0200 Subject: [PATCH 1/2] add templating for kustomization.yaml --- src/_nebari/constants.py | 2 ++ src/_nebari/stages/base.py | 17 +++++++++++++++-- .../stages/kubernetes_kuberhealthy/__init__.py | 7 +++++++ ...stomization.yaml => kustomization.yaml.tmpl} | 8 ++++++-- .../template/values.yaml | 2 -- .../__init__.py | 6 ++++++ ...stomization.yaml => kustomization.yaml.tmpl} | 2 +- .../stages/kubernetes_services/__init__.py | 1 + 8 files changed, 38 insertions(+), 7 deletions(-) rename src/_nebari/stages/kubernetes_kuberhealthy/template/{kustomization.yaml => kustomization.yaml.tmpl} (58%) rename src/_nebari/stages/kubernetes_kuberhealthy_healthchecks/template/{kustomization.yaml => kustomization.yaml.tmpl} (90%) diff --git a/src/_nebari/constants.py b/src/_nebari/constants.py index 6b779b144..958df0931 100644 --- a/src/_nebari/constants.py +++ b/src/_nebari/constants.py @@ -7,6 +7,8 @@ # https://www.hashicorp.com/license-faq TERRAFORM_VERSION = "1.5.7" +KUBERHEALTHY_HELM_VERSION = "100" + # 04-kubernetes-ingress DEFAULT_TRAEFIK_IMAGE_TAG = "2.9.1" diff --git a/src/_nebari/stages/base.py b/src/_nebari/stages/base.py index dd56d7ed6..ee5f1dd60 100644 --- a/src/_nebari/stages/base.py +++ b/src/_nebari/stages/base.py @@ -7,6 +7,7 @@ import tempfile from typing import Any, Dict, List, Tuple +from jinja2 import Environment, FileSystemLoader from kubernetes import client, config from kubernetes.client.rest import ApiException @@ -24,6 +25,10 @@ def template_directory(self): def stage_prefix(self): return pathlib.Path("stages") / self.name + @property + def kustomize_vars(self): + return {} + failed_to_create = False error_message = "" @@ -68,13 +73,19 @@ def check( sys.exit(1) def render(self) -> Dict[pathlib.Path, str]: + env = Environment(loader=FileSystemLoader(self.template_directory)) contents = {} - if not (self.template_directory / "kustomization.yaml").exists(): + if not (self.template_directory / "kustomization.yaml.tmpl").exists(): raise FileNotFoundError( f"ERROR: After stage={self.name} " - "kustomization.yaml file not found in template directory" + "kustomization.yaml.tmpl template file not found in template directory" ) + kustomize_template = env.get_template("kustomization.yaml.tmpl") + rendered_kustomization = kustomize_template.render(**self.kustomize_vars) + with open(self.template_directory / "kustomization.yaml", "w") as f: + f.write(rendered_kustomization) + with tempfile.TemporaryDirectory() as temp_dir: kustomize.run_kustomize_subprocess( [ @@ -113,6 +124,8 @@ def render(self) -> Dict[pathlib.Path, str]: ), ) ] = f.read() + # cleanup generated kustomization.yaml + pathlib.Path(self.template_directory, "kustomization.yaml").unlink() # clean up downloaded helm charts charts_dir = pathlib.Path(self.template_directory, "charts") diff --git a/src/_nebari/stages/kubernetes_kuberhealthy/__init__.py b/src/_nebari/stages/kubernetes_kuberhealthy/__init__.py index ecd6b7bf8..af3a178e2 100644 --- a/src/_nebari/stages/kubernetes_kuberhealthy/__init__.py +++ b/src/_nebari/stages/kubernetes_kuberhealthy/__init__.py @@ -21,6 +21,13 @@ class KuberHealthyStage(NebariKustomizeStage): input_schema = InputSchema output_schema = OutputSchema + @property + def kustomize_vars(self): + return { + "namespace": self.config.namespace, + "kuberhealthy_helm_version": self.config.monitoring.healthchecks.kuberhealthy_helm_version, + } + @contextlib.contextmanager def deploy( self, stage_outputs: Dict[str, Dict[str, Any]], disable_prompt: bool = False diff --git a/src/_nebari/stages/kubernetes_kuberhealthy/template/kustomization.yaml b/src/_nebari/stages/kubernetes_kuberhealthy/template/kustomization.yaml.tmpl similarity index 58% rename from src/_nebari/stages/kubernetes_kuberhealthy/template/kustomization.yaml rename to src/_nebari/stages/kubernetes_kuberhealthy/template/kustomization.yaml.tmpl index 6b2e0c12d..d6a943837 100644 --- a/src/_nebari/stages/kubernetes_kuberhealthy/template/kustomization.yaml +++ b/src/_nebari/stages/kubernetes_kuberhealthy/template/kustomization.yaml.tmpl @@ -6,6 +6,10 @@ helmCharts: - name: kuberhealthy repo: https://kuberhealthy.github.io/kuberhealthy/helm-repos releaseName: kuberhealthy - namespace: dev - version: "100" # v.2.7.1 + namespace: {{ namespace }} + version: "{{ kuberhealthy_version }}" # v.2.7.1 valuesFile: values.yaml + valuesInline: + prometheus: + serviceMonitor: + namespace: {{ namespace }} diff --git a/src/_nebari/stages/kubernetes_kuberhealthy/template/values.yaml b/src/_nebari/stages/kubernetes_kuberhealthy/template/values.yaml index 64f47970f..abea2f285 100644 --- a/src/_nebari/stages/kubernetes_kuberhealthy/template/values.yaml +++ b/src/_nebari/stages/kubernetes_kuberhealthy/template/values.yaml @@ -1,6 +1,4 @@ prometheus: enabled: true - serviceMonitor: enabled: true - namespace: dev diff --git a/src/_nebari/stages/kubernetes_kuberhealthy_healthchecks/__init__.py b/src/_nebari/stages/kubernetes_kuberhealthy_healthchecks/__init__.py index 8f543a1a3..3756c88ed 100644 --- a/src/_nebari/stages/kubernetes_kuberhealthy_healthchecks/__init__.py +++ b/src/_nebari/stages/kubernetes_kuberhealthy_healthchecks/__init__.py @@ -21,6 +21,12 @@ class KuberHealthyStage(NebariKustomizeStage): input_schema = InputSchema output_schema = OutputSchema + @property + def kustomize_vars(self): + return { + "namespace": self.config.namespace, + } + @contextlib.contextmanager def deploy( self, stage_outputs: Dict[str, Dict[str, Any]], disable_prompt: bool = False diff --git a/src/_nebari/stages/kubernetes_kuberhealthy_healthchecks/template/kustomization.yaml b/src/_nebari/stages/kubernetes_kuberhealthy_healthchecks/template/kustomization.yaml.tmpl similarity index 90% rename from src/_nebari/stages/kubernetes_kuberhealthy_healthchecks/template/kustomization.yaml rename to src/_nebari/stages/kubernetes_kuberhealthy_healthchecks/template/kustomization.yaml.tmpl index 5171296b1..06e8f4aaa 100644 --- a/src/_nebari/stages/kubernetes_kuberhealthy_healthchecks/template/kustomization.yaml +++ b/src/_nebari/stages/kubernetes_kuberhealthy_healthchecks/template/kustomization.yaml.tmpl @@ -14,4 +14,4 @@ patches: patch: |- - op: replace path: /metadata/namespace - value: "dev" + value: "{{ namespace }}" diff --git a/src/_nebari/stages/kubernetes_services/__init__.py b/src/_nebari/stages/kubernetes_services/__init__.py index ee2143216..bd4dfd759 100644 --- a/src/_nebari/stages/kubernetes_services/__init__.py +++ b/src/_nebari/stages/kubernetes_services/__init__.py @@ -228,6 +228,7 @@ class MonitoringOverrides(schema.Base): class Healthchecks(schema.Base): enabled: bool = False + kuberhealthy_helm_version: str = constants.KUBERHEALTHY_HELM_VERSION class Monitoring(schema.Base): From 3a7766d8cbb0237775ce61c245a5d97e92654a77 Mon Sep 17 00:00:00 2001 From: Chuck McAndrew <6248903+dcmcand@users.noreply.github.com> Date: Thu, 5 Sep 2024 12:04:45 +0200 Subject: [PATCH 2/2] update dependencies and fix typo --- pyproject.toml | 2 +- src/_nebari/stages/base.py | 8 +++++--- .../template/kustomization.yaml.tmpl | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a560d64c3..e7b0629b3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,6 +60,7 @@ dependencies = [ "bcrypt==4.0.1", "boto3==1.34.63", "cloudflare==2.11.7", + "jinja2", "kubernetes==27.2.0", "pluggy==1.3.0", "prompt-toolkit==3.0.36", @@ -83,7 +84,6 @@ dev = [ "diagrams", "escapism", "importlib-metadata<5.0", - "jinja2", "mypy==1.6.1", "paramiko", "pre-commit", diff --git a/src/_nebari/stages/base.py b/src/_nebari/stages/base.py index ee5f1dd60..1899872eb 100644 --- a/src/_nebari/stages/base.py +++ b/src/_nebari/stages/base.py @@ -15,6 +15,8 @@ from _nebari.stages.tf_objects import NebariTerraformState from nebari.hookspecs import NebariStage +KUSTOMIZATION_TEMPLATE = "kustomization.yaml.tmpl" + class NebariKustomizeStage(NebariStage): @property @@ -76,12 +78,12 @@ def render(self) -> Dict[pathlib.Path, str]: env = Environment(loader=FileSystemLoader(self.template_directory)) contents = {} - if not (self.template_directory / "kustomization.yaml.tmpl").exists(): + if not (self.template_directory / KUSTOMIZATION_TEMPLATE).exists(): raise FileNotFoundError( f"ERROR: After stage={self.name} " - "kustomization.yaml.tmpl template file not found in template directory" + f"{KUSTOMIZATION_TEMPLATE} template file not found in template directory" ) - kustomize_template = env.get_template("kustomization.yaml.tmpl") + kustomize_template = env.get_template(KUSTOMIZATION_TEMPLATE) rendered_kustomization = kustomize_template.render(**self.kustomize_vars) with open(self.template_directory / "kustomization.yaml", "w") as f: f.write(rendered_kustomization) diff --git a/src/_nebari/stages/kubernetes_kuberhealthy/template/kustomization.yaml.tmpl b/src/_nebari/stages/kubernetes_kuberhealthy/template/kustomization.yaml.tmpl index d6a943837..204b4a95e 100644 --- a/src/_nebari/stages/kubernetes_kuberhealthy/template/kustomization.yaml.tmpl +++ b/src/_nebari/stages/kubernetes_kuberhealthy/template/kustomization.yaml.tmpl @@ -7,7 +7,7 @@ helmCharts: repo: https://kuberhealthy.github.io/kuberhealthy/helm-repos releaseName: kuberhealthy namespace: {{ namespace }} - version: "{{ kuberhealthy_version }}" # v.2.7.1 + version: "{{ kuberhealthy_helm_version }}" valuesFile: values.yaml valuesInline: prometheus: