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(crd-generator): Ensure deterministic ordering of CRD versions #5784

Merged
merged 10 commits into from
Mar 12, 2024

Conversation

baloo42
Copy link
Contributor

@baloo42 baloo42 commented Mar 9, 2024

Description

If multiple versions of a CustomResource exist, the result of CRD generation is not deterministic. While this doesn't change the behaviour of Kubernetes, since the version order does not matter, it causes unnecessary changes if checked into git or applied to the cluster.

This PR resolves this issue by sorting the versions based on priority.

The implementation adds a decorator to the CRDGenerator which is configured to run late in the handler chain after all versions has been added. The decorator sorts the versions by priority so that the version with the highest priority comes first.

To be able to reuse existing functionality regarding sorting by priority and to keep the internal Version class package-private, a new method has been added to KubernetesVersionPriority.
The method allows creating a new sorted list by providing a generic list of objects and a version provider function.

Fixes #5508

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

@baloo42 baloo42 marked this pull request as ready for review March 9, 2024 22:20
@baloo42 baloo42 changed the title CRDGenerator: Fix deterministic ordering of CustomResourceDefinitionVersions (#5508) CRDGenerator: Fix deterministic ordering of CustomResourceDefinitionVersions Mar 9, 2024
@baloo42 baloo42 changed the title CRDGenerator: Fix deterministic ordering of CustomResourceDefinitionVersions fix (crd-generator): Ensure deterministic ordering of CRD versions Mar 11, 2024
@baloo42 baloo42 changed the title fix (crd-generator): Ensure deterministic ordering of CRD versions fix(crd-generator): Ensure deterministic ordering of CRD versions Mar 11, 2024
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
14.4% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

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.

LGTM thanks a lot for those changes @baloo42 !
@metacosm do you wanna have a look?

Copy link
Collaborator

@metacosm metacosm left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

* The kubernetes specs v1.17 at <a href=
* "https://v1-17.docs.kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definition-versioning/#version-priority">Version
* The kubernetes specs v1.28 at <a href=
* "https://v1-28.docs.kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#version-priority">Version
Copy link
Member

Choose a reason for hiding this comment

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

nit, do we really need to have version in the documentation? I checked https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#version-priority also works okay

@manusa manusa added this to the 6.11.0 milestone Mar 12, 2024
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 49d3647 into fabric8io:main Mar 12, 2024
16 of 19 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.

Ensure that the result of CRD generation is always the same, if Java classes do not change
5 participants