Skip to content

Commit 8c1f961

Browse files
committed
Stop supporting default conversions
1 parent 2f08dc1 commit 8c1f961

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,20 +442,20 @@ func (c *Converter) doConversion(src, dest interface{}, flags FieldMatchingFlags
442442
if fn, ok := c.generatedConversionFuncs.untyped[pair]; ok {
443443
return fn(src, dest, scope)
444444
}
445-
// TODO: consider everything past this point deprecated - we want to support only point to point top level
446-
// conversions
447445

448446
dv, err := EnforcePtr(dest)
449447
if err != nil {
450448
return err
451449
}
452-
if !dv.CanAddr() && !dv.CanSet() {
453-
return fmt.Errorf("can't write to dest")
454-
}
455450
sv, err := EnforcePtr(src)
456451
if err != nil {
457452
return err
458453
}
454+
return fmt.Errorf("converting (%s) to (%s): unknown conversion", sv.Type(), dv.Type())
455+
456+
// TODO: Everything past this point is deprecated.
457+
// Remove in 1.20 once we're sure it didn't break anything.
458+
459459
// Leave something on the stack, so that calls to struct tag getters never fail.
460460
scope.srcStack.push(scopeStackElem{})
461461
scope.destStack.push(scopeStackElem{})

staging/src/k8s.io/apimachinery/pkg/runtime/scheme.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,19 @@ func (s *Scheme) AddKnownTypeWithName(gvk schema.GroupVersionKind, obj Object) {
211211
}
212212
}
213213
s.typeToGVK[t] = append(s.typeToGVK[t], gvk)
214+
215+
// if the type implements DeepCopyInto(<obj>), register a self-conversion
216+
if m := reflect.ValueOf(obj).MethodByName("DeepCopyInto"); m.IsValid() && m.Type().NumIn() == 1 && m.Type().NumOut() == 0 && m.Type().In(0) == reflect.TypeOf(obj) {
217+
if err := s.AddGeneratedConversionFunc(obj, obj, func(a, b interface{}, scope conversion.Scope) error {
218+
// copy a to b
219+
reflect.ValueOf(a).MethodByName("DeepCopyInto").Call([]reflect.Value{reflect.ValueOf(b)})
220+
// clear TypeMeta to match legacy reflective conversion
221+
b.(Object).GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{})
222+
return nil
223+
}); err != nil {
224+
panic(err)
225+
}
226+
}
214227
}
215228

216229
// KnownTypes returns the types known for the given version.

0 commit comments

Comments
 (0)