diff --git a/pkg/runtime/scheme.go b/pkg/runtime/scheme.go index e05d827092d..c5450e3dea5 100644 --- a/pkg/runtime/scheme.go +++ b/pkg/runtime/scheme.go @@ -459,15 +459,12 @@ func (s *Scheme) DecodeInto(data []byte, obj Object) error { } // Copy does a deep copy of an API object. Useful mostly for tests. -// TODO(dbsmith): implement directly instead of via Encode/Decode -// TODO(claytonc): Copy cannot be used for objects which do not encode type information, such -// as lists of runtime.Objects -func (s *Scheme) Copy(obj Object) (Object, error) { - data, err := s.EncodeToVersion(obj, "") +func (s *Scheme) Copy(src Object) (Object, error) { + dst, err := s.raw.DeepCopy(src) if err != nil { return nil, err } - return s.Decode(data) + return dst.(Object), nil } func (s *Scheme) CopyOrDie(obj Object) Object { diff --git a/pkg/tools/etcd_helper.go b/pkg/tools/etcd_helper.go index 954b0d80bd3..ca5ce551148 100644 --- a/pkg/tools/etcd_helper.go +++ b/pkg/tools/etcd_helper.go @@ -102,6 +102,7 @@ func recordEtcdRequestLatency(verb, resource string, startTime time.Time) { type EtcdHelper struct { Client EtcdGetSet Codec runtime.Codec + Copier runtime.ObjectCopier // optional, no atomic operations can be performed without this interface Versioner EtcdVersioner // prefix for all etcd keys @@ -119,11 +120,13 @@ type EtcdHelper struct { // NewEtcdHelper creates a helper that works against objects that use the internal // Kubernetes API objects. +// TODO: Refactor to take a runtiem.ObjectCopier func NewEtcdHelper(client EtcdGetSet, codec runtime.Codec, prefix string) EtcdHelper { return EtcdHelper{ Client: client, Codec: codec, Versioner: APIObjectVersioner{}, + Copier: api.Scheme, PathPrefix: prefix, cache: util.NewCache(maxEtcdCacheEntries), } @@ -237,7 +240,7 @@ func (h *EtcdHelper) getFromCache(index uint64) (runtime.Object, bool) { if found { // We should not return the object itself to avoid poluting the cache if someone // modifies returned values. - objCopy, err := api.Scheme.DeepCopy(obj) + objCopy, err := h.Copier.Copy(obj.(runtime.Object)) if err != nil { glog.Errorf("Error during DeepCopy of cached object: %q", err) return nil, false @@ -254,7 +257,7 @@ func (h *EtcdHelper) addToCache(index uint64, obj runtime.Object) { defer func() { cacheAddLatency.Observe(float64(time.Since(startTime) / time.Microsecond)) }() - objCopy, err := api.Scheme.DeepCopy(obj) + objCopy, err := h.Copier.Copy(obj) if err != nil { glog.Errorf("Error during DeepCopy of cached object: %q", err) return