Skip to content

Commit

Permalink
feat(sync-apps): support Helm value files as apps/*.yaml
Browse files Browse the repository at this point in the history
Auto detect if values are nested under a top-level `config` element.
  • Loading branch information
abisek9186-baloise committed Sep 9, 2021
1 parent 5783b85 commit b5833f6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
ENTRYPOINT ["gitopscli"]
12 changes: 12 additions & 0 deletions docs/commands/sync-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 20 additions & 6 deletions gitopscli/commands/sync_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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()
Expand All @@ -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:
Expand All @@ -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]:
Expand Down
7 changes: 4 additions & 3 deletions tests/commands/test_sync_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit b5833f6

Please sign in to comment.