diff --git a/pkg/kubectl/cmd/drain_test.go b/pkg/kubectl/cmd/drain_test.go index 7a28cf62f40..9cd2e330b28 100644 --- a/pkg/kubectl/cmd/drain_test.go +++ b/pkg/kubectl/cmd/drain_test.go @@ -74,10 +74,9 @@ func TestMain(m *testing.M) { }, Status: v1.NodeStatus{}, } - clone, _ := api.Scheme.DeepCopy(node) // A copy of the same node, but cordoned. - cordoned_node = clone.(*v1.Node) + cordoned_node = node.DeepCopy() cordoned_node.Spec.Unschedulable = true os.Exit(m.Run()) } diff --git a/pkg/kubectl/cmd/patch_test.go b/pkg/kubectl/cmd/patch_test.go index 6c23d126fbd..7bfd7146100 100644 --- a/pkg/kubectl/cmd/patch_test.go +++ b/pkg/kubectl/cmd/patch_test.go @@ -130,8 +130,7 @@ func TestPatchNoop(t *testing.T) { // Patched { - copied, _ := api.Scheme.DeepCopy(patchObject) - patchObject = copied.(*api.Service) + patchObject = patchObject.DeepCopy() if patchObject.Annotations == nil { patchObject.Annotations = map[string]string{} } @@ -150,11 +149,7 @@ func TestPatchNoop(t *testing.T) { func TestPatchObjectFromFileOutput(t *testing.T) { _, svc, _ := testData() - svcCopyObj, err := api.Scheme.DeepCopy(&svc.Items[0]) - if err != nil { - t.Fatal(err) - } - svcCopy := svcCopyObj.(*api.Service) + svcCopy := svc.Items[0].DeepCopy() if svcCopy.Labels == nil { svcCopy.Labels = map[string]string{} } diff --git a/pkg/kubectl/cmd/taint_test.go b/pkg/kubectl/cmd/taint_test.go index d6b2939ddbf..0bdf71409ac 100644 --- a/pkg/kubectl/cmd/taint_test.go +++ b/pkg/kubectl/cmd/taint_test.go @@ -49,10 +49,9 @@ func generateNodeAndTaintedNode(oldTaints []v1.Taint, newTaints []v1.Taint) (*v1 }, Status: v1.NodeStatus{}, } - clone, _ := api.Scheme.DeepCopy(node) // A copy of the same node, but tainted. - taintedNode = clone.(*v1.Node) + taintedNode = node.DeepCopy() taintedNode.Spec.Taints = newTaints return node, taintedNode diff --git a/pkg/kubectl/cmd/util/editor/editoptions.go b/pkg/kubectl/cmd/util/editor/editoptions.go index f855cb16e7a..03e77da5e3f 100644 --- a/pkg/kubectl/cmd/util/editor/editoptions.go +++ b/pkg/kubectl/cmd/util/editor/editoptions.go @@ -445,10 +445,7 @@ func GetApplyPatch(obj runtime.Object, codec runtime.Encoder) ([]byte, []byte, t if err != nil { return nil, []byte(""), types.MergePatchType, err } - objCopy, err := api.Scheme.Copy(obj) - if err != nil { - return nil, beforeJSON, types.MergePatchType, err - } + objCopy := obj.DeepCopyObject() accessor := meta.NewAccessor() annotations, err := accessor.Annotations(objCopy) if err != nil { diff --git a/pkg/kubectl/delete_test.go b/pkg/kubectl/delete_test.go index 0d2f5ea9daa..66803de1470 100644 --- a/pkg/kubectl/delete_test.go +++ b/pkg/kubectl/delete_test.go @@ -201,10 +201,7 @@ func TestReplicationControllerStop(t *testing.T) { } for _, test := range tests { - copiedForWatch, err := api.Scheme.Copy(test.Objs[0]) - if err != nil { - t.Fatalf("%s unexpected error: %v", test.Name, err) - } + copiedForWatch := test.Objs[0].DeepCopyObject() fake := fake.NewSimpleClientset(test.Objs...) fakeWatch := watch.NewFake() fake.PrependWatchReactor("replicationcontrollers", testcore.DefaultWatchReactor(fakeWatch, nil)) @@ -214,7 +211,7 @@ func TestReplicationControllerStop(t *testing.T) { }() reaper := ReplicationControllerReaper{fake.Core(), time.Millisecond, time.Millisecond} - err = reaper.Stop(ns, name, 0, nil) + err := reaper.Stop(ns, name, 0, nil) if !reflect.DeepEqual(err, test.StopError) { t.Errorf("%s unexpected error: %v", test.Name, err) continue diff --git a/pkg/kubectl/rolling_updater.go b/pkg/kubectl/rolling_updater.go index b15e20c6e22..3259d6dd976 100644 --- a/pkg/kubectl/rolling_updater.go +++ b/pkg/kubectl/rolling_updater.go @@ -772,12 +772,8 @@ type updateRcFunc func(controller *api.ReplicationController) // 3. Update the resource func updateRcWithRetries(rcClient coreclient.ReplicationControllersGetter, namespace string, rc *api.ReplicationController, applyUpdate updateRcFunc) (*api.ReplicationController, error) { // Deep copy the rc in case we failed on Get during retry loop - obj, err := api.Scheme.Copy(rc) - if err != nil { - return nil, fmt.Errorf("failed to deep copy rc before updating it: %v", err) - } - oldRc := obj.(*api.ReplicationController) - err = retry.RetryOnConflict(retry.DefaultBackoff, func() (e error) { + oldRc := rc.DeepCopy() + err := retry.RetryOnConflict(retry.DefaultBackoff, func() (e error) { // Apply the update, then attempt to push it to the apiserver. applyUpdate(rc) if rc, e = rcClient.ReplicationControllers(namespace).Update(rc); e == nil { @@ -807,12 +803,8 @@ type updatePodFunc func(controller *api.Pod) // 3. Update the resource func updatePodWithRetries(podClient coreclient.PodsGetter, namespace string, pod *api.Pod, applyUpdate updatePodFunc) (*api.Pod, error) { // Deep copy the pod in case we failed on Get during retry loop - obj, err := api.Scheme.Copy(pod) - if err != nil { - return nil, fmt.Errorf("failed to deep copy pod before updating it: %v", err) - } - oldPod := obj.(*api.Pod) - err = retry.RetryOnConflict(retry.DefaultBackoff, func() (e error) { + oldPod := pod.DeepCopy() + err := retry.RetryOnConflict(retry.DefaultBackoff, func() (e error) { // Apply the update, then attempt to push it to the apiserver. applyUpdate(pod) if pod, e = podClient.Pods(namespace).Update(pod); e == nil { diff --git a/pkg/kubectl/testing/BUILD b/pkg/kubectl/testing/BUILD index cef4a41c33a..21e5a1e4588 100644 --- a/pkg/kubectl/testing/BUILD +++ b/pkg/kubectl/testing/BUILD @@ -6,6 +6,7 @@ load( go_library( name = "go_default_library", srcs = [ + "doc.go", "types.generated.go", "types.go", "zz_generated.deepcopy.go", diff --git a/pkg/kubectl/testing/doc.go b/pkg/kubectl/testing/doc.go new file mode 100644 index 00000000000..0ea50660981 --- /dev/null +++ b/pkg/kubectl/testing/doc.go @@ -0,0 +1,19 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// +k8s:deepcopy-gen=package + +package testing diff --git a/pkg/kubectl/testing/zz_generated.deepcopy.go b/pkg/kubectl/testing/zz_generated.deepcopy.go index d12b262572c..79c1b4003db 100644 --- a/pkg/kubectl/testing/zz_generated.deepcopy.go +++ b/pkg/kubectl/testing/zz_generated.deepcopy.go @@ -26,7 +26,9 @@ import ( reflect "reflect" ) -// Deprecated: GetGeneratedDeepCopyFuncs returns the generated funcs, since we aren't registering them. +// GetGeneratedDeepCopyFuncs returns the generated funcs, since we aren't registering them. +// +// Deprecated: deepcopy registration will go away when static deepcopy is fully implemented. func GetGeneratedDeepCopyFuncs() []conversion.GeneratedDeepCopyFunc { return []conversion.GeneratedDeepCopyFunc{ {Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error { @@ -61,19 +63,19 @@ func (in *TestStruct) DeepCopyInto(out *TestStruct) { return } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, creating a new TestStruct. -func (x *TestStruct) DeepCopy() *TestStruct { - if x == nil { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TestStruct. +func (in *TestStruct) DeepCopy() *TestStruct { + if in == nil { return nil } out := new(TestStruct) - x.DeepCopyInto(out) + in.DeepCopyInto(out) return out } // DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (x *TestStruct) DeepCopyObject() runtime.Object { - if c := x.DeepCopy(); c != nil { +func (in *TestStruct) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { return c } else { return nil