mirror of
https://github.com/kubernetes/client-go.git
synced 2025-07-05 03:06:23 +00:00
client-go: simplify deepcopy calls
Kubernetes-commit: ed423054ba0089a6d58fb0e048fe1acfd966f0d7
This commit is contained in:
parent
7be903ab67
commit
7cdaec7d07
@ -183,10 +183,7 @@ func (t *tracker) List(gvr schema.GroupVersionResource, gvk schema.GroupVersionK
|
|||||||
if err := meta.SetList(list, matchingObjs); err != nil {
|
if err := meta.SetList(list, matchingObjs); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if list, err = t.scheme.Copy(list); err != nil {
|
return list.DeepCopyObject(), nil
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return list, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tracker) Get(gvr schema.GroupVersionResource, ns, name string) (runtime.Object, error) {
|
func (t *tracker) Get(gvr schema.GroupVersionResource, ns, name string) (runtime.Object, error) {
|
||||||
@ -214,11 +211,7 @@ func (t *tracker) Get(gvr schema.GroupVersionResource, ns, name string) (runtime
|
|||||||
// Only one object should match in the tracker if it works
|
// Only one object should match in the tracker if it works
|
||||||
// correctly, as Add/Update methods enforce kind/namespace/name
|
// correctly, as Add/Update methods enforce kind/namespace/name
|
||||||
// uniqueness.
|
// uniqueness.
|
||||||
obj, err := t.scheme.Copy(matchingObjs[0])
|
obj := matchingObjs[0].DeepCopyObject()
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if status, ok := obj.(*metav1.Status); ok {
|
if status, ok := obj.(*metav1.Status); ok {
|
||||||
if status.Status != metav1.StatusSuccess {
|
if status.Status != metav1.StatusSuccess {
|
||||||
return nil, &errors.StatusError{ErrStatus: *status}
|
return nil, &errors.StatusError{ErrStatus: *status}
|
||||||
@ -280,10 +273,7 @@ func (t *tracker) add(gvr schema.GroupVersionResource, obj runtime.Object, ns st
|
|||||||
// To avoid the object from being accidentally modified by caller
|
// To avoid the object from being accidentally modified by caller
|
||||||
// after it's been added to the tracker, we always store the deep
|
// after it's been added to the tracker, we always store the deep
|
||||||
// copy.
|
// copy.
|
||||||
obj, err := t.scheme.Copy(obj)
|
obj = obj.DeepCopyObject()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
newMeta, err := meta.Accessor(obj)
|
newMeta, err := meta.Accessor(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
2
tools/cache/BUILD
vendored
2
tools/cache/BUILD
vendored
@ -33,7 +33,6 @@ go_test(
|
|||||||
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/watch:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/watch:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/kubernetes/scheme:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/tools/cache/testing:go_default_library",
|
"//vendor/k8s.io/client-go/tools/cache/testing:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@ -78,7 +77,6 @@ go_library(
|
|||||||
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/watch:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/watch:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/kubernetes/scheme:go_default_library",
|
|
||||||
"//vendor/k8s.io/client-go/rest:go_default_library",
|
"//vendor/k8s.io/client-go/rest:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/tools/pager:go_default_library",
|
"//vendor/k8s.io/client-go/tools/pager:go_default_library",
|
||||||
],
|
],
|
||||||
|
7
tools/cache/index_test.go
vendored
7
tools/cache/index_test.go
vendored
@ -22,7 +22,6 @@ import (
|
|||||||
|
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/client-go/kubernetes/scheme"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func testIndexFunc(obj interface{}) ([]string, error) {
|
func testIndexFunc(obj interface{}) ([]string, error) {
|
||||||
@ -119,11 +118,7 @@ func TestMultiIndexKeys(t *testing.T) {
|
|||||||
t.Errorf("Expected 0 pods but got %v", len(elmoPods))
|
t.Errorf("Expected 0 pods but got %v", len(elmoPods))
|
||||||
}
|
}
|
||||||
|
|
||||||
obj, err := scheme.Scheme.DeepCopy(pod2)
|
copyOfPod2 := pod2.DeepCopy()
|
||||||
if err != nil {
|
|
||||||
t.Errorf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
copyOfPod2 := obj.(*v1.Pod)
|
|
||||||
copyOfPod2.Annotations["users"] = "oscar"
|
copyOfPod2.Annotations["users"] = "oscar"
|
||||||
index.Update(copyOfPod2)
|
index.Update(copyOfPod2)
|
||||||
bertPods, err = index.ByIndex("byUser", "bert")
|
bertPods, err = index.ByIndex("byUser", "bert")
|
||||||
|
16
tools/cache/mutation_detector.go
vendored
16
tools/cache/mutation_detector.go
vendored
@ -26,7 +26,6 @@ import (
|
|||||||
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/diff"
|
"k8s.io/apimachinery/pkg/util/diff"
|
||||||
"k8s.io/client-go/kubernetes/scheme"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var mutationDetectionEnabled = false
|
var mutationDetectionEnabled = false
|
||||||
@ -96,18 +95,13 @@ func (d *defaultCacheMutationDetector) AddObject(obj interface{}) {
|
|||||||
if _, ok := obj.(DeletedFinalStateUnknown); ok {
|
if _, ok := obj.(DeletedFinalStateUnknown); ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, ok := obj.(runtime.Object); !ok {
|
if obj, ok := obj.(runtime.Object); ok {
|
||||||
return
|
copiedObj := obj.DeepCopyObject()
|
||||||
}
|
|
||||||
|
|
||||||
copiedObj, err := scheme.Scheme.Copy(obj.(runtime.Object))
|
d.lock.Lock()
|
||||||
if err != nil {
|
defer d.lock.Unlock()
|
||||||
return
|
d.cachedObjs = append(d.cachedObjs, cacheObj{cached: obj, copied: copiedObj})
|
||||||
}
|
}
|
||||||
|
|
||||||
d.lock.Lock()
|
|
||||||
defer d.lock.Unlock()
|
|
||||||
d.cachedObjs = append(d.cachedObjs, cacheObj{cached: obj, copied: copiedObj})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *defaultCacheMutationDetector) CompareObjects() {
|
func (d *defaultCacheMutationDetector) CompareObjects() {
|
||||||
|
1
tools/cache/testing/BUILD
vendored
1
tools/cache/testing/BUILD
vendored
@ -27,7 +27,6 @@ go_library(
|
|||||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/watch:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/watch:go_default_library",
|
||||||
"//vendor/k8s.io/client-go/kubernetes/scheme:go_default_library",
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
13
tools/cache/testing/fake_controller_source.go
vendored
13
tools/cache/testing/fake_controller_source.go
vendored
@ -28,7 +28,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
"k8s.io/client-go/kubernetes/scheme"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewFakeControllerSource() *FakeControllerSource {
|
func NewFakeControllerSource() *FakeControllerSource {
|
||||||
@ -153,11 +152,7 @@ func (f *FakeControllerSource) getListItemsLocked() ([]runtime.Object, error) {
|
|||||||
// Otherwise, if they make a change and write it back, they
|
// Otherwise, if they make a change and write it back, they
|
||||||
// will inadvertently change our canonical copy (in
|
// will inadvertently change our canonical copy (in
|
||||||
// addition to racing with other clients).
|
// addition to racing with other clients).
|
||||||
objCopy, err := scheme.Scheme.DeepCopy(obj)
|
list = append(list, obj.DeepCopyObject())
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
list = append(list, objCopy.(runtime.Object))
|
|
||||||
}
|
}
|
||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
@ -242,11 +237,7 @@ func (f *FakeControllerSource) Watch(options metav1.ListOptions) (watch.Interfac
|
|||||||
// it back, they will inadvertently change the our
|
// it back, they will inadvertently change the our
|
||||||
// canonical copy (in addition to racing with other
|
// canonical copy (in addition to racing with other
|
||||||
// clients).
|
// clients).
|
||||||
objCopy, err := scheme.Scheme.DeepCopy(c.Object)
|
changes = append(changes, watch.Event{Type: c.Type, Object: c.Object.DeepCopyObject()})
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
changes = append(changes, watch.Event{Type: c.Type, Object: objCopy.(runtime.Object)})
|
|
||||||
}
|
}
|
||||||
return f.Broadcaster.WatchWithPrefix(changes), nil
|
return f.Broadcaster.WatchWithPrefix(changes), nil
|
||||||
} else if rc > len(f.changes) {
|
} else if rc > len(f.changes) {
|
||||||
|
Loading…
Reference in New Issue
Block a user