diff options
author | Ryan Harper <ryan.harper@canonical.com> | 2019-02-07 22:38:41 +0000 |
---|---|---|
committer | Server Team CI Bot <josh.powers+server-team-bot@canonical.com> | 2019-02-07 22:38:41 +0000 |
commit | cf30836645473c62599e838ab48b2d31677fa584 (patch) | |
tree | 1d161bc740fdc28f4366520acc265dd57d47debf /cloudinit | |
parent | e9bf4f23209fecab15ff63427655e95bfa0934a7 (diff) | |
download | vyos-cloud-init-cf30836645473c62599e838ab48b2d31677fa584.tar.gz vyos-cloud-init-cf30836645473c62599e838ab48b2d31677fa584.zip |
netplan: Don't render yaml aliases when dumping netplan
Cloud-init rendered netplan with duplicate aliases if a network config
included "global" nameserver/search values. Netplan uses can read yaml
files which do use aliaes but cloud-init did not render a single yaml
dictionary, instead it combined yaml sections into a single document
which sometimes resulted in duplicate aliases being present.
This branch introduces a yaml SafeDumper class which can set the
'ignore_aliases' attribute. This is not enabled by default but callers
to util.yaml_dumps can pass a boolean to toggle this. The netplan
render uses noalias=True and the resulting yaml output does not contain
any aliases.
LP: #1815051
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/net/netplan.py | 3 | ||||
-rw-r--r-- | cloudinit/safeyaml.py | 7 | ||||
-rw-r--r-- | cloudinit/util.py | 17 |
3 files changed, 19 insertions, 8 deletions
diff --git a/cloudinit/net/netplan.py b/cloudinit/net/netplan.py index 21517fda..e54a34e5 100644 --- a/cloudinit/net/netplan.py +++ b/cloudinit/net/netplan.py @@ -361,7 +361,8 @@ class Renderer(renderer.Renderer): if section: dump = util.yaml_dumps({name: section}, explicit_start=False, - explicit_end=False) + explicit_end=False, + noalias=True) txt = util.indent(dump, ' ' * 4) return [txt] return [] diff --git a/cloudinit/safeyaml.py b/cloudinit/safeyaml.py index 7bcf9dd3..3bd5e03d 100644 --- a/cloudinit/safeyaml.py +++ b/cloudinit/safeyaml.py @@ -17,6 +17,13 @@ _CustomSafeLoader.add_constructor( _CustomSafeLoader.construct_python_unicode) +class NoAliasSafeDumper(yaml.dumper.SafeDumper): + """A class which avoids constructing anchors/aliases on yaml dump""" + + def ignore_aliases(self, data): + return True + + def load(blob): return(yaml.load(blob, Loader=_CustomSafeLoader)) diff --git a/cloudinit/util.py b/cloudinit/util.py index a8a232b6..2be528a0 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1596,14 +1596,17 @@ def json_dumps(data): separators=(',', ': '), default=json_serialize_default) -def yaml_dumps(obj, explicit_start=True, explicit_end=True): +def yaml_dumps(obj, explicit_start=True, explicit_end=True, noalias=False): """Return data in nicely formatted yaml.""" - return yaml.safe_dump(obj, - line_break="\n", - indent=4, - explicit_start=explicit_start, - explicit_end=explicit_end, - default_flow_style=False) + + return yaml.dump(obj, + line_break="\n", + indent=4, + explicit_start=explicit_start, + explicit_end=explicit_end, + default_flow_style=False, + Dumper=(safeyaml.NoAliasSafeDumper + if noalias else yaml.dumper.Dumper)) def ensure_dir(path, mode=None): |