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

start refactoring toward python grpc client #56

Merged
merged 8 commits into from
Sep 24, 2021
Merged
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
6 changes: 3 additions & 3 deletions templates/argo-cd/createapp-wftpl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ spec:
value: "hanu-deploy-apps"
- name: app_name
value: "lma"
- name: repository_url
- name: manifest_repo_url
value: "https://github.com/openinfradev/decapod-manifests"
- name: revision
value: main
value: "main"
templates:
- name: createApp
inputs:
Expand Down Expand Up @@ -56,7 +56,7 @@ spec:
- name: NAMESPACE
value: "{{inputs.parameters.namespace}}"
- name: REPO
value: "{{workflow.parameters.repository_url}}"
value: "{{workflow.parameters.manifest_repo_url}}"
- name: REVISION
value: "{{workflow.parameters.revision}}"

Expand Down
4 changes: 3 additions & 1 deletion templates/decapod-apps/lma-uniformed-wftpl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ spec:
value: "hanu-reference"
- name: app_name
value: "lma"
- name: repository_url
- name: site_repo_url
value: "https://github.com/openinfradev/decapod-site"
- name: manifest_repo_url
value: "https://github.com/openinfradev/decapod-manifests"
- name: revision
value: main
Expand Down
2 changes: 1 addition & 1 deletion templates/decapod-apps/openstack-components-wf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spec:
value: "hanu-reference"
- name: app_name
value: "openstack"
- name: repository_url
- name: manifest_repo_url
value: "https://github.com/openinfradev/decapod-manifests"
- name: revision
value: main
Expand Down
2 changes: 1 addition & 1 deletion templates/decapod-apps/openstack-infra-wftpl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spec:
value: "hanu-reference"
- name: app_name
value: "openstack"
- name: repository_url
- name: manifest_repo_url
value: "https://github.com/openinfradev/decapod-manifests"
- name: revision
value: main
Expand Down
2 changes: 1 addition & 1 deletion templates/decapod-apps/service-mesh-wf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spec:
value: hanu-reference
- name: app_name
value: service-mesh
- name: repository_url
- name: manifest_repo_url
value: 'https://github.com/openinfradev/decapod-manifests'
- name: revision
value: main
Expand Down
153 changes: 0 additions & 153 deletions templates/decapod-apps/tks-lma-federation-wftpl.yaml

This file was deleted.

136 changes: 136 additions & 0 deletions templates/decapod-manifests/update-decapod-manifest-wftpl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
name: update-decapod-manifest
namespace: argo
spec:
templates:
- name: updateManifest
synchronization:
mutex:
name: default
inputs:
parameters:
- name: action
value: "update"
- name: cluster_name
value: "hanu-reference"
- name: app_group
value: "lma"
- name: chart
value: "thanos"
# kv_map_str usage #
# - 'update' action: {"k1": "v1", "k2": "v2"}
# - 'insert' action: above format or value as list type. For eg, {"key": [v1, v2]}
- name: kv_map_str
value: ""
script:
image: sktdev/python-centos-wf-worker:v1.0
command: ["python"]
env:
source: |
import sys
import argparse
import git
import ruamel.yaml
import os
import json

action = "{{inputs.parameters.action}}"
clusterName = "{{inputs.parameters.cluster_name}}"
appGroup = "{{inputs.parameters.app_group}}"
chartName = "{{inputs.parameters.chart}}"
kvMapStr = "{{inputs.parameters.kv_map_str}}"
kvMapStr2 = kvMapStr.replace("'", "\"")
kvMap = json.loads(kvMapStr2)

repo = None
config = {}
commit_msg = ''
numChanged = 0
Copy link
Contributor

Choose a reason for hiding this comment

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

가독성을 위해 true, false flag 도 괜찮을 것 같습니다. 물론 중요하진 않습니다 ㅎㅎ

Copy link
Contributor Author

Choose a reason for hiding this comment

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

그렇네요. 굳이 수정된 갯수까진 알 필요 없으니 boolean 값으로 바꾸는게 좋겠네요

sitePath = './decapod-site'
siteFileName = "{}/{}/site-values.yaml".format(clusterName,appGroup)
siteFileNameFull = "{}/{}".format(sitePath, siteFileName)

# Clone or re-use decapod-site repository #
if not os.path.isdir(sitePath):
print("Cloning repository...")

repo = git.Repo.clone_from("{{workflow.parameters.site_repo_url}}", 'decapod-site')
with repo.config_writer() as git_config:
git_config.set_value('user', 'email', '[email protected]')
git_config.set_value('user', 'name', 'TKS Argo')
else:
print("The repo already exists. Pulling latest updates..")

repo = git.Repo(sitePath)
repo.remotes.origin.pull()

with open(siteFileNameFull, 'r') as f:
config = ruamel.yaml.round_trip_load(f, preserve_quotes=True)

charts = config["charts"]
chartFound = [chart for chart in charts if chart['name'] == chartName][0]

if action == 'update':
for k,v in kvMap.items():
if (chartFound['override'][k] == v):
print("The value {} isn't changed. Skipping update...".format(v))
else:
chartFound['override'][k] = v
numChanged += 1
commit_msg = "update items for {} chart in '{}' cluster".format(chartName, clusterName)
elif action == 'insert':
for k,v in kvMap.items():
# If the value is list type, then append one by one iterating over the list.
if isinstance(v, list):
for item in v:
if item in chartFound['override'][k]:
print("The value {} already exists.".format(item))
else:
chartFound['override'][k].append(item)
numChanged += 1
elif (v in chartFound['override'][k]):
print("The value {} already exists.".format(v))
else:
chartFound['override'][k].append(v)
numChanged += 1
commit_msg = "insert items for {} chart in '{}' cluster".format(chartName, clusterName)
elif action == 'delete':
for k,v in kvMap.items():
if (v in chartFound['override'][k]):
print("Found value '{}'. Deleting it...".format(v))
chartFound['override'][k].remove(v)
numChanged += 1
else:
print("The value {} doesn't exist.".format(v))
commit_msg = "delete items for {} chart in '{}' cluster".format(chartName, clusterName)
else:
sys.exit("Wrong action type")

if numChanged == 0:
print("Nothing updated. Exiting task..")
sys.exit(0)

###############################
# Commit and push the changed #
###############################

with open(siteFileNameFull, 'w') as f:
ruamel.yaml.round_trip_dump(config, f)

diff = repo.git.diff(repo.head.commit.tree)
print(diff)

# Provide a list of the files to stage
repo.index.add([siteFileName])

# Provide a commit message
repo.index.commit(commit_msg)
res = repo.remotes.origin.push()[0]

# flag '256' means successful fast-forward
if res.flags != 256:
print(res.summary)
sys.exit("Push failed!")