code-generator/client-gen: decouple core group from package name 'api'

Signed-off-by: Dr. Stefan Schimanski <stefan.schimanski@gmail.com>
This commit is contained in:
Dr. Stefan Schimanski 2024-05-28 09:03:04 +02:00
parent 7e52d34852
commit 6b2f779bd4
No known key found for this signature in database
GPG Key ID: 4C68E0F19F95EC33
7 changed files with 16 additions and 18 deletions

View File

@ -15,6 +15,7 @@ limitations under the License.
*/
// +k8s:deepcopy-gen=package
// +groupName=
// Package core contains the latest (or "internal") version of the
// Kubernetes API objects. This is the API objects as represented in memory.

View File

@ -18,6 +18,7 @@ limitations under the License.
// +k8s:deepcopy-gen=package
// +k8s:protobuf-gen=package
// +k8s:prerelease-lifecycle-gen=true
// +groupName=
// Package v1 is the v1 version of the core API.
package v1 // import "k8s.io/api/core/v1"

View File

@ -71,9 +71,9 @@ func TestGVPackageFlag(t *testing.T) {
{
args: []string{"api/v1", "api"},
expectedGroups: []types.GroupVersions{
{PackageName: "core", Group: types.Group("api"), Versions: []types.PackageVersion{
{Version: "v1", Package: "core/v1"},
{Version: "", Package: "core"},
{PackageName: "api", Group: types.Group("api"), Versions: []types.PackageVersion{
{Version: "v1", Package: "api/v1"},
{Version: "", Package: "api"},
}},
},
},

View File

@ -71,31 +71,25 @@ func (g *genGroup) Imports(c *generator.Context) (imports []string) {
func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer) error {
sw := generator.NewSnippetWriter(w, c, "$", "$")
apiPath := func(group string) string {
if group == "core" {
return `"/api"`
}
return `"` + g.apiPath + `"`
}
groupName := g.group
if g.group == "core" {
groupName = ""
}
// allow user to define a group name that's different from the one parsed from the directory.
p := c.Universe.Package(g.inputPackage)
groupName := g.group
if override := gengo.ExtractCommentTags("+", p.Comments)["groupName"]; override != nil {
groupName = override[0]
}
apiPath := `"` + g.apiPath + `"`
if groupName == "" {
apiPath = `"/api"`
}
m := map[string]interface{}{
"group": g.group,
"version": g.version,
"groupName": groupName,
"GroupGoName": g.groupGoName,
"Version": namer.IC(g.version),
"types": g.types,
"apiPath": apiPath(g.group),
"apiPath": apiPath,
"schemaGroupVersion": c.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/runtime/schema", Name: "GroupVersion"}),
"runtimeAPIVersionInternal": c.Universe.Variable(types.Name{Package: "k8s.io/apimachinery/pkg/runtime", Name: "APIVersionInternal"}),
"restConfig": c.Universe.Type(types.Name{Package: "k8s.io/client-go/rest", Name: "Config"}),

View File

@ -42,7 +42,7 @@ func (g Group) String() string {
}
func (g Group) NonEmpty() string {
if g == "api" {
if g == "" {
return "core"
}
return string(g)
@ -76,7 +76,7 @@ type GroupVersionKind struct {
}
func (gv GroupVersion) ToAPIVersion() string {
if len(gv.Group) > 0 && gv.Group.NonEmpty() != "core" {
if len(gv.Group) > 0 && gv.Group != "" {
return gv.Group.String() + "/" + gv.Version.String()
} else {
return gv.Version.String()

View File

@ -15,5 +15,6 @@ limitations under the License.
*/
// +k8s:deepcopy-gen=package
// +groupName=
package core // import "k8s.io/code-generator/examples/apiserver/apis/core"

View File

@ -18,5 +18,6 @@ limitations under the License.
// +k8s:deepcopy-gen=package
// +k8s:defaulter-gen=TypeMeta
// +k8s:conversion-gen=k8s.io/code-generator/examples/apiserver/apis/core
// +groupName=
package v1 // import "k8s.io/code-generator/examples/apiserver/apis/core/v1"