-
Notifications
You must be signed in to change notification settings - Fork 19
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
filter yaml elements #174
Changes from 4 commits
2f317dc
4025d3c
61e17b5
5e28ce3
d7c10c9
0aa3834
652254a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
YAML_BLACKLIST: List[str] = [] | ||
|
||
|
||
class SyncAppsCommand(Command): | ||
@dataclass(frozen=True) | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This list is always empty There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
|
@@ -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}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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"), | ||
|
There was a problem hiding this comment.
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