Skip to content

Commit 0a61fd9

Browse files
authored
Merge pull request kubernetes#85891 from wojtek-t/remove_old_conversions
Remove old-style conversions registration
2 parents 32883e4 + 839b440 commit 0a61fd9

File tree

10 files changed

+293
-404
lines changed

10 files changed

+293
-404
lines changed

staging/src/k8s.io/apimachinery/pkg/apis/meta/internalversion/register.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,6 @@ func addToGroupVersion(scheme *runtime.Scheme) error {
4747
if err := scheme.AddIgnoredConversionType(&metav1.TypeMeta{}, &metav1.TypeMeta{}); err != nil {
4848
return err
4949
}
50-
err := scheme.AddConversionFuncs(
51-
metav1.Convert_string_To_labels_Selector,
52-
metav1.Convert_labels_Selector_To_string,
53-
54-
metav1.Convert_string_To_fields_Selector,
55-
metav1.Convert_fields_Selector_To_string,
56-
57-
metav1.Convert_Map_string_To_string_To_v1_LabelSelector,
58-
metav1.Convert_v1_LabelSelector_To_Map_string_To_string,
59-
)
60-
if err != nil {
61-
return err
62-
}
6350
// ListOptions is the only options struct which needs conversion (it exposes labels and fields
6451
// as selectors for convenience). The other types have only a single representation today.
6552
scheme.AddKnownTypes(SchemeGroupVersion,

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/register.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ func AddMetaToScheme(scheme *runtime.Scheme) error {
9595
&PartialObjectMetadataList{},
9696
)
9797

98-
return scheme.AddConversionFuncs(
99-
Convert_Slice_string_To_v1_IncludeObjectPolicy,
100-
)
98+
return nil
10199
}
102100

103101
func init() {

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ go_library(
2121
"//staging/src/k8s.io/apimachinery/pkg/conversion:go_default_library",
2222
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
2323
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
24-
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
2524
"//vendor/github.com/gogo/protobuf/proto:go_default_library",
2625
],
2726
)

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/generated.pb.go

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

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/generated.proto

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

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1/register.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package v1beta1
1919
import (
2020
"k8s.io/apimachinery/pkg/runtime"
2121
"k8s.io/apimachinery/pkg/runtime/schema"
22-
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2322
)
2423

2524
// GroupName is the group name for this API.
@@ -33,12 +32,6 @@ func Kind(kind string) schema.GroupKind {
3332
return SchemeGroupVersion.WithKind(kind).GroupKind()
3433
}
3534

36-
// scheme is the registry for the common types that adhere to the meta v1beta1 API spec.
37-
var scheme = runtime.NewScheme()
38-
39-
// ParameterCodec knows about query parameters used with the meta v1beta1 API spec.
40-
var ParameterCodec = runtime.NewParameterCodec(scheme)
41-
4235
// AddMetaToScheme registers base meta types into schemas.
4336
func AddMetaToScheme(scheme *runtime.Scheme) error {
4437
scheme.AddKnownTypes(SchemeGroupVersion,
@@ -48,14 +41,5 @@ func AddMetaToScheme(scheme *runtime.Scheme) error {
4841
&PartialObjectMetadataList{},
4942
)
5043

51-
return scheme.AddConversionFuncs(
52-
Convert_Slice_string_To_v1beta1_IncludeObjectPolicy,
53-
)
54-
}
55-
56-
func init() {
57-
utilruntime.Must(AddMetaToScheme(scheme))
58-
59-
// register manually. This usually goes through the SchemeBuilder, which we cannot use here.
60-
utilruntime.Must(RegisterDefaults(scheme))
44+
return nil
6145
}

staging/src/k8s.io/apimachinery/pkg/conversion/converter.go

Lines changed: 21 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ type Converter struct {
5454
generatedConversionFuncs ConversionFuncs
5555

5656
// Set of conversions that should be treated as a no-op
57-
ignoredConversions map[typePair]struct{}
57+
ignoredConversions map[typePair]struct{}
58+
ignoredUntypedConversions map[typePair]struct{}
5859

5960
// This is a map from a source field type and name, to a list of destination
6061
// field type and name.
@@ -83,17 +84,23 @@ type Converter struct {
8384
// NewConverter creates a new Converter object.
8485
func NewConverter(nameFn NameFunc) *Converter {
8586
c := &Converter{
86-
conversionFuncs: NewConversionFuncs(),
87-
generatedConversionFuncs: NewConversionFuncs(),
88-
ignoredConversions: make(map[typePair]struct{}),
89-
nameFunc: nameFn,
90-
structFieldDests: make(map[typeNamePair][]typeNamePair),
91-
structFieldSources: make(map[typeNamePair][]typeNamePair),
87+
conversionFuncs: NewConversionFuncs(),
88+
generatedConversionFuncs: NewConversionFuncs(),
89+
ignoredConversions: make(map[typePair]struct{}),
90+
ignoredUntypedConversions: make(map[typePair]struct{}),
91+
nameFunc: nameFn,
92+
structFieldDests: make(map[typeNamePair][]typeNamePair),
93+
structFieldSources: make(map[typeNamePair][]typeNamePair),
9294

9395
inputFieldMappingFuncs: make(map[reflect.Type]FieldMappingFunc),
9496
inputDefaultFlags: make(map[reflect.Type]FieldMatchingFlags),
9597
}
96-
c.RegisterConversionFunc(Convert_Slice_byte_To_Slice_byte)
98+
c.RegisterUntypedConversionFunc(
99+
(*[]byte)(nil), (*[]byte)(nil),
100+
func(a, b interface{}, s Scope) error {
101+
return Convert_Slice_byte_To_Slice_byte(a.(*[]byte), b.(*[]byte), s)
102+
},
103+
)
97104
return c
98105
}
99106

@@ -153,31 +160,14 @@ type FieldMappingFunc func(key string, sourceTag, destTag reflect.StructTag) (so
153160

154161
func NewConversionFuncs() ConversionFuncs {
155162
return ConversionFuncs{
156-
fns: make(map[typePair]reflect.Value),
157163
untyped: make(map[typePair]ConversionFunc),
158164
}
159165
}
160166

161167
type ConversionFuncs struct {
162-
fns map[typePair]reflect.Value
163168
untyped map[typePair]ConversionFunc
164169
}
165170

166-
// Add adds the provided conversion functions to the lookup table - they must have the signature
167-
// `func(type1, type2, Scope) error`. Functions are added in the order passed and will override
168-
// previously registered pairs.
169-
func (c ConversionFuncs) Add(fns ...interface{}) error {
170-
for _, fn := range fns {
171-
fv := reflect.ValueOf(fn)
172-
ft := fv.Type()
173-
if err := verifyConversionFunctionSignature(ft); err != nil {
174-
return err
175-
}
176-
c.fns[typePair{ft.In(0).Elem(), ft.In(1).Elem()}] = fv
177-
}
178-
return nil
179-
}
180-
181171
// AddUntyped adds the provided conversion function to the lookup table for the types that are
182172
// supplied as a and b. a and b must be pointers or an error is returned. This method overwrites
183173
// previously defined functions.
@@ -197,12 +187,6 @@ func (c ConversionFuncs) AddUntyped(a, b interface{}, fn ConversionFunc) error {
197187
// both other and c, with other conversions taking precedence.
198188
func (c ConversionFuncs) Merge(other ConversionFuncs) ConversionFuncs {
199189
merged := NewConversionFuncs()
200-
for k, v := range c.fns {
201-
merged.fns[k] = v
202-
}
203-
for k, v := range other.fns {
204-
merged.fns[k] = v
205-
}
206190
for k, v := range c.untyped {
207191
merged.untyped[k] = v
208192
}
@@ -360,29 +344,6 @@ func verifyConversionFunctionSignature(ft reflect.Type) error {
360344
return nil
361345
}
362346

363-
// RegisterConversionFunc registers a conversion func with the
364-
// Converter. conversionFunc must take three parameters: a pointer to the input
365-
// type, a pointer to the output type, and a conversion.Scope (which should be
366-
// used if recursive conversion calls are desired). It must return an error.
367-
//
368-
// Example:
369-
// c.RegisterConversionFunc(
370-
// func(in *Pod, out *v1.Pod, s Scope) error {
371-
// // conversion logic...
372-
// return nil
373-
// })
374-
// DEPRECATED: Will be removed in favor of RegisterUntypedConversionFunc
375-
func (c *Converter) RegisterConversionFunc(conversionFunc interface{}) error {
376-
return c.conversionFuncs.Add(conversionFunc)
377-
}
378-
379-
// Similar to RegisterConversionFunc, but registers conversion function that were
380-
// automatically generated.
381-
// DEPRECATED: Will be removed in favor of RegisterGeneratedUntypedConversionFunc
382-
func (c *Converter) RegisterGeneratedConversionFunc(conversionFunc interface{}) error {
383-
return c.generatedConversionFuncs.Add(conversionFunc)
384-
}
385-
386347
// RegisterUntypedConversionFunc registers a function that converts between a and b by passing objects of those
387348
// types to the provided function. The function *must* accept objects of a and b - this machinery will not enforce
388349
// any other guarantee.
@@ -409,6 +370,7 @@ func (c *Converter) RegisterIgnoredConversion(from, to interface{}) error {
409370
return fmt.Errorf("expected pointer arg for 'to' param 1, got: %v", typeTo)
410371
}
411372
c.ignoredConversions[typePair{typeFrom.Elem(), typeTo.Elem()}] = struct{}{}
373+
c.ignoredUntypedConversions[typePair{typeFrom, typeTo}] = struct{}{}
412374
return nil
413375
}
414376

@@ -491,6 +453,11 @@ func (c *Converter) doConversion(src, dest interface{}, flags FieldMatchingFlags
491453
flags: flags,
492454
meta: meta,
493455
}
456+
457+
// ignore conversions of this type
458+
if _, ok := c.ignoredUntypedConversions[pair]; ok {
459+
return nil
460+
}
494461
if fn, ok := c.conversionFuncs.untyped[pair]; ok {
495462
return fn(src, dest, scope)
496463
}
@@ -517,35 +484,6 @@ func (c *Converter) doConversion(src, dest interface{}, flags FieldMatchingFlags
517484
return f(sv, dv, scope)
518485
}
519486

520-
// callCustom calls 'custom' with sv & dv. custom must be a conversion function.
521-
func (c *Converter) callCustom(sv, dv, custom reflect.Value, scope *scope) error {
522-
if !sv.CanAddr() {
523-
sv2 := reflect.New(sv.Type())
524-
sv2.Elem().Set(sv)
525-
sv = sv2
526-
} else {
527-
sv = sv.Addr()
528-
}
529-
if !dv.CanAddr() {
530-
if !dv.CanSet() {
531-
return scope.errorf("can't addr or set dest.")
532-
}
533-
dvOrig := dv
534-
dv := reflect.New(dvOrig.Type())
535-
defer func() { dvOrig.Set(dv) }()
536-
} else {
537-
dv = dv.Addr()
538-
}
539-
args := []reflect.Value{sv, dv, reflect.ValueOf(scope)}
540-
ret := custom.Call(args)[0].Interface()
541-
// This convolution is necessary because nil interfaces won't convert
542-
// to errors.
543-
if ret == nil {
544-
return nil
545-
}
546-
return ret.(error)
547-
}
548-
549487
// callUntyped calls predefined conversion func.
550488
func (c *Converter) callUntyped(sv, dv reflect.Value, f ConversionFunc, scope *scope) error {
551489
if !dv.CanAddr() {
@@ -577,19 +515,6 @@ func (c *Converter) convert(sv, dv reflect.Value, scope *scope) error {
577515
}
578516

579517
// Convert sv to dv.
580-
if fv, ok := c.conversionFuncs.fns[pair]; ok {
581-
if c.Debug != nil {
582-
c.Debug.Logf("Calling custom conversion of '%v' to '%v'", st, dt)
583-
}
584-
return c.callCustom(sv, dv, fv, scope)
585-
}
586-
if fv, ok := c.generatedConversionFuncs.fns[pair]; ok {
587-
if c.Debug != nil {
588-
c.Debug.Logf("Calling generated conversion of '%v' to '%v'", st, dt)
589-
}
590-
return c.callCustom(sv, dv, fv, scope)
591-
}
592-
593518
pair = typePair{reflect.PtrTo(sv.Type()), reflect.PtrTo(dv.Type())}
594519
if f, ok := c.conversionFuncs.untyped[pair]; ok {
595520
return c.callUntyped(sv, dv, f, scope)

0 commit comments

Comments
 (0)