Skip to content

Commit

Permalink
feat: Integrate helm-values into deploy command (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarojasm95 authored Nov 5, 2024
1 parent 3a39a0e commit 1c5a298
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 42 deletions.
22 changes: 9 additions & 13 deletions aladdin/commands/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import os
import sys
import tempfile
import typing

from aladdin.lib.arg_tools import (
COMMON_OPTION_PARSER, HELM_OPTION_PARSER, CHART_OPTION_PARSER, container_command
Expand Down Expand Up @@ -49,7 +49,7 @@ def deploy(
force=False,
force_helm=False,
repo=None,
set_override_values=None,
set_override_values: typing.List[str] = None,
values_files=None
):
chart = chart or project
Expand All @@ -58,6 +58,7 @@ def deploy(
set_override_values = []
helm = Helm()
cr = ClusterRules(namespace=namespace)
cluster_code = os.environ["CLUSTER_CODE"]
git_account = load_git_configs()["account"]
git_url = f"git@github.com:{git_account}/{repo}.git"
git_ref = Git.extract_hash(git_ref, git_url)
Expand All @@ -73,23 +74,18 @@ def deploy(
with working_directory(tmpdirname):
helm_chart_path = ProjectConf().get_helm_chart_path(chart)

# We need to use --set-string in case the git ref is all digits
helm_args = ["--set-string", f"deploy.imageTag={git_ref}"]
values = HelmRules.get_helm_values()
values.update({
"project.name": project,
})
helm_args = [
f"--values=aladdin://{cluster_code}",
]
# Add user-specified values files
if values_files:
for file_path in values_files:
helm_args.append(f"--values={os.path.join(helm_chart_path, 'values', file_path)}")
for file_path in (values_files or []):
helm_args.append(f"--values={os.path.join(helm_chart_path, 'values', file_path)}")
# Update with --set-override-values
values.update(dict(value.split("=") for value in set_override_values))
values = dict(value.split("=") for value in set_override_values)

helm.upgrade(
HelmRules.get_release_name(chart),
helm_chart_path,
cr.values_files,
namespace,
force=force_helm,
dry_run=dry_run,
Expand Down
1 change: 1 addition & 0 deletions aladdin/commands/helm_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def helm_values(
chart_path,
ClusterRules().values_files,
ClusterRules().namespace,
# We need to use --set-string in case the git ref is all digits
helm_args=["--set-string", f"deploy.imageTag={git_ref}"],
**HelmRules.get_helm_values(),
)
Expand Down
42 changes: 21 additions & 21 deletions aladdin/lib/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import functools
import shelve
import shutil
from contextlib import contextmanager
from contextlib import contextmanager, closing
from collections import defaultdict

from aladdin.lib.cluster_rules import ClusterRules
Expand Down Expand Up @@ -42,26 +42,26 @@ def wrapper(certificate_scope):
if not ClusterRules().certificate_lookup_cache:
return func(certificate_scope)

cache = shelve.open(str(cache_path))
data: dict = cache.get(certificate_scope) or {}
with closing(shelve.open(str(cache_path))) as cache:
data: dict = cache.get(certificate_scope) or {}

age = time.time() - data.get("time", 0)
value = data.get("value")
ttl = ttls[value]
if (
not data
or age > ttl.total_seconds()
):
value = func(certificate_scope)
cache[certificate_scope] = {
"value": value,
"time": time.time(),
}
cache.close()
elif value:
logging.info(
"Found CACHED certificate %s for %s", value, certificate_scope
)
return value
age = time.time() - data.get("time", 0)
value = data.get("value")
ttl = ttls[value]
if (
not data
or age > ttl.total_seconds()
):
value = func(certificate_scope)
cache[certificate_scope] = {
"value": value,
"time": time.time(),
}
elif value:
logging.info(
"Found CACHED certificate %s for %s",
value, certificate_scope
)
return value

return wrapper
12 changes: 5 additions & 7 deletions aladdin/lib/k8s/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,10 @@ def upgrade(
self,
release_name: str,
chart_path: str,
values_files: List[str],
namespace: str,
force=False,
dry_run=False,
helm_args: list = None,
all_values=True,
**values,
):
if helm_args is None:
Expand All @@ -149,12 +147,12 @@ def upgrade(
"--install",
f"--namespace={namespace}",
]
if all_values:
command.append(f"--values={chart_path}/values.yaml")

command = self.prepare_command(
command, chart_path, values_files, namespace, helm_args=helm_args, **values
)
for set_name, set_val in values.items():
command.extend(["--set", "{}={}".format(set_name, set_val)])

if helm_args:
command.extend(helm_args)

logger.info("Executing: %s", " ".join(command))
return subprocess.run(["helm", *command], check=True)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "aladdin"
version = "1.29.8.8"
version = "1.29.8.9"
description = ""
authors = ["Fivestars <dev@fivestars.com>"]
include = [
Expand Down

0 comments on commit 1c5a298

Please sign in to comment.