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

Fix exception handling in coroutines #69

Merged
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
18 changes: 13 additions & 5 deletions make_argocd_fly/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def __init__(self, app_name: str, env_name: str, app_viewer: ResourceViewer = No
self.config = get_config()
self.resources = None

async def prepare(self) -> str:
pass

@abstractmethod
async def generate_resources(self) -> None:
pass
Expand Down Expand Up @@ -143,7 +146,7 @@ async def _run_kustomize(self, dir_path: str) -> str:

return stdout.decode("utf-8")

async def _prepare_kustomization_directory(self) -> str:
async def prepare(self) -> str:
config = get_config()
tmp_dir = config.get_tmp_dir()

Expand All @@ -156,20 +159,25 @@ async def _prepare_kustomization_directory(self) -> str:
if yml_child.element_rel_path.endswith('.j2'):
template_vars = merge_dicts({}, self.config.get_vars(), self.config.get_env_vars(self.env_name),
self.config.get_app_vars(self.env_name, self.app_name))
content = renderer.render(content, template_vars, yml_child.element_rel_path)
try:
content = renderer.render(content, template_vars, yml_child.element_rel_path)
except Exception as e:
log.error('Error rendering template {}: {}'.format(yml_child.element_rel_path, e))
raise

for resource_kind, resource_name, resource_yml in multi_resource_parser(content):
file_path = os.path.join(self.env_name, os.path.dirname(yml_child.element_rel_path), generate_filename(resource_kind, resource_name))
tmp_resource_writer.store_resource(file_path, resource_yml)

await tmp_resource_writer.write_resources()

return os.path.join(tmp_dir, self.get_app_rel_path())

async def generate_resources(self) -> None:
log.debug('Generating resources for application {} in environment {}'.format(self.app_name, self.env_name))

tmp_source_viewer = ResourceViewer(await self._prepare_kustomization_directory())
config = get_config()
tmp_dir = config.get_tmp_dir()

tmp_source_viewer = ResourceViewer(os.path.join(tmp_dir, self.get_app_rel_path()))
tmp_source_viewer.build()

yml_child = tmp_source_viewer.get_element(os.path.join(self.env_name, 'kustomization.yml'))
Expand Down
10 changes: 8 additions & 2 deletions make_argocd_fly/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,14 @@ async def generate(render_envs, render_apps) -> None:
config = get_config()
apps = create_applications(render_apps, render_envs)

log.debug('Rendering resources')
await asyncio.gather(*[app.generate_resources() for app in apps])
try:
log.debug('Generating temporary files')
await asyncio.gather(*[asyncio.create_task(app.prepare()) for app in apps])

log.debug('Rendering resources')
await asyncio.gather(*[asyncio.create_task(app.generate_resources()) for app in apps])
except Exception:
raise

output_writer = ResourceWriter(config.get_output_dir())
for app in apps:
Expand Down
9 changes: 6 additions & 3 deletions make_argocd_fly/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ async def _write_resource(self, file_path: str, resource_yml: str) -> None:
explicit_start=True)

async def write_resources(self) -> None:
await asyncio.gather(
*[self._write_resource(file_path, resource_yml) for file_path, resource_yml in self.resources.items()]
)
try:
await asyncio.gather(
*[asyncio.create_task(self._write_resource(file_path, resource_yml)) for file_path, resource_yml in self.resources.items()]
)
except Exception:
raise
2 changes: 2 additions & 0 deletions tests/manual/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ vars:
finalizers:
- resources-finalizer.argocd.argoproj.io
namespace: kube-system
version: 0.1.0
app:
resource: Deployment_thanos.yml
json_var: json
Expand Down Expand Up @@ -58,6 +59,7 @@ envs:
subdirectory/app_7: {app_deployer: service_deployer, project: management, destination_namespace: kube-default}
subdirectory_2/app_8: {app_deployer: service_deployer, project: management, destination_namespace: kube-default}
app_9: {app_deployer: service_deployer, project: management, destination_namespace: kube-default}
app_10: {app_deployer: service_deployer, project: management, destination_namespace: kube-default}
vars:
argocd:
api_server: management-api-server
Expand Down
40 changes: 40 additions & 0 deletions tests/manual/output/management/app_10/Deployment_hello-world.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app.kubernetes.io/instance: hello-world
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: hello-world
app.kubernetes.io/version: 1.16.0
helm.sh/chart: hello-world-0.1.0
name: hello-world
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/instance: hello-world
app.kubernetes.io/name: hello-world
template:
metadata:
labels:
app.kubernetes.io/instance: hello-world
app.kubernetes.io/name: hello-world
spec:
containers:
- image: nginx:1.16.0
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
path: /
port: http
name: hello-world
ports:
- containerPort: 80
name: http
protocol: TCP
readinessProbe:
httpGet:
path: /
port: http
serviceAccountName: hello-world
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
app.kubernetes.io/instance: hello-world
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: hello-world
app.kubernetes.io/version: 1.16.0
helm.sh/chart: hello-world-0.1.0
name: hello-world
21 changes: 21 additions & 0 deletions tests/manual/output/management/app_10/Service_hello-world.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/instance: hello-world
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: hello-world
app.kubernetes.io/version: 1.16.0
helm.sh/chart: hello-world-0.1.0
name: hello-world
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: http
selector:
app.kubernetes.io/instance: hello-world
app.kubernetes.io/name: hello-world
type: ClusterIP
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: app-10-management
namespace: argocd
finalizers:
- resources-finalizer.argocd.argoproj.io
spec:
project: management
source:
repoURL: url
targetRevision: revision
path: output/management/app_10
destination:
server: management-api-server
namespace: kube-default
syncPolicy:
syncOptions:
- ServerSideApply=true
11 changes: 11 additions & 0 deletions tests/manual/source/app_10/kustomization.yml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

helmCharts:
- name: hello-world
namespace: {{ namespace }}
releaseName: hello-world
repo: https://helm.github.io/examples
version: {{ version }}
valuesInLine: {}