Skip to content

Commit

Permalink
code-generator/client-gen: decouple core group from package name 'api'
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Stefan Schimanski <stefan.schimanski@gmail.com>
  • Loading branch information
sttts committed Jun 27, 2024
1 parent 7e52d34 commit 6b2f779
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 18 deletions.
1 change: 1 addition & 0 deletions pkg/apis/core/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions staging/src/k8s.io/api/core/v1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (g Group) String() string {
}

func (g Group) NonEmpty() string {
if g == "api" {
if g == "" {
return "core"
}
return string(g)
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ limitations under the License.
*/

// +k8s:deepcopy-gen=package
// +groupName=

package core // import "k8s.io/code-generator/examples/apiserver/apis/core"
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 6b2f779

Please sign in to comment.