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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #5388: Generate deterministic CRDs #5390

Merged

Conversation

Donnerbart
Copy link
Contributor

@Donnerbart Donnerbart commented Aug 14, 2023

Description

Fixes #5388

I'm not sure if you want the provided Complex custom resource example as is in your code base, since it has nested ObjectMetaData, StatefulSetSpec and ServiceSpec and is tested against a checked in YAML file. So on every change on those K8s resources, the checked in file needs to be adapted (for example, non-breaking changes like adding new fields).

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change
  • Chore (non-breaking change which doesn't affect codebase;
    test, version modification, documentation, etc.)

Checklist

  • Code contributed by me aligns with current project license: Apache 2.0
  • I Added CHANGELOG entry regarding this change
  • I have implemented unit tests to cover my changes
  • I have added/updated the javadocs and other documentation accordingly
  • No new bugs, code smells, etc. in SonarCloud report
  • I tested my code in Kubernetes
  • I tested my code in OpenShift

@Donnerbart Donnerbart force-pushed the improvement/generate-deterministic-crds branch 5 times, most recently from ac54a0d to ba3ed8e Compare August 15, 2023 12:13
@Donnerbart Donnerbart marked this pull request as ready for review August 15, 2023 12:17
@Donnerbart Donnerbart changed the title Generate deterministic CRDs Fix #5388: Generate deterministic CRDs Aug 15, 2023
Copy link
Member

@andreaTP andreaTP left a comment

Choose a reason for hiding this comment

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

@Donnerbart thanks a lot for taking the time to go through this!

I have a couple of questions but let me know your thoughts!

@andreaTP
Copy link
Member

LGTM but I wish @metacosm can do a final review here.

@andreaTP andreaTP requested a review from metacosm August 16, 2023 10:16
@andreaTP
Copy link
Member

@shawkins input would be great too, at least to verify that those changes are not affecting somehow the KC operator.

@Donnerbart Donnerbart force-pushed the improvement/generate-deterministic-crds branch from ba3ed8e to 92c575b Compare August 16, 2023 11:06
@Donnerbart
Copy link
Contributor Author

I've changed the comparison for JSONSchemaProps to use type first, since id doesn't seem to be populated (but still fallback to the id if type is the same value).

I also added more JavaDoc to the DeterministicObjectMapper and its builder class.

@Donnerbart
Copy link
Contributor Author

The license check is failing for the checked in YAML file in the test resources.
Since the file is used for a byte-wise comparison, it cannot be changed.

I see that there are already some exclusions for test resources:

            <exclude>kubernetes-client/src/test/resources/**/*</exclude>
            <exclude>kubernetes-client/src/test/resources/mockito-extensions/*</exclude>
            <exclude>**/src/test/resources/ssl/*</exclude>
            <exclude>**/src/test/resources/io/fabric8/java/generator/approvals/*.*.approved.txt</exclude>
            <exclude>**/it/**/expected/**/*.expected</exclude>

I don't want to change <exclude>kubernetes-client/src/test/resources/**/*</exclude> to match all modules, so I've added <exclude>**/src/test/resources/*.yml</exclude>, which is hopefully narrow enough to not violate the license requirements.

@shawkins
Copy link
Contributor

@andreaTP @Donnerbart I'll double check specifically with keycloak generation, but glancing at the changes I don't see anything that is too concerning. However there is a lot of complexity that doesn't seem to be needed - with all of the updates to the mapper the only unsorted array in the complex json schema that I see are enum properties. Also printer columns are already sorted by path. Could we consider reducing the changes to 3ea9b08

Or do you have more examples of where arrays / collections end up unsorted in the jsonschema?

@Donnerbart Donnerbart force-pushed the improvement/generate-deterministic-crds branch 2 times, most recently from 71e6705 to dc66d70 Compare August 16, 2023 12:54
@Donnerbart
Copy link
Contributor Author

@shawkins That's a great finding! I've double checked our complex example, since there were for sure more nondeterministic fields. Looks like these were all properties, that are now sorted via .configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true).

So indeed, with the correctly sorted enums that quite hacky way of adding the collection serializer is not needed anymore.

I also cleaned up the complex example, by adding simplified, but stable versions of the K8s models being used. This still provides enough properties, that were unsorted before, but won't break on future changes on the K8s model classes.

@Donnerbart Donnerbart force-pushed the improvement/generate-deterministic-crds branch from dc66d70 to bc86bb1 Compare August 16, 2023 13:02
@andreaTP
Copy link
Member

@Donnerbart the CI failure on Win seems to be relevant, probably the encoding of the file is causing a difference.
I would be completely ok if, instead of comparing byte-by-byte we compare with String::equal the full lines.

This approach might also play nicer with the License check, e.g. you can include the License header in the committed file and skip it during the comparison.

@Donnerbart
Copy link
Contributor Author

@andreaTP Yeah, when I saw Windows I had the same thought. Will look into this first thing tomorrow (it's already end of workday for me).

I guess you don't want any helper dependency like Apache Commons or so in the test scope, so I'll write that comparison by hand. Will also look into adding the license header then.

@andreaTP
Copy link
Member

Thanks for getting back, no rush!

I guess you don't want any helper dependency like Apache Commons or so in the test scope, so I'll write that comparison by hand.

I (personally) have no issues with adding helper dependencies to the test scope ...

@Donnerbart Donnerbart force-pushed the improvement/generate-deterministic-crds branch 3 times, most recently from dde5d5a to 1c11ac2 Compare August 17, 2023 10:36
@Donnerbart Donnerbart force-pushed the improvement/generate-deterministic-crds branch 6 times, most recently from ee5dd11 to 40af55d Compare August 17, 2023 10:54
@Donnerbart
Copy link
Contributor Author

Donnerbart commented Aug 17, 2023

  • Removed the license check exclusion for YAML files in test resources.
  • Added the license header to the checked in YAML file.
  • Changed the file comparison to BufferedReader::readLine (that should handle all types of newlines).
  • Skipped leading comments and newlines for file comparison.
  • Improved the JavaDoc on the Complex example classes.
  • Fixed the description in CHANGELOG.md.

@Donnerbart Donnerbart force-pushed the improvement/generate-deterministic-crds branch from 40af55d to a1bbd92 Compare August 17, 2023 11:00
Copy link
Contributor

@shawkins shawkins left a comment

Choose a reason for hiding this comment

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

LGTM, just one minor comment.

@Donnerbart Donnerbart force-pushed the improvement/generate-deterministic-crds branch from a1bbd92 to 6af16f3 Compare August 22, 2023 10:00
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

100.0% 100.0% Coverage
0.2% 0.2% Duplication

warning The version of Java (11.0.20) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 17.
Read more here

@manusa manusa added this to the 6.9.0 milestone Aug 29, 2023
Copy link
Member

@manusa manusa left a comment

Choose a reason for hiding this comment

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

LGTM, thx!

@manusa manusa merged commit d7c1552 into fabric8io:main Aug 29, 2023
@Donnerbart Donnerbart deleted the improvement/generate-deterministic-crds branch August 29, 2023 14:33
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.

CRD generator is not deterministic
6 participants