Skip to content

Commit 9e0646c

Browse files
Merge pull request #46 from baloise-incubator/feat/new_gitopsconfig_format
Feat/new gitopsconfig format
2 parents babb610 + 2ca90f0 commit 9e0646c

4 files changed

Lines changed: 85 additions & 41 deletions

File tree

doc/commands/create-preview.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,30 @@ gitopscli create-preview \
5656
--pr-id 4711 \
5757
--auto-merge
5858
```
59+
60+
## .gitops.config.yaml
61+
Make sure that your application repository contains a `.gitops.config.yaml` file.
62+
63+
```yaml
64+
deploymentConfig:
65+
# The organisation name of your deployment repo
66+
org: DPL
67+
# The repostiory name of your deployment repo
68+
repository: incubator-non-prod
69+
# The name of the application that is used in your deployment repo
70+
applicationName: example
71+
72+
previewConfig:
73+
route:
74+
host:
75+
# your router host.
76+
#{SHA256_8CHAR_BRANCH_HASH} gets replaced by a shortened hash of your feature branch name
77+
template: example-{SHA256_8CHAR_BRANCH_HASH}.example.tld
78+
replace:
79+
# Paths that should be replaced
80+
- path: image.tag
81+
variable: GIT_COMMIT # this is the latest git hash of the PR branch
82+
- path: route.host
83+
variable: ROUTE_HOST # this is the resolved SHA256_8CHAR_BRANCH_HASH from above
84+
85+
```

gitopscli/commands/create_preview.py

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,39 @@ def create_preview_command(
7070
logging.info("Using the preview template folder: %s", preview_template_folder_name)
7171
new_preview_folder_name = gitops_config.application_name + "-" + shortened_branch_hash + "-preview"
7272
logging.info("New folder for preview: %s", new_preview_folder_name)
73-
route_host = None
74-
logging.info("New folder for preview: %s", new_preview_folder_name)
7573
branch_preview_env_already_exist = os.path.exists(root_git.get_full_file_path(new_preview_folder_name))
7674
logging.info("Is preview env already existing for branch? %s", branch_preview_env_already_exist)
77-
if gitops_config.route_paths:
78-
route_host = gitops_config.route_host.replace("previewplaceholder", shortened_branch_hash)
79-
logging.info("Created route host: %s", route_host)
8075
if not branch_preview_env_already_exist:
81-
__create_new_preview_env(
82-
branch, gitops_config, new_preview_folder_name, preview_template_folder_name, root_git, route_host,
83-
)
76+
__create_new_preview_env(branch, new_preview_folder_name, preview_template_folder_name, root_git)
8477
new_image_tag = apps_git.get_last_commit_hash()
8578
logging.info("Using image tag from last app repo commit: %s", new_image_tag)
86-
for image_path in gitops_config.image_paths:
87-
__replace_image_tag_value(
88-
apps_git, image_path, new_image_tag, new_preview_folder_name, parent_id, pr_id, root_git
79+
route_host = None
80+
value_replaced = False
81+
for replacement in gitops_config.replacements:
82+
route_host, value_replaced = __replace_value(
83+
gitops_config,
84+
new_image_tag,
85+
new_preview_folder_name,
86+
replacement,
87+
root_git,
88+
route_host,
89+
shortened_branch_hash,
90+
value_replaced,
8991
)
92+
if not value_replaced:
93+
__no_deployment_needed(apps_git, new_image_tag, parent_id, pr_id)
94+
sys.exit(0)
95+
root_git.commit(f"Upated preview environment for {gitops_config.application_name}.")
9096
root_git.push(branch)
9197
logging.info("Pushed branch %s", branch)
9298
pr_comment_text = f"""
93-
Preview created successfully. Access it here: https://{route_host}.
99+
New Preview Environment for {gitops_config.application_name} created successfully. Access it here:
100+
https://{route_host}
101+
"""
102+
if branch_preview_env_already_exist:
103+
pr_comment_text = f"""
104+
Preview Environment for {gitops_config.application_name} updated successfully. Access it here:
105+
https://{route_host}
94106
"""
95107
logging.info("Creating PullRequest comment for pr with id %s and content: %s", pr_id, pr_comment_text)
96108
apps_git.add_pull_request_comment(pr_id, pr_comment_text, parent_id)
@@ -103,16 +115,33 @@ def create_preview_command(
103115
__merge_pullrequest(branch, pull_request, root_git)
104116

105117

106-
def __replace_image_tag_value(apps_git, image_path, new_image_tag, new_preview_folder_name, parent_id, pr_id, root_git):
107-
yaml_replace_path = image_path["yamlpath"]
108-
logging.info("Replacing property %s with value: %s", yaml_replace_path, new_image_tag)
109-
value_replaced = update_yaml_file(
110-
root_git.get_full_file_path(new_preview_folder_name + "/values.yaml"), yaml_replace_path, new_image_tag,
118+
def __replace_value(
119+
gitops_config,
120+
new_image_tag,
121+
new_preview_folder_name,
122+
replacement,
123+
root_git,
124+
route_host,
125+
shortened_branch_hash,
126+
value_replaced,
127+
):
128+
replacement_value = None
129+
logging.info("Replacement: %s", replacement)
130+
replacement_path_ = replacement["path"]
131+
replacement_variable = replacement["variable"]
132+
if replacement_variable == "GIT_COMMIT":
133+
replacement_value = new_image_tag
134+
elif replacement_variable == "ROUTE_HOST":
135+
route_host = gitops_config.route_host.replace("{SHA256_8CHAR_BRANCH_HASH}", shortened_branch_hash)
136+
logging.info("Created route host: %s", route_host)
137+
replacement_value = route_host
138+
else:
139+
logging.info("Unknown replacement variable: %s", replacement_variable)
140+
value_replaced = value_replaced | update_yaml_file(
141+
root_git.get_full_file_path(new_preview_folder_name + "/values.yaml"), replacement_path_, replacement_value,
111142
)
112-
if not value_replaced:
113-
__no_deployment_needed(apps_git, new_image_tag, parent_id, pr_id)
114-
sys.exit(0)
115-
root_git.commit(f"changed '{yaml_replace_path}' to '{new_image_tag}'")
143+
logging.info("Replacing property %s with value: %s", replacement_path_, replacement_value)
144+
return route_host, value_replaced
116145

117146

118147
def __no_deployment_needed(apps_git, new_image_tag, parent_id, pr_id):
@@ -125,7 +154,7 @@ def __no_deployment_needed(apps_git, new_image_tag, parent_id, pr_id):
125154

126155

127156
def __create_new_preview_env(
128-
branch, gitops_config, new_preview_folder_name, preview_template_folder_name, root_git, route_host,
157+
branch, new_preview_folder_name, preview_template_folder_name, root_git,
129158
):
130159
shutil.copytree(
131160
root_git.get_full_file_path(preview_template_folder_name), root_git.get_full_file_path(new_preview_folder_name),
@@ -134,15 +163,7 @@ def __create_new_preview_env(
134163
logging.info("Looking for Chart.yaml at: %s", chart_file_path)
135164
if root_git.get_full_file_path(chart_file_path):
136165
update_yaml_file(root_git.get_full_file_path(chart_file_path), "name", new_preview_folder_name)
137-
if gitops_config.route_paths:
138-
for route_path in gitops_config.route_paths:
139-
yaml_replace_path = route_path["hostpath"]
140-
logging.info("Replacing property %s with value: %s", yaml_replace_path, route_host)
141-
update_yaml_file(
142-
root_git.get_full_file_path(new_preview_folder_name + "/values.yaml"), yaml_replace_path, route_host,
143-
)
144166
root_git.commit(f"Initiated new preview env for branch {branch}'")
145-
return route_host
146167

147168

148169
def __create_pullrequest(branch, gitops_config, root_git):

gitopscli/yaml/gitops_config.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,20 @@ def __init__(self, filename):
88

99
@property
1010
def application_name(self):
11-
return self._data.get("application-name")
11+
return self._data["deploymentConfig"]["applicationName"]
1212

1313
@property
1414
def team_config_org(self):
15-
return self._data.get("team-config-org")
15+
return self._data["deploymentConfig"]["org"]
1616

1717
@property
1818
def team_config_repo(self):
19-
return self._data.get("team-config-repo")
19+
return self._data["deploymentConfig"]["repository"]
2020

2121
@property
2222
def route_host(self):
23-
return self._data.get("routehost")
23+
return self._data["previewConfig"]["route"]["host"]["template"]
2424

2525
@property
26-
def route_paths(self):
27-
return self._data.get("routepaths", [])
28-
29-
@property
30-
def image_paths(self):
31-
return self._data.get("imagepaths", [])
26+
def replacements(self):
27+
return self._data["previewConfig"]["replace"]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="gitopscli",
5-
version="0.1.0",
5+
version="2.0.0",
66
packages=find_packages(),
77
entry_points={"console_scripts": ["gitopscli = gitopscli.__main__:main"]},
88
)

0 commit comments

Comments
 (0)