From 80394811f0d2a65ee40ca2a22ce72749cafc4d7e Mon Sep 17 00:00:00 2001 From: Tristan Cacqueray Date: Fri, 10 Apr 2020 12:49:28 +0000 Subject: [PATCH] Add schema validation error message This change updates the json_to_dhall task to provides a better failure message. This change also refactors the CR spec transformation using jinja combinaison filter to be able to use the raw spec directly. Finally this change removes a copy of the spec from the local playbook by using the flat cr_spec file directly. Change-Id: I6ed5c6746f01a8a277d50bdef317e56dfde5f66d --- playbooks/files/local.yaml | 18 +----------------- roles/zuul/defaults/main.yaml | 5 +++++ roles/zuul/library/json_to_dhall.py | 4 ++-- roles/zuul/tasks/main.yaml | 23 +++++++++++++---------- 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/playbooks/files/local.yaml b/playbooks/files/local.yaml index df7f65b..520f220 100644 --- a/playbooks/files/local.yaml +++ b/playbooks/files/local.yaml @@ -9,23 +9,7 @@ meta: name: zuul namespace: default - spec: - executor: - count: 1 - ssh_key: - secretName: executor-ssh-key - merger: - count: 1 - scheduler: - config: - secretName: zuul-yaml-conf - launcher: - config: - secretName: nodepool-yaml-conf - connections: - gits: - - baseurl: https://opendev.org - name: opendev.org + spec: "{{ lookup('file', './cr_spec.yaml') | from_yaml }}" pre_tasks: - name: "Create necessary secrets" k8s: diff --git a/roles/zuul/defaults/main.yaml b/roles/zuul/defaults/main.yaml index d7a54ef..a6cc677 100644 --- a/roles/zuul/defaults/main.yaml +++ b/roles/zuul/defaults/main.yaml @@ -6,3 +6,8 @@ zuul_app_path: "/opt/ansible/conf/zuul" # Here we use zuul_spec to get un-modified cr # see: https://github.com/operator-framework/operator-sdk/issues/1770 raw_spec: "{{ vars['_operator_zuul-ci_org_zuul_spec'] | default(spec) }}" + +# Provide sensible default for non optional attributes: +spec_defaults: + web: {} + externalConfig: {} diff --git a/roles/zuul/library/json_to_dhall.py b/roles/zuul/library/json_to_dhall.py index 25cda79..49a0577 100755 --- a/roles/zuul/library/json_to_dhall.py +++ b/roles/zuul/library/json_to_dhall.py @@ -24,7 +24,7 @@ def pread(args: List[str], stdin: str) -> str: proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = proc.communicate(stdin.encode('utf-8')) if stderr: - raise RuntimeError("Command failed: " + stderr.decode('utf-8')) + raise RuntimeError(stderr.decode('utf-8')) return stdout.decode('utf-8') @@ -43,7 +43,7 @@ def ansible_main(): try: module.exit_json(changed=True, result=run(p['schema'], p['json'])) except Exception as e: - module.fail_json(msg="Dhall expression failed:" + str(e)) + module.fail_json(msg="Dhall expression failed", error=str(e)) def cli_main(): diff --git a/roles/zuul/tasks/main.yaml b/roles/zuul/tasks/main.yaml index d065597..5c029cb 100644 --- a/roles/zuul/tasks/main.yaml +++ b/roles/zuul/tasks/main.yaml @@ -9,18 +9,21 @@ schema: "({{ zuul_app_path }}/input.dhall).Input.Type" json: "{{ rspec | to_json }}" vars: - rspec: - name: "{{ zuul_name }}" - merger: "{{ raw_spec['merger'] | default({}) }}" - executor: "{{ raw_spec['executor'] | default({}) }}" - web: "{{ raw_spec['web'] | default({}) }}" - scheduler: "{{ raw_spec['scheduler'] | default({}) }}" - launcher: "{{ raw_spec['launcher'] | default({}) }}" - jobVolumes: "{{ raw_spec['jobVolumes'] | default([]) }}" - externalConfig: "{{ raw_spec['externalConfig'] | default({}) }}" - connections: "{{ raw_spec['connections'] | default({}) }}" + rspec: "{{ spec_defaults | combine(raw_spec) | combine({'name': zuul_name}) }}" + failed_when: false register: _cr_input +- name: Explain schema conversion issue + when: _cr_input.error is defined + fail: + msg: | + The provided Zuul spec is incorrect: + + {{ _cr_input.error }} + + Attributes starting with a `-` are expected. + Attributes starting with a `+` were provided but not expected. + - name: Convert expression to kubernetes objects dhall_to_json: expression: "{{ zuul_app_path }}/resources.dhall {{ _cr_input.result }}"