Skip to content

Commit 0b49deb

Browse files
committed
replace usage of kapi.Scheme.Copy() with .DeepCopy()
1 parent d485992 commit 0b49deb

File tree

13 files changed

+27
-82
lines changed

13 files changed

+27
-82
lines changed

pkg/apps/strategy/support/lifecycle_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ func TestHookExecutor_executeExecNewPodSucceeded(t *testing.T) {
109109
go func() {
110110
<-podCreated
111111
podsWatch.Add(createdPod)
112-
podCopy, _ := kapi.Scheme.Copy(createdPod)
113-
updatedPod := podCopy.(*kapi.Pod)
112+
updatedPod := createdPod.DeepCopy()
114113
updatedPod.Status.Phase = kapi.PodSucceeded
115114
podsWatch.Modify(updatedPod)
116115
}()
@@ -175,8 +174,7 @@ func TestHookExecutor_executeExecNewPodFailed(t *testing.T) {
175174
go func() {
176175
<-podCreated
177176
podsWatch.Add(createdPod)
178-
podCopy, _ := kapi.Scheme.Copy(createdPod)
179-
updatedPod := podCopy.(*kapi.Pod)
177+
updatedPod := createdPod.DeepCopy()
180178
updatedPod.Status.Phase = kapi.PodFailed
181179
podsWatch.Modify(updatedPod)
182180
}()

pkg/build/controller/policy/policy.go

-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package policy
33
import (
44
"github.com/golang/glog"
55

6-
kapi "k8s.io/kubernetes/pkg/api"
7-
86
buildapi "github.com/openshift/origin/pkg/build/apis/build"
97
buildclient "github.com/openshift/origin/pkg/build/client"
108
buildlister "github.com/openshift/origin/pkg/build/generated/listers/build/internalversion"
@@ -107,11 +105,3 @@ func GetNextConfigBuild(lister buildlister.BuildLister, namespace, buildConfigNa
107105
}
108106
return nextBuilds, hasRunningBuilds, nil
109107
}
110-
111-
func copyOrDie(build *buildapi.Build) *buildapi.Build {
112-
obj, err := kapi.Scheme.Copy(build)
113-
if err != nil {
114-
panic(err)
115-
}
116-
return obj.(*buildapi.Build)
117-
}

pkg/build/controller/policy/serial_latest_only.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (s *SerialLatestOnlyPolicy) cancelPreviousBuilds(build *buildapi.Build) []e
8080
var result = []error{}
8181
for _, b := range builds {
8282
err := wait.Poll(500*time.Millisecond, 5*time.Second, func() (bool, error) {
83-
b = copyOrDie(b)
83+
b = b.DeepCopy()
8484
b.Status.Cancelled = true
8585
err := s.BuildUpdater.Update(b.Namespace, b)
8686
if err != nil && errors.IsConflict(err) {

pkg/build/generator/generator.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,7 @@ func (g *BuildGenerator) generateBuildFromConfig(ctx apirequest.Context, bc *bui
458458
// Need to copy the buildConfig here so that it doesn't share pointers with
459459
// the build object which could be (will be) modified later.
460460
buildName := getNextBuildName(bc)
461-
obj, _ := kapi.Scheme.Copy(bc)
462-
bcCopy := obj.(*buildapi.BuildConfig)
461+
bcCopy := bc.DeepCopy()
463462
serviceAccount := getServiceAccount(bcCopy, g.DefaultServiceAccountName)
464463
t := true
465464
build := &buildapi.Build{
@@ -791,8 +790,7 @@ func UpdateCustomImageEnv(strategy *buildapi.CustomBuildStrategy, newImage strin
791790

792791
// generateBuildFromBuild creates a new build based on a given Build.
793792
func generateBuildFromBuild(build *buildapi.Build, buildConfig *buildapi.BuildConfig) *buildapi.Build {
794-
obj, _ := kapi.Scheme.Copy(build)
795-
buildCopy := obj.(*buildapi.Build)
793+
buildCopy := build.DeepCopy()
796794

797795
newBuild := &buildapi.Build{
798796
Spec: buildCopy.Spec,

pkg/diagnostics/cluster/route_validation.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
99
"k8s.io/apimachinery/pkg/util/validation/field"
1010
"k8s.io/client-go/rest"
11-
kapi "k8s.io/kubernetes/pkg/api"
1211
kapihelper "k8s.io/kubernetes/pkg/api/helper"
1312
"k8s.io/kubernetes/pkg/apis/authorization"
1413
authorizationtypedclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/authorization/internalversion"
@@ -79,13 +78,7 @@ func (d *RouteCertificateValidation) Check() types.DiagnosticResult {
7978
}
8079

8180
for _, route := range routes.Items {
82-
copied, err := kapi.Scheme.Copy(&route)
83-
if err != nil {
84-
r.Error("DRouCert2003", err, fmt.Errorf("unable to copy route: %v", err).Error())
85-
return r
86-
}
87-
original := copied.(*routeapi.Route)
88-
81+
original := route.DeepCopy()
8982
errs := validation.ExtendedValidateRoute(&route)
9083

9184
if len(errs) == 0 {

pkg/image/controller/trigger/image_trigger_controller_test.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -956,12 +956,7 @@ func updateBuildConfigImages(bc *buildapi.BuildConfig, tagRetriever trigger.TagR
956956
continue
957957
}
958958
if updated == nil {
959-
copied, err := kapi.Scheme.Copy(bc)
960-
if err != nil {
961-
return nil, err
962-
}
963-
bc = copied.(*buildapi.BuildConfig)
964-
updated = bc
959+
updated = bc.DeepCopy()
965960
}
966961
p = bc.Spec.Triggers[i].ImageChange
967962
p.LastTriggeredImageID = latest

pkg/image/trigger/deploymentconfigs/deploymentconfigs.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,7 @@ func UpdateDeploymentConfigImages(dc *appsapi.DeploymentConfig, tagRetriever tri
164164
if updated != nil {
165165
return
166166
}
167-
copied, err := kapi.Scheme.Copy(dc)
168-
if err != nil {
169-
return
170-
}
171-
dc = copied.(*appsapi.DeploymentConfig)
167+
dc = dc.DeepCopy()
172168
updated = dc
173169
}
174170

pkg/quota/admission/clusterresourcequota/accessor.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,9 @@ func (e *clusterQuotaAccessor) UpdateQuotaStatus(newQuota *kapi.ResourceQuota) e
6363
}
6464
clusterQuota = e.checkCache(clusterQuota)
6565

66-
// make a copy
67-
obj, err := kapi.Scheme.Copy(clusterQuota)
68-
if err != nil {
69-
return err
70-
}
7166
// re-assign objectmeta
72-
clusterQuota = obj.(*quotaapi.ClusterResourceQuota)
67+
// make a copy
68+
clusterQuota = clusterQuota.DeepCopy()
7369
clusterQuota.ObjectMeta = newQuota.ObjectMeta
7470
clusterQuota.Namespace = ""
7571

pkg/quota/controller/clusterquotamapping/clusterquotamapping_test.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,7 @@ func runFuzzer(t *testing.T) {
110110

111111
quota := NewQuota(name)
112112
finalQuotas[name] = quota
113-
copied, err := kapi.Scheme.Copy(quota)
114-
if err != nil {
115-
t.Fatal(err)
116-
}
113+
copied := quota.DeepCopy()
117114
if exists {
118115
quotaActions[name] = append(quotaActions[name], fmt.Sprintf("updating %v to %v", name, quota.Spec.Selector))
119116
quotaWatch.Modify(copied)
@@ -149,10 +146,7 @@ func runFuzzer(t *testing.T) {
149146

150147
ns := NewNamespace(name)
151148
finalNamespaces[name] = ns
152-
copied, err := kapi.Scheme.Copy(ns)
153-
if err != nil {
154-
t.Fatal(err)
155-
}
149+
copied := ns.DeepCopy()
156150
if exists {
157151
namespaceActions[name] = append(namespaceActions[name], fmt.Sprintf("updating %v to %v", name, ns.Labels))
158152
nsWatch.Modify(copied)

pkg/quota/controller/clusterquotareconciliation/reconciliation_controller.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,7 @@ func (c *ClusterQuotaReconcilationController) worker() {
248248

249249
// syncResourceQuotaFromKey syncs a quota key
250250
func (c *ClusterQuotaReconcilationController) syncQuotaForNamespaces(originalQuota *quotaapi.ClusterResourceQuota, workItems []workItem) (error, []workItem /* to retry */) {
251-
obj, err := kapi.Scheme.Copy(originalQuota)
252-
if err != nil {
253-
return err, workItems
254-
}
255-
quota := obj.(*quotaapi.ClusterResourceQuota)
251+
quota := originalQuota.DeepCopy()
256252

257253
// get the list of namespaces that match this cluster quota
258254
matchingNamespaceNamesList, quotaSelector := c.clusterQuotaMapper.GetNamespacesFor(quota.Name)

pkg/route/apis/route/validation/validation_test.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
99
"k8s.io/apimachinery/pkg/util/diff"
1010
"k8s.io/apimachinery/pkg/util/intstr"
11-
kapi "k8s.io/kubernetes/pkg/api"
1211

1312
routeapi "github.com/openshift/origin/pkg/route/apis/route"
1413
)
@@ -1291,11 +1290,7 @@ func TestValidateRouteUpdate(t *testing.T) {
12911290
}
12921291

12931292
for i, tc := range tests {
1294-
copied, err := kapi.Scheme.Copy(tc.route)
1295-
if err != nil {
1296-
t.Fatal(err)
1297-
}
1298-
newRoute := copied.(*routeapi.Route)
1293+
newRoute := tc.route.DeepCopy()
12991294
tc.change(newRoute)
13001295
errs := ValidateRouteUpdate(newRoute, tc.route)
13011296
if len(errs) != tc.expectedErrors {

pkg/route/registry/route/strategy_test.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"k8s.io/apimachinery/pkg/util/validation/field"
1010
"k8s.io/apiserver/pkg/authentication/user"
1111
apirequest "k8s.io/apiserver/pkg/endpoints/request"
12-
kapi "k8s.io/kubernetes/pkg/api"
1312
authorizationapi "k8s.io/kubernetes/pkg/apis/authorization"
1413

1514
routeapi "github.com/openshift/origin/pkg/route/apis/route"
@@ -88,21 +87,21 @@ func TestEmptyDefaultCACertificate(t *testing.T) {
8887
},
8988
}
9089
for i, testCase := range testCases {
91-
copied, _ := kapi.Scheme.Copy(testCase.route)
92-
if err := DecorateLegacyRouteWithEmptyDestinationCACertificates(copied.(*routeapi.Route)); err != nil {
90+
copied := testCase.route.DeepCopy()
91+
if err := DecorateLegacyRouteWithEmptyDestinationCACertificates(copied); err != nil {
9392
t.Errorf("%d: unexpected error: %v", i, err)
9493
continue
9594
}
96-
routeStrategy{}.PrepareForCreate(nil, copied.(*routeapi.Route))
95+
routeStrategy{}.PrepareForCreate(nil, copied)
9796
if !reflect.DeepEqual(testCase.route, copied) {
9897
t.Errorf("%d: unexpected change: %#v", i, copied)
9998
continue
10099
}
101-
if err := DecorateLegacyRouteWithEmptyDestinationCACertificates(copied.(*routeapi.Route)); err != nil {
100+
if err := DecorateLegacyRouteWithEmptyDestinationCACertificates(copied); err != nil {
102101
t.Errorf("%d: unexpected error: %v", i, err)
103102
continue
104103
}
105-
routeStrategy{}.PrepareForUpdate(nil, copied.(*routeapi.Route), &routeapi.Route{})
104+
routeStrategy{}.PrepareForUpdate(nil, copied, &routeapi.Route{})
106105
if !reflect.DeepEqual(testCase.route, copied) {
107106
t.Errorf("%d: unexpected change: %#v", i, copied)
108107
continue

pkg/security/controller/namespace_security_allocation_controller.go

+8-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
kcoreclient "k8s.io/client-go/kubernetes/typed/core/v1"
1212
"k8s.io/client-go/tools/cache"
1313
"k8s.io/client-go/util/workqueue"
14-
kapi "k8s.io/kubernetes/pkg/api"
1514

1615
"github.com/golang/glog"
1716
"github.com/openshift/origin/pkg/security"
@@ -157,14 +156,10 @@ func (c *NamespaceSecurityDefaultsController) allocate(ns *v1.Namespace) error {
157156
return nil
158157
}
159158

160-
obj, err := kapi.Scheme.Copy(ns)
161-
if err != nil {
162-
return err
163-
}
164-
ns = obj.(*v1.Namespace)
159+
nsCopy := ns.DeepCopy()
165160

166-
if ns.Annotations == nil {
167-
ns.Annotations = make(map[string]string)
161+
if nsCopy.Annotations == nil {
162+
nsCopy.Annotations = make(map[string]string)
168163
}
169164

170165
// do uid allocation
@@ -174,15 +169,15 @@ func (c *NamespaceSecurityDefaultsController) allocate(ns *v1.Namespace) error {
174169
}
175170
tx.Add(func() error { return c.uidAllocator.Release(block) })
176171

177-
ns.Annotations[security.UIDRangeAnnotation] = block.String()
178-
ns.Annotations[security.SupplementalGroupsAnnotation] = block.String()
179-
if _, ok := ns.Annotations[security.MCSAnnotation]; !ok {
172+
nsCopy.Annotations[security.UIDRangeAnnotation] = block.String()
173+
nsCopy.Annotations[security.SupplementalGroupsAnnotation] = block.String()
174+
if _, ok := nsCopy.Annotations[security.MCSAnnotation]; !ok {
180175
if label := c.mcsAllocator(block); label != nil {
181-
ns.Annotations[security.MCSAnnotation] = label.String()
176+
nsCopy.Annotations[security.MCSAnnotation] = label.String()
182177
}
183178
}
184179

185-
_, err = c.client.Update(ns)
180+
_, err = c.client.Update(nsCopy)
186181
if err == nil {
187182
tx.Commit()
188183
}

0 commit comments

Comments
 (0)