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

Named arguments for path formatting #1252

Merged

Conversation

Adirio
Copy link
Contributor

@Adirio Adirio commented Dec 10, 2019

Description

This PR allows the usage of named-arguments while formatting files' paths.

Resources have a Replacer method that returns an strings.Replacer that is used to format paths. The strings.Replacer dictionary is as follows:

  • %[group]s: Resource.Group
  • %[group-package-name]s: Resource.GroupPackageName
  • %[version]s: Resource.Version
  • %[kind]s: strings.ToLower(Resource.Kind)
  • %[plural]s: strings.ToLower(Resource.Plural)

Motivation

Despite the fact that the path can be provided to these file structs, access to some of the fields is restricted to the default path. This formatting ("%[kind]s" getting replaced by strings.ToLower(resource.Kind)) does not only apply to default paths. Paths provided outside the GetInputfunction do also benefit from it.

For example, if you want the types file to be inside a directory of its version, this can be done in the default package because you have access to the resource, but it can't be done before that. By providing this path temnplating options paths can be configured before having access to the exact resource. This allows to provide paths before having access to the resource like in a flag (for implementing a flag that is in kubebuilder` but breaks the scaffolding right now, the boilerplate path.

This PR is part of a bigger change tracked in #1218 but can be applied rightaway.

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 10, 2019
@k8s-ci-robot
Copy link
Contributor

Hi @Adirio. 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. Regular contributors should join the org to skip this step.

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.

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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Dec 10, 2019
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Dec 10, 2019
@Adirio Adirio changed the title Named arguments for path formatting WIP Named arguments for path formatting Dec 10, 2019
@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Dec 10, 2019
@Adirio Adirio force-pushed the scaffold-enhancement/path-utils branch from c0d2cb2 to 8a87980 Compare December 10, 2019 15:49
@Adirio Adirio changed the title WIP Named arguments for path formatting Named arguments for path formatting Dec 10, 2019
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 10, 2019
@Adirio
Copy link
Contributor Author

Adirio commented Dec 10, 2019

/assign @mengqiy

This is ready to review, tests passed before squashing so they should also pass now.

@mengqiy
Copy link
Member

mengqiy commented Dec 13, 2019

/assign @DirectXMan12

pkg/fmt/alias.go Outdated Show resolved Hide resolved
@kubernetes-sigs kubernetes-sigs deleted a comment from Adirio Dec 13, 2019
@Adirio Adirio force-pushed the scaffold-enhancement/path-utils branch 3 times, most recently from 81cb6e5 to 542a8f7 Compare December 16, 2019 10:14
@Adirio

This comment has been minimized.

@Adirio

This comment has been minimized.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 31, 2020
@Adirio
Copy link
Contributor Author

Adirio commented Jan 31, 2020

Will rebase and update this after #1340 and related PRs

Copy link
Contributor

@droot droot left a comment

Choose a reason for hiding this comment

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

Took a quick look, have a question.

pkg/scaffold/internal/util/path.go Outdated Show resolved Hide resolved
cmd/webhook_v1.go Outdated Show resolved Hide resolved
@Adirio Adirio force-pushed the scaffold-enhancement/path-utils branch from f661257 to ecc1e16 Compare February 6, 2020 14:39
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 6, 2020
@Adirio Adirio force-pushed the scaffold-enhancement/path-utils branch 4 times, most recently from 3a0b1a7 to 5210199 Compare February 6, 2020 15:22
@Adirio

This comment has been minimized.

@Adirio Adirio closed this Feb 6, 2020
@Adirio Adirio reopened this Feb 6, 2020
@Adirio Adirio force-pushed the scaffold-enhancement/path-utils branch from 5210199 to b1cf2c0 Compare February 6, 2020 23:03
@Adirio Adirio force-pushed the scaffold-enhancement/path-utils branch from b1cf2c0 to f7261fb Compare February 24, 2020 07:13
@Adirio
Copy link
Contributor Author

Adirio commented Feb 24, 2020

Rebased.

@droot Shall we merge this PR?

Copy link
Contributor

@droot droot left a comment

Choose a reason for hiding this comment

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

@Adirio I took a look with fresh eyes and I have some comments. Happy to discuss these on slack (I am @droot on slack as well) to resolve quickly.
Sorry about the back and forth on this.

pkg/model/resource/resource.go Outdated Show resolved Hide resolved
pkg/model/resource/resource.go Show resolved Hide resolved
if _, err := os.Stat(file); os.IsNotExist(err) {
if coreDomain, found := coreGroups[opts.Group]; found {
pkg = path.Join("k8s.io", "api", opts.Group, opts.Version)
pkg = replacer.Replace(path.Join("k8s.io", "api", "%[group]s", "%[version]s"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Re-reading the code with fresh eyes, I realized, we introduced many new concepts while working through the iterations:

  • replacer (strings.Replacer)
  • Keys that can be replaced (kind, package-name, group etc...) which now contributors will have to dig through resource.go's Replace function to understand what all is supported.

I am not sure if the benefits are worth introducing this complexity. I know I should have spotted these in my earlier reviews, but didn't, so sorry about that.

One thing I realized, one thing is missing though: quick abstraction to get-the-types-path-dir to store ..._types.go and get-the-controller-path-dir to store ..._controller.go.

For example, in this function, we need get-the-types-path-dir in three places, it will help to have a helper just to do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The helper issue has been solved, the complexity concern is being discussed.

pkg/scaffold/api.go Outdated Show resolved Hide resolved
pkg/scaffold/api.go Outdated Show resolved Hide resolved
pkg/scaffold/api.go Outdated Show resolved Hide resolved
@Adirio Adirio force-pushed the scaffold-enhancement/path-utils branch 2 times, most recently from cffabfc to de43fda Compare February 26, 2020 21:17
@Adirio
Copy link
Contributor Author

Adirio commented Feb 26, 2020

/test pull-kubebuilder-e2e-k8s-1-16-2

@DirectXMan12 DirectXMan12 removed their assignment Feb 26, 2020
Copy link
Contributor

@droot droot left a comment

Choose a reason for hiding this comment

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

few minor nits, then we are good to go.

pkg/model/resource/resource.go Outdated Show resolved Hide resolved
pkg/model/resource/resource.go Outdated Show resolved Hide resolved
@Adirio Adirio force-pushed the scaffold-enhancement/path-utils branch from de43fda to f35d6b6 Compare February 26, 2020 22:52
Signed-off-by: Adrian Orive <adrian.orive.oneca@gmail.com>
@Adirio Adirio force-pushed the scaffold-enhancement/path-utils branch from f35d6b6 to ec5c4ad Compare February 26, 2020 23:00
@droot droot added lgtm "Looks good to me", indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Feb 26, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

Approval requirements bypassed by manually added approval.

This pull-request has been approved by: Adirio

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

@Adirio
Copy link
Contributor Author

Adirio commented Feb 26, 2020

/test pull-kubebuilder-e2e-k8s-1-17-0

@k8s-ci-robot k8s-ci-robot merged commit f0c25d0 into kubernetes-sigs:master Feb 26, 2020
@Adirio Adirio deleted the scaffold-enhancement/path-utils branch February 27, 2020 07:03
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. 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.

None yet

6 participants