From b5833f6acac7bb94275006a2430a81184c0ae686 Mon Sep 17 00:00:00 2001 From: Abhishek Saurabh <90322483+abisek9186-baloise@users.noreply.github.com> Date: Thu, 9 Sep 2021 15:54:05 +0200 Subject: [PATCH] feat(sync-apps): support Helm value files as `apps/*.yaml` Auto detect if values are nested under a top-level `config` element. --- Dockerfile | 2 +- docs/commands/sync-apps.md | 12 ++++++++++++ gitopscli/commands/sync_apps.py | 26 ++++++++++++++++++++------ tests/commands/test_sync_apps.py | 7 ++++--- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index d2048899..80664874 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,4 +23,4 @@ RUN python -m pytest -vv -s --typeguard-packages=gitopscli FROM base-image AS runtime-image COPY --from=build-image /opt/venv /opt/venv -ENTRYPOINT ["gitopscli"] \ No newline at end of file +ENTRYPOINT ["gitopscli"] diff --git a/docs/commands/sync-apps.md b/docs/commands/sync-apps.md index 31c52746..18a33513 100644 --- a/docs/commands/sync-apps.md +++ b/docs/commands/sync-apps.md @@ -46,6 +46,18 @@ applications: app-xy-staging: app-xy-test: ``` +or + +```yaml +config: + repository: https://github.com/company-deployments/team-1-app-config-repo.git # link to your apps root repository + +# The applications that are synced by the `sync-app` command: + applications: + app-xy-production: # <- every entry corresponds to a directory in the apps root repository + app-xy-staging: + app-xy-test: +``` ## Example diff --git a/gitopscli/commands/sync_apps.py b/gitopscli/commands/sync_apps.py index 97494386..178b6a22 100644 --- a/gitopscli/commands/sync_apps.py +++ b/gitopscli/commands/sync_apps.py @@ -43,9 +43,13 @@ def __sync_apps(team_config_git_repo: GitRepo, root_config_git_repo: GitRepo, gi logging.info("Found %s app(s) in apps repository: %s", len(repo_apps), ", ".join(repo_apps)) logging.info("Searching apps repository in root repository's 'apps/' directory...") - apps_config_file, apps_config_file_name, current_repo_apps, apps_from_other_repos = __find_apps_config_from_repo( - team_config_git_repo, root_config_git_repo - ) + ( + apps_config_file, + apps_config_file_name, + current_repo_apps, + apps_from_other_repos, + found_apps_path, + ) = __find_apps_config_from_repo(team_config_git_repo, root_config_git_repo) if current_repo_apps == repo_apps: logging.info("Root repository already up-to-date. I'm done here.") @@ -54,16 +58,17 @@ def __sync_apps(team_config_git_repo: GitRepo, root_config_git_repo: GitRepo, gi __check_if_app_already_exists(repo_apps, apps_from_other_repos) logging.info("Sync applications in root repository's %s.", apps_config_file_name) - merge_yaml_element(apps_config_file, "applications", {repo_app: {} for repo_app in repo_apps}) + merge_yaml_element(apps_config_file, found_apps_path, {repo_app: {} for repo_app in repo_apps}) __commit_and_push(team_config_git_repo, root_config_git_repo, git_user, git_email, apps_config_file_name) def __find_apps_config_from_repo( team_config_git_repo: GitRepo, root_config_git_repo: GitRepo -) -> Tuple[str, str, Set[str], Set[str]]: +) -> Tuple[str, str, Set[str], Set[str], str]: apps_from_other_repos: Set[str] = set() # Set for all entries in .applications from each config repository found_app_config_file = None found_app_config_file_name = None + found_apps_path = "applications" found_app_config_apps: Set[str] = set() bootstrap_entries = __get_bootstrap_entries(root_config_git_repo) team_config_git_repo_clone_url = team_config_git_repo.get_clone_url() @@ -77,6 +82,9 @@ def __find_apps_config_from_repo( app_config_content = yaml_file_load(app_config_file) except FileNotFoundError as ex: raise GitOpsException(f"File '{app_file_name}' not found in root repository.") from ex + if "config" in app_config_content: + app_config_content = app_config_content["config"] + found_apps_path = "config.applications" if "repository" not in app_config_content: raise GitOpsException(f"Cannot find key 'repository' in '{app_file_name}'") if app_config_content["repository"] == team_config_git_repo_clone_url: @@ -90,7 +98,13 @@ def __find_apps_config_from_repo( if found_app_config_file is None or found_app_config_file_name is None: raise GitOpsException(f"Couldn't find config file for apps repository in root repository's 'apps/' directory") - return found_app_config_file, found_app_config_file_name, found_app_config_apps, apps_from_other_repos + return ( + found_app_config_file, + found_app_config_file_name, + found_app_config_apps, + apps_from_other_repos, + found_apps_path, + ) def __get_applications_from_app_config(app_config: Any) -> Set[str]: diff --git a/tests/commands/test_sync_apps.py b/tests/commands/test_sync_apps.py index 9af2bb20..ae532333 100644 --- a/tests/commands/test_sync_apps.py +++ b/tests/commands/test_sync_apps.py @@ -72,8 +72,7 @@ def setUp(self): "bootstrap": [{"name": "team-non-prod"}, {"name": "other-team-non-prod"}], }, "/tmp/root-config-repo/apps/team-non-prod.yaml": { - "repository": "https://team.config.repo.git", - "applications": {"some-other-app-1": None}, + "config": {"repository": "https://team.config.repo.git", "applications": {"some-other-app-1": None},} }, "/tmp/root-config-repo/apps/other-team-non-prod.yaml": { "repository": "https://other-team.config.repo.git", @@ -116,7 +115,9 @@ def test_sync_apps_happy_flow(self): call.GitRepo_root.get_full_file_path("apps/other-team-non-prod.yaml"), call.yaml_file_load("/tmp/root-config-repo/apps/other-team-non-prod.yaml"), call.logging.info("Sync applications in root repository's %s.", "apps/team-non-prod.yaml"), - call.merge_yaml_element("/tmp/root-config-repo/apps/team-non-prod.yaml", "applications", {"my-app": {}}), + call.merge_yaml_element( + "/tmp/root-config-repo/apps/team-non-prod.yaml", "config.applications", {"my-app": {}} + ), call.GitRepo_team.get_author_from_last_commit(), call.GitRepo_root.commit("GIT_USER", "GIT_EMAIL", "author updated apps/team-non-prod.yaml"), call.GitRepo_root.push(),