Skip to content

Commit 7f3d235

Browse files
authored
Merge pull request kubernetes#129307 from LionelJouin/fix-129290
Add missing imports in register-gen
2 parents ef54ac8 + c2afe49 commit 7f3d235

File tree

28 files changed

+861
-488
lines changed

28 files changed

+861
-488
lines changed

hack/update-codegen.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,60 @@ function codegen::conversions() {
456456
fi
457457
}
458458

459+
# Register generation
460+
#
461+
# Any package that wants register functions generated must include a
462+
# comment-tag in column 0 of one file of the form:
463+
# // +k8s:register-gen=package
464+
#
465+
function codegen::register() {
466+
# Build the tool.
467+
GOPROXY=off go install \
468+
k8s.io/code-generator/cmd/register-gen
469+
470+
# The result file, in each pkg, of register generation.
471+
local output_file="${GENERATED_FILE_PREFIX}register.go"
472+
473+
# All directories that request any form of register generation.
474+
if [[ "${DBG_CODEGEN}" == 1 ]]; then
475+
kube::log::status "DBG: finding all +k8s:register-gen tags"
476+
fi
477+
local tag_dirs=()
478+
kube::util::read-array tag_dirs < <( \
479+
grep -l --null '+k8s:register-gen=' "${ALL_K8S_TAG_FILES[@]}" \
480+
| while read -r -d $'\0' F; do dirname "${F}"; done \
481+
| sort -u)
482+
if [[ "${DBG_CODEGEN}" == 1 ]]; then
483+
kube::log::status "DBG: found ${#tag_dirs[@]} +k8s:register-gen tagged dirs"
484+
fi
485+
486+
local tag_pkgs=()
487+
for dir in "${tag_dirs[@]}"; do
488+
tag_pkgs+=("./$dir")
489+
done
490+
491+
kube::log::status "Generating register code for ${#tag_pkgs[@]} targets"
492+
if [[ "${DBG_CODEGEN}" == 1 ]]; then
493+
kube::log::status "DBG: running register-gen for:"
494+
for dir in "${tag_dirs[@]}"; do
495+
kube::log::status "DBG: $dir"
496+
done
497+
fi
498+
499+
git_find -z ':(glob)**'/"${output_file}" | xargs -0 rm -f
500+
501+
register-gen \
502+
-v "${KUBE_VERBOSE}" \
503+
--go-header-file "${BOILERPLATE_FILENAME}" \
504+
--output-file "${output_file}" \
505+
"${tag_pkgs[@]}" \
506+
"$@"
507+
508+
if [[ "${DBG_CODEGEN}" == 1 ]]; then
509+
kube::log::status "Generated register code"
510+
fi
511+
}
512+
459513
# $@: directories to exclude
460514
# example:
461515
# k8s_tag_files_except foo bat/qux

staging/src/k8s.io/code-generator/cmd/register-gen/generators/register_external.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2018 The Kubernetes Authors.
2+
Copyright 2025 The Kubernetes Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -61,11 +61,15 @@ func (g *registerExternalGenerator) Finalize(context *generator.Context, w io.Wr
6161

6262
sw := generator.NewSnippetWriter(w, context, "$", "$")
6363
m := map[string]interface{}{
64-
"groupName": g.gv.Group,
65-
"version": g.gv.Version,
66-
"types": typesToGenerateOnlyNames,
67-
"addToGroupVersion": context.Universe.Function(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "AddToGroupVersion"}),
68-
"groupVersion": context.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "GroupVersion"}),
64+
"groupName": g.gv.Group,
65+
"version": g.gv.Version,
66+
"types": typesToGenerateOnlyNames,
67+
"addToGroupVersion": context.Universe.Function(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "AddToGroupVersion"}),
68+
"groupVersion": context.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "GroupVersion"}),
69+
"schemaGroupVersion": context.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/runtime/schema", Name: "GroupVersion"}),
70+
"schemaGroupResource": context.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/runtime/schema", Name: "GroupResource"}),
71+
"scheme": context.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/runtime", Name: "Scheme"}),
72+
"schemeBuilder": context.Universe.Type(types.Name{Package: "k8s.io/apimachinery/pkg/runtime", Name: "SchemeBuilder"}),
6973
}
7074
sw.Do(registerExternalTypesTemplate, m)
7175
return sw.Error()
@@ -80,16 +84,16 @@ var GroupVersion = $.groupVersion|raw${Group: GroupName, Version: "$.version$"}
8084
8185
// SchemeGroupVersion is group version used to register these objects
8286
// Deprecated: use GroupVersion instead.
83-
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: "$.version$"}
87+
var SchemeGroupVersion = $.schemaGroupVersion|raw${Group: GroupName, Version: "$.version$"}
8488
8589
// Resource takes an unqualified resource and returns a Group qualified GroupResource
86-
func Resource(resource string) schema.GroupResource {
90+
func Resource(resource string) $.schemaGroupResource|raw$ {
8791
return SchemeGroupVersion.WithResource(resource).GroupResource()
8892
}
8993
9094
var (
9195
// localSchemeBuilder and AddToScheme will stay in k8s.io/kubernetes.
92-
SchemeBuilder runtime.SchemeBuilder
96+
SchemeBuilder $.schemeBuilder|raw$
9397
localSchemeBuilder = &SchemeBuilder
9498
// Deprecated: use Install instead
9599
AddToScheme = localSchemeBuilder.AddToScheme
@@ -104,7 +108,7 @@ func init() {
104108
}
105109
106110
// Adds the list of known types to Scheme.
107-
func addKnownTypes(scheme *runtime.Scheme) error {
111+
func addKnownTypes(scheme *$.scheme|raw$) error {
108112
scheme.AddKnownTypes(SchemeGroupVersion,
109113
$range .types -$
110114
&$.${},
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//go:build !ignore_autogenerated
2+
3+
/*
4+
Copyright 2025 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
//go:generate go run k8s.io/code-generator/cmd/register-gen --output-file zz_generated.register.go --go-header-file=../../../examples/hack/boilerplate.go.txt k8s.io/code-generator/cmd/register-gen/output_tests/...
20+
package outputtests
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// +k8s:register-gen=simpletype
18+
19+
package v1
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1
18+
19+
import (
20+
"k8s.io/apimachinery/pkg/runtime"
21+
)
22+
23+
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
24+
func (in *SimpleType) DeepCopyInto(out *SimpleType) {
25+
*out = *in
26+
}
27+
28+
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Inner.
29+
func (in *SimpleType) DeepCopy() *SimpleType {
30+
if in == nil {
31+
return nil
32+
}
33+
out := new(SimpleType)
34+
in.DeepCopyInto(out)
35+
return out
36+
}
37+
38+
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
39+
func (in *SimpleType) DeepCopyObject() runtime.Object {
40+
if c := in.DeepCopy(); c != nil {
41+
return c
42+
}
43+
return nil
44+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
22+
23+
type SimpleType struct {
24+
metav1.TypeMeta `json:",inline"`
25+
}

staging/src/k8s.io/code-generator/cmd/register-gen/output_tests/simpletype/v1/zz_generated.register.go

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/code-generator/examples/HyphenGroup/apis/example/v1/zz_generated.register.go

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)