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

let client-gen understand vendor #45839

Merged
merged 2 commits into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/libs/go2idl/client-gen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ filegroup(
":package-srcs",
"//cmd/libs/go2idl/client-gen/args:all-srcs",
"//cmd/libs/go2idl/client-gen/generators:all-srcs",
"//cmd/libs/go2idl/client-gen/path:all-srcs",
"//cmd/libs/go2idl/client-gen/test_apis/testgroup:all-srcs",
"//cmd/libs/go2idl/client-gen/testoutput/clientset_generated/test_internalclientset:all-srcs",
"//cmd/libs/go2idl/client-gen/types:all-srcs",
Expand Down
1 change: 1 addition & 0 deletions cmd/libs/go2idl/client-gen/generators/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ go_library(
"//cmd/libs/go2idl/client-gen/args:go_default_library",
"//cmd/libs/go2idl/client-gen/generators/fake:go_default_library",
"//cmd/libs/go2idl/client-gen/generators/scheme:go_default_library",
"//cmd/libs/go2idl/client-gen/path:go_default_library",
"//cmd/libs/go2idl/client-gen/types:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/k8s.io/gengo/args:go_default_library",
Expand Down
4 changes: 3 additions & 1 deletion cmd/libs/go2idl/client-gen/generators/client_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
clientgenargs "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/args"
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/generators/fake"
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/generators/scheme"
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/path"
clientgentypes "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/types"

"github.com/golang/glog"
Expand Down Expand Up @@ -221,7 +222,8 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat

gvToTypes := map[clientgentypes.GroupVersion][]*types.Type{}
for gv, inputDir := range customArgs.GroupVersionToInputPath {
p := context.Universe.Package(inputDir)
// Package are indexed with the vendor prefix stripped
p := context.Universe.Package(path.Vendorless(inputDir))
for n, t := range p.Types {
// filter out types which are not included in user specified overrides.
typesOverride, ok := includedTypesOverrides[gv]
Expand Down
1 change: 1 addition & 0 deletions cmd/libs/go2idl/client-gen/generators/fake/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
deps = [
"//cmd/libs/go2idl/client-gen/args:go_default_library",
"//cmd/libs/go2idl/client-gen/generators/scheme:go_default_library",
"//cmd/libs/go2idl/client-gen/path:go_default_library",
"//cmd/libs/go2idl/client-gen/types:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/k8s.io/gengo/generator:go_default_library",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/gengo/generator"
"k8s.io/gengo/namer"
"k8s.io/gengo/types"
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/path"
)

// genFakeForType produces a file for each top-level type.
Expand Down Expand Up @@ -99,7 +100,7 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io.
}

// allow user to define a group name that's different from the one parsed from the directory.
p := c.Universe.Package(g.inputPackage)
p := c.Universe.Package(path.Vendorless(g.inputPackage))
if override := types.ExtractCommentTags("+", p.DocComments)["groupName"]; override != nil {
groupName = override[0]
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/libs/go2idl/client-gen/generators/generator_for_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"k8s.io/gengo/generator"
"k8s.io/gengo/namer"
"k8s.io/gengo/types"
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/path"
)

// genGroup produces a file for a group client, e.g. ExtensionsClient for the extension group.
Expand Down Expand Up @@ -76,7 +77,7 @@ func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer
groupName = ""
}
// allow user to define a group name that's different from the one parsed from the directory.
p := c.Universe.Package(g.inputPackage)
p := c.Universe.Package(path.Vendorless(g.inputPackage))
if override := types.ExtractCommentTags("+", p.DocComments)["groupName"]; override != nil {
groupName = override[0]
}
Expand All @@ -95,7 +96,7 @@ func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer
"restDefaultKubernetesUserAgent": c.Universe.Function(types.Name{Package: "k8s.io/client-go/rest", Name: "DefaultKubernetesUserAgent"}),
"restRESTClientInterface": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Interface"}),
"restRESTClientFor": c.Universe.Function(types.Name{Package: "k8s.io/client-go/rest", Name: "RESTClientFor"}),
"SchemeGroupVersion": c.Universe.Variable(types.Name{Package: g.inputPackage, Name: "SchemeGroupVersion"}),
"SchemeGroupVersion": c.Universe.Variable(types.Name{Package: path.Vendorless(g.inputPackage), Name: "SchemeGroupVersion"}),
}
sw.Do(groupInterfaceTemplate, m)
sw.Do(groupClientTemplate, m)
Expand Down
1 change: 1 addition & 0 deletions cmd/libs/go2idl/client-gen/generators/scheme/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
srcs = ["generator_for_scheme.go"],
tags = ["automanaged"],
deps = [
"//cmd/libs/go2idl/client-gen/path:go_default_library",
"//cmd/libs/go2idl/client-gen/types:go_default_library",
"//vendor/k8s.io/gengo/generator:go_default_library",
"//vendor/k8s.io/gengo/namer:go_default_library",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/gengo/generator"
"k8s.io/gengo/namer"
"k8s.io/gengo/types"
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/path"
clientgentypes "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/types"
)

Expand Down Expand Up @@ -66,10 +67,10 @@ func (g *GenScheme) Imports(c *generator.Context) (imports []string) {
packagePath = filepath.Dir(packagePath)
}
packagePath = filepath.Join(packagePath, "install")
imports = append(imports, strings.ToLower(fmt.Sprintf("%s \"%s\"", group.Group.NonEmpty(), packagePath)))
imports = append(imports, strings.ToLower(fmt.Sprintf("%s \"%s\"", group.Group.NonEmpty(), path.Vendorless(packagePath))))
break
} else {
imports = append(imports, strings.ToLower(fmt.Sprintf("%s%s \"%s\"", group.Group.NonEmpty(), version.NonEmpty(), packagePath)))
imports = append(imports, strings.ToLower(fmt.Sprintf("%s%s \"%s\"", group.Group.NonEmpty(), version.NonEmpty(), path.Vendorless(packagePath))))
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions cmd/libs/go2idl/client-gen/path/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package(default_visibility = ["//visibility:public"])
Copy link
Contributor

Choose a reason for hiding this comment

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

Please separate generated and non-generated code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, ignore this.


licenses(["notice"])

load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)

go_library(
name = "go_default_library",
srcs = ["path.go"],
tags = ["automanaged"],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
31 changes: 31 additions & 0 deletions cmd/libs/go2idl/client-gen/path/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package path

import "strings"

// Vendorless removes the longest match of "*/vendor/" from the front of p.
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add why that works, maybe with one example?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added.

// It is useful if a package locates in vendor/, e.g.,
// k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/apis/meta/v1, because gengo
// indexes the package with its import path, e.g.,
// k8s.io/apimachinery/pkg/apis/meta/v1,
func Vendorless(p string) string {
if pos := strings.LastIndex(p, "/vendor/"); pos != -1 {
return p[pos+len("/vendor/"):]
}
return p
}
1 change: 1 addition & 0 deletions hack/.linted_packages
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cmd/kubectl
cmd/kubelet
cmd/libs/go2idl/client-gen
cmd/libs/go2idl/client-gen/generators
cmd/libs/go2idl/client-gen/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.

Eh, this is the only change to hack/ and I don't think it is worth bothering one of hack/OWNERS to approve it. I'll try to manually add the approve tag.

cmd/libs/go2idl/client-gen/test_apis/testgroup/install
cmd/libs/go2idl/conversion-gen
cmd/libs/go2idl/deepcopy-gen
Expand Down