Merge pull request #64641 from sttts/sttts-ObjectConvertor-invariant

Automatic merge from submit-queue (batch tested with PRs 64641, 64532). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

apimachinery: adapt ObjectConvertor invariant

We avoid deepcopies in the codec/conversion stack by re-using data structures. This means that the out object of a conversion must be deepcopied before mutation in order to not mutate the in object as well.

This guarantees that e.g. runtime.Encode (which uses conversion from internal -> versioned) does not mutate the input. This would be highly unexpected (and we do not mention possible mutation of the input for runtime.Encode).
This commit is contained in:
Kubernetes Submit Queue 2018-06-02 09:15:04 -07:00 committed by GitHub
commit c22a32bdbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -174,13 +174,16 @@ type ObjectVersioner interface {
// ObjectConvertor converts an object to a different version. // ObjectConvertor converts an object to a different version.
type ObjectConvertor interface { type ObjectConvertor interface {
// Convert attempts to convert one object into another, or returns an error. This method does // Convert attempts to convert one object into another, or returns an error. This
// not guarantee the in object is not mutated. The context argument will be passed to // method does not mutate the in object, but the in and out object might share data structures,
// all nested conversions. // i.e. the out object cannot be mutated without mutating the in object as well.
// The context argument will be passed to all nested conversions.
Convert(in, out, context interface{}) error Convert(in, out, context interface{}) error
// ConvertToVersion takes the provided object and converts it the provided version. This // ConvertToVersion takes the provided object and converts it the provided version. This
// method does not guarantee that the in object is not mutated. This method is similar to // method does not mutate the in object, but the in and out object might share data structures,
// Convert() but handles specific details of choosing the correct output version. // i.e. the out object cannot be mutated without mutating the in object as well.
// This method is similar to Convert() but handles specific details of choosing the correct
// output version.
ConvertToVersion(in Object, gv GroupVersioner) (out Object, err error) ConvertToVersion(in Object, gv GroupVersioner) (out Object, err error)
ConvertFieldLabel(version, kind, label, value string) (string, string, error) ConvertFieldLabel(version, kind, label, value string) (string, string, error)
} }