Skip to content

Commit

Permalink
replace usage of kapi.Scheme.Copy() with .DeepCopy()
Browse files Browse the repository at this point in the history
  • Loading branch information
mfojtik committed Nov 24, 2017
1 parent d485992 commit 0b49deb
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 82 deletions.
6 changes: 2 additions & 4 deletions pkg/apps/strategy/support/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ func TestHookExecutor_executeExecNewPodSucceeded(t *testing.T) {
go func() {
<-podCreated
podsWatch.Add(createdPod)
podCopy, _ := kapi.Scheme.Copy(createdPod)
updatedPod := podCopy.(*kapi.Pod)
updatedPod := createdPod.DeepCopy()
updatedPod.Status.Phase = kapi.PodSucceeded
podsWatch.Modify(updatedPod)
}()
Expand Down Expand Up @@ -175,8 +174,7 @@ func TestHookExecutor_executeExecNewPodFailed(t *testing.T) {
go func() {
<-podCreated
podsWatch.Add(createdPod)
podCopy, _ := kapi.Scheme.Copy(createdPod)
updatedPod := podCopy.(*kapi.Pod)
updatedPod := createdPod.DeepCopy()
updatedPod.Status.Phase = kapi.PodFailed
podsWatch.Modify(updatedPod)
}()
Expand Down
10 changes: 0 additions & 10 deletions pkg/build/controller/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package policy
import (
"github.com/golang/glog"

kapi "k8s.io/kubernetes/pkg/api"

buildapi "github.com/openshift/origin/pkg/build/apis/build"
buildclient "github.com/openshift/origin/pkg/build/client"
buildlister "github.com/openshift/origin/pkg/build/generated/listers/build/internalversion"
Expand Down Expand Up @@ -107,11 +105,3 @@ func GetNextConfigBuild(lister buildlister.BuildLister, namespace, buildConfigNa
}
return nextBuilds, hasRunningBuilds, nil
}

func copyOrDie(build *buildapi.Build) *buildapi.Build {
obj, err := kapi.Scheme.Copy(build)
if err != nil {
panic(err)
}
return obj.(*buildapi.Build)
}
2 changes: 1 addition & 1 deletion pkg/build/controller/policy/serial_latest_only.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (s *SerialLatestOnlyPolicy) cancelPreviousBuilds(build *buildapi.Build) []e
var result = []error{}
for _, b := range builds {
err := wait.Poll(500*time.Millisecond, 5*time.Second, func() (bool, error) {
b = copyOrDie(b)
b = b.DeepCopy()
b.Status.Cancelled = true
err := s.BuildUpdater.Update(b.Namespace, b)
if err != nil && errors.IsConflict(err) {
Expand Down
6 changes: 2 additions & 4 deletions pkg/build/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,7 @@ func (g *BuildGenerator) generateBuildFromConfig(ctx apirequest.Context, bc *bui
// Need to copy the buildConfig here so that it doesn't share pointers with
// the build object which could be (will be) modified later.
buildName := getNextBuildName(bc)
obj, _ := kapi.Scheme.Copy(bc)
bcCopy := obj.(*buildapi.BuildConfig)
bcCopy := bc.DeepCopy()
serviceAccount := getServiceAccount(bcCopy, g.DefaultServiceAccountName)
t := true
build := &buildapi.Build{
Expand Down Expand Up @@ -791,8 +790,7 @@ func UpdateCustomImageEnv(strategy *buildapi.CustomBuildStrategy, newImage strin

// generateBuildFromBuild creates a new build based on a given Build.
func generateBuildFromBuild(build *buildapi.Build, buildConfig *buildapi.BuildConfig) *buildapi.Build {
obj, _ := kapi.Scheme.Copy(build)
buildCopy := obj.(*buildapi.Build)
buildCopy := build.DeepCopy()

newBuild := &buildapi.Build{
Spec: buildCopy.Spec,
Expand Down
9 changes: 1 addition & 8 deletions pkg/diagnostics/cluster/route_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/client-go/rest"
kapi "k8s.io/kubernetes/pkg/api"
kapihelper "k8s.io/kubernetes/pkg/api/helper"
"k8s.io/kubernetes/pkg/apis/authorization"
authorizationtypedclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/authorization/internalversion"
Expand Down Expand Up @@ -79,13 +78,7 @@ func (d *RouteCertificateValidation) Check() types.DiagnosticResult {
}

for _, route := range routes.Items {
copied, err := kapi.Scheme.Copy(&route)
if err != nil {
r.Error("DRouCert2003", err, fmt.Errorf("unable to copy route: %v", err).Error())
return r
}
original := copied.(*routeapi.Route)

original := route.DeepCopy()
errs := validation.ExtendedValidateRoute(&route)

if len(errs) == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -956,12 +956,7 @@ func updateBuildConfigImages(bc *buildapi.BuildConfig, tagRetriever trigger.TagR
continue
}
if updated == nil {
copied, err := kapi.Scheme.Copy(bc)
if err != nil {
return nil, err
}
bc = copied.(*buildapi.BuildConfig)
updated = bc
updated = bc.DeepCopy()
}
p = bc.Spec.Triggers[i].ImageChange
p.LastTriggeredImageID = latest
Expand Down
6 changes: 1 addition & 5 deletions pkg/image/trigger/deploymentconfigs/deploymentconfigs.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,7 @@ func UpdateDeploymentConfigImages(dc *appsapi.DeploymentConfig, tagRetriever tri
if updated != nil {
return
}
copied, err := kapi.Scheme.Copy(dc)
if err != nil {
return
}
dc = copied.(*appsapi.DeploymentConfig)
dc = dc.DeepCopy()
updated = dc
}

Expand Down
8 changes: 2 additions & 6 deletions pkg/quota/admission/clusterresourcequota/accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,9 @@ func (e *clusterQuotaAccessor) UpdateQuotaStatus(newQuota *kapi.ResourceQuota) e
}
clusterQuota = e.checkCache(clusterQuota)

// make a copy
obj, err := kapi.Scheme.Copy(clusterQuota)
if err != nil {
return err
}
// re-assign objectmeta
clusterQuota = obj.(*quotaapi.ClusterResourceQuota)
// make a copy
clusterQuota = clusterQuota.DeepCopy()
clusterQuota.ObjectMeta = newQuota.ObjectMeta
clusterQuota.Namespace = ""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ func runFuzzer(t *testing.T) {

quota := NewQuota(name)
finalQuotas[name] = quota
copied, err := kapi.Scheme.Copy(quota)
if err != nil {
t.Fatal(err)
}
copied := quota.DeepCopy()
if exists {
quotaActions[name] = append(quotaActions[name], fmt.Sprintf("updating %v to %v", name, quota.Spec.Selector))
quotaWatch.Modify(copied)
Expand Down Expand Up @@ -149,10 +146,7 @@ func runFuzzer(t *testing.T) {

ns := NewNamespace(name)
finalNamespaces[name] = ns
copied, err := kapi.Scheme.Copy(ns)
if err != nil {
t.Fatal(err)
}
copied := ns.DeepCopy()
if exists {
namespaceActions[name] = append(namespaceActions[name], fmt.Sprintf("updating %v to %v", name, ns.Labels))
nsWatch.Modify(copied)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,7 @@ func (c *ClusterQuotaReconcilationController) worker() {

// syncResourceQuotaFromKey syncs a quota key
func (c *ClusterQuotaReconcilationController) syncQuotaForNamespaces(originalQuota *quotaapi.ClusterResourceQuota, workItems []workItem) (error, []workItem /* to retry */) {
obj, err := kapi.Scheme.Copy(originalQuota)
if err != nil {
return err, workItems
}
quota := obj.(*quotaapi.ClusterResourceQuota)
quota := originalQuota.DeepCopy()

// get the list of namespaces that match this cluster quota
matchingNamespaceNamesList, quotaSelector := c.clusterQuotaMapper.GetNamespacesFor(quota.Name)
Expand Down
7 changes: 1 addition & 6 deletions pkg/route/apis/route/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/apimachinery/pkg/util/intstr"
kapi "k8s.io/kubernetes/pkg/api"

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

for i, tc := range tests {
copied, err := kapi.Scheme.Copy(tc.route)
if err != nil {
t.Fatal(err)
}
newRoute := copied.(*routeapi.Route)
newRoute := tc.route.DeepCopy()
tc.change(newRoute)
errs := ValidateRouteUpdate(newRoute, tc.route)
if len(errs) != tc.expectedErrors {
Expand Down
11 changes: 5 additions & 6 deletions pkg/route/registry/route/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/authentication/user"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
kapi "k8s.io/kubernetes/pkg/api"
authorizationapi "k8s.io/kubernetes/pkg/apis/authorization"

routeapi "github.com/openshift/origin/pkg/route/apis/route"
Expand Down Expand Up @@ -88,21 +87,21 @@ func TestEmptyDefaultCACertificate(t *testing.T) {
},
}
for i, testCase := range testCases {
copied, _ := kapi.Scheme.Copy(testCase.route)
if err := DecorateLegacyRouteWithEmptyDestinationCACertificates(copied.(*routeapi.Route)); err != nil {
copied := testCase.route.DeepCopy()
if err := DecorateLegacyRouteWithEmptyDestinationCACertificates(copied); err != nil {
t.Errorf("%d: unexpected error: %v", i, err)
continue
}
routeStrategy{}.PrepareForCreate(nil, copied.(*routeapi.Route))
routeStrategy{}.PrepareForCreate(nil, copied)
if !reflect.DeepEqual(testCase.route, copied) {
t.Errorf("%d: unexpected change: %#v", i, copied)
continue
}
if err := DecorateLegacyRouteWithEmptyDestinationCACertificates(copied.(*routeapi.Route)); err != nil {
if err := DecorateLegacyRouteWithEmptyDestinationCACertificates(copied); err != nil {
t.Errorf("%d: unexpected error: %v", i, err)
continue
}
routeStrategy{}.PrepareForUpdate(nil, copied.(*routeapi.Route), &routeapi.Route{})
routeStrategy{}.PrepareForUpdate(nil, copied, &routeapi.Route{})
if !reflect.DeepEqual(testCase.route, copied) {
t.Errorf("%d: unexpected change: %#v", i, copied)
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
kcoreclient "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
kapi "k8s.io/kubernetes/pkg/api"

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

obj, err := kapi.Scheme.Copy(ns)
if err != nil {
return err
}
ns = obj.(*v1.Namespace)
nsCopy := ns.DeepCopy()

if ns.Annotations == nil {
ns.Annotations = make(map[string]string)
if nsCopy.Annotations == nil {
nsCopy.Annotations = make(map[string]string)
}

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

ns.Annotations[security.UIDRangeAnnotation] = block.String()
ns.Annotations[security.SupplementalGroupsAnnotation] = block.String()
if _, ok := ns.Annotations[security.MCSAnnotation]; !ok {
nsCopy.Annotations[security.UIDRangeAnnotation] = block.String()
nsCopy.Annotations[security.SupplementalGroupsAnnotation] = block.String()
if _, ok := nsCopy.Annotations[security.MCSAnnotation]; !ok {
if label := c.mcsAllocator(block); label != nil {
ns.Annotations[security.MCSAnnotation] = label.String()
nsCopy.Annotations[security.MCSAnnotation] = label.String()
}
}

_, err = c.client.Update(ns)
_, err = c.client.Update(nsCopy)
if err == nil {
tx.Commit()
}
Expand Down

0 comments on commit 0b49deb

Please sign in to comment.