Skip to content

✨ Add option to omit controller-gen version in generated output - #1437

Open
pedjak wants to merge 1 commit into
kubernetes-sigs:mainfrom
pedjak:feat/optional-tool-version
Open

✨ Add option to omit controller-gen version in generated output#1437
pedjak wants to merge 1 commit into
kubernetes-sigs:mainfrom
pedjak:feat/optional-tool-version

Conversation

@pedjak

@pedjak pedjak commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

What does this do, and why do we need it?

When controller-gen runs, it embeds its version into generated files:

  • CRD manifests include a controller-gen.kubebuilder.io/version annotation
  • ApplyConfiguration Go files include the binary name (which may contain a version suffix) in the Code generated by comment header

When a team upgrades controller-gen, every generated file shows a diff even though the actual schema, RBAC rules, or deepcopy logic is unchanged. This creates large, noisy pull requests that obscure real changes and complicate code review.

This PR adds per-generator options to opt out of including version information:

  • crd:omitToolVersion=true — omits the controller-gen.kubebuilder.io/version annotation from CRD manifests
  • applyconfiguration:omitToolVersion=true — normalizes the generator name to controller-gen in the Code generated by comment, preventing version-suffixed binary names from leaking into output

Both options default to false, preserving current behavior.

Fixes #1436

@k8s-ci-robot k8s-ci-robot added the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Jun 17, 2026
@k8s-ci-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: pedjak
Once this PR has been reviewed and has the lgtm label, please assign vincepri for approval. For more information see the Code Review Process.

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

Details 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 needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jun 17, 2026
@k8s-ci-robot

Copy link
Copy Markdown
Contributor

Hi @pedjak. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 17, 2026
@pedjak
pedjak force-pushed the feat/optional-tool-version branch from 0350788 to 20bbcde Compare June 17, 2026 16:14
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Jun 17, 2026
@pedjak

pedjak commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

/assign @sbueringer @alvaroaleman @camilamacedo86

@pedjak

pedjak commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

Comment thread pkg/crd/gen.go Outdated
// (controller-gen.kubebuilder.io/version) should be added to the CRD.
//
// Left unspecified, the default is true.
AddToolVersionAnnotation *bool `marker:",optional"`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The current behaviour is to have that by default so should not be skip something like?
I do not think we would like to change the default behaviour.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@camilamacedo86 Agreed, the default behavior stays unchanged — the annotation is always added unless you explicitly opt out.

Would you prefer a naming like SkipToolVersionAnnotation (default false, set to true to opt out)? Or do you have another suggestion in mind?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done — renamed to SkipToolVersionAnnotation (default false, set to true to opt out). Also renamed the applyconfiguration field to SkipToolVersion for consistency.

@pedjak pedjak changed the title ✨ Add options to suppress controller-gen version in generated output ✨ Add options to skip controller-gen version in generated output Jun 26, 2026
@pedjak
pedjak force-pushed the feat/optional-tool-version branch from 20bbcde to f79d4d5 Compare June 26, 2026 15:37
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: pedjak
Once this PR has been reviewed and has the lgtm label, please assign vincepri for approval. For more information see the Code Review Process.

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

Details 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

Comment thread pkg/crd/gen.go Outdated
// (controller-gen.kubebuilder.io/version) should be skipped when generating the CRD.
//
// Left unspecified, the default is false.
SkipToolVersionAnnotation *bool `marker:",optional"`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not keep the same name for both places ?
Why not SkipToolVersion here as well?

WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done — renamed to OmitToolVersion in both generators for consistency.

Comment thread pkg/applyconfiguration/gen.go Outdated
ExternalApplyConfigurations []string `marker:",optional"`

// SkipToolVersion specifies if the tool version should be excluded from the
// "Code generated by" comment header. When true, the generator name is

@camilamacedo86 camilamacedo86 Jun 28, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

helpgen only uses the first physical line of a field comment as the summary. Since the first sentence wraps onto a second line, the generated summary gets cut off:

Summary: "specifies if the tool version should be excluded from the"

That is what controller-gen applyconfiguration -w shows, so the help text looks truncated.

Could we make the first line a complete sentence and move the extra explanation into the details in both one? For example:

For CRD

// OmitToolVersion omits the controller-gen version annotation from the generated CRD.
//
// When set, the controller-gen.kubebuilder.io/version annotation is not written
// to the CRD's metadata. This only affects that annotation; the generated schema
// is unchanged. Left unspecified, the default is false.

And For Here:

// OmitToolVersion omits the tool version from the "Code generated by" file header.
//
// When set, the generator name in the header is normalized to "controller-gen"
// regardless of the binary name, instead of any version-bearing name. This only
// affects the header comment; no annotations are involved. Left unspecified, the
// default is false.

Also, it makes clear what it does.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch — restructured the godoc so the first line is a complete sentence in both generators. The generated help summaries now read cleanly.


Expect(string(content)).To(HavePrefix("// Code generated by controller-gen. DO NOT EDIT."))
})
})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this test should also cover the default case.

Right now it only checks that the generated header is:

// Code generated by controller-gen. DO NOT EDIT.

That passes because the marker changes the generator name to controller-gen. But the test does not prove that the marker caused the change.

Could we add a second case that runs without skipToolVersion=true and checks that the header is not controller-gen?

It("should leave the generator name unchanged when SkipToolVersion is unset", func() {
    // run without skipToolVersion=true

    Expect(string(content)).NotTo(HavePrefix(
        "// Code generated by controller-gen. DO NOT EDIT.",
    ))
})

Then the existing test can keep checking the marker case:

It("should normalize the generator name when SkipToolVersion is true", func() {
    // run with skipToolVersion=true

    Expect(string(content)).To(HavePrefix(
        "// Code generated by controller-gen. DO NOT EDIT.",
    ))
})

Together, these prove that SkipToolVersion is what changes the generated header, instead of the test passing because of the test binary name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a default-case test that verifies the header preserves the original generator name when OmitToolVersion is unset. Also added os.RemoveAll(tmpDir) to AfterEach to clean up the temp directory.

Comment thread pkg/applyconfiguration/gen.go Outdated
// When SkipToolVersion is true, override os.Args[0] so that gengo's
// GoBoilerplate (which reads filepath.Base(os.Args[0])) emits a
// version-free "Code generated by controller-gen" header.
if d.SkipToolVersion != nil && *d.SkipToolVersion {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

WDYT about OmitToolVersion?
Omit seems to be a bette verb, wdyt?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed — renamed to OmitToolVersion everywhere. Much clearer.

@pedjak
pedjak force-pushed the feat/optional-tool-version branch from f79d4d5 to 1a7e9ad Compare June 29, 2026 14:19
@pedjak pedjak changed the title ✨ Add options to skip controller-gen version in generated output ✨ Add option to omit controller-gen version in generated output Jun 29, 2026

@camilamacedo86 camilamacedo86 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/ok-to-test

@kubernetes-prow kubernetes-prow Bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 3, 2026

By("searching for version annotation")
Expect(out.buf.String()).NotTo(ContainSubstring("controller-gen.kubebuilder.io/version"))
})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we have the other way around here?
Should add the version annotation by default when omittoolversion is not informed at all.

@pedjak pedjak Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

@camilamacedo86

Copy link
Copy Markdown
Member

Hi @pedjak,

./hack/verify-boilerplate.sh
Boilerplate header is wrong for: /home/prow/go/src/sigs.k8s.io/controller-tools/pkg/applyconfiguration/applyconfiguration_integration_test.go

The current error message is not clear. The actual fix is to remove the existing year from the header of applyconfiguration_integration_test.go.

This failure is caused by the new Python check introduced by @sbueringer and @alvaroaleman here:
#1451

@sbueringer @alvaroaleman, honestly, the Python implementation seems more complex than necessary for this requirement, and it does not appear to correctly ignore files that existed before the check was introduced.

The most straightforward fix would be to remove the year from all existing license headers. Otherwise, we will continue to see inconsistent failures in files that were not changed.

The check should also clearly explain that the year must be removed. The current error message does not identify the actual problem.

@sbueringer

sbueringer commented Aug 3, 2026

Copy link
Copy Markdown
Member

The most straightforward fix would be to remove the year from all existing license headers. Otherwise, we will continue to see inconsistent failures in files that were not changed.

No, we should not modify existing copyright headers to remove the year, see:

Their guidance is not to modify existing copyright lines in existing files, but to start using the new line for all new file contributions.
This is because attribution notices shouldn't be removed or replaced, rather just added to, without a very specific and legal reviewed reason.
kubernetes/steering#299

The issue here is not that the boilerplate verification somehow didn't check this file before. It always verifies all files.

The reason why this check fails now is because we intentionally have different rules for generated and not generated files:

  • For generated files the year should be not there
  • For not generated files we tolerate the year until 2026, afterwards the year should not be there

The problem here in this PR is that we are introducing the "DO NOT EDIT" string into this file. Because of that the file is now detected as a generated file and accordingly we are now getting a hint that we should remove the year.

I see two options on how to resolve this:

  • avoid using "DO NOT EDIT" in the unit test
  • add the test file to this list in boilerplate.py (requires a rebase onto main first)
    # list all the files contain 'DO NOT EDIT', but are not generated
    skipped_ungenerated_files = ['hack/boilerplate/boilerplate.py']

Add per-generator options to control whether controller-gen version
information is included in generated files:

- crd:omitToolVersion — omits the
  controller-gen.kubebuilder.io/version annotation from CRD manifests
- applyconfiguration:omitToolVersion — omits the tool version from
  the "Code generated by" comment header

Both default to false, preserving current behavior.
@pedjak
pedjak force-pushed the feat/optional-tool-version branch from 1a7e9ad to 90a5751 Compare August 6, 2026 10:41
@pedjak

pedjak commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

@camilamacedo86 @sbueringer — rebased onto main and went with option 2.

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

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make controller-gen version in generated output optional

5 participants