From ed7ee37e85388dfa9c2afdaed57f7a7ce21e1694 Mon Sep 17 00:00:00 2001 From: Mangirdas Judeikis Date: Thu, 12 Dec 2024 17:31:55 +0200 Subject: [PATCH] Move to internal type and change variables to support dashed imports. Signed-off-by: Mangirdas Judeikis On-behalf-of: SAP mangirdas.judeikis@sap.com --- .../v1/clustertesttype.go | 0 .../v1/exampledashed_client.go | 0 .../v1/fake/clustertesttype.go | 0 .../v1/fake/exampledashed_client.go | 0 .../v1/fake/testtype.go | 0 .../v1/testtype.go | 0 .../example-dashed/v1/clustertesttype.go | 72 ++++++ .../example-dashed/v1/exampledashed_client.go | 95 +++++++ .../example-dashed/v1/fake/clustertesttype.go | 202 +++++++++++++++ .../v1/fake/exampledashed_client.go | 73 ++++++ .../typed/example-dashed/v1/fake/testtype.go | 238 ++++++++++++++++++ .../typed/example-dashed/v1/testtype.go | 86 +++++++ pkg/generators/clientgen/clientgen.go | 19 +- pkg/generators/informergen/informergen.go | 22 +- pkg/generators/listergen/listergen.go | 14 +- pkg/internal/clientgen/clientset.go | 14 +- pkg/internal/clientgen/fake_clientset.go | 5 +- pkg/internal/clientgen/fake_group.go | 4 +- pkg/internal/clientgen/fake_type.go | 3 +- pkg/internal/clientgen/group.go | 4 +- pkg/internal/clientgen/scheme.go | 9 +- pkg/internal/clientgen/type.go | 8 +- pkg/internal/informergen/factory.go | 10 +- pkg/internal/informergen/generic.go | 6 +- pkg/internal/informergen/groupinterface.go | 4 +- pkg/internal/informergen/informer.go | 4 +- pkg/internal/informergen/versioninterface.go | 4 +- pkg/internal/listergen/expansions.go | 4 +- pkg/internal/listergen/lister.go | 4 +- pkg/parser/types.go | 46 +++- 30 files changed, 875 insertions(+), 75 deletions(-) rename examples/pkg/kcp/clients/clientset/versioned/typed/{exampledashed => example-dashed}/v1/clustertesttype.go (100%) rename examples/pkg/kcp/clients/clientset/versioned/typed/{exampledashed => example-dashed}/v1/exampledashed_client.go (100%) rename examples/pkg/kcp/clients/clientset/versioned/typed/{exampledashed => example-dashed}/v1/fake/clustertesttype.go (100%) rename examples/pkg/kcp/clients/clientset/versioned/typed/{exampledashed => example-dashed}/v1/fake/exampledashed_client.go (100%) rename examples/pkg/kcp/clients/clientset/versioned/typed/{exampledashed => example-dashed}/v1/fake/testtype.go (100%) rename examples/pkg/kcp/clients/clientset/versioned/typed/{exampledashed => example-dashed}/v1/testtype.go (100%) create mode 100644 examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/clustertesttype.go create mode 100644 examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/exampledashed_client.go create mode 100644 examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/fake/clustertesttype.go create mode 100644 examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/fake/exampledashed_client.go create mode 100644 examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/fake/testtype.go create mode 100644 examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/testtype.go diff --git a/examples/pkg/kcp/clients/clientset/versioned/typed/exampledashed/v1/clustertesttype.go b/examples/pkg/kcp/clients/clientset/versioned/typed/example-dashed/v1/clustertesttype.go similarity index 100% rename from examples/pkg/kcp/clients/clientset/versioned/typed/exampledashed/v1/clustertesttype.go rename to examples/pkg/kcp/clients/clientset/versioned/typed/example-dashed/v1/clustertesttype.go diff --git a/examples/pkg/kcp/clients/clientset/versioned/typed/exampledashed/v1/exampledashed_client.go b/examples/pkg/kcp/clients/clientset/versioned/typed/example-dashed/v1/exampledashed_client.go similarity index 100% rename from examples/pkg/kcp/clients/clientset/versioned/typed/exampledashed/v1/exampledashed_client.go rename to examples/pkg/kcp/clients/clientset/versioned/typed/example-dashed/v1/exampledashed_client.go diff --git a/examples/pkg/kcp/clients/clientset/versioned/typed/exampledashed/v1/fake/clustertesttype.go b/examples/pkg/kcp/clients/clientset/versioned/typed/example-dashed/v1/fake/clustertesttype.go similarity index 100% rename from examples/pkg/kcp/clients/clientset/versioned/typed/exampledashed/v1/fake/clustertesttype.go rename to examples/pkg/kcp/clients/clientset/versioned/typed/example-dashed/v1/fake/clustertesttype.go diff --git a/examples/pkg/kcp/clients/clientset/versioned/typed/exampledashed/v1/fake/exampledashed_client.go b/examples/pkg/kcp/clients/clientset/versioned/typed/example-dashed/v1/fake/exampledashed_client.go similarity index 100% rename from examples/pkg/kcp/clients/clientset/versioned/typed/exampledashed/v1/fake/exampledashed_client.go rename to examples/pkg/kcp/clients/clientset/versioned/typed/example-dashed/v1/fake/exampledashed_client.go diff --git a/examples/pkg/kcp/clients/clientset/versioned/typed/exampledashed/v1/fake/testtype.go b/examples/pkg/kcp/clients/clientset/versioned/typed/example-dashed/v1/fake/testtype.go similarity index 100% rename from examples/pkg/kcp/clients/clientset/versioned/typed/exampledashed/v1/fake/testtype.go rename to examples/pkg/kcp/clients/clientset/versioned/typed/example-dashed/v1/fake/testtype.go diff --git a/examples/pkg/kcp/clients/clientset/versioned/typed/exampledashed/v1/testtype.go b/examples/pkg/kcp/clients/clientset/versioned/typed/example-dashed/v1/testtype.go similarity index 100% rename from examples/pkg/kcp/clients/clientset/versioned/typed/exampledashed/v1/testtype.go rename to examples/pkg/kcp/clients/clientset/versioned/typed/example-dashed/v1/testtype.go diff --git a/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/clustertesttype.go b/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/clustertesttype.go new file mode 100644 index 000000000..c44b7ac44 --- /dev/null +++ b/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/clustertesttype.go @@ -0,0 +1,72 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* +Copyright The KCP 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. +*/ + +// Code generated by kcp code-generator. DO NOT EDIT. + +package v1 + +import ( + "context" + + kcpclient "github.com/kcp-dev/apimachinery/v2/pkg/client" + "github.com/kcp-dev/logicalcluster/v3" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/watch" + + exampledashedv1 "acme.corp/pkg/apis/example-dashed/v1" + exampledashedv1client "acme.corp/pkg/generated/clientset/versioned/typed/example-dashed/v1" +) + +// ClusterTestTypesClusterGetter has a method to return a ClusterTestTypeClusterInterface. +// A group's cluster client should implement this interface. +type ClusterTestTypesClusterGetter interface { + ClusterTestTypes() ClusterTestTypeClusterInterface +} + +// ClusterTestTypeClusterInterface can operate on ClusterTestTypes across all clusters, +// or scope down to one cluster and return a exampledashedv1client.ClusterTestTypeInterface. +type ClusterTestTypeClusterInterface interface { + Cluster(logicalcluster.Path) exampledashedv1client.ClusterTestTypeInterface + List(ctx context.Context, opts metav1.ListOptions) (*exampledashedv1.ClusterTestTypeList, error) + Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) +} + +type clusterTestTypesClusterInterface struct { + clientCache kcpclient.Cache[*exampledashedv1client.ExampledashedV1Client] +} + +// Cluster scopes the client down to a particular cluster. +func (c *clusterTestTypesClusterInterface) Cluster(clusterPath logicalcluster.Path) exampledashedv1client.ClusterTestTypeInterface { + if clusterPath == logicalcluster.Wildcard { + panic("A specific cluster must be provided when scoping, not the wildcard.") + } + + return c.clientCache.ClusterOrDie(clusterPath).ClusterTestTypes() +} + +// List returns the entire collection of all ClusterTestTypes across all clusters. +func (c *clusterTestTypesClusterInterface) List(ctx context.Context, opts metav1.ListOptions) (*exampledashedv1.ClusterTestTypeList, error) { + return c.clientCache.ClusterOrDie(logicalcluster.Wildcard).ClusterTestTypes().List(ctx, opts) +} + +// Watch begins to watch all ClusterTestTypes across all clusters. +func (c *clusterTestTypesClusterInterface) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) { + return c.clientCache.ClusterOrDie(logicalcluster.Wildcard).ClusterTestTypes().Watch(ctx, opts) +} diff --git a/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/exampledashed_client.go b/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/exampledashed_client.go new file mode 100644 index 000000000..abd31bf4f --- /dev/null +++ b/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/exampledashed_client.go @@ -0,0 +1,95 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* +Copyright The KCP 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. +*/ + +// Code generated by kcp code-generator. DO NOT EDIT. + +package v1 + +import ( + "net/http" + + kcpclient "github.com/kcp-dev/apimachinery/v2/pkg/client" + "github.com/kcp-dev/logicalcluster/v3" + + "k8s.io/client-go/rest" + + exampledashedv1 "acme.corp/pkg/generated/clientset/versioned/typed/example-dashed/v1" +) + +type ExampledashedV1ClusterInterface interface { + ExampledashedV1ClusterScoper + TestTypesClusterGetter + ClusterTestTypesClusterGetter +} + +type ExampledashedV1ClusterScoper interface { + Cluster(logicalcluster.Path) exampledashedv1.ExampledashedV1Interface +} + +type ExampledashedV1ClusterClient struct { + clientCache kcpclient.Cache[*exampledashedv1.ExampledashedV1Client] +} + +func (c *ExampledashedV1ClusterClient) Cluster(clusterPath logicalcluster.Path) exampledashedv1.ExampledashedV1Interface { + if clusterPath == logicalcluster.Wildcard { + panic("A specific cluster must be provided when scoping, not the wildcard.") + } + return c.clientCache.ClusterOrDie(clusterPath) +} + +func (c *ExampledashedV1ClusterClient) TestTypes() TestTypeClusterInterface { + return &testTypesClusterInterface{clientCache: c.clientCache} +} + +func (c *ExampledashedV1ClusterClient) ClusterTestTypes() ClusterTestTypeClusterInterface { + return &clusterTestTypesClusterInterface{clientCache: c.clientCache} +} + +// NewForConfig creates a new ExampledashedV1ClusterClient for the given config. +// NewForConfig is equivalent to NewForConfigAndClient(c, httpClient), +// where httpClient was generated with rest.HTTPClientFor(c). +func NewForConfig(c *rest.Config) (*ExampledashedV1ClusterClient, error) { + client, err := rest.HTTPClientFor(c) + if err != nil { + return nil, err + } + return NewForConfigAndClient(c, client) +} + +// NewForConfigAndClient creates a new ExampledashedV1ClusterClient for the given config and http client. +// Note the http client provided takes precedence over the configured transport values. +func NewForConfigAndClient(c *rest.Config, h *http.Client) (*ExampledashedV1ClusterClient, error) { + cache := kcpclient.NewCache(c, h, &kcpclient.Constructor[*exampledashedv1.ExampledashedV1Client]{ + NewForConfigAndClient: exampledashedv1.NewForConfigAndClient, + }) + if _, err := cache.Cluster(logicalcluster.Name("root").Path()); err != nil { + return nil, err + } + return &ExampledashedV1ClusterClient{clientCache: cache}, nil +} + +// NewForConfigOrDie creates a new ExampledashedV1ClusterClient for the given config and +// panics if there is an error in the config. +func NewForConfigOrDie(c *rest.Config) *ExampledashedV1ClusterClient { + client, err := NewForConfig(c) + if err != nil { + panic(err) + } + return client +} diff --git a/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/fake/clustertesttype.go b/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/fake/clustertesttype.go new file mode 100644 index 000000000..0c236458f --- /dev/null +++ b/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/fake/clustertesttype.go @@ -0,0 +1,202 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* +Copyright The KCP 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. +*/ + +// Code generated by kcp code-generator. DO NOT EDIT. + +package fake + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/kcp-dev/logicalcluster/v3" + + kcptesting "github.com/kcp-dev/client-go/third_party/k8s.io/client-go/testing" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/watch" + "k8s.io/client-go/testing" + + exampledashedv1 "acme.corp/pkg/apis/example-dashed/v1" + applyconfigurationsexampledashedv1 "acme.corp/pkg/generated/applyconfigurations/example-dashed/v1" + exampledashedv1client "acme.corp/pkg/generated/clientset/versioned/typed/example-dashed/v1" +) + +var clusterTestTypesResource = schema.GroupVersionResource{Group: "example-dashed.some.corp", Version: "v1", Resource: "clustertesttypes"} +var clusterTestTypesKind = schema.GroupVersionKind{Group: "example-dashed.some.corp", Version: "v1", Kind: "ClusterTestType"} + +type clusterTestTypesClusterClient struct { + *kcptesting.Fake +} + +// Cluster scopes the client down to a particular cluster. +func (c *clusterTestTypesClusterClient) Cluster(clusterPath logicalcluster.Path) exampledashedv1client.ClusterTestTypeInterface { + if clusterPath == logicalcluster.Wildcard { + panic("A specific cluster must be provided when scoping, not the wildcard.") + } + + return &clusterTestTypesClient{Fake: c.Fake, ClusterPath: clusterPath} +} + +// List takes label and field selectors, and returns the list of ClusterTestTypes that match those selectors across all clusters. +func (c *clusterTestTypesClusterClient) List(ctx context.Context, opts metav1.ListOptions) (*exampledashedv1.ClusterTestTypeList, error) { + obj, err := c.Fake.Invokes(kcptesting.NewRootListAction(clusterTestTypesResource, clusterTestTypesKind, logicalcluster.Wildcard, opts), &exampledashedv1.ClusterTestTypeList{}) + if obj == nil { + return nil, err + } + + label, _, _ := testing.ExtractFromListOptions(opts) + if label == nil { + label = labels.Everything() + } + list := &exampledashedv1.ClusterTestTypeList{ListMeta: obj.(*exampledashedv1.ClusterTestTypeList).ListMeta} + for _, item := range obj.(*exampledashedv1.ClusterTestTypeList).Items { + if label.Matches(labels.Set(item.Labels)) { + list.Items = append(list.Items, item) + } + } + return list, err +} + +// Watch returns a watch.Interface that watches the requested ClusterTestTypes across all clusters. +func (c *clusterTestTypesClusterClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) { + return c.Fake.InvokesWatch(kcptesting.NewRootWatchAction(clusterTestTypesResource, logicalcluster.Wildcard, opts)) +} + +type clusterTestTypesClient struct { + *kcptesting.Fake + ClusterPath logicalcluster.Path +} + +func (c *clusterTestTypesClient) Create(ctx context.Context, clusterTestType *exampledashedv1.ClusterTestType, opts metav1.CreateOptions) (*exampledashedv1.ClusterTestType, error) { + obj, err := c.Fake.Invokes(kcptesting.NewRootCreateAction(clusterTestTypesResource, c.ClusterPath, clusterTestType), &exampledashedv1.ClusterTestType{}) + if obj == nil { + return nil, err + } + return obj.(*exampledashedv1.ClusterTestType), err +} + +func (c *clusterTestTypesClient) Update(ctx context.Context, clusterTestType *exampledashedv1.ClusterTestType, opts metav1.UpdateOptions) (*exampledashedv1.ClusterTestType, error) { + obj, err := c.Fake.Invokes(kcptesting.NewRootUpdateAction(clusterTestTypesResource, c.ClusterPath, clusterTestType), &exampledashedv1.ClusterTestType{}) + if obj == nil { + return nil, err + } + return obj.(*exampledashedv1.ClusterTestType), err +} + +func (c *clusterTestTypesClient) UpdateStatus(ctx context.Context, clusterTestType *exampledashedv1.ClusterTestType, opts metav1.UpdateOptions) (*exampledashedv1.ClusterTestType, error) { + obj, err := c.Fake.Invokes(kcptesting.NewRootUpdateSubresourceAction(clusterTestTypesResource, c.ClusterPath, "status", clusterTestType), &exampledashedv1.ClusterTestType{}) + if obj == nil { + return nil, err + } + return obj.(*exampledashedv1.ClusterTestType), err +} + +func (c *clusterTestTypesClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error { + _, err := c.Fake.Invokes(kcptesting.NewRootDeleteActionWithOptions(clusterTestTypesResource, c.ClusterPath, name, opts), &exampledashedv1.ClusterTestType{}) + return err +} + +func (c *clusterTestTypesClient) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error { + action := kcptesting.NewRootDeleteCollectionAction(clusterTestTypesResource, c.ClusterPath, listOpts) + + _, err := c.Fake.Invokes(action, &exampledashedv1.ClusterTestTypeList{}) + return err +} + +func (c *clusterTestTypesClient) Get(ctx context.Context, name string, options metav1.GetOptions) (*exampledashedv1.ClusterTestType, error) { + obj, err := c.Fake.Invokes(kcptesting.NewRootGetAction(clusterTestTypesResource, c.ClusterPath, name), &exampledashedv1.ClusterTestType{}) + if obj == nil { + return nil, err + } + return obj.(*exampledashedv1.ClusterTestType), err +} + +// List takes label and field selectors, and returns the list of ClusterTestTypes that match those selectors. +func (c *clusterTestTypesClient) List(ctx context.Context, opts metav1.ListOptions) (*exampledashedv1.ClusterTestTypeList, error) { + obj, err := c.Fake.Invokes(kcptesting.NewRootListAction(clusterTestTypesResource, clusterTestTypesKind, c.ClusterPath, opts), &exampledashedv1.ClusterTestTypeList{}) + if obj == nil { + return nil, err + } + + label, _, _ := testing.ExtractFromListOptions(opts) + if label == nil { + label = labels.Everything() + } + list := &exampledashedv1.ClusterTestTypeList{ListMeta: obj.(*exampledashedv1.ClusterTestTypeList).ListMeta} + for _, item := range obj.(*exampledashedv1.ClusterTestTypeList).Items { + if label.Matches(labels.Set(item.Labels)) { + list.Items = append(list.Items, item) + } + } + return list, err +} + +func (c *clusterTestTypesClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) { + return c.Fake.InvokesWatch(kcptesting.NewRootWatchAction(clusterTestTypesResource, c.ClusterPath, opts)) +} + +func (c *clusterTestTypesClient) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (*exampledashedv1.ClusterTestType, error) { + obj, err := c.Fake.Invokes(kcptesting.NewRootPatchSubresourceAction(clusterTestTypesResource, c.ClusterPath, name, pt, data, subresources...), &exampledashedv1.ClusterTestType{}) + if obj == nil { + return nil, err + } + return obj.(*exampledashedv1.ClusterTestType), err +} + +func (c *clusterTestTypesClient) Apply(ctx context.Context, applyConfiguration *applyconfigurationsexampledashedv1.ClusterTestTypeApplyConfiguration, opts metav1.ApplyOptions) (*exampledashedv1.ClusterTestType, error) { + if applyConfiguration == nil { + return nil, fmt.Errorf("applyConfiguration provided to Apply must not be nil") + } + data, err := json.Marshal(applyConfiguration) + if err != nil { + return nil, err + } + name := applyConfiguration.Name + if name == nil { + return nil, fmt.Errorf("applyConfiguration.Name must be provided to Apply") + } + obj, err := c.Fake.Invokes(kcptesting.NewRootPatchSubresourceAction(clusterTestTypesResource, c.ClusterPath, *name, types.ApplyPatchType, data), &exampledashedv1.ClusterTestType{}) + if obj == nil { + return nil, err + } + return obj.(*exampledashedv1.ClusterTestType), err +} + +func (c *clusterTestTypesClient) ApplyStatus(ctx context.Context, applyConfiguration *applyconfigurationsexampledashedv1.ClusterTestTypeApplyConfiguration, opts metav1.ApplyOptions) (*exampledashedv1.ClusterTestType, error) { + if applyConfiguration == nil { + return nil, fmt.Errorf("applyConfiguration provided to Apply must not be nil") + } + data, err := json.Marshal(applyConfiguration) + if err != nil { + return nil, err + } + name := applyConfiguration.Name + if name == nil { + return nil, fmt.Errorf("applyConfiguration.Name must be provided to Apply") + } + obj, err := c.Fake.Invokes(kcptesting.NewRootPatchSubresourceAction(clusterTestTypesResource, c.ClusterPath, *name, types.ApplyPatchType, data, "status"), &exampledashedv1.ClusterTestType{}) + if obj == nil { + return nil, err + } + return obj.(*exampledashedv1.ClusterTestType), err +} diff --git a/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/fake/exampledashed_client.go b/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/fake/exampledashed_client.go new file mode 100644 index 000000000..5e4267925 --- /dev/null +++ b/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/fake/exampledashed_client.go @@ -0,0 +1,73 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* +Copyright The KCP 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. +*/ + +// Code generated by kcp code-generator. DO NOT EDIT. + +package fake + +import ( + "github.com/kcp-dev/logicalcluster/v3" + + kcptesting "github.com/kcp-dev/client-go/third_party/k8s.io/client-go/testing" + "k8s.io/client-go/rest" + + exampledashedv1 "acme.corp/pkg/generated/clientset/versioned/typed/example-dashed/v1" + kcpexampledashedv1 "acme.corp/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1" +) + +var _ kcpexampledashedv1.ExampledashedV1ClusterInterface = (*ExampledashedV1ClusterClient)(nil) + +type ExampledashedV1ClusterClient struct { + *kcptesting.Fake +} + +func (c *ExampledashedV1ClusterClient) Cluster(clusterPath logicalcluster.Path) exampledashedv1.ExampledashedV1Interface { + if clusterPath == logicalcluster.Wildcard { + panic("A specific cluster must be provided when scoping, not the wildcard.") + } + return &ExampledashedV1Client{Fake: c.Fake, ClusterPath: clusterPath} +} + +func (c *ExampledashedV1ClusterClient) TestTypes() kcpexampledashedv1.TestTypeClusterInterface { + return &testTypesClusterClient{Fake: c.Fake} +} + +func (c *ExampledashedV1ClusterClient) ClusterTestTypes() kcpexampledashedv1.ClusterTestTypeClusterInterface { + return &clusterTestTypesClusterClient{Fake: c.Fake} +} + +var _ exampledashedv1.ExampledashedV1Interface = (*ExampledashedV1Client)(nil) + +type ExampledashedV1Client struct { + *kcptesting.Fake + ClusterPath logicalcluster.Path +} + +func (c *ExampledashedV1Client) RESTClient() rest.Interface { + var ret *rest.RESTClient + return ret +} + +func (c *ExampledashedV1Client) TestTypes(namespace string) exampledashedv1.TestTypeInterface { + return &testTypesClient{Fake: c.Fake, ClusterPath: c.ClusterPath, Namespace: namespace} +} + +func (c *ExampledashedV1Client) ClusterTestTypes() exampledashedv1.ClusterTestTypeInterface { + return &clusterTestTypesClient{Fake: c.Fake, ClusterPath: c.ClusterPath} +} diff --git a/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/fake/testtype.go b/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/fake/testtype.go new file mode 100644 index 000000000..678b8730f --- /dev/null +++ b/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/fake/testtype.go @@ -0,0 +1,238 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* +Copyright The KCP 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. +*/ + +// Code generated by kcp code-generator. DO NOT EDIT. + +package fake + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/kcp-dev/logicalcluster/v3" + + kcptesting "github.com/kcp-dev/client-go/third_party/k8s.io/client-go/testing" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/watch" + "k8s.io/client-go/testing" + + exampledashedv1 "acme.corp/pkg/apis/example-dashed/v1" + examplev1 "acme.corp/pkg/apis/example/v1" + applyconfigurationsexampledashedv1 "acme.corp/pkg/generated/applyconfigurations/example-dashed/v1" + exampledashedv1client "acme.corp/pkg/generated/clientset/versioned/typed/example-dashed/v1" + kcpexampledashedv1 "acme.corp/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1" +) + +var testTypesResource = schema.GroupVersionResource{Group: "example-dashed.some.corp", Version: "v1", Resource: "testtypes"} +var testTypesKind = schema.GroupVersionKind{Group: "example-dashed.some.corp", Version: "v1", Kind: "TestType"} + +type testTypesClusterClient struct { + *kcptesting.Fake +} + +// Cluster scopes the client down to a particular cluster. +func (c *testTypesClusterClient) Cluster(clusterPath logicalcluster.Path) kcpexampledashedv1.TestTypesNamespacer { + if clusterPath == logicalcluster.Wildcard { + panic("A specific cluster must be provided when scoping, not the wildcard.") + } + + return &testTypesNamespacer{Fake: c.Fake, ClusterPath: clusterPath} +} + +// List takes label and field selectors, and returns the list of TestTypes that match those selectors across all clusters. +func (c *testTypesClusterClient) List(ctx context.Context, opts metav1.ListOptions) (*exampledashedv1.TestTypeList, error) { + obj, err := c.Fake.Invokes(kcptesting.NewListAction(testTypesResource, testTypesKind, logicalcluster.Wildcard, metav1.NamespaceAll, opts), &exampledashedv1.TestTypeList{}) + if obj == nil { + return nil, err + } + + label, _, _ := testing.ExtractFromListOptions(opts) + if label == nil { + label = labels.Everything() + } + list := &exampledashedv1.TestTypeList{ListMeta: obj.(*exampledashedv1.TestTypeList).ListMeta} + for _, item := range obj.(*exampledashedv1.TestTypeList).Items { + if label.Matches(labels.Set(item.Labels)) { + list.Items = append(list.Items, item) + } + } + return list, err +} + +// Watch returns a watch.Interface that watches the requested TestTypes across all clusters. +func (c *testTypesClusterClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) { + return c.Fake.InvokesWatch(kcptesting.NewWatchAction(testTypesResource, logicalcluster.Wildcard, metav1.NamespaceAll, opts)) +} + +type testTypesNamespacer struct { + *kcptesting.Fake + ClusterPath logicalcluster.Path +} + +func (n *testTypesNamespacer) Namespace(namespace string) exampledashedv1client.TestTypeInterface { + return &testTypesClient{Fake: n.Fake, ClusterPath: n.ClusterPath, Namespace: namespace} +} + +type testTypesClient struct { + *kcptesting.Fake + ClusterPath logicalcluster.Path + Namespace string +} + +func (c *testTypesClient) Create(ctx context.Context, testType *exampledashedv1.TestType, opts metav1.CreateOptions) (*exampledashedv1.TestType, error) { + obj, err := c.Fake.Invokes(kcptesting.NewCreateAction(testTypesResource, c.ClusterPath, c.Namespace, testType), &exampledashedv1.TestType{}) + if obj == nil { + return nil, err + } + return obj.(*exampledashedv1.TestType), err +} + +func (c *testTypesClient) Update(ctx context.Context, testType *exampledashedv1.TestType, opts metav1.UpdateOptions) (*exampledashedv1.TestType, error) { + obj, err := c.Fake.Invokes(kcptesting.NewUpdateAction(testTypesResource, c.ClusterPath, c.Namespace, testType), &exampledashedv1.TestType{}) + if obj == nil { + return nil, err + } + return obj.(*exampledashedv1.TestType), err +} + +func (c *testTypesClient) UpdateStatus(ctx context.Context, testType *exampledashedv1.TestType, opts metav1.UpdateOptions) (*exampledashedv1.TestType, error) { + obj, err := c.Fake.Invokes(kcptesting.NewUpdateSubresourceAction(testTypesResource, c.ClusterPath, "status", c.Namespace, testType), &exampledashedv1.TestType{}) + if obj == nil { + return nil, err + } + return obj.(*exampledashedv1.TestType), err +} + +func (c *testTypesClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error { + _, err := c.Fake.Invokes(kcptesting.NewDeleteActionWithOptions(testTypesResource, c.ClusterPath, c.Namespace, name, opts), &exampledashedv1.TestType{}) + return err +} + +func (c *testTypesClient) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error { + action := kcptesting.NewDeleteCollectionAction(testTypesResource, c.ClusterPath, c.Namespace, listOpts) + + _, err := c.Fake.Invokes(action, &exampledashedv1.TestTypeList{}) + return err +} + +func (c *testTypesClient) Get(ctx context.Context, name string, options metav1.GetOptions) (*exampledashedv1.TestType, error) { + obj, err := c.Fake.Invokes(kcptesting.NewGetAction(testTypesResource, c.ClusterPath, c.Namespace, name), &exampledashedv1.TestType{}) + if obj == nil { + return nil, err + } + return obj.(*exampledashedv1.TestType), err +} + +// List takes label and field selectors, and returns the list of TestTypes that match those selectors. +func (c *testTypesClient) List(ctx context.Context, opts metav1.ListOptions) (*exampledashedv1.TestTypeList, error) { + obj, err := c.Fake.Invokes(kcptesting.NewListAction(testTypesResource, testTypesKind, c.ClusterPath, c.Namespace, opts), &exampledashedv1.TestTypeList{}) + if obj == nil { + return nil, err + } + + label, _, _ := testing.ExtractFromListOptions(opts) + if label == nil { + label = labels.Everything() + } + list := &exampledashedv1.TestTypeList{ListMeta: obj.(*exampledashedv1.TestTypeList).ListMeta} + for _, item := range obj.(*exampledashedv1.TestTypeList).Items { + if label.Matches(labels.Set(item.Labels)) { + list.Items = append(list.Items, item) + } + } + return list, err +} + +func (c *testTypesClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) { + return c.Fake.InvokesWatch(kcptesting.NewWatchAction(testTypesResource, c.ClusterPath, c.Namespace, opts)) +} + +func (c *testTypesClient) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (*exampledashedv1.TestType, error) { + obj, err := c.Fake.Invokes(kcptesting.NewPatchSubresourceAction(testTypesResource, c.ClusterPath, c.Namespace, name, pt, data, subresources...), &exampledashedv1.TestType{}) + if obj == nil { + return nil, err + } + return obj.(*exampledashedv1.TestType), err +} + +func (c *testTypesClient) Apply(ctx context.Context, applyConfiguration *applyconfigurationsexampledashedv1.TestTypeApplyConfiguration, opts metav1.ApplyOptions) (*exampledashedv1.TestType, error) { + if applyConfiguration == nil { + return nil, fmt.Errorf("applyConfiguration provided to Apply must not be nil") + } + data, err := json.Marshal(applyConfiguration) + if err != nil { + return nil, err + } + name := applyConfiguration.Name + if name == nil { + return nil, fmt.Errorf("applyConfiguration.Name must be provided to Apply") + } + obj, err := c.Fake.Invokes(kcptesting.NewPatchSubresourceAction(testTypesResource, c.ClusterPath, c.Namespace, *name, types.ApplyPatchType, data), &exampledashedv1.TestType{}) + if obj == nil { + return nil, err + } + return obj.(*exampledashedv1.TestType), err +} + +func (c *testTypesClient) ApplyStatus(ctx context.Context, applyConfiguration *applyconfigurationsexampledashedv1.TestTypeApplyConfiguration, opts metav1.ApplyOptions) (*exampledashedv1.TestType, error) { + if applyConfiguration == nil { + return nil, fmt.Errorf("applyConfiguration provided to Apply must not be nil") + } + data, err := json.Marshal(applyConfiguration) + if err != nil { + return nil, err + } + name := applyConfiguration.Name + if name == nil { + return nil, fmt.Errorf("applyConfiguration.Name must be provided to Apply") + } + obj, err := c.Fake.Invokes(kcptesting.NewPatchSubresourceAction(testTypesResource, c.ClusterPath, c.Namespace, *name, types.ApplyPatchType, data, "status"), &exampledashedv1.TestType{}) + if obj == nil { + return nil, err + } + return obj.(*exampledashedv1.TestType), err +} + +func (c *testTypesClient) CreateField(ctx context.Context, testTypeName string, field *examplev1.Field, opts metav1.CreateOptions) (*examplev1.Field, error) { + obj, err := c.Fake.Invokes(kcptesting.NewCreateSubresourceAction(testTypesResource, c.ClusterPath, testTypeName, "field", c.Namespace, field), &examplev1.Field{}) + if obj == nil { + return nil, err + } + return obj.(*examplev1.Field), err +} + +func (c *testTypesClient) UpdateField(ctx context.Context, testTypeName string, field *examplev1.Field, opts metav1.UpdateOptions) (*examplev1.Field, error) { + obj, err := c.Fake.Invokes(kcptesting.NewUpdateSubresourceAction(testTypesResource, c.ClusterPath, "field", c.Namespace, field), &examplev1.Field{}) + if obj == nil { + return nil, err + } + return obj.(*examplev1.Field), err +} + +func (c *testTypesClient) GetField(ctx context.Context, testTypeName string, options metav1.GetOptions) (*examplev1.Field, error) { + obj, err := c.Fake.Invokes(kcptesting.NewGetSubresourceAction(testTypesResource, c.ClusterPath, "field", c.Namespace, testTypeName), &examplev1.Field{}) + if obj == nil { + return nil, err + } + return obj.(*examplev1.Field), err +} diff --git a/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/testtype.go b/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/testtype.go new file mode 100644 index 000000000..0835c7995 --- /dev/null +++ b/examples/pkg/kcpexisting/clients/clientset/versioned/typed/example-dashed/v1/testtype.go @@ -0,0 +1,86 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* +Copyright The KCP 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. +*/ + +// Code generated by kcp code-generator. DO NOT EDIT. + +package v1 + +import ( + "context" + + kcpclient "github.com/kcp-dev/apimachinery/v2/pkg/client" + "github.com/kcp-dev/logicalcluster/v3" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/watch" + + exampledashedv1 "acme.corp/pkg/apis/example-dashed/v1" + exampledashedv1client "acme.corp/pkg/generated/clientset/versioned/typed/example-dashed/v1" +) + +// TestTypesClusterGetter has a method to return a TestTypeClusterInterface. +// A group's cluster client should implement this interface. +type TestTypesClusterGetter interface { + TestTypes() TestTypeClusterInterface +} + +// TestTypeClusterInterface can operate on TestTypes across all clusters, +// or scope down to one cluster and return a TestTypesNamespacer. +type TestTypeClusterInterface interface { + Cluster(logicalcluster.Path) TestTypesNamespacer + List(ctx context.Context, opts metav1.ListOptions) (*exampledashedv1.TestTypeList, error) + Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) +} + +type testTypesClusterInterface struct { + clientCache kcpclient.Cache[*exampledashedv1client.ExampledashedV1Client] +} + +// Cluster scopes the client down to a particular cluster. +func (c *testTypesClusterInterface) Cluster(clusterPath logicalcluster.Path) TestTypesNamespacer { + if clusterPath == logicalcluster.Wildcard { + panic("A specific cluster must be provided when scoping, not the wildcard.") + } + + return &testTypesNamespacer{clientCache: c.clientCache, clusterPath: clusterPath} +} + +// List returns the entire collection of all TestTypes across all clusters. +func (c *testTypesClusterInterface) List(ctx context.Context, opts metav1.ListOptions) (*exampledashedv1.TestTypeList, error) { + return c.clientCache.ClusterOrDie(logicalcluster.Wildcard).TestTypes(metav1.NamespaceAll).List(ctx, opts) +} + +// Watch begins to watch all TestTypes across all clusters. +func (c *testTypesClusterInterface) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) { + return c.clientCache.ClusterOrDie(logicalcluster.Wildcard).TestTypes(metav1.NamespaceAll).Watch(ctx, opts) +} + +// TestTypesNamespacer can scope to objects within a namespace, returning a exampledashedv1client.TestTypeInterface. +type TestTypesNamespacer interface { + Namespace(string) exampledashedv1client.TestTypeInterface +} + +type testTypesNamespacer struct { + clientCache kcpclient.Cache[*exampledashedv1client.ExampledashedV1Client] + clusterPath logicalcluster.Path +} + +func (n *testTypesNamespacer) Namespace(namespace string) exampledashedv1client.TestTypeInterface { + return n.clientCache.ClusterOrDie(n.clusterPath).TestTypes(namespace) +} diff --git a/pkg/generators/clientgen/clientgen.go b/pkg/generators/clientgen/clientgen.go index d6d10a229..62767e04a 100644 --- a/pkg/generators/clientgen/clientgen.go +++ b/pkg/generators/clientgen/clientgen.go @@ -154,7 +154,7 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error { for group, versions := range groupVersionKinds { for version, kinds := range versions { - groupDir := filepath.Join(clientsetDir, "typed", group.PackageName(), version.PackageName()) + groupDir := filepath.Join(clientsetDir, "typed", group.PackagePath(), version.PackageName()) outputFile := filepath.Join(groupDir, group.PackageName()+"_client.go") logger := logger.WithValues( "group", group.String(), @@ -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)) @@ -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), } } diff --git a/pkg/generators/informergen/informergen.go b/pkg/generators/informergen/informergen.go index 157585c87..40240f6b8 100644 --- a/pkg/generators/informergen/informergen.go +++ b/pkg/generators/informergen/informergen.go @@ -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 } @@ -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(), @@ -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)) @@ -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), "-", ""), } } diff --git a/pkg/generators/listergen/listergen.go b/pkg/generators/listergen/listergen.go index 0aa7ebe48..2576c08af 100644 --- a/pkg/generators/listergen/listergen.go +++ b/pkg/generators/listergen/listergen.go @@ -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(), @@ -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), "-", ""), } } diff --git a/pkg/internal/clientgen/clientset.go b/pkg/internal/clientgen/clientset.go index 943a15fbb..7791c2e43 100644 --- a/pkg/internal/clientgen/clientset.go +++ b/pkg/internal/clientgen/clientset.go @@ -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" ) @@ -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 @@ -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, @@ -51,7 +51,7 @@ var clientset = ` // Code generated by kcp code-generator. DO NOT EDIT. -package {{.name}} +package {{.packageName}} import ( "fmt" @@ -66,14 +66,14 @@ 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 -}} } @@ -81,7 +81,7 @@ type ClusterInterface interface { 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 -}} } diff --git a/pkg/internal/clientgen/fake_clientset.go b/pkg/internal/clientgen/fake_clientset.go index 511f61e70..1173e04eb 100644 --- a/pkg/internal/clientgen/fake_clientset.go +++ b/pkg/internal/clientgen/fake_clientset.go @@ -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" ) @@ -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 diff --git a/pkg/internal/clientgen/fake_group.go b/pkg/internal/clientgen/fake_group.go index cd3b96b0b..e2536b1e5 100644 --- a/pkg/internal/clientgen/fake_group.go +++ b/pkg/internal/clientgen/fake_group.go @@ -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 diff --git a/pkg/internal/clientgen/fake_type.go b/pkg/internal/clientgen/fake_type.go index c52abf527..2a62914e4 100644 --- a/pkg/internal/clientgen/fake_type.go +++ b/pkg/internal/clientgen/fake_type.go @@ -8,7 +8,6 @@ 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" @@ -16,7 +15,7 @@ import ( 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 diff --git a/pkg/internal/clientgen/group.go b/pkg/internal/clientgen/group.go index 9421c1733..f0b065b2b 100644 --- a/pkg/internal/clientgen/group.go +++ b/pkg/internal/clientgen/group.go @@ -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 diff --git a/pkg/internal/clientgen/scheme.go b/pkg/internal/clientgen/scheme.go index 6b5524e35..278dcd7f6 100644 --- a/pkg/internal/clientgen/scheme.go +++ b/pkg/internal/clientgen/scheme.go @@ -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" @@ -51,7 +50,7 @@ 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 -}} ) @@ -59,7 +58,7 @@ 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 -}} } diff --git a/pkg/internal/clientgen/type.go b/pkg/internal/clientgen/type.go index bb5a95e33..2e0ed611e 100644 --- a/pkg/internal/clientgen/type.go +++ b/pkg/internal/clientgen/type.go @@ -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 @@ -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. diff --git a/pkg/internal/informergen/factory.go b/pkg/internal/informergen/factory.go index 9563eeaa9..31ada8247 100644 --- a/pkg/internal/informergen/factory.go +++ b/pkg/internal/informergen/factory.go @@ -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" @@ -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}} @@ -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}} diff --git a/pkg/internal/informergen/generic.go b/pkg/internal/informergen/generic.go index 18ce9b072..d43b7eaa8 100644 --- a/pkg/internal/informergen/generic.go +++ b/pkg/internal/informergen/generic.go @@ -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" @@ -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 -}} ) diff --git a/pkg/internal/informergen/groupinterface.go b/pkg/internal/informergen/groupinterface.go index 9848d4336..110a93fd9 100644 --- a/pkg/internal/informergen/groupinterface.go +++ b/pkg/internal/informergen/groupinterface.go @@ -18,6 +18,7 @@ package informergen import ( "io" + "strings" "text/template" "k8s.io/code-generator/cmd/client-gen/types" @@ -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, @@ -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}}" diff --git a/pkg/internal/informergen/informer.go b/pkg/internal/informergen/informer.go index 91b2b35f8..d19835a82 100644 --- a/pkg/internal/informergen/informer.go +++ b/pkg/internal/informergen/informer.go @@ -20,14 +20,12 @@ import ( "io" "text/template" - "k8s.io/code-generator/cmd/client-gen/types" - "github.com/kcp-dev/code-generator/v2/pkg/parser" ) type Informer struct { // Group is the group in this informer. - Group types.GroupVersionInfo + Group parser.Group // Kind is the kind in this file. Kind parser.Kind diff --git a/pkg/internal/informergen/versioninterface.go b/pkg/internal/informergen/versioninterface.go index fb20d35ac..41905a2bf 100644 --- a/pkg/internal/informergen/versioninterface.go +++ b/pkg/internal/informergen/versioninterface.go @@ -18,6 +18,7 @@ package informergen import ( "io" + "strings" "text/template" "k8s.io/code-generator/cmd/client-gen/types" @@ -51,6 +52,7 @@ func (v *VersionInterface) WriteContent(w io.Writer) error { "version": v.Version, "kinds": v.Kinds, "packagePath": v.PackagePath, + "packageName": strings.ReplaceAll(v.Version.PackageName(), "-", ""), "useUpstreamInterfaces": v.UseUpstreamInterfaces, } return templ.Execute(w, m) @@ -62,7 +64,7 @@ var versionInterfaceTemplate = ` // Code generated by kcp code-generator. DO NOT EDIT. -package {{.version.PackageName}} +package {{.packageName}} import ( "{{.packagePath}}/internalinterfaces" diff --git a/pkg/internal/listergen/expansions.go b/pkg/internal/listergen/expansions.go index 13edd29ab..5c4f3089c 100644 --- a/pkg/internal/listergen/expansions.go +++ b/pkg/internal/listergen/expansions.go @@ -4,8 +4,6 @@ import ( "io" "text/template" - clientgentypes "k8s.io/code-generator/cmd/client-gen/types" - "github.com/kcp-dev/code-generator/v2/pkg/parser" ) @@ -13,7 +11,7 @@ type Expansions struct { // Group is: // - the name of the API group, e.g. "authorization", // - the version and package path of the API, e.g. "v1" and "k8s.io/api/rbac/v1" - Group clientgentypes.GroupVersionInfo + Group parser.Group // Kind is the kind for which we are generating listers, e.g. "ClusterRole" Kind parser.Kind diff --git a/pkg/internal/listergen/lister.go b/pkg/internal/listergen/lister.go index 48ff2416c..b1755a3e5 100644 --- a/pkg/internal/listergen/lister.go +++ b/pkg/internal/listergen/lister.go @@ -4,8 +4,6 @@ import ( "io" "text/template" - clientgentypes "k8s.io/code-generator/cmd/client-gen/types" - "github.com/kcp-dev/code-generator/v2/pkg/parser" ) @@ -13,7 +11,7 @@ type Lister struct { // Group is: // - the name of the API group, e.g. "authorization", // - the version and package path of the API, e.g. "v1" and "k8s.io/api/rbac/v1" - Group clientgentypes.GroupVersionInfo + Group parser.Group // Kind is the kind for which we are generating listers, e.g. "ClusterRole" Kind parser.Kind diff --git a/pkg/parser/types.go b/pkg/parser/types.go index 00fbd86e5..cd98ae610 100644 --- a/pkg/parser/types.go +++ b/pkg/parser/types.go @@ -17,6 +17,8 @@ limitations under the License. package parser import ( + "strings" + "k8s.io/apimachinery/pkg/util/sets" "k8s.io/code-generator/cmd/client-gen/types" @@ -34,7 +36,32 @@ type Kind struct { type Group struct { types.Group - GoName string + GoName string `marker:",+groupGoName"` + Version Version + PackageAlias string + LowerCaseGroupGoName string +} + +func (g Group) GoPackageAlias() string { + if g.PackageAlias == "" { + panic("PackageAlias is empty. Programmer error.") + } + return strings.ReplaceAll(g.PackageAlias, "-", "") +} + +func (g Group) GroupGoName() string { + if g.GoName == "" { + panic("GroupGoName is empty. Programmer error.") + } + return strings.ReplaceAll(g.GoName, "-", "") // Code base is litterate with either GoName or GroupGoName. We should unify this. +} + +func (g Group) PackagePath() string { + return strings.ToLower(g.Group.PackageName()) +} + +func (g Group) PackageName() string { + return strings.ToLower(strings.ReplaceAll(g.Group.PackageName(), "-", "")) } func (k *Kind) Plural() string { @@ -53,6 +80,23 @@ func (k *Kind) SupportsListWatch() bool { return k.SupportedVerbs.HasAll("list", "watch") } +type Version string + +func (v Version) String() string { + return string(v) +} + +func (v Version) NonEmpty() string { + if v == "" { + return "internalVersion" + } + return v.String() +} + +func (v Version) PackageName() string { + return strings.ToLower(v.NonEmpty()) +} + // TODO(skuznets): // add an e2e for a kind that has no verbs, but uses an extension for something // then ensure we add in fake_type.go entries for the extension