Preserve quotations when loading yaml using ruamel.yaml

RoundTripLoader in ruamel.yaml will omit superfluous quotation
marks. In some cases, the result by loading a yaml with and
dumping it out will be unreadable by PyYAML. For example, the
pattern:
  hosts: ["mon-logstash:port"]
will be transformed to:
  hosts: [mon-logstash:port]
Currently, the Armada project is using PyYAML. It's better to keep
the  quotes to allow the following process have to use PyYAML.

This commit adds the parameter preserve_quotes=True to the
RoundTripLoader to perserve the quotes when loading the original
charts.

Tested with a fresh install, upload the stx-monitor tarball with right
formatted yaml files and successful apply the application.

Change-Id: I1fd16b24bfb62ffc3bc885babbf6eb8a9356c421
Depends-on: https://review.opendev.org/#/c/754501/
Partial-Bug: 1896530
Signed-off-by: Yuxing Jiang <yuxing.jiang@windriver.com>
This commit is contained in:
Yuxing Jiang
2020-09-25 17:46:58 -04:00
parent 642d05e44f
commit e4a13c34de
2 changed files with 13 additions and 3 deletions

View File

@@ -536,7 +536,11 @@ class AppOperator(object):
if os.path.exists(app_manifest_file):
with open(app_manifest_file, 'r') as f:
charts = list(yaml.load_all(f, Loader=yaml.RoundTripLoader))
# The RoundTripLoader removes the superfluous quotes by default,
# resulting the dumped out charts not readable in Armada.
# Set preserve_quotes=True to preserve all the quotes.
charts = list(yaml.load_all(
f, Loader=yaml.RoundTripLoader, preserve_quotes=True))
for chart in charts:
if "armada/Chart/" in chart['schema']:

View File

@@ -94,8 +94,11 @@ class ArmadaManifestOperator(object):
if os.path.exists(summary_fqpn):
self.manifest_path = os.path.dirname(summary_fqpn)
with open(summary_fqpn, 'r') as f:
# The RoundTripLoader removes the superfluous quotes by default,
# resulting the dumped out charts not readable in Armada.
# Set preserve_quotes=True to preserve all the quotes.
files_written = list(yaml.load_all(
f, Loader=yaml.RoundTripLoader))[0]
f, Loader=yaml.RoundTripLoader, preserve_quotes=True))[0]
return files_written
def load(self, manifest_fqpn):
@@ -111,8 +114,11 @@ class ArmadaManifestOperator(object):
self.delete_manifest = "%s-del%s" % os.path.splitext(manifest_fqpn)
with open(manifest_fqpn, 'r') as f:
# The RoundTripLoader removes the superfluous quotes by default,
# resulting the dumped out charts not readable in Armada.
# Set preserve_quotes=True to preserve all the quotes.
self.content = list(yaml.load_all(
f, Loader=yaml.RoundTripLoader))
f, Loader=yaml.RoundTripLoader, preserve_quotes=True))
# Generate the lookup tables
# For the individual chart docs