diff --git a/codegen/cmd/injection-gen/generators/namer.go b/codegen/cmd/injection-gen/generators/namer.go new file mode 100644 index 0000000000..7e7d61a55a --- /dev/null +++ b/codegen/cmd/injection-gen/generators/namer.go @@ -0,0 +1,58 @@ +/* +Copyright 2016 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 generators + +import ( + "k8s.io/gengo/namer" + "k8s.io/gengo/types" +) + +// TagOverrideNamer is a namer which pulls names from a given tag, if specified, +// and otherwise falls back to a different namer. +type tagOverrideNamer struct { + tagName string + fallback namer.Namer +} + +// Name returns the tag value if it exists. It no tag was found the fallback namer will be used +func (n *tagOverrideNamer) Name(t *types.Type) string { + if nameOverride := extractTag(n.tagName, append(t.SecondClosestCommentLines, t.CommentLines...)); nameOverride != "" { + return nameOverride + } + + return n.fallback.Name(t) +} + +// NewTagOverrideNamer creates a namer.Namer which uses the contents of the given tag as +// the name, or falls back to another Namer if the tag is not present. +func newTagOverrideNamer(tagName string, fallback namer.Namer) namer.Namer { + return &tagOverrideNamer{ + tagName: tagName, + fallback: fallback, + } +} + +// extractTag gets the comment-tags for the key. If the tag did not exist, it +// returns the empty string. +func extractTag(key string, lines []string) string { + val, present := types.ExtractCommentTags("+", lines)[key] + if !present || len(val) < 1 { + return "" + } + + return val[0] +} diff --git a/codegen/cmd/injection-gen/generators/namesystems.go b/codegen/cmd/injection-gen/generators/namesystems.go index a99e1957ce..7c1b1ed8ad 100644 --- a/codegen/cmd/injection-gen/generators/namesystems.go +++ b/codegen/cmd/injection-gen/generators/namesystems.go @@ -19,7 +19,6 @@ package generators import ( "strings" - codegennamer "k8s.io/code-generator/pkg/namer" "k8s.io/gengo/namer" "k8s.io/gengo/types" ) @@ -47,7 +46,7 @@ func NameSystems() namer.NameSystems { "publicPlural": publicPluralNamer, "allLowercasePlural": namer.NewAllLowercasePluralNamer(pluralExceptions), "lowercaseSingular": &lowercaseSingularNamer{}, - "apiGroup": codegennamer.NewTagOverrideNamer("publicPlural", publicPluralNamer), + "apiGroup": newTagOverrideNamer("publicPlural", publicPluralNamer), "versionedClientset": &versionedClientsetNamer{public: publicNamer}, } } diff --git a/go.mod b/go.mod index a531e60d95..6d80500b22 100644 --- a/go.mod +++ b/go.mod @@ -36,7 +36,7 @@ require ( k8s.io/apimachinery v0.30.3 k8s.io/client-go v0.30.3 k8s.io/code-generator v0.30.3 - k8s.io/gengo v0.0.0-20240129211411-f967bbeff4b4 + k8s.io/gengo v0.0.0-20240404160639-a0386bf69313 k8s.io/klog/v2 v2.130.1 k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 knative.dev/hack v0.0.0-20240801232131-441a19fc9ead @@ -88,7 +88,7 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20240808171019-573a1156607a // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect - k8s.io/gengo/v2 v2.0.0-20240404160639-a0386bf69313 // indirect + k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 // indirect k8s.io/kube-openapi v0.0.0-20240808142205-8e686545bdb8 // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect diff --git a/go.sum b/go.sum index b6297772d3..e350827b36 100644 --- a/go.sum +++ b/go.sum @@ -712,10 +712,10 @@ k8s.io/client-go v0.30.3 h1:bHrJu3xQZNXIi8/MoxYtZBBWQQXwy16zqJwloXXfD3k= k8s.io/client-go v0.30.3/go.mod h1:8d4pf8vYu665/kUbsxWAQ/JDBNWqfFeZnvFiVdmx89U= k8s.io/code-generator v0.30.3 h1:bmtnLJKagDS5f5uOEpLyJiDfIMKXGMKgOLBdde+w0Mc= k8s.io/code-generator v0.30.3/go.mod h1:PFgBiv+miFV7TZYp+RXgROkhA+sWYZ+mtpbMLofMke8= -k8s.io/gengo v0.0.0-20240129211411-f967bbeff4b4 h1:izq7u3SJBdOAuA5YYe1/PIp9jczrih/jGlKRRt0G7bQ= -k8s.io/gengo v0.0.0-20240129211411-f967bbeff4b4/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E= -k8s.io/gengo/v2 v2.0.0-20240404160639-a0386bf69313 h1:bKcdZJOPICVmIIuaM9+MXmapE94dn5AYv5ODs1jA43o= -k8s.io/gengo/v2 v2.0.0-20240404160639-a0386bf69313/go.mod h1:VH3AT8AaQOqiGjMF9p0/IM1Dj+82ZwjfxUP1IxaHE+8= +k8s.io/gengo v0.0.0-20240404160639-a0386bf69313 h1:wBIDZID8ju9pwOiLlV22YYKjFGtiNSWgHf5CnKLRUuM= +k8s.io/gengo v0.0.0-20240404160639-a0386bf69313/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E= +k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 h1:NGrVE502P0s0/1hudf8zjgwki1X/TByhmAoILTarmzo= +k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70/go.mod h1:VH3AT8AaQOqiGjMF9p0/IM1Dj+82ZwjfxUP1IxaHE+8= k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y= k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk= k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= diff --git a/hack/update-codegen.sh b/hack/update-codegen.sh index 2fb36989fe..e56abb7fc4 100755 --- a/hack/update-codegen.sh +++ b/hack/update-codegen.sh @@ -41,7 +41,14 @@ ${REPO_ROOT_DIR}/hack/generate-knative.sh "injection" \ # Based on: https://github.com/kubernetes/kubernetes/blob/8ddabd0da5cc54761f3216c08e99fa1a9f7ee2c5/hack/lib/init.sh#L116 # The '-path' is a hack to workaround the lack of portable `-depth 2`. K8S_TYPES=$(find ./vendor/k8s.io/api -type d -path '*/*/*/*/*/*' | cut -d'/' -f 5-6 | sort | sed 's@/@:@g' | - grep -v "admission:" | grep -v "imagepolicy:" | grep -v "abac:" | grep -v "componentconfig:") + grep -v "abac:" | \ + grep -v "admission:" | \ + grep -v "admissionregistration:" \ + grep -v "componentconfig:" | \ + grep -v "imagepolicy:" | \ + grep -v "resource:" | \ + grep -v "storagemigration:" \ +) OUTPUT_PKG="knative.dev/pkg/client/injection/kube" \ VERSIONED_CLIENTSET_PKG="k8s.io/client-go/kubernetes" \ @@ -65,24 +72,19 @@ VERSIONED_CLIENTSET_PKG="k8s.io/apiextensions-apiserver/pkg/client/clientset/cli group "Knative Codegen" # Only deepcopy the Duck types, as they are not real resources. -${CODEGEN_PKG}/generate-groups.sh "deepcopy" \ - knative.dev/pkg/client knative.dev/pkg/apis \ - "duck:v1alpha1,v1beta1,v1" \ - --go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt - -# Depends on generate-groups.sh to install bin/deepcopy-gen -go run k8s.io/code-generator/cmd/deepcopy-gen --input-dirs \ - $(echo \ +go run k8s.io/code-generator/cmd/deepcopy-gen \ + --output-file zz_generated.deepcopy.go \ + --go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt \ knative.dev/pkg/apis \ + knative.dev/pkg/apis/duck/v1alpha1 \ + knative.dev/pkg/apis/duck/v1beta1 \ + knative.dev/pkg/apis/duck/v1 \ knative.dev/pkg/tracker \ knative.dev/pkg/logging \ knative.dev/pkg/metrics \ knative.dev/pkg/testing \ knative.dev/pkg/testing/duck \ - knative.dev/pkg/webhook/resourcesemantics/conversion/internal \ - | sed "s/ /,/g") \ - -O zz_generated.deepcopy \ - --go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt + knative.dev/pkg/webhook/resourcesemantics/conversion/internal group "Update deps post-codegen" diff --git a/vendor/k8s.io/gengo/v2/namer/namer.go b/vendor/k8s.io/gengo/v2/namer/namer.go index 37877eb492..e82fe66adc 100644 --- a/vendor/k8s.io/gengo/v2/namer/namer.go +++ b/vendor/k8s.io/gengo/v2/namer/namer.go @@ -281,12 +281,12 @@ func (ns *NameStrategy) Name(t *types.Type) string { case types.Func: // TODO: add to name test parts := []string{"Func"} - for _, param := range t.Signature.Parameters { - parts = append(parts, ns.removePrefixAndSuffix(ns.Name(param.Type))) + for _, pt := range t.Signature.Parameters { + parts = append(parts, ns.removePrefixAndSuffix(ns.Name(pt))) } parts = append(parts, "Returns") - for _, result := range t.Signature.Results { - parts = append(parts, ns.removePrefixAndSuffix(ns.Name(result.Type))) + for _, rt := range t.Signature.Results { + parts = append(parts, ns.removePrefixAndSuffix(ns.Name(rt))) } name = ns.Join(ns.Prefix, parts, ns.Suffix) default: @@ -374,12 +374,12 @@ func (r *rawNamer) Name(t *types.Type) string { case types.Func: // TODO: add to name test params := []string{} - for _, param := range t.Signature.Parameters { - params = append(params, r.Name(param.Type)) + for _, pt := range t.Signature.Parameters { + params = append(params, r.Name(pt)) } results := []string{} - for _, result := range t.Signature.Results { - results = append(results, r.Name(result.Type)) + for _, rt := range t.Signature.Results { + results = append(results, r.Name(rt)) } name = "func(" + strings.Join(params, ",") + ")" if len(results) == 1 { diff --git a/vendor/k8s.io/gengo/v2/parser/parse.go b/vendor/k8s.io/gengo/v2/parser/parse.go index e7acf4954d..a5993d1639 100644 --- a/vendor/k8s.io/gengo/v2/parser/parse.go +++ b/vendor/k8s.io/gengo/v2/parser/parse.go @@ -587,16 +587,9 @@ func goNameToName(in string) types.Name { return types.Name{Name: in} } - // There may be '.' characters within a generic. Temporarily remove - // the generic. - genericIndex := strings.IndexRune(in, '[') - if genericIndex == -1 { - genericIndex = len(in) - } - // Otherwise, if there are '.' characters present, the name has a // package path in front. - nameParts := strings.Split(in[:genericIndex], ".") + nameParts := strings.Split(in, ".") name := types.Name{Name: in} if n := len(nameParts); n >= 2 { // The final "." is the name of the type--previous ones must @@ -609,16 +602,12 @@ func goNameToName(in string) types.Name { func (p *Parser) convertSignature(u types.Universe, t *gotypes.Signature) *types.Signature { signature := &types.Signature{} for i := 0; i < t.Params().Len(); i++ { - signature.Parameters = append(signature.Parameters, &types.ParamResult{ - Name: t.Params().At(i).Name(), - Type: p.walkType(u, nil, t.Params().At(i).Type()), - }) + signature.Parameters = append(signature.Parameters, p.walkType(u, nil, t.Params().At(i).Type())) + signature.ParameterNames = append(signature.ParameterNames, t.Params().At(i).Name()) } for i := 0; i < t.Results().Len(); i++ { - signature.Results = append(signature.Results, &types.ParamResult{ - Name: t.Results().At(i).Name(), - Type: p.walkType(u, nil, t.Results().At(i).Type()), - }) + signature.Results = append(signature.Results, p.walkType(u, nil, t.Results().At(i).Type())) + signature.ResultNames = append(signature.ResultNames, t.Results().At(i).Name()) } if r := t.Recv(); r != nil { signature.Receiver = p.walkType(u, nil, r.Type()) @@ -745,27 +734,6 @@ func (p *Parser) walkType(u types.Universe, useName *types.Name, in gotypes.Type } out.Kind = types.Alias out.Underlying = p.walkType(u, nil, t.Underlying()) - case *gotypes.Struct: - name := goNameToName(t.String()) - tpMap := map[string]*types.Type{} - if t.TypeParams().Len() != 0 { - // Remove generics, then readd them without the encoded - // type, e.g. Foo[T any] => Foo[T] - var tpNames []string - for i := 0; i < t.TypeParams().Len(); i++ { - tp := t.TypeParams().At(i) - tpName := tp.Obj().Name() - tpNames = append(tpNames, tpName) - tpMap[tpName] = p.walkType(u, nil, tp.Constraint()) - } - name.Name = fmt.Sprintf("%s[%s]", strings.SplitN(name.Name, "[", 2)[0], strings.Join(tpNames, ",")) - } - - if out := u.Type(name); out.Kind != types.Unknown { - return out // short circuit if we've already made this. - } - out = p.walkType(u, &name, t.Underlying()) - out.TypeParams = tpMap default: // gotypes package makes everything "named" with an // underlying anonymous type--we remove that annoying @@ -792,15 +760,6 @@ func (p *Parser) walkType(u types.Universe, useName *types.Name, in gotypes.Type } } return out - case *gotypes.TypeParam: - // DO NOT retrieve the type from the universe. The default type-param name is only the - // generic variable name. Ideally, it would be namespaced by package and struct but it is - // not. Thus, if we try to use the universe, we would start polluting it. - // e.g. if Foo[T] and Bar[T] exists, we'd mistakenly use the same type T for both. - return &types.Type{ - Name: name, - Kind: types.TypeParam, - } default: out := u.Type(name) if out.Kind != types.Unknown { diff --git a/vendor/k8s.io/gengo/v2/types/types.go b/vendor/k8s.io/gengo/v2/types/types.go index 7bbca01733..e9c8319c65 100644 --- a/vendor/k8s.io/gengo/v2/types/types.go +++ b/vendor/k8s.io/gengo/v2/types/types.go @@ -98,7 +98,6 @@ const ( DeclarationOf Kind = "DeclarationOf" Unknown Kind = "" Unsupported Kind = "Unsupported" - TypeParam Kind = "TypeParam" // Protobuf is protobuf type. Protobuf Kind = "Protobuf" @@ -325,9 +324,6 @@ type Type struct { // If Kind == Struct Members []Member - // If Kind == Struct - TypeParams map[string]*Type - // If Kind == Map, Slice, Pointer, or Chan Elem *Type @@ -427,20 +423,14 @@ func (m Member) String() string { return m.Name + " " + m.Type.String() } -// ParamResult represents a parameter or a result of a method's signature. -type ParamResult struct { - // The name of the parameter or result. - Name string - // The type of this parameter or result. - Type *Type -} - // Signature is a function's signature. type Signature struct { // If a method of some type, this is the type it's a member of. - Receiver *Type - Parameters []*ParamResult - Results []*ParamResult + Receiver *Type + Parameters []*Type + ParameterNames []string + Results []*Type + ResultNames []string // True if the last in parameter is of the form ...T. Variadic bool diff --git a/vendor/modules.txt b/vendor/modules.txt index c886b056bc..3919dce054 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -892,14 +892,14 @@ k8s.io/code-generator/cmd/register-gen/generators k8s.io/code-generator/pkg/namer k8s.io/code-generator/pkg/util k8s.io/code-generator/third_party/forked/golang/reflect -# k8s.io/gengo v0.0.0-20240129211411-f967bbeff4b4 +# k8s.io/gengo v0.0.0-20240404160639-a0386bf69313 ## explicit; go 1.13 k8s.io/gengo/args k8s.io/gengo/generator k8s.io/gengo/namer k8s.io/gengo/parser k8s.io/gengo/types -# k8s.io/gengo/v2 v2.0.0-20240404160639-a0386bf69313 +# k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 ## explicit; go 1.20 k8s.io/gengo/v2 k8s.io/gengo/v2/generator