Skip to content

Commit

Permalink
Move to internal type and change variables to support dashed imports.
Browse files Browse the repository at this point in the history
Signed-off-by: Mangirdas Judeikis <[email protected]>
On-behalf-of: SAP [email protected]
  • Loading branch information
mjudeikis committed Dec 12, 2024
1 parent 057d69d commit 472bbd5
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 74 deletions.
17 changes: 9 additions & 8 deletions pkg/generators/clientgen/clientgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
}

// adapted from https://github.com/kubernetes/kubernetes/blob/8f269d6df2a57544b73d5ca35e04451373ef334c/staging/src/k8s.io/code-generator/cmd/client-gen/types/helpers.go#L87-L103
func toGroupVersionInfos(groupVersionKinds map[parser.Group]map[types.PackageVersion][]parser.Kind) []types.GroupVersionInfo {
var info []types.GroupVersionInfo
func toGroupVersionInfos(groupVersionKinds map[parser.Group]map[types.PackageVersion][]parser.Kind) []parser.Group {
var info []parser.Group
for group, versions := range groupVersionKinds {
for version := range versions {
info = append(info, toGroupVersionInfo(group, version))
Expand All @@ -240,12 +240,13 @@ func toGroupVersionInfos(groupVersionKinds map[parser.Group]map[types.PackageVer
}

// adapted from https://github.com/kubernetes/kubernetes/blob/8f269d6df2a57544b73d5ca35e04451373ef334c/staging/src/k8s.io/code-generator/cmd/client-gen/types/helpers.go#L87-L103
func toGroupVersionInfo(group parser.Group, version types.PackageVersion) types.GroupVersionInfo {
return types.GroupVersionInfo{
func toGroupVersionInfo(group parser.Group, version types.PackageVersion) parser.Group {
goName := strings.ReplaceAll(group.GoName, "-", "")
return parser.Group{
Group: group.Group,
Version: types.Version(namer.IC(version.Version.String())),
PackageAlias: strings.ToLower(group.GoName + version.Version.NonEmpty()),
GroupGoName: group.GoName,
LowerCaseGroupGoName: namer.IL(group.GoName),
Version: parser.Version(namer.IC(version.Version.String())),
PackageAlias: strings.ToLower(goName + version.Version.NonEmpty()),
GoName: goName,
LowerCaseGroupGoName: namer.IL(goName),
}
}
22 changes: 11 additions & 11 deletions pkg/generators/informergen/informergen.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
return err
}

gvks := map[types.Group]map[types.Version][]parser.Kind{}
gvks := map[types.Group]map[parser.Version][]parser.Kind{}
for group, versions := range groupVersionKinds {
for version, kinds := range versions {
info := toGroupVersionInfo(group, version)
if _, exists := gvks[info.Group]; !exists {
gvks[info.Group] = map[types.Version][]parser.Kind{}
gvks[info.Group] = map[parser.Version][]parser.Kind{}
}
gvks[info.Group][info.Version] = kinds
}
Expand All @@ -178,7 +178,7 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
}

for group, versions := range groupVersionKinds {
groupDir := filepath.Join(informersDir, group.PackageName())
groupDir := filepath.Join(informersDir, group.PackagePath())
outputFile := filepath.Join(groupDir, "interface.go")
logger := logger.WithValues(
"group", group.String(),
Expand Down Expand Up @@ -246,8 +246,8 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
}

// adapted from https://github.com/kubernetes/kubernetes/blob/8f269d6df2a57544b73d5ca35e04451373ef334c/staging/src/k8s.io/code-generator/cmd/client-gen/types/helpers.go#L87-L103
func toGroupVersionInfos(groupVersionKinds map[parser.Group]map[types.PackageVersion][]parser.Kind) []types.GroupVersionInfo {
var info []types.GroupVersionInfo
func toGroupVersionInfos(groupVersionKinds map[parser.Group]map[types.PackageVersion][]parser.Kind) []parser.Group {
var info []parser.Group
for group, versions := range groupVersionKinds {
for version := range versions {
info = append(info, toGroupVersionInfo(group, version))
Expand All @@ -260,12 +260,12 @@ func toGroupVersionInfos(groupVersionKinds map[parser.Group]map[types.PackageVer
}

// adapted from https://github.com/kubernetes/kubernetes/blob/8f269d6df2a57544b73d5ca35e04451373ef334c/staging/src/k8s.io/code-generator/cmd/client-gen/types/helpers.go#L87-L103
func toGroupVersionInfo(group parser.Group, version types.PackageVersion) types.GroupVersionInfo {
return types.GroupVersionInfo{
func toGroupVersionInfo(group parser.Group, version types.PackageVersion) parser.Group {
return parser.Group{
Group: group.Group,
Version: types.Version(namer.IC(version.Version.String())),
PackageAlias: strings.ToLower(group.GoName + version.Version.NonEmpty()),
GroupGoName: group.GoName,
LowerCaseGroupGoName: namer.IL(group.GoName),
PackageAlias: strings.ReplaceAll(strings.ToLower(group.GoName+version.Version.NonEmpty()), "-", ""),
Version: parser.Version(namer.IC(version.Version.String())),
GoName: strings.ReplaceAll(group.GoName, "-", ""),
LowerCaseGroupGoName: strings.ReplaceAll(namer.IL(group.GoName), "-", ""),
}
}
14 changes: 7 additions & 7 deletions pkg/generators/listergen/listergen.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
for version, kinds := range versions {
groupInfo := toGroupVersionInfo(group, version)
for _, kind := range kinds {
listerDir := filepath.Join("listers", group.PackageName(), version.PackageName())
listerDir := filepath.Join("listers", group.PackagePath(), version.PackageName())
outputFile := filepath.Join(listerDir, strings.ToLower(kind.String())+".go")
logger := klog.Background().WithValues(
"group", group.String(),
Expand Down Expand Up @@ -126,12 +126,12 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
}

// adapted from https://github.com/kubernetes/kubernetes/blob/8f269d6df2a57544b73d5ca35e04451373ef334c/staging/src/k8s.io/code-generator/cmd/client-gen/types/helpers.go#L87-L103
func toGroupVersionInfo(group parser.Group, version types.PackageVersion) types.GroupVersionInfo {
return types.GroupVersionInfo{
func toGroupVersionInfo(group parser.Group, version types.PackageVersion) parser.Group {
return parser.Group{
Group: group.Group,
Version: types.Version(namer.IC(version.Version.String())),
PackageAlias: strings.ToLower(group.GoName + version.Version.NonEmpty()),
GroupGoName: group.GoName,
LowerCaseGroupGoName: namer.IL(group.GoName),
Version: parser.Version(namer.IC(version.Version.String())),
PackageAlias: strings.ReplaceAll(strings.ToLower(group.GoName+version.Version.NonEmpty()), "-", ""),
GoName: strings.ReplaceAll(group.GoName, "-", ""),
LowerCaseGroupGoName: strings.ReplaceAll(namer.IL(group.GoName), "-", ""),
}
}
14 changes: 7 additions & 7 deletions pkg/internal/clientgen/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"strings"
"text/template"

"k8s.io/code-generator/cmd/client-gen/types"

"github.com/kcp-dev/code-generator/v2/pkg/parser"
"github.com/kcp-dev/code-generator/v2/pkg/util"
)

Expand All @@ -15,7 +14,7 @@ type ClientSet struct {
Name string

// Groups are the groups in this client-set.
Groups []types.GroupVersionInfo
Groups []parser.Group

// PackagePath is the package under which this client-set will be exposed.
// TODO(skuznets) we should be able to figure this out from the output dir, ideally
Expand All @@ -38,6 +37,7 @@ func (c *ClientSet) WriteContent(w io.Writer) error {

m := map[string]interface{}{
"name": c.Name,
"packageName": strings.ReplaceAll(c.Name, "-", ""),
"packagePath": c.PackagePath,
"groups": c.Groups,
"singleClusterClientPackagePath": c.SingleClusterClientPackagePath,
Expand All @@ -51,7 +51,7 @@ var clientset = `
// Code generated by kcp code-generator. DO NOT EDIT.
package {{.name}}
package {{.packageName}}
import (
"fmt"
Expand All @@ -66,22 +66,22 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/util/flowcontrol"
{{range .groups}} {{.PackageAlias}} "{{$.packagePath}}/typed/{{.Group.PackageName}}/{{.Version.PackageName}}"
{{range .groups}} {{.GoPackageAlias}} "{{$.packagePath}}/typed/{{.Group.PackageName}}/{{.Version.PackageName}}"
{{end -}}
)
type ClusterInterface interface {
Cluster(logicalcluster.Path) client.Interface
Discovery() discovery.DiscoveryInterface
{{range .groups}} {{.GroupGoName}}{{.Version}}() {{.PackageAlias}}.{{.GroupGoName}}{{.Version}}ClusterInterface
{{range .groups}} {{.GroupGoName}}{{.Version}}() {{.GoPackageAlias}}.{{.GroupGoName}}{{.Version}}ClusterInterface
{{end -}}
}
// ClusterClientset contains the clients for groups.
type ClusterClientset struct {
*discovery.DiscoveryClient
clientCache kcpclient.Cache[*client.Clientset]
{{range .groups}} {{.LowerCaseGroupGoName}}{{.Version}} *{{.PackageAlias}}.{{.GroupGoName}}{{.Version}}ClusterClient
{{range .groups}} {{.LowerCaseGroupGoName}}{{.Version}} *{{.GoPackageAlias}}.{{.GroupGoName}}{{.Version}}ClusterClient
{{end -}}
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/internal/clientgen/fake_clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"strings"
"text/template"

"k8s.io/code-generator/cmd/client-gen/types"

"github.com/kcp-dev/code-generator/v2/pkg/parser"
"github.com/kcp-dev/code-generator/v2/pkg/util"
)

Expand All @@ -15,7 +14,7 @@ type FakeClientset struct {
Name string

// Groups are the groups in this client-set.
Groups []types.GroupVersionInfo
Groups []parser.Group

// PackagePath is the package under which this client-set will be exposed.
// TODO(skuznets) we should be able to figure this out from the output dir, ideally
Expand Down
4 changes: 1 addition & 3 deletions pkg/internal/clientgen/fake_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"strings"
"text/template"

"k8s.io/code-generator/cmd/client-gen/types"

"github.com/kcp-dev/code-generator/v2/pkg/parser"
"github.com/kcp-dev/code-generator/v2/pkg/util"
)

type FakeGroup struct {
// Group is the group in this client.
Group types.GroupVersionInfo
Group parser.Group

// Kinds are the kinds in the group.
Kinds []parser.Kind
Expand Down
3 changes: 1 addition & 2 deletions pkg/internal/clientgen/fake_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import (
"text/template"

"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/code-generator/cmd/client-gen/types"

"github.com/kcp-dev/code-generator/v2/pkg/parser"
"github.com/kcp-dev/code-generator/v2/pkg/util"
)

type FakeTypedClient struct {
// Group is the group in this client.
Group types.GroupVersionInfo
Group parser.Group

// Kind is the kinds in this file.
Kind parser.Kind
Expand Down
4 changes: 1 addition & 3 deletions pkg/internal/clientgen/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"strings"
"text/template"

"k8s.io/code-generator/cmd/client-gen/types"

"github.com/kcp-dev/code-generator/v2/pkg/parser"
"github.com/kcp-dev/code-generator/v2/pkg/util"
)

type Group struct {
// Group is the group in this client.
Group types.GroupVersionInfo
Group parser.Group

// Kinds are the kinds in the group.
Kinds []parser.Kind
Expand Down
9 changes: 4 additions & 5 deletions pkg/internal/clientgen/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"strings"
"text/template"

"k8s.io/code-generator/cmd/client-gen/types"

"github.com/kcp-dev/code-generator/v2/pkg/parser"
"github.com/kcp-dev/code-generator/v2/pkg/util"
)

type Scheme struct {
// Groups are the groups in this client-set.
Groups []types.GroupVersionInfo
Groups []parser.Group

// APIPackagePath is the root directory under which API types exist.
// e.g. "k8s.io/api"
Expand Down Expand Up @@ -51,15 +50,15 @@ import (
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
{{range .groups}} {{.PackageAlias}} "{{$.apiPackagePath}}/{{.Group.PackageName}}/{{.Version.PackageName}}"
{{range .groups}} {{.GoPackageAlias}} "{{$.apiPackagePath}}/{{.Group.PackageName}}/{{.Version.PackageName}}"
{{end -}}
)
var Scheme = runtime.NewScheme()
var Codecs = serializer.NewCodecFactory(Scheme)
var ParameterCodec = runtime.NewParameterCodec(Scheme)
var localSchemeBuilder = runtime.SchemeBuilder{
{{range .groups}} {{.PackageAlias}}.AddToScheme,
{{range .groups}} {{.GoPackageAlias}}.AddToScheme,
{{end -}}
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/internal/clientgen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"strings"
"text/template"

"k8s.io/code-generator/cmd/client-gen/types"

"github.com/kcp-dev/code-generator/v2/pkg/parser"
"github.com/kcp-dev/code-generator/v2/pkg/util"
)

type TypedClient struct {
// Group is the group in this client.
Group types.GroupVersionInfo
Group parser.Group

// Kind is the kinds in this file.
Kind parser.Kind
Expand Down Expand Up @@ -62,10 +60,10 @@ import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
{{.group.PackageAlias}} "{{.apiPackagePath}}/{{.group.Group.PackageName}}/{{.group.Version.PackageName}}"
{{.group.GoPackageAlias}} "{{.apiPackagePath}}/{{.group.Group.PackageName}}/{{.group.Version.PackageName}}"
{{end}}
{{.group.PackageAlias}}client "{{.singleClusterClientPackagePath}}/typed/{{.group.Group.PackageName}}/{{.group.Version.PackageName}}"
{{.group.GoPackageAlias}}client "{{.singleClusterClientPackagePath}}/typed/{{.group.Group.PackageName}}/{{.group.Version.PackageName}}"
)
// {{.kind.Plural}}ClusterGetter has a method to return a {{.kind.String}}ClusterInterface.
Expand Down
10 changes: 5 additions & 5 deletions pkg/internal/informergen/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import (
upstreaminformers "{{.singleClusterInformerPackagePath}}"
{{end -}}
{{range .groups}} {{.Group.PackageName}}informers "{{$.packagePath}}/{{.Group.PackageName}}"
{{range .groups}} {{.PackageName}}informers "{{$.packagePath}}/{{.Group.PackageName}}"
{{end -}}
"{{.packagePath}}/internalinterfaces"
Expand Down Expand Up @@ -329,12 +329,12 @@ type SharedInformerFactory interface {
// InformerFor returns the SharedIndexInformer for obj.
InformerFor(obj runtime.Object, newFunc internalinterfaces.NewInformerFunc) kcpcache.ScopeableSharedIndexInformer
{{range .groups}} {{.GoName}}() {{.Group.PackageName}}informers.ClusterInterface
{{range .groups}} {{.GroupGoName}}() {{.PackageName}}informers.ClusterInterface
{{end -}}
}
{{range .groups}}
func (f *sharedInformerFactory) {{.GoName}}() {{.Group.PackageName}}informers.ClusterInterface {
func (f *sharedInformerFactory) {{.GroupGoName}}() {{.PackageName}}informers.ClusterInterface {
return {{.Group.PackageName}}informers.New(f, f.tweakListOptions)
}
{{end}}
Expand Down Expand Up @@ -485,13 +485,13 @@ type SharedScopedInformerFactory interface {
ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
{{range .groups}} {{.GoName}}() {{.Group.PackageName}}informers.Interface
{{range .groups}} {{.GroupGoName}}() {{.PackageName}}informers.Interface
{{end -}}
}
{{range .groups}}
func (f *sharedScopedInformerFactory) {{.GoName}}() {{.Group.PackageName}}informers.Interface {
func (f *sharedScopedInformerFactory) {{.GroupGoName}}() {{.PackageName}}informers.Interface {
return {{.Group.PackageName}}informers.NewScoped(f, f.namespace, f.tweakListOptions)
}
{{end}}
Expand Down
6 changes: 3 additions & 3 deletions pkg/internal/informergen/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import (

type Generic struct {
// Groups are the groups in this informer factory.
Groups []types.GroupVersionInfo
Groups []parser.Group

// GroupVersionKinds are all the kinds we need to support,indexed by group and version.
GroupVersionKinds map[types.Group]map[types.Version][]parser.Kind
GroupVersionKinds map[types.Group]map[parser.Version][]parser.Kind

// APIPackagePath is the root directory under which API types exist.
// e.g. "k8s.io/api"
Expand Down Expand Up @@ -78,7 +78,7 @@ import (
upstreaminformers "{{.singleClusterInformerPackagePath}}"
{{end -}}
{{range .groups}} {{.PackageAlias}} "{{$.apiPackagePath}}/{{.Group.PackageName}}/{{.Version.PackageName}}"
{{range .groups}} {{.GoPackageAlias}} "{{$.apiPackagePath}}/{{.Group.PackageName}}/{{.Version.PackageName}}"
{{end -}}
)
Expand Down
4 changes: 3 additions & 1 deletion pkg/internal/informergen/groupinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package informergen

import (
"io"
"strings"
"text/template"

"k8s.io/code-generator/cmd/client-gen/types"
Expand Down Expand Up @@ -49,6 +50,7 @@ func (g GroupInterface) WriteContent(w io.Writer) error {

m := map[string]interface{}{
"group": g.Group,
"packageName": strings.ReplaceAll(g.Group.PackageName(), "-", ""),
"packagePath": g.PackagePath,
"versions": g.Versions,
"useUpstreamInterfaces": g.UseUpstreamInterfaces,
Expand All @@ -62,7 +64,7 @@ var groupInterface = `
// Code generated by kcp code-generator. DO NOT EDIT.
package {{.group.Group.PackageName}}
package {{.packageName}}
import (
{{range .versions}} "{{$.packagePath}}/{{$.group.Group.PackageName}}/{{.PackageName}}"
Expand Down
Loading

0 comments on commit 472bbd5

Please sign in to comment.