Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Default-gen changes for moving API types to k8s.io/api #54

Merged
merged 2 commits into from
May 31, 2017

Conversation

caesarxuchao
Copy link
Member

@caesarxuchao caesarxuchao commented May 12, 2017

Currently, the k8s:defaulter-gen tag is in the k8s.io/kubernetes/pkg/apis/<group>/<version>/doc.go, co-located with the input (API types) and the output (default func) of default-gen.

After kubernetes/kubernetes#44065 is implemented, the input/output/tag are not going to be in the same package:

  • The input, the API types will be moved to k8s.io/api;
  • The output, the generated default funcs, will stay in k8s.io/kubernetes, because only apiserver should ever need them; the tag (i.e., doc.go) will also stay in k8s.io/kubernetes.

This PR adds the k8s:defaulter-gen-input tag, so that the API types can be supplied from a different package.

@lavalamp @thockin

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label May 12, 2017
@k8s-reviewable
Copy link

This change is Reviewable

@caesarxuchao
Copy link
Member Author

I'll add some tests.

@caesarxuchao
Copy link
Member Author

@mbohlool could you help review it? Thanks.

@mbohlool
Copy link
Contributor

LGTM, do you want to add some tests before merging this?

@caesarxuchao
Copy link
Member Author

Yes, after the other is merged.

@caesarxuchao
Copy link
Member Author

caesarxuchao commented May 30, 2017

The tests PR (#55) is not going anywhere, can we get this merged before code freeze? It's a noop currently until i set up the codegen tags in kubernetes/kubernetes#44784.

parser/parse.go Outdated
@@ -260,6 +260,18 @@ func (b *Builder) AddDirTo(dir string, u *types.Universe) error {
return b.findTypesIn(canonicalizeImportPath(b.buildPackages[dir].ImportPath), u)
}

// AddDirToAndReturnAddedPath is the same as AddDirTo except that it also
// returns the path of the added package.
func (b *Builder) AddDirToAndReturnAddedPath(dir string, u *types.Universe) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of doing this, add a method that canonicalizes a path and returns a *Package object.

parser/parse.go Outdated
@@ -260,6 +260,26 @@ func (b *Builder) AddDirTo(dir string, u *types.Universe) error {
return b.findTypesIn(canonicalizeImportPath(b.buildPackages[dir].ImportPath), u)
}

func (b *Builder) CanonicalPath(dir string) (string, error) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean adding a method like this?

Copy link
Member

Choose a reason for hiding this comment

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

how about DirectoryToPackage(dir string, u *types.Universe) (*types.Package, error)

Copy link
Member Author

Choose a reason for hiding this comment

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

Returning the package is not going to help me. I need the canonicalizeImportPath(package.ImportPath).

parser/parse.go Outdated

// AddDirToAndReturnAddedPath is the same as AddDirTo except that it also
// returns the path of the added package.
func (b *Builder) AddDirToAndReturnAddedPath(dir string, u *types.Universe) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

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

and remove this function

parser/parse.go Outdated
@@ -260,6 +260,16 @@ func (b *Builder) AddDirTo(dir string, u *types.Universe) error {
return b.findTypesIn(canonicalizeImportPath(b.buildPackages[dir].ImportPath), u)
}

// DirectoryToPackage returns the package path given a dir. dir must be already
// parsed.
func (b *Builder) DirectoryToPackagePath(dir string) (string, error) {
Copy link
Member Author

Choose a reason for hiding this comment

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

How about this?

Returning Package is not going to help me, because what I need is canonicalizeImportPath(package.ImportPath).

I actually feel my original code is better. This version makes the generator call context.AddDir and context.DirectoryToPackagePath, while my version only calls context.AddDirAndReturnAddedPkgPath.

Copy link
Member Author

Choose a reason for hiding this comment

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

I still think my first version is better. I can give the method a better name, maybe AddDirectory (just to be different from AddDir). WDYT?

glog.Fatalf("cannot translate dir %s to package path package %s", inputDir)
}
var ok bool
typesPkg, ok = context.Universe[inputPkgPath]
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this just getting the package? Why do you need its path?

Copy link
Member Author

Choose a reason for hiding this comment

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

context.Universe[inputPkgPath] returns k8s.io/gengo/types#Package.
context.AddDirTo calls builder to parse the source code, which only knows go/build#Package and go/types#Package.

The translation from directory to canonical path is done in builder, i can add a method to it to export the functionality, and then export this method via context. But I think that is worse than just return the package path via AddDirTo.

@caesarxuchao caesarxuchao force-pushed the fix-defaut-gen branch 2 times, most recently from 4e6277e to 6bcea36 Compare May 31, 2017 20:15
@caesarxuchao
Copy link
Member Author

Updated. PTAL. Thanks.

parser/parse.go Outdated
// generator (rather than just at init time. 'dir' must be a single go package.
// GOPATH, GOROOT, and the location of your go binary (`which go`) will all be
// searched if dir doesn't literally resolve.
// Deprecated. Please use AddDirectoryTo.
Copy link
Member

Choose a reason for hiding this comment

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

Please leave the former godoc comment and just append this note :)

// AddDir adds a Go package to the context. The specified path must be a single
// go package import path. GOPATH, GOROOT, and the location of your go binary
// (`which go`) will all be searched, in the normal Go fashion.
// Deprecated. Please use AddDirectory.
Copy link
Member

Choose a reason for hiding this comment

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

Same

@lavalamp
Copy link
Member

LGTM

@lavalamp lavalamp merged commit c79c13d into kubernetes:master May 31, 2017
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants