Skip to content

Commit

Permalink
Better support for env variable substitution (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
expobrain authored Jul 18, 2022
1 parent a3faf78 commit f63a57d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.1.3

- Better support for env variable substitution in manifest files.

# v0.1.2

- made the `--project-name` parameter optional
Expand Down
9 changes: 7 additions & 2 deletions komposer/core/extra_manifest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import itertools
import os
from collections.abc import Mapping, Sequence
from typing import Any, Union

from envsubst import envsubst

from komposer.exceptions import (
ExtraManifestInvalidYamlError,
ExtraManifestMissingMetadataError,
Expand All @@ -16,7 +19,7 @@ def _komposer_service_prefix_value(context: Context) -> str:
return context.manifest_prefix


komposer_env_variables_map = {"${KOMPOSER_SERVICE_PREFIX}": _komposer_service_prefix_value}
komposer_env_variables_map = {"KOMPOSER_SERVICE_PREFIX": _komposer_service_prefix_value}


def update_item_metadata_labels(item: Mapping, labels: Mapping) -> None:
Expand All @@ -39,7 +42,9 @@ def update_item_env_configmapkeyref_name(item: Mapping, manifest_prefix: str) ->
def replace_env_variables(context: Context, extra_manifest_str: str) -> str:
for env_name, env_fn in komposer_env_variables_map.items():
env_value = env_fn(context)
extra_manifest_str = extra_manifest_str.replace(env_name, env_value)
os.environ[env_name] = env_value

extra_manifest_str = envsubst(extra_manifest_str)

return extra_manifest_str

Expand Down
14 changes: 13 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ skip_glob = ".venv"

[tool.poetry]
name = "komposer"
version = "0.1.2"
version = "0.1.3"
description = "Tool to convert a Docker Compose file into a Kubernetes manifest"
repository = "https://github.com/expobrain/komposer"
authors = ["Daniele Esposti <[email protected]>"]
Expand All @@ -29,6 +29,7 @@ pydantic = "^1.9.1"
PyYAML = "^6.0"
stringcase = "^1.2.0"
python-dotenv = "^0.20.0"
envsubst = "^0.1.5"

[tool.poetry.dev-dependencies]
autoflake = "^1.4"
Expand Down Expand Up @@ -70,3 +71,7 @@ warn_return_any = true
[[tool.mypy.overrides]]
module = "stringcase"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "envsubst"
ignore_missing_imports = true
48 changes: 48 additions & 0 deletions tests/core/extra_manifest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,54 @@ def context_with_extra_manifest(temporary_path: Path) -> Context:
],
id="List with single item with ${KOMPOSER_SERVICE_PREFIX} env var",
),
pytest.param(
textwrap.dedent(
"""
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Job
metadata:
name: job-1
spec:
template:
spec:
containers:
- args:
- ping
- ${KOMPOSER_SERVICE_PREFIX:-default}-service-1
"""
),
[
{
"apiVersion": "v1",
"kind": "Job",
"metadata": {
"name": "test-repository-test-branch-job-1",
"labels": {
"repository": "test-repository",
"branch": "test-branch",
},
},
"spec": {
"template": {
"spec": {
"containers": [
{
"args": [
"ping",
"test-repository-test-branch-service-1",
],
}
]
}
}
},
}
],
id="List with single item with ${KOMPOSER_SERVICE_PREFIX} env var with default",
),
pytest.param(
textwrap.dedent(
"""
Expand Down

0 comments on commit f63a57d

Please sign in to comment.