Skip to content

fix: allow multiple docs in one yaml#58

Merged
arnarg merged 2 commits into
arnarg:mainfrom
mirosval:main
Sep 17, 2025
Merged

fix: allow multiple docs in one yaml#58
arnarg merged 2 commits into
arnarg:mainfrom
mirosval:main

Conversation

@mirosval

Copy link
Copy Markdown
Contributor

I was trying to follow the Typed Resources Docs to create a definition for IPAddressPools and L2Advertisements from MetalLB.

I was unable to find a better source of their CRDs other than this one: https://github.com/metallb/metallb/blob/aa2fa5744e4bcd7ce5c44c8d3ba60c4fa360d303/config/manifests/metallb-native.yaml which seems to contain all of the CRDs in one file.

This resulted in the following error:

error: builder for '/nix/store/vs429dhh4hm5h0kn0lqg6dh2a35i8m19-metallb-jsonschema.json.drv' failed with exit code 1;
       last 24 log lines:
       > Running phase: unpackPhase
       > unpacking source archive /nix/store/l4hh20lihcm07gxkjq254fadxg9ky821-source
       > source root is source
       > Running phase: installPhase
       > Traceback (most recent call last):
       >   File "/nix/store/m9dgdsksa5myjzkc09r2wsaq7dv80nqd-crd2jsonschema.py", line 228, in <module>
       >     schema = generate_jsonschema(prefix, files, attr_name_overrides)
       >   File "/nix/store/m9dgdsksa5myjzkc09r2wsaq7dv80nqd-crd2jsonschema.py", line 51, in generate_jsonschema
       >     data = yaml.safe_load(f)
       >   File "/nix/store/q6hizrxhfjfhk63ji8dfmkd0k05zya6g-python3-3.13.5-env/lib/python3.13/site-packages/yaml/__init__.py", line 125, in safe_load
       >     return load(stream, SafeLoader)
       >   File "/nix/store/q6hizrxhfjfhk63ji8dfmkd0k05zya6g-python3-3.13.5-env/lib/python3.13/site-packages/yaml/__init__.py", line 81, in load
       >     return loader.get_single_data()
       >            ~~~~~~~~~~~~~~~~~~~~~~^^
       >   File "/nix/store/q6hizrxhfjfhk63ji8dfmkd0k05zya6g-python3-3.13.5-env/lib/python3.13/site-packages/yaml/constructor.py", line 49, in get_single_data
       >     node = self.get_single_node()
       >   File "/nix/store/q6hizrxhfjfhk63ji8dfmkd0k05zya6g-python3-3.13.5-env/lib/python3.13/site-packages/yaml/composer.py", line 41, in get_single_node
       >     raise ComposerError("expected a single document in the stream",
       >             document.start_mark, "but found another document",
       >             event.start_mark)
       > yaml.composer.ComposerError: expected a single document in the stream
       >   in "charts/metallb/charts/crds/templates/crds.yaml", line 1, column 1
       > but found another document
       >   in "charts/metallb/charts/crds/templates/crds.yaml", line 121, column 1

I've noticed that you're using pyyaml.safe_load(), so I've just converted that piece of code to use pyyaml.safe_load_all() instead and then iterate over the documents within the file.

However this caused another issue, as there is no real detection that we're looking at a CRD, so I've added a basic check for that as well.

The PR is just these 3 lines:

docs = yaml.safe_load_all(f)

for data in docs:
    if "spec" in data and "kind" in data and data["kind"] == "CustomResourceDefinition":

the rest is just indented to match.

With this change I was able to properly generate and use the MetalLB resources.

@arnarg arnarg left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this contribution! It looks good but could you also add a unit test for this change in ./pkgs/generators/crd/test_crd2jsonschema.py? :)

@mirosval

Copy link
Copy Markdown
Contributor Author

@arnarg Thank you for the review and an amazing project! I've added a test, LMK if there's anything else you'd like me to look at.

@mirosval
mirosval requested a review from arnarg September 17, 2025 15:41

@arnarg arnarg left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you!

@arnarg
arnarg merged commit e9e4863 into arnarg:main Sep 17, 2025
4 checks passed
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.

2 participants