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 all 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
2 changes: 2 additions & 0 deletions docs/commands/sync-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ root-config-repo/
└── bootstrap
└── values.yaml
```
### app specific values
app specific values may be set using a values.yaml file directly in the app directory. gitopscli will process these values and remove key that would be blacklisted for security purpose and then store them in the result files under app key.

**bootstrap/values.yaml**
```yaml
Expand Down
33 changes: 31 additions & 2 deletions gitopscli/commands/sync_apps.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
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


YAML_BLACKLIST: List[str] = []


class SyncAppsCommand(Command):
@dataclass(frozen=True)
class Args(GitApiConfig):
Expand Down Expand Up @@ -58,10 +61,36 @@ 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:
yml_result = values.copy()
for key in values.keys():
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:
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
17 changes: 16 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,19 @@ 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.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