Skip to content

Commit

Permalink
Simplify deployment with local Helm charts (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
raminqaf authored Aug 30, 2023
1 parent 7ac3c40 commit 277d5ec
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 23 deletions.
3 changes: 1 addition & 2 deletions docs/docs/schema/pipeline.json
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,7 @@
"required": [
"name",
"namespace",
"app",
"repo_config"
"app"
],
"title": "KubernetesApp",
"type": "object"
Expand Down
24 changes: 11 additions & 13 deletions kpops/component_handlers/helm_wrapper/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,17 @@ def upgrade_install(
with tempfile.NamedTemporaryFile("w") as values_file:
yaml.safe_dump(values, values_file)

command = ["helm"]
command.extend(
[
"upgrade",
release_name,
chart,
"--install",
"--namespace",
namespace,
"--values",
values_file.name,
]
)
command = [
"helm",
"upgrade",
release_name,
chart,
"--install",
"--namespace",
namespace,
"--values",
values_file.name,
]
command.extend(flags.to_command())
if dry_run:
command.append("--dry-run")
Expand Down
2 changes: 1 addition & 1 deletion kpops/components/base_components/kafka_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def __install_clean_up_job(
"""Install clean up job
:param release_name: Name of the Helm release
:param suffix: Suffix to add to the realease name, e.g. "-clean"
:param suffix: Suffix to add to the release name, e.g. "-clean"
:param values: The Helm values for the chart
:param dry_run: Whether to do a dry run of the command
:return: Install clean up job with helm, return the output of the installation
Expand Down
10 changes: 6 additions & 4 deletions kpops/components/base_components/kubernetes_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class KubernetesApp(PipelineComponent):
:param app: Application-specific settings
:param repo_config: Configuration of the Helm chart repo to be used for
deploying the component, defaults to None
deploying the component, defaults to None this means that the command "helm repo add" is not called and Helm
expects a path to local Helm chart.
:param namespace: Namespace in which the component shall be deployed
:param version: Helm chart version, defaults to None
"""
Expand All @@ -56,8 +57,8 @@ class KubernetesApp(PipelineComponent):
default=...,
description=describe_attr("app", __doc__),
)
repo_config: HelmRepoConfig = Field(
default=...,
repo_config: HelmRepoConfig | None = Field(
default=None,
description=describe_attr("repo_config", __doc__),
)
version: str | None = Field(
Expand Down Expand Up @@ -102,8 +103,9 @@ def helm_chart(self) -> str:
@property
def helm_flags(self) -> HelmFlags:
"""Return shared flags for Helm commands"""
auth_flags = self.repo_config.repo_auth_flags.dict() if self.repo_config else {}
return HelmFlags(
**self.repo_config.repo_auth_flags.dict(),
**auth_flags,
version=self.version,
create_namespace=self.config.create_namespace,
)
Expand Down
44 changes: 41 additions & 3 deletions tests/components/test_kubernetes_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest
from pytest_mock import MockerFixture
from typing_extensions import override

from kpops.cli.pipeline_config import PipelineConfig
from kpops.component_handlers import ComponentHandlers
Expand Down Expand Up @@ -105,14 +106,15 @@ def test_should_lazy_load_helm_wrapper_and_not_repo_add(

def test_should_lazy_load_helm_wrapper_and_call_repo_add_when_implemented(
self,
kubernetes_app: KubernetesApp,
config: PipelineConfig,
handlers: ComponentHandlers,
helm_mock: MagicMock,
mocker: MockerFixture,
app_value: KubernetesTestValue,
):
repo_config = HelmRepoConfig(repository_name="test-repo", url="mock://test")
repo_config = HelmRepoConfig(
repository_name="test-repo", url="https://test.com/charts/"
)
kubernetes_app = KubernetesApp(
name="test-kubernetes-app",
config=config,
Expand All @@ -135,7 +137,7 @@ def test_should_lazy_load_helm_wrapper_and_call_repo_add_when_implemented(
assert helm_mock.mock_calls == [
mocker.call.add_repo(
"test-repo",
"mock://test",
"https://test.com/charts/",
RepoAuthFlags(),
),
mocker.call.upgrade_install(
Expand All @@ -148,6 +150,42 @@ def test_should_lazy_load_helm_wrapper_and_call_repo_add_when_implemented(
),
]

def test_should_deploy_app_with_local_helm_chart(
self,
config: PipelineConfig,
handlers: ComponentHandlers,
helm_mock: MagicMock,
app_value: KubernetesTestValue,
):
class AppWithLocalChart(KubernetesApp):
repo_config: None = None

@property
@override
def helm_chart(self) -> str:
return "path/to/helm/charts/"

app_with_local_chart = AppWithLocalChart(
name="test-app-with-local-chart",
config=config,
handlers=handlers,
app=app_value,
namespace="test-namespace",
)

app_with_local_chart.deploy(dry_run=False)

helm_mock.add_repo.assert_not_called()

helm_mock.upgrade_install.assert_called_once_with(
"test-app-with-local-chart",
"path/to/helm/charts/",
False,
"test-namespace",
{"nameOverride": "test-value"},
HelmUpgradeInstallFlags(),
)

def test_should_raise_not_implemented_error_when_helm_chart_is_not_set(
self,
kubernetes_app: KubernetesApp,
Expand Down

0 comments on commit 277d5ec

Please sign in to comment.