mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-12 05:21:58 +00:00
Use runtime.Copier instead of hardcoding api.Scheme
Allow other schemes to be supported by etcd_helper.go runtime.Scheme.Copy() should be using the built in DeepCopy()
This commit is contained in:
parent
8ebd896351
commit
6970dda54e
@ -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.
|
// Copy does a deep copy of an API object. Useful mostly for tests.
|
||||||
// TODO(dbsmith): implement directly instead of via Encode/Decode
|
func (s *Scheme) Copy(src Object) (Object, error) {
|
||||||
// TODO(claytonc): Copy cannot be used for objects which do not encode type information, such
|
dst, err := s.raw.DeepCopy(src)
|
||||||
// as lists of runtime.Objects
|
|
||||||
func (s *Scheme) Copy(obj Object) (Object, error) {
|
|
||||||
data, err := s.EncodeToVersion(obj, "")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return s.Decode(data)
|
return dst.(Object), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scheme) CopyOrDie(obj Object) Object {
|
func (s *Scheme) CopyOrDie(obj Object) Object {
|
||||||
|
@ -102,6 +102,7 @@ func recordEtcdRequestLatency(verb, resource string, startTime time.Time) {
|
|||||||
type EtcdHelper struct {
|
type EtcdHelper struct {
|
||||||
Client EtcdGetSet
|
Client EtcdGetSet
|
||||||
Codec runtime.Codec
|
Codec runtime.Codec
|
||||||
|
Copier runtime.ObjectCopier
|
||||||
// optional, no atomic operations can be performed without this interface
|
// optional, no atomic operations can be performed without this interface
|
||||||
Versioner EtcdVersioner
|
Versioner EtcdVersioner
|
||||||
// prefix for all etcd keys
|
// prefix for all etcd keys
|
||||||
@ -119,11 +120,13 @@ type EtcdHelper struct {
|
|||||||
|
|
||||||
// NewEtcdHelper creates a helper that works against objects that use the internal
|
// NewEtcdHelper creates a helper that works against objects that use the internal
|
||||||
// Kubernetes API objects.
|
// Kubernetes API objects.
|
||||||
|
// TODO: Refactor to take a runtiem.ObjectCopier
|
||||||
func NewEtcdHelper(client EtcdGetSet, codec runtime.Codec, prefix string) EtcdHelper {
|
func NewEtcdHelper(client EtcdGetSet, codec runtime.Codec, prefix string) EtcdHelper {
|
||||||
return EtcdHelper{
|
return EtcdHelper{
|
||||||
Client: client,
|
Client: client,
|
||||||
Codec: codec,
|
Codec: codec,
|
||||||
Versioner: APIObjectVersioner{},
|
Versioner: APIObjectVersioner{},
|
||||||
|
Copier: api.Scheme,
|
||||||
PathPrefix: prefix,
|
PathPrefix: prefix,
|
||||||
cache: util.NewCache(maxEtcdCacheEntries),
|
cache: util.NewCache(maxEtcdCacheEntries),
|
||||||
}
|
}
|
||||||
@ -237,7 +240,7 @@ func (h *EtcdHelper) getFromCache(index uint64) (runtime.Object, bool) {
|
|||||||
if found {
|
if found {
|
||||||
// We should not return the object itself to avoid poluting the cache if someone
|
// We should not return the object itself to avoid poluting the cache if someone
|
||||||
// modifies returned values.
|
// modifies returned values.
|
||||||
objCopy, err := api.Scheme.DeepCopy(obj)
|
objCopy, err := h.Copier.Copy(obj.(runtime.Object))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Error during DeepCopy of cached object: %q", err)
|
glog.Errorf("Error during DeepCopy of cached object: %q", err)
|
||||||
return nil, false
|
return nil, false
|
||||||
@ -254,7 +257,7 @@ func (h *EtcdHelper) addToCache(index uint64, obj runtime.Object) {
|
|||||||
defer func() {
|
defer func() {
|
||||||
cacheAddLatency.Observe(float64(time.Since(startTime) / time.Microsecond))
|
cacheAddLatency.Observe(float64(time.Since(startTime) / time.Microsecond))
|
||||||
}()
|
}()
|
||||||
objCopy, err := api.Scheme.DeepCopy(obj)
|
objCopy, err := h.Copier.Copy(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Error during DeepCopy of cached object: %q", err)
|
glog.Errorf("Error during DeepCopy of cached object: %q", err)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user