Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Updating WriteYAML to not add empty line to start files #626

Merged

Conversation

robscott
Copy link
Member

@robscott robscott commented Sep 29, 2021

Currently generated YAML files all have \n---\n prepended to them. Although that's not an issue when using kubectl apply with those files, it requires extra work when trying to do something simple like parse CRD yaml from a controller. Although not a significant issue, it's caused us (Gateway API + associated controller authors) to have to workaround it in several places. When parsing YAML files like this, the leading new line is parsed as an empty yaml struct, so we need to write code that can handle this empty/invalid object that starts all our generated YAML CRDs.

It looks like this code originated from #208, and this particular part of the change does not seem like it was intentional.

Initially I wrote this to simply not add the delimiter to the start of the file, but as @howardjohn pointed out, that would make the concatenated files invalid YAML. This change does mean that concatenating YAML files that don't have a trailing new line will not work. Of course all files generated by this tool do have a trailing new line, and that seems like a reasonable expectation of other YAML files.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Sep 29, 2021
@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Sep 29, 2021
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Sep 29, 2021
@robscott robscott force-pushed the yaml-delimiter-cleanup branch 3 times, most recently from b3a1559 to 1fe1610 Compare September 29, 2021 16:12
Copy link

@howardjohn howardjohn left a comment

Choose a reason for hiding this comment

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

hypothetically it means cat *.yaml is no longer valid yaml doc. I think its a good change, just one hypothetical reason it could be useful

@robscott robscott changed the title 馃悰 Updating WriteYAML to not add delimiter to start files 馃悰 Updating WriteYAML to not add empty line to start files Sep 29, 2021
@robscott
Copy link
Member Author

@howardjohn that's a good point, I was on the fence with the approach here but ended up deciding to go back to a delimiter to start the file, just without the extra prepended new line, hopefully that's a reasonable balance here.

Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

thanks!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 30, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman, robscott

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 30, 2021
@k8s-ci-robot k8s-ci-robot merged commit 384bf3e into kubernetes-sigs:master Sep 30, 2021
a-hilaly added a commit to a-hilaly/ack-code-generator that referenced this pull request Sep 9, 2022
Recently we bumped `controller-tools` version to `v0.9.2` to support
building controller with go1.19. This change sneakily impacted the
`build-controller-release.sh` script, which caused our prow testing jobs
to fail.

To summarize the bug we can generate the `role.yaml` file using
controller-gen `0.7.0` and `0.9.2` and inspect the differences:

```bash
diff role-controller-gen0.9.2.yaml role-controller-gen0.7.0.yaml
0a1
>
```

We can observe that the new line that used it be generated at the top of
each file is no longer there, due to the fix introduced in 0.8.0 more
precisely in kubernetes-sigs/controller-tools#626

This change combined with the command `tail -n +8` in
https://github.com/aws-controllers-k8s/code-generator/blob/v0.20.0/scripts/build-controller-release.sh#L237
causes the script to cut an extra line which happens to be declaration
of rules section in a controller ClusterRole manifest. Hence the error
observed in the prow logs:

```bash
Error: INSTALLATION FAILED: YAML parse error on emrcontainers-chart/templates/cluster-role-controller.yaml: error converting YAML to JSON: yaml: line 8: did not find expected key
```

This patch fixes the `tail` command to properly cut the generated
`role.yaml` file and avoid deleteng the `rules` section.
ack-bot pushed a commit to aws-controllers-k8s/code-generator that referenced this pull request Sep 9, 2022
This patch fixes the `tail` command to properly cut the generated
`role.yaml` file and avoid deleteng the `rules` section.

Context:

Recently we bumped `controller-tools` version to `v0.9.2` to support
building controller with go1.19. This change sneakily impacted the
`build-controller-release.sh` script, which caused our prow testing jobs
to fail.

To summarize the bug we can generate the `role.yaml` file using
controller-gen `0.7.0` and `0.9.2` and inspect the differences:

```bash
diff role-controller-gen0.9.2.yaml role-controller-gen0.7.0.yaml
0a1
>
```

We can observe that the new line that used it be generated at the top of
each file is no longer there, due to the fix introduced in 0.8.0 more
precisely in kubernetes-sigs/controller-tools#626

This change combined with the command `tail -n +8` in the [build
controller release script](https://github.com/aws-controllers-k8s/code-generator/blob/v0.20.0/scripts/build-controller-release.sh#L237) causes the script to cut an extra line which
happens to be declaration of rules section in a controller ClusterRole
manifest. Hence the error observed in the prow logs:

```bash
Error: INSTALLATION FAILED: YAML parse error on emrcontainers-chart/templates/cluster-role-controller.yaml: error converting YAML to JSON: yaml: line 8: did not find expected key
```

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants