Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter yaml elements #174

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions gitopscli/commands/sync_apps.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import logging
import os
from dataclasses import dataclass
from typing import Any, Set, Tuple
from typing import Any, Set, Tuple, List, Dict
from gitopscli.git_api import GitApiConfig, GitRepo, GitRepoApiFactory
from gitopscli.io_api.yaml_util import merge_yaml_element, yaml_file_load
from gitopscli.gitops_exception import GitOpsException
from .command import Command

# array of allowed values for __clean_yaml function
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get rid of comment, you change variable name if the name itself is not descriptive enough

YAML_BLACKLIST: List[str] = []


class SyncAppsCommand(Command):
@dataclass(frozen=True)
Expand Down Expand Up @@ -58,10 +61,41 @@ 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, found_apps_path, {repo_app: {} for repo_app in repo_apps})
merge_yaml_element(
apps_config_file,
found_apps_path,
{repo_app: __clean_repo_app(team_config_git_repo, 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 __clean_yaml(values: Dict[str, Any]) -> Any:
# storing yaml to allow deletion while iterating
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you get rid of the comments? If you think you need them, I would extract them to subfunctions and name these accordingly

yml_result = values.copy()
# iterating through yaml keys
for key in values.keys():
logging.info("processing %s ", key)
# checking if key is in blacklist and remove it if necessary
if key in YAML_BLACKLIST:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This list is always empty

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The list is set in case of keywords that would need to be removed. by default it's empty and allow all keywords.

logging.info("value %s removed", key)
del yml_result[key]
else:
# check if the key have a sub-level for recursive call
if isinstance(values[key], dict):
yml_result[key] = __clean_yaml(values[key].copy())
return yml_result


def __clean_repo_app(team_config_git_repo: GitRepo, app_name: str) -> Any:
app_spec_file = team_config_git_repo.get_full_file_path(f"{app_name}/values.yaml")
try:
app_config_content = yaml_file_load(app_spec_file)
return __clean_yaml(app_config_content)
except FileNotFoundError as ex:
logging.exception("no specific app settings file found for %s", app_name, exc_info=ex)
return {}


def __find_apps_config_from_repo(
team_config_git_repo: GitRepo, root_config_git_repo: GitRepo
) -> Tuple[str, str, Set[str], Set[str], str]:
Expand Down
21 changes: 20 additions & 1 deletion tests/commands/test_sync_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from gitopscli.commands.sync_apps import SyncAppsCommand
from gitopscli.io_api.yaml_util import merge_yaml_element, yaml_file_load
from gitopscli.gitops_exception import GitOpsException

from .mock_mixin import MockMixin

ARGS = SyncAppsCommand.Args(
Expand Down Expand Up @@ -78,6 +79,9 @@ def setUp(self):
"repository": "https://other-team.config.repo.git",
"applications": {"some-other-app-2": None},
},
"/tmp/team-config-repo/my-app/values.yaml": {
"config": {"repository": "https://team.config.repo.git", "applications": {"some-other-app-1": None}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a app/team config yaml in a app/team config repo? We are using app/team config yamls in the root config repo to find app/team config repos: https://baloise.github.io/gitopscli/commands/sync-apps/#root-config-repository

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this implementation answer COP-1705 to have app specific configuration.

},
}[file_path]

self.merge_yaml_element_mock = self.monkey_patch(merge_yaml_element)
Expand Down Expand Up @@ -115,8 +119,23 @@ 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.GitRepo_team.get_full_file_path("my-app/values.yaml"),
call.yaml_file_load("/tmp/team-config-repo/my-app/values.yaml"),
call.logging.info("processing %s ", "config"),
call.logging.info("processing %s ", "repository"),
call.logging.info("processing %s ", "applications"),
call.logging.info("processing %s ", "some-other-app-1"),
call.merge_yaml_element(
"/tmp/root-config-repo/apps/team-non-prod.yaml", "config.applications", {"my-app": {}}
"/tmp/root-config-repo/apps/team-non-prod.yaml",
"config.applications",
{
"my-app": {
"config": {
"repository": "https://team.config.repo.git",
"applications": {"some-other-app-1": None},
}
}
},
),
call.GitRepo_team.get_author_from_last_commit(),
call.GitRepo_root.commit("GIT_USER", "GIT_EMAIL", "author updated apps/team-non-prod.yaml"),
Expand Down