mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 06:54:01 +00:00
Revert Clayton's #26179 so I can recreate it
His PR cam during the middle of this development cycle, and it was easier to burn it down and recreate it than try to patch it into an existing series and re-test every assumption. This behavior will be re-introduced in subsequent commits.
This commit is contained in:
parent
3494f8e2f9
commit
e18b2f3a2e
@ -105,7 +105,6 @@ func (g *GeneratorArgs) NewBuilder() (*parser.Builder, error) {
|
||||
b.AddBuildTags(g.GeneratedBuildTag)
|
||||
|
||||
for _, d := range g.InputDirs {
|
||||
d = strings.TrimLeft(d, "+-*")
|
||||
if g.Recursive {
|
||||
if err := b.AddDirRecursive(d); err != nil {
|
||||
return nil, fmt.Errorf("unable to add directory %q: %v", d, err)
|
||||
|
@ -19,7 +19,6 @@ package generators
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
@ -32,15 +31,6 @@ import (
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
// Constraints is a set of optional limitations on what deep copy will generate.
|
||||
type Constraints struct {
|
||||
// PackageConstraints is an optional set of package prefixes that constrain which types
|
||||
// will have inline deep copy methods generated for. Any type outside of these packages
|
||||
// (if specified) will not have a function generated and will result in a call to the
|
||||
// cloner.DeepCopy method.
|
||||
PackageConstraints []string
|
||||
}
|
||||
|
||||
// TODO: This is created only to reduce number of changes in a single PR.
|
||||
// Remove it and use PublicNamer instead.
|
||||
func deepCopyNamer() *namer.NameStrategy {
|
||||
@ -72,32 +62,7 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
|
||||
glog.Fatalf("Failed loading boilerplate: %v", err)
|
||||
}
|
||||
|
||||
initInputs := sets.NewString()
|
||||
explicitInputs := sets.NewString()
|
||||
inputs := sets.NewString()
|
||||
for _, s := range arguments.InputDirs {
|
||||
switch {
|
||||
case strings.HasPrefix(s, "+"):
|
||||
// packages with '+' prefix get functions generated for everything except gencopy=false, but
|
||||
// no init function
|
||||
s = strings.TrimPrefix(s, "+")
|
||||
inputs.Insert(s)
|
||||
case strings.HasPrefix(s, "-"):
|
||||
// packages with '-' prefix only get functions generated for those with gencopy=true
|
||||
s = strings.TrimPrefix(s, "-")
|
||||
inputs.Insert(s)
|
||||
explicitInputs.Insert(s)
|
||||
default:
|
||||
inputs.Insert(s)
|
||||
initInputs.Insert(s)
|
||||
}
|
||||
}
|
||||
|
||||
var restrictRange []string
|
||||
if c, ok := arguments.CustomArgs.(Constraints); ok {
|
||||
restrictRange = c.PackageConstraints
|
||||
}
|
||||
|
||||
inputs := sets.NewString(arguments.InputDirs...)
|
||||
packages := generator.Packages{}
|
||||
header := append([]byte(fmt.Sprintf("// +build !%s\n\n", arguments.GeneratedBuildTag)), boilerplate...)
|
||||
header = append(header, []byte(
|
||||
@ -108,31 +73,11 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
|
||||
for _, p := range context.Universe {
|
||||
copyableType := false
|
||||
for _, t := range p.Types {
|
||||
if copyableWithinPackage(t, explicitInputs.Has(t.Name.Package)) && inputs.Has(t.Name.Package) {
|
||||
if copyableWithinPackage(t) && inputs.Has(t.Name.Package) {
|
||||
copyableType = true
|
||||
}
|
||||
}
|
||||
if copyableType {
|
||||
// TODO: replace this with a more sophisticated algorithm that generates private copy methods
|
||||
// (like auto_DeepCopy_...) for any type that is outside of the PackageConstraints. That would
|
||||
// avoid having to make a reflection call.
|
||||
canInlineTypeFn := func(c *generator.Context, t *types.Type) bool {
|
||||
// types must be public structs or have a custom DeepCopy_<method> already defined
|
||||
if publicCopyFunctionDefined(c, t) {
|
||||
return true
|
||||
}
|
||||
if !copyableWithinPackage(t, explicitInputs.Has(t.Name.Package)) {
|
||||
return false
|
||||
}
|
||||
// only packages within the restricted range can be inlined
|
||||
for _, s := range restrictRange {
|
||||
if strings.HasPrefix(t.Name.Package, s) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
path := p.Path
|
||||
packages = append(packages,
|
||||
&generator.DefaultPackage{
|
||||
@ -142,7 +87,7 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
|
||||
GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
|
||||
generators = []generator.Generator{}
|
||||
generators = append(
|
||||
generators, NewGenDeepCopy("deep_copy_generated", path, initInputs.Has(path), explicitInputs.Has(path), canInlineTypeFn))
|
||||
generators, NewGenDeepCopy("deep_copy_generated", path, inputs.Has(path)))
|
||||
return generators
|
||||
},
|
||||
FilterFunc: func(c *generator.Context, t *types.Type) bool {
|
||||
@ -154,9 +99,6 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
|
||||
return packages
|
||||
}
|
||||
|
||||
// CanInlineTypeFunc should return true if the provided type can be converted to a function call
|
||||
type CanInlineTypeFunc func(*generator.Context, *types.Type) bool
|
||||
|
||||
const (
|
||||
apiPackagePath = "k8s.io/kubernetes/pkg/api"
|
||||
conversionPackagePath = "k8s.io/kubernetes/pkg/conversion"
|
||||
@ -165,30 +107,23 @@ const (
|
||||
// genDeepCopy produces a file with autogenerated deep-copy functions.
|
||||
type genDeepCopy struct {
|
||||
generator.DefaultGen
|
||||
|
||||
targetPackage string
|
||||
imports namer.ImportTracker
|
||||
typesForInit []*types.Type
|
||||
generateInitFunc bool
|
||||
requireExplicitTag bool
|
||||
canInlineTypeFn CanInlineTypeFunc
|
||||
|
||||
context *generator.Context
|
||||
targetPackage string
|
||||
imports namer.ImportTracker
|
||||
typesForInit []*types.Type
|
||||
generateInitFunc bool
|
||||
|
||||
globalVariables map[string]interface{}
|
||||
}
|
||||
|
||||
func NewGenDeepCopy(sanitizedName, targetPackage string, generateInitFunc, requireExplicitTag bool, canInlineTypeFn CanInlineTypeFunc) generator.Generator {
|
||||
func NewGenDeepCopy(sanitizedName, targetPackage string, generateInitFunc bool) generator.Generator {
|
||||
return &genDeepCopy{
|
||||
DefaultGen: generator.DefaultGen{
|
||||
OptionalName: sanitizedName,
|
||||
},
|
||||
targetPackage: targetPackage,
|
||||
imports: generator.NewImportTracker(),
|
||||
typesForInit: make([]*types.Type, 0),
|
||||
generateInitFunc: generateInitFunc,
|
||||
requireExplicitTag: requireExplicitTag,
|
||||
canInlineTypeFn: canInlineTypeFn,
|
||||
targetPackage: targetPackage,
|
||||
imports: generator.NewImportTracker(),
|
||||
typesForInit: make([]*types.Type, 0),
|
||||
generateInitFunc: generateInitFunc,
|
||||
}
|
||||
}
|
||||
|
||||
@ -199,29 +134,15 @@ func (g *genDeepCopy) Namers(c *generator.Context) namer.NameSystems {
|
||||
|
||||
func (g *genDeepCopy) Filter(c *generator.Context, t *types.Type) bool {
|
||||
// Filter out all types not copyable within the package.
|
||||
copyable := copyableWithinPackage(t, g.requireExplicitTag)
|
||||
copyable := copyableWithinPackage(t)
|
||||
if copyable {
|
||||
g.typesForInit = append(g.typesForInit, t)
|
||||
}
|
||||
return copyable
|
||||
}
|
||||
|
||||
// publicCopyFunctionDefined returns true if a DeepCopy function has already been defined in a given
|
||||
// package, which allows more efficient deep copy implementations to be defined by the caller.
|
||||
func publicCopyFunctionDefined(c *generator.Context, t *types.Type) bool {
|
||||
p, ok := c.Universe[t.Name.Package]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return p.Functions["DeepCopy_"+path.Base(t.Name.Package)+"_"+t.Name.Name] != nil
|
||||
}
|
||||
|
||||
func copyableWithinPackage(t *types.Type, explicitCopyRequired bool) bool {
|
||||
tag := types.ExtractCommentTags("+", t.CommentLines)["gencopy"]
|
||||
if tag == "false" {
|
||||
return false
|
||||
}
|
||||
if explicitCopyRequired && tag != "true" {
|
||||
func copyableWithinPackage(t *types.Type) bool {
|
||||
if types.ExtractCommentTags("+", t.CommentLines)["gencopy"] == "false" {
|
||||
return false
|
||||
}
|
||||
// TODO: Consider generating functions for other kinds too.
|
||||
@ -280,7 +201,6 @@ func (g *genDeepCopy) funcNameTmpl(t *types.Type) string {
|
||||
}
|
||||
|
||||
func (g *genDeepCopy) Init(c *generator.Context, w io.Writer) error {
|
||||
g.context = c
|
||||
cloner := c.Universe.Type(types.Name{Package: conversionPackagePath, Name: "Cloner"})
|
||||
g.imports.AddType(cloner)
|
||||
g.globalVariables = map[string]interface{}{
|
||||
@ -366,7 +286,7 @@ func (g *genDeepCopy) doMap(t *types.Type, sw *generator.SnippetWriter) {
|
||||
sw.Do("}\n", nil)
|
||||
default:
|
||||
sw.Do("for key, val := range in {\n", nil)
|
||||
if g.canInlineTypeFn(g.context, t.Elem) {
|
||||
if copyableWithinPackage(t.Elem) {
|
||||
sw.Do("newVal := new($.|raw$)\n", t.Elem)
|
||||
funcName := g.funcNameTmpl(t.Elem)
|
||||
sw.Do(fmt.Sprintf("if err := %s(val, newVal, c); err != nil {\n", funcName), argsFromType(t.Elem))
|
||||
@ -398,7 +318,7 @@ func (g *genDeepCopy) doSlice(t *types.Type, sw *generator.SnippetWriter) {
|
||||
sw.Do("for i := range in {\n", nil)
|
||||
if t.Elem.IsAssignable() {
|
||||
sw.Do("(*out)[i] = in[i]\n", nil)
|
||||
} else if g.canInlineTypeFn(g.context, t.Elem) {
|
||||
} else if copyableWithinPackage(t.Elem) {
|
||||
funcName := g.funcNameTmpl(t.Elem)
|
||||
sw.Do(fmt.Sprintf("if err := %s(in[i], &(*out)[i], c); err != nil {\n", funcName), argsFromType(t.Elem))
|
||||
sw.Do("return err\n", nil)
|
||||
@ -437,7 +357,7 @@ func (g *genDeepCopy) doStruct(t *types.Type, sw *generator.SnippetWriter) {
|
||||
sw.Do("out.$.name$ = nil\n", args)
|
||||
sw.Do("}\n", nil)
|
||||
case types.Struct:
|
||||
if g.canInlineTypeFn(g.context, t) {
|
||||
if copyableWithinPackage(t) {
|
||||
funcName := g.funcNameTmpl(t)
|
||||
sw.Do(fmt.Sprintf("if err := %s(in.$.name$, &out.$.name$, c); err != nil {\n", funcName), args)
|
||||
sw.Do("return err\n", nil)
|
||||
@ -468,9 +388,9 @@ func (g *genDeepCopy) doInterface(t *types.Type, sw *generator.SnippetWriter) {
|
||||
|
||||
func (g *genDeepCopy) doPointer(t *types.Type, sw *generator.SnippetWriter) {
|
||||
sw.Do("*out = new($.Elem|raw$)\n", t)
|
||||
if t.Elem.IsAssignable() {
|
||||
if t.Elem.Kind == types.Builtin {
|
||||
sw.Do("**out = *in", nil)
|
||||
} else if g.canInlineTypeFn(g.context, t.Elem) {
|
||||
} else if copyableWithinPackage(t.Elem) {
|
||||
funcName := g.funcNameTmpl(t.Elem)
|
||||
sw.Do(fmt.Sprintf("if err := %s(*in, *out, c); err != nil {\n", funcName), argsFromType(t.Elem))
|
||||
sw.Do("return err\n", nil)
|
||||
|
@ -31,26 +31,8 @@ import (
|
||||
func main() {
|
||||
arguments := args.Default()
|
||||
|
||||
arguments.CustomArgs = generators.Constraints{
|
||||
// Types outside of this package will be inlined.
|
||||
PackageConstraints: []string{"k8s.io/kubernetes/"},
|
||||
}
|
||||
|
||||
// Override defaults. These are Kubernetes specific input locations.
|
||||
arguments.InputDirs = []string{
|
||||
// generate all types, but do not register them
|
||||
"+k8s.io/kubernetes/pkg/api/unversioned",
|
||||
|
||||
"-k8s.io/kubernetes/pkg/api/meta",
|
||||
"-k8s.io/kubernetes/pkg/api/meta/metatypes",
|
||||
"-k8s.io/kubernetes/pkg/api/resource",
|
||||
"-k8s.io/kubernetes/pkg/conversion",
|
||||
"-k8s.io/kubernetes/pkg/labels",
|
||||
"-k8s.io/kubernetes/pkg/runtime",
|
||||
"-k8s.io/kubernetes/pkg/runtime/serializer",
|
||||
"-k8s.io/kubernetes/pkg/util/intstr",
|
||||
"-k8s.io/kubernetes/pkg/util/sets",
|
||||
|
||||
"k8s.io/kubernetes/pkg/api",
|
||||
"k8s.io/kubernetes/pkg/api/v1",
|
||||
"k8s.io/kubernetes/pkg/apis/authentication.k8s.io",
|
||||
|
@ -2498,7 +2498,11 @@ func DeepCopy_api_Preconditions(in Preconditions, out *Preconditions, c *convers
|
||||
if in.UID != nil {
|
||||
in, out := in.UID, &out.UID
|
||||
*out = new(types.UID)
|
||||
**out = *in
|
||||
if newVal, err := c.DeepCopy(*in); err != nil {
|
||||
return err
|
||||
} else {
|
||||
**out = newVal.(types.UID)
|
||||
}
|
||||
} else {
|
||||
out.UID = nil
|
||||
}
|
||||
|
@ -83,7 +83,6 @@ option go_package = "resource";
|
||||
// writing some sort of special handling code in the hopes that that will
|
||||
// cause implementors to also use a fixed point implementation.
|
||||
//
|
||||
// +gencopy=false
|
||||
// +protobuf=true
|
||||
// +protobuf.embed=string
|
||||
// +protobuf.options.marshal=false
|
||||
|
@ -87,7 +87,6 @@ import (
|
||||
// writing some sort of special handling code in the hopes that that will
|
||||
// cause implementors to also use a fixed point implementation.
|
||||
//
|
||||
// +gencopy=false
|
||||
// +protobuf=true
|
||||
// +protobuf.embed=string
|
||||
// +protobuf.options.marshal=false
|
||||
|
@ -21,8 +21,9 @@ limitations under the License.
|
||||
package unversioned
|
||||
|
||||
import (
|
||||
conversion "k8s.io/kubernetes/pkg/conversion"
|
||||
time "time"
|
||||
|
||||
conversion "k8s.io/kubernetes/pkg/conversion"
|
||||
)
|
||||
|
||||
func DeepCopy_unversioned_APIGroup(in APIGroup, out *APIGroup, c *conversion.Cloner) error {
|
||||
|
@ -2446,7 +2446,11 @@ func DeepCopy_v1_Preconditions(in Preconditions, out *Preconditions, c *conversi
|
||||
if in.UID != nil {
|
||||
in, out := in.UID, &out.UID
|
||||
*out = new(types.UID)
|
||||
**out = *in
|
||||
if newVal, err := c.DeepCopy(*in); err != nil {
|
||||
return err
|
||||
} else {
|
||||
**out = newVal.(types.UID)
|
||||
}
|
||||
} else {
|
||||
out.UID = nil
|
||||
}
|
||||
|
@ -576,7 +576,11 @@ func DeepCopy_extensions_NetworkPolicyPort(in NetworkPolicyPort, out *NetworkPol
|
||||
if in.Protocol != nil {
|
||||
in, out := in.Protocol, &out.Protocol
|
||||
*out = new(api.Protocol)
|
||||
**out = *in
|
||||
if newVal, err := c.DeepCopy(*in); err != nil {
|
||||
return err
|
||||
} else {
|
||||
**out = newVal.(api.Protocol)
|
||||
}
|
||||
} else {
|
||||
out.Protocol = nil
|
||||
}
|
||||
|
@ -888,7 +888,11 @@ func DeepCopy_v1beta1_NetworkPolicyPort(in NetworkPolicyPort, out *NetworkPolicy
|
||||
if in.Protocol != nil {
|
||||
in, out := in.Protocol, &out.Protocol
|
||||
*out = new(v1.Protocol)
|
||||
**out = *in
|
||||
if newVal, err := c.DeepCopy(*in); err != nil {
|
||||
return err
|
||||
} else {
|
||||
**out = newVal.(v1.Protocol)
|
||||
}
|
||||
} else {
|
||||
out.Protocol = nil
|
||||
}
|
||||
|
@ -69,7 +69,6 @@ option go_package = "runtime";
|
||||
// in the Object. (TODO: In the case where the object is of an unknown type, a
|
||||
// runtime.Unknown object will be created and stored.)
|
||||
//
|
||||
// +gencopy=true
|
||||
// +protobuf=true
|
||||
message RawExtension {
|
||||
// Raw is the underlying serialization of this object.
|
||||
@ -89,7 +88,6 @@ message RawExtension {
|
||||
// TypeMeta is provided here for convenience. You may use it directly from this package or define
|
||||
// your own with the same fields.
|
||||
//
|
||||
// +gencopy=true
|
||||
// +protobuf=true
|
||||
message TypeMeta {
|
||||
optional string apiVersion = 1;
|
||||
@ -103,7 +101,6 @@ message TypeMeta {
|
||||
// TODO: Make this object have easy access to field based accessors and settors for
|
||||
// metadata and field mutatation.
|
||||
//
|
||||
// +gencopy=true
|
||||
// +protobuf=true
|
||||
message Unknown {
|
||||
optional TypeMeta typeMeta = 1;
|
||||
|
@ -40,7 +40,6 @@ import (
|
||||
// TypeMeta is provided here for convenience. You may use it directly from this package or define
|
||||
// your own with the same fields.
|
||||
//
|
||||
// +gencopy=true
|
||||
// +protobuf=true
|
||||
type TypeMeta struct {
|
||||
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty" protobuf:"bytes,1,opt,name=apiVersion"`
|
||||
@ -93,7 +92,6 @@ const (
|
||||
// in the Object. (TODO: In the case where the object is of an unknown type, a
|
||||
// runtime.Unknown object will be created and stored.)
|
||||
//
|
||||
// +gencopy=true
|
||||
// +protobuf=true
|
||||
type RawExtension struct {
|
||||
// Raw is the underlying serialization of this object.
|
||||
@ -111,7 +109,6 @@ type RawExtension struct {
|
||||
// TODO: Make this object have easy access to field based accessors and settors for
|
||||
// metadata and field mutatation.
|
||||
//
|
||||
// +gencopy=true
|
||||
// +protobuf=true
|
||||
type Unknown struct {
|
||||
TypeMeta `json:",inline" protobuf:"bytes,1,opt,name=typeMeta"`
|
||||
|
@ -30,7 +30,6 @@ option go_package = "intstr";
|
||||
// accept a name or number.
|
||||
// TODO: Rename to Int32OrString
|
||||
//
|
||||
// +gencopy=true
|
||||
// +protobuf=true
|
||||
// +protobuf.options.(gogoproto.goproto_stringer)=false
|
||||
message IntOrString {
|
||||
|
@ -32,7 +32,6 @@ import (
|
||||
// accept a name or number.
|
||||
// TODO: Rename to Int32OrString
|
||||
//
|
||||
// +gencopy=true
|
||||
// +protobuf=true
|
||||
// +protobuf.options.(gogoproto.goproto_stringer)=false
|
||||
type IntOrString struct {
|
||||
|
Loading…
Reference in New Issue
Block a user