Skip to content

✨ feat(cli): mark resource flags as required to improve completion#5647

Merged
k8s-ci-robot merged 1 commit intokubernetes-sigs:masterfrom
vitorfloriano:create-api-mark-flags-required
Apr 19, 2026
Merged

✨ feat(cli): mark resource flags as required to improve completion#5647
k8s-ci-robot merged 1 commit intokubernetes-sigs:masterfrom
vitorfloriano:create-api-mark-flags-required

Conversation

@vitorfloriano
Copy link
Copy Markdown
Contributor

@vitorfloriano vitorfloriano commented Apr 18, 2026

This PR mark the kind and version as required.

This makes tab completion suggest those flags when completing the create api and create webhooks commands, instead of showing the files in the current directory.

👎🏻 Current behavior:

$ kubebuilder create api<TAB><TAB>  (shows files)
.agents/            dist/               hack/               OWNERS_ALIASES      test_e2e.sh
AGENTS.md           docs/               internal/           pkg/                test.sh
bin/                .git/               kubebuilder         README.md           VERSIONING.md
build/              .github/            LICENSE             RELEASE.md          .yamllint
code-of-conduct.md  .gitignore          main.go             roadmap/            .yamllint-helm
CONTRIBUTING.md     .golangci.yml       Makefile            SECURITY_CONTACTS   
DESIGN.md           go.mod              netlify.toml        test/               
designs/            go.sum              OWNERS              testdata/    

👍🏻 New behavior:

$ kubebuilder create api<TAB><TAB> (does not show files)
$ kubebuilder create api --<TAB> (shows resource flags instead)
--kind     (Resource Kind (e.g., CronJob, Deployment))        --version  (Resource Version (e.g., v1, v1beta1))  

Edit: Only version and kind are marked as required, so completion shows them proactively.

After the two flags are passed, tab completion shows a hint at other flags (Cobra's ActiveHelp):

$ kubebuilder create api --kind MyKind --version v1alpha 
Type '--' and press TAB to list more flags

Just a nice little polish in UX. 💅🏻

Relates to #5446

@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Apr 18, 2026
Comment thread pkg/cli/cmd_helpers.go Outdated
var options *resourceOptions
if requiresResource {
options = bindResourceFlags(cmd.Flags())
cobra.CheckErr(cmd.MarkFlagRequired("group"))
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 there might be an edge case where group can be nil for core types—could we add a check for that?

That said, I really like this idea. It should help us enforce consistency across the whole project. 🎉

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.

We could add a check IF the validation occurred before flag binding, which is not the case. We mark the flags as required in the initialization hook, and only then the plugin validates the input.

So I don't think it is possible...can't think of a way to make this happen.

But, MarkFlagRequired() does not validate the input, it only check if it is present, so --group "" would still work fine.

@k8s-triage-robot
Copy link
Copy Markdown

Unknown CLA label state. Rechecking for CLA labels.

Send feedback to sig-contributor-experience at kubernetes/community.

/check-cla
/easycla

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 18, 2026
@camilamacedo86 camilamacedo86 requested a review from Copilot April 18, 2026 18:13
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to improve kubebuilder CLI UX by making the resource identity flags (--group, --version, --kind) show up as required in shell completion for commands that scaffold resources (notably create api and create webhook), preventing default completion from listing files in the current directory.

Changes:

  • Marks group, version, and kind flags as required for commands whose plugins implement plugin.RequiresResource.

Comment thread pkg/cli/cmd_helpers.go Outdated
Comment thread pkg/cli/cmd_helpers.go Outdated
Comment thread pkg/cli/cmd_helpers.go Outdated
@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 Apr 18, 2026
Comment thread pkg/cli/api.go
) ([]cobra.Completion, cobra.ShellCompDirective) {
completions := []cobra.Completion{}
if len(args) == 0 && toComplete == "" {
completions = cobra.AppendActiveHelp(completions, "Type '--' and press TAB to list more flags")
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 I removed the group flag requirement and added ActiveHelp to hint users about other flags. It's going to work like this:

Only version and kind are marked as required, so completion shows them proactively:

$ kubebuilder create api<TAB>
--kind     (Resource Kind (e.g., CronJob, Deployment))  --version  (Resource Version (e.g., v1, v1beta1))

After the two flags are passed, tab completion shows a hint at other flags (Cobra's ActiveHelp):

$ kubebuilder create api --kind MyKind --version v1alpha 
Type '--' and press TAB to list more flags

I'm not 100% happy with the hint message, though. We can change that.

Comment thread pkg/cli/cmd_helpers.go
Comment on lines +271 to +276
if err := cmd.MarkFlagRequired("version"); err != nil {
return nil, fmt.Errorf("failed to mark 'version' flag as required: %w", err)
}
if err := cmd.MarkFlagRequired("kind"); err != nil {
return nil, fmt.Errorf("failed to mark 'kind' flag as required: %w", err)
}
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.

Also added proper err check, as pointed by @copilot.

Comment thread pkg/cli/api.go
*/

//nolint:dupl
package cli
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.

We need to fix the lint issue here, run make lint-fix to check it out.

The kind and version are now required.This makes
completion suggest those flags when completing
the create api and create webhooks commands.

Just a nice little improvement in UX.
@vitorfloriano vitorfloriano force-pushed the create-api-mark-flags-required branch from f57f1f3 to c896d0d Compare April 19, 2026 13:50
Copy link
Copy Markdown
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 19, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: camilamacedo86, vitorfloriano

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

The pull request process is described 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 the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 19, 2026
@k8s-ci-robot k8s-ci-robot merged commit 4ef6a45 into kubernetes-sigs:master Apr 19, 2026
20 checks passed
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.

5 participants