Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template the kustomization.yaml file #2667

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -83,7 +84,6 @@ dev = [
"diagrams",
"escapism",
"importlib-metadata<5.0",
"jinja2",
"mypy==1.6.1",
"paramiko",
"pre-commit",
Expand Down
2 changes: 2 additions & 0 deletions src/_nebari/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
19 changes: 17 additions & 2 deletions src/_nebari/stages/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
import tempfile
from typing import Any, Dict, List, Tuple

from jinja2 import Environment, FileSystemLoader
aktech marked this conversation as resolved.
Show resolved Hide resolved
from kubernetes import client, config
from kubernetes.client.rest import ApiException

from _nebari.provider import helm, kubernetes, kustomize, terraform
from _nebari.stages.tf_objects import NebariTerraformState
from nebari.hookspecs import NebariStage

KUSTOMIZATION_TEMPLATE = "kustomization.yaml.tmpl"


class NebariKustomizeStage(NebariStage):
@property
Expand All @@ -24,6 +27,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 = ""

Expand Down Expand Up @@ -68,13 +75,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_TEMPLATE).exists():
raise FileNotFoundError(
f"ERROR: After stage={self.name} "
"kustomization.yaml file not found in template directory"
f"{KUSTOMIZATION_TEMPLATE} template file not found in template directory"
)
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)

with tempfile.TemporaryDirectory() as temp_dir:
kustomize.run_kustomize_subprocess(
[
Expand Down Expand Up @@ -113,6 +126,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")
Expand Down
7 changes: 7 additions & 0 deletions src/_nebari/stages/kubernetes_kuberhealthy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_helm_version }}"
valuesFile: values.yaml
valuesInline:
prometheus:
serviceMonitor:
namespace: {{ namespace }}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
prometheus:
enabled: true

serviceMonitor:
enabled: true
namespace: dev
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ patches:
patch: |-
- op: replace
path: /metadata/namespace
value: "dev"
value: "{{ namespace }}"
1 change: 1 addition & 0 deletions src/_nebari/stages/kubernetes_services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading