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

feat: Support Jinja in our override files #73

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
53 changes: 26 additions & 27 deletions libsentrykube/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import yaml

from collections import OrderedDict
from jinja2 import Environment, FileSystemLoader, TemplateNotFound
from libsentrykube.config import Config
from libsentrykube.customer import load_customer_data
from libsentrykube.utils import workspace_root, deep_merge_dict
Expand Down Expand Up @@ -92,9 +93,11 @@ def get_service_values(service_name: str, external: bool = False) -> dict:
else:
service_path = get_service_path(service_name)
try:
with open(service_path / "_values.yaml", "rb") as f:
values = yaml.safe_load(f)
except FileNotFoundError:
template = Environment(
loader=FileSystemLoader(service_path),
).get_template("_values.yaml")
values = yaml.safe_load(template.render())
except TemplateNotFound:
values = {}
return values

Expand Down Expand Up @@ -133,16 +136,15 @@ def get_service_value_overrides(
If "external=True" is specified, treat the service name as the full service path.
"""
try:
service_override_file = (
get_service_value_override_path(service_name, region_name, external)
/ f"{cluster_name}.yaml"
)

with open(service_override_file, "rb") as f:
values = yaml.safe_load(f)
template = Environment(
loader=FileSystemLoader(
get_service_value_override_path(service_name, region_name, external)
),
).get_template(f"{cluster_name}.yaml")
values = yaml.safe_load(template.render())

return values
except FileNotFoundError:
except TemplateNotFound:
return {}


Expand All @@ -156,16 +158,15 @@ def get_common_regional_override(
settings shared across all clusters in that region.
"""
try:
common_service_override_file = (
get_service_value_override_path(service_name, region_name, external)
/ "_values.yaml"
)
template = Environment(
loader=FileSystemLoader(
get_service_value_override_path(service_name, region_name, external)
),
).get_template("_values.yaml")
values = yaml.safe_load(template.render())

with open(common_service_override_file, "rb") as f:
common_override_values = yaml.safe_load(f)

return common_override_values
except FileNotFoundError:
return values
except TemplateNotFound:
return {}


Expand Down Expand Up @@ -209,13 +210,11 @@ def get_hierarchical_value_overrides(
continue

try:
service_override_file = (
service_regions_path / override_group.name / "_values.yaml"
)

with open(service_override_file, "rb") as f:
base_values = yaml.safe_load(f)
except FileNotFoundError:
template = Environment(
loader=FileSystemLoader(service_regions_path / override_group.name),
).get_template("_values.yaml")
base_values = yaml.safe_load(template.render())
except TemplateNotFound:
base_values = {}

if region_name == "saas":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{% set some_jinja = "some_jinja" %}
key2:
subkey2_3:
- value2_3_1_replaced
subkey2_4:
- value2_4_1_replaced
subkey2_5:
- {{ some_jinja}}
1 change: 1 addition & 0 deletions libsentrykube/tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"subkey2_4": [
"value2_4_1_replaced" # From the managed file
],
"subkey2_5": ["some_jinja"], # From jinja variable
}
},
"service2": {},
Expand Down
Loading