Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit baf97c9

Browse files
committed
First use of new deep-copy generator.
1 parent 13a853f commit baf97c9

File tree

7 files changed

+849
-586
lines changed

7 files changed

+849
-586
lines changed

cmd/libs/go2idl/deepcopy-gen/generators/deepcopy.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package generators
1818

1919
import (
2020
"io"
21-
"os"
2221
"path/filepath"
2322
"strings"
2423

@@ -69,9 +68,7 @@ func Packages(_ *generator.Context, arguments *args.GeneratorArgs) generator.Pac
6968
PackagePath: inputDir,
7069
HeaderText: append(boilerplate, []byte(
7170
`
72-
// This file was autogenerated by the command:
73-
// $ `+strings.Join(os.Args, " ")+`
74-
// Do not edit it manually!
71+
// This file was autogenerated by deepcopy-gen. Do not edit it manually!
7572
7673
`)...),
7774
GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
@@ -157,7 +154,26 @@ func (g *genDeepCopy) copyableWithinPackage(t *types.Type) bool {
157154
return false
158155
}
159156
// TODO: Consider generating functions for other kinds too.
160-
return t.Kind == types.Struct
157+
if t.Kind != types.Struct {
158+
return false
159+
}
160+
// TODO: This should be removed once we start generating public DeepCopy
161+
// functions per directory.
162+
if t.Name.Package != g.targetPackage {
163+
// We won't be able to access private fields.
164+
// Thus, this type cannot have private fields.
165+
for _, member := range t.Members {
166+
if strings.ToLower(member.Name[:1]) == member.Name[:1] {
167+
return false
168+
}
169+
// TODO: This is a temporary hack, to make avoid generating function
170+
// for conversion.Equalities. We should get rid of it.
171+
if member.Embedded {
172+
return false
173+
}
174+
}
175+
}
176+
return true
161177
}
162178

163179
func (g *genDeepCopy) isOtherPackage(pkg string) bool {
@@ -189,7 +205,11 @@ func (g *genDeepCopy) Imports(c *generator.Context) (imports []string) {
189205
func (g *genDeepCopy) Init(c *generator.Context, w io.Writer) error {
190206
sw := generator.NewSnippetWriter(w, c, "$", "$")
191207
sw.Do("func init() {\n", nil)
192-
sw.Do("if err := api.Scheme.AddGeneratedDeepCopyFuncs(\n", nil)
208+
if g.targetPackage == apiPackagePath {
209+
sw.Do("if err := Scheme.AddGeneratedDeepCopyFuncs(\n", nil)
210+
} else {
211+
sw.Do("if err := api.Scheme.AddGeneratedDeepCopyFuncs(\n", nil)
212+
}
193213
for _, t := range g.typesForInit {
194214
sw.Do("deepCopy_$.|public$,\n", t)
195215
}
@@ -203,6 +223,7 @@ func (g *genDeepCopy) Init(c *generator.Context, w io.Writer) error {
203223

204224
// GenerateType makes the body of a file implementing a set for type t.
205225
func (g *genDeepCopy) GenerateType(c *generator.Context, t *types.Type, w io.Writer) error {
226+
glog.Errorf("--> %v", t)
206227
sw := generator.NewSnippetWriter(w, c, "$", "$")
207228
sw.Do("func deepCopy_$.|public$(in $.|raw$, out *$.|raw$, c *conversion.Cloner) error {\n", t)
208229
g.generateFor(t, sw)

cmd/libs/go2idl/deepcopy-gen/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,17 @@ import (
3131
func main() {
3232
arguments := args.Default()
3333

34+
// Override defaults. These are Kubernetes specific input locations.
35+
arguments.InputDirs = []string{
36+
"k8s.io/kubernetes/pkg/api",
37+
}
38+
3439
if err := arguments.Execute(
3540
generators.NameSystems(),
3641
generators.DefaultNameSystem(),
3742
generators.Packages,
3843
); err != nil {
3944
glog.Fatalf("Error: %v", err)
4045
}
46+
glog.Info("Completed successfully.")
4147
}

hack/after-build/run-codegen.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ source "${KUBE_ROOT}/hack/lib/init.sh"
2323

2424
kube::golang::setup_env
2525

26-
setgen=$(kube::util::find-binary "set-gen")
2726
clientgen=$(kube::util::find-binary "client-gen")
27+
deepcopygen=$(kube::util::find-binary "deepcopy-gen")
28+
setgen=$(kube::util::find-binary "set-gen")
2829

2930
# Please do not add any logic to this shell script. Add logic to the go code
3031
# that generates the set-gen program.
@@ -33,6 +34,7 @@ clientgen=$(kube::util::find-binary "client-gen")
3334
# update- and verify- scripts.
3435
${clientgen} "$@"
3536
${clientgen} -t "$@"
37+
${deepcopygen} "$@"
3638
${setgen} "$@"
3739

3840
# You may add additional calls of code generators like set-gen above.

hack/after-build/update-generated-deep-copies.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,18 @@ function generate_deep_copies() {
5050
# Ensure that the version being processed is registered by setting
5151
# KUBE_API_VERSIONS.
5252
if [ -z ${ver##*/} ]; then
53-
apiVersions=""
53+
apiVersions=""
54+
else
55+
apiVersions="${ver}"
5456
fi
5557
KUBE_API_VERSIONS="${apiVersions}" generate_version "${ver}"
5658
done
5759
}
5860

5961
# v1 is in the group ""
60-
DEFAULT_VERSIONS="/ v1 extensions/ extensions/v1beta1 componentconfig/ componentconfig/v1alpha1 metrics/ metrics/v1alpha1"
62+
# Currently pkg/api/deep_copy_generated.go is generated by the new go2idl generator.
63+
# All others (mentioned above) are still generated by the old reflection-based generator.
64+
# TODO: Migrate these to the new generator.
65+
DEFAULT_VERSIONS="v1 extensions/ extensions/v1beta1 componentconfig/ componentconfig/v1alpha1 metrics/ metrics/v1alpha1"
6166
VERSIONS=${VERSIONS:-$DEFAULT_VERSIONS}
6267
generate_deep_copies "$VERSIONS"

hack/update-codegen.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ source "${KUBE_ROOT}/hack/lib/init.sh"
2626
kube::golang::setup_env
2727

2828
"${KUBE_ROOT}/hack/build-go.sh" cmd/libs/go2idl/client-gen
29+
"${KUBE_ROOT}/hack/build-go.sh" cmd/libs/go2idl/deepcopy-gen
2930
"${KUBE_ROOT}/hack/build-go.sh" cmd/libs/go2idl/set-gen
3031

3132
"${KUBE_ROOT}/hack/after-build/run-codegen.sh" "$@"

hack/verify-codegen.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ source "${KUBE_ROOT}/hack/lib/init.sh"
2626
kube::golang::setup_env
2727

2828
"${KUBE_ROOT}/hack/build-go.sh" cmd/libs/go2idl/client-gen
29+
"${KUBE_ROOT}/hack/build-go.sh" cmd/libs/go2idl/deepcopy-gen
2930
"${KUBE_ROOT}/hack/build-go.sh" cmd/libs/go2idl/set-gen
3031

3132
"${KUBE_ROOT}/hack/after-build/run-codegen.sh" --verify-only

0 commit comments

Comments
 (0)