Skip to content

fix(util): avoid passing None to load_yaml on failed jinja render - #7032

Open
sundeep8967 wants to merge 1 commit into
canonical:mainfrom
sundeep8967:fix/read-conf-jinja-render-none-handling
Open

fix(util): avoid passing None to load_yaml on failed jinja render#7032
sundeep8967 wants to merge 1 commit into
canonical:mainfrom
sundeep8967:fix/read-conf-jinja-render-none-handling

Conversation

@sundeep8967

Copy link
Copy Markdown

Problem

When a templated YAML config cannot be rendered (for example, when referencing a missing nested attribute like {{ ds.meta_data.placement.region }} on a platform where it is not defined), render_jinja_payload_from_file() logs a warning and returns None.

In util.read_conf(), config_file was being unconditionally overwritten with this None return value:

config_file = render_jinja_payload_from_file(...)

Subsequently, load_yaml(config_file) was called with config_file=None, resulting in decode_binary(None) raising:

AttributeError: 'NoneType' object has no attribute 'decode'

and causing the init-local / init boot stage to fail.

Solution

  1. In util.read_conf(), capture the result of render_jinja_payload_from_file() in a temporary variable and only update config_file if the rendered output is not None.
  2. Added unit test test_read_conf_with_failed_jinja_render_undefined_attribute in tests/unittests/test_util.py asserting that when jinja rendering fails due to an undefined attribute, the original configuration is preserved and loaded without raising an AttributeError.

Fixes #7007

Testing

  • Verified test failure prior to the fix (AttributeError on decode_binary(None) reproduced).
  • Ran pytest tests/unittests/test_util.py — all 318 tests passed.

When render_jinja_payload_from_file returns None (e.g. due to an
undefined attribute error), read_conf previously assigned config_file
to None, causing load_yaml to crash with an AttributeError. Only update
config_file when a valid rendered payload is returned.

Fixes canonicalGH-7007
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] cloud-init-local.service crashes with AttributeError on failed jinja template render

1 participant