Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to use .DeepCopy() instead of kapi.Scheme.DeepCopy() #17444

Merged
merged 3 commits into from
Nov 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions pkg/apps/controller/deployer/deployer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,16 +490,7 @@ func (c *DeploymentController) setDeployerPodsOwnerRef(deployment *v1.Replicatio
continue
}
glog.V(4).Infof("setting ownerRef for pod %s/%s to deployment %s/%s", pod.Namespace, pod.Name, deployment.Namespace, deployment.Name)
objCopy, err := kapi.Scheme.DeepCopy(pod)
if err != nil {
errors = append(errors, err)
continue
}
newPod, ok := objCopy.(*v1.Pod)
if !ok {
errors = append(errors, fmt.Errorf("object %#+v is not a pod", objCopy))
continue
}
newPod := pod.DeepCopy()
newPod.SetOwnerReferences([]metav1.OwnerReference{{
APIVersion: "v1",
Name: deployment.Name,
Expand Down
16 changes: 2 additions & 14 deletions pkg/apps/strategy/support/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,20 +381,8 @@ func makeHookPod(hook *deployapi.LifecycleHook, rc *kapi.ReplicationController,
}

gracePeriod := int64(10)

var podSecurityContextCopy *kapi.PodSecurityContext
if ctx, err := kapi.Scheme.DeepCopy(rc.Spec.Template.Spec.SecurityContext); err != nil {
return nil, fmt.Errorf("unable to copy pod securityContext: %v", err)
} else {
podSecurityContextCopy = ctx.(*kapi.PodSecurityContext)
}

var securityContextCopy *kapi.SecurityContext
if ctx, err := kapi.Scheme.DeepCopy(baseContainer.SecurityContext); err != nil {
return nil, fmt.Errorf("unable to copy securityContext: %v", err)
} else {
securityContextCopy = ctx.(*kapi.SecurityContext)
}
podSecurityContextCopy := rc.Spec.Template.Spec.SecurityContext.DeepCopy()
securityContextCopy := baseContainer.SecurityContext.DeepCopy()

pod := &kapi.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down
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
8 changes: 3 additions & 5 deletions pkg/authorization/rulevalidation/compact_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
kapi "k8s.io/kubernetes/pkg/api"

authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
)
Expand Down Expand Up @@ -106,10 +105,9 @@ func TestCompactRules(t *testing.T) {

for k, tc := range testcases {
rules := tc.Rules
originalRules, err := kapi.Scheme.DeepCopy(tc.Rules)
if err != nil {
t.Errorf("%s: couldn't copy rules: %v", k, err)
continue
originalRules := make([]authorizationapi.PolicyRule, len(tc.Rules))
for i, r := range tc.Rules {
r.DeepCopyInto(&originalRules[i])
}
compacted, err := CompactRules(tc.Rules)
if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions pkg/build/apis/build/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ func ValidateBuildUpdate(build *buildapi.Build, older *buildapi.Build) field.Err
}

// lie about the old build's pushsecret value so we can allow it to be updated.
olderCopy, err := buildutil.BuildDeepCopy(older)
if err != nil {
glog.V(2).Infof("Error copying build for update validation: %v", err)
allErrs = append(allErrs, field.InternalError(field.NewPath(""), fmt.Errorf("Unable to copy build for update validation: %v", err)))
}
olderCopy := older.DeepCopy()
olderCopy.Spec.Output.PushSecret = build.Spec.Output.PushSecret

if !kapihelper.Semantic.DeepEqual(build.Spec, olderCopy.Spec) {
Expand Down
12 changes: 4 additions & 8 deletions pkg/build/controller/build/build_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,10 +748,9 @@ func (bc *BuildController) createBuildPod(build *buildapi.Build) (*buildUpdate,

// image reference resolution requires a copy of the build
var err error
build, err = buildutil.BuildDeepCopy(build)
if err != nil {
return nil, fmt.Errorf("unable to copy build %s: %v", buildDesc(build), err)
}

// TODO: Rename this to buildCopy
build = build.DeepCopy()

// Resolve all Docker image references to valid values.
if err := bc.resolveImageReferences(build, update); err != nil {
Expand Down Expand Up @@ -1085,10 +1084,7 @@ func (bc *BuildController) handleBuildConfig(bcNamespace string, bcName string)
// and applies that patch using the REST client
func (bc *BuildController) patchBuild(build *buildapi.Build, update *buildUpdate) (*buildapi.Build, error) {
// Create a patch using the buildUpdate object
updatedBuild, err := buildutil.BuildDeepCopy(build)
if err != nil {
return nil, fmt.Errorf("cannot create a deep copy of build %s: %v", buildDesc(build), err)
}
updatedBuild := build.DeepCopy()
update.apply(updatedBuild)

patch, err := validation.CreateBuildPatch(build, updatedBuild)
Expand Down
5 changes: 1 addition & 4 deletions pkg/build/controller/build/build_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,7 @@ func TestHandleBuild(t *testing.T) {
t.Errorf("%s: did not get an update. Expected: %v", tc.name, tc.expectUpdate)
return
}
expectedBuild, err := buildutil.BuildDeepCopy(tc.build)
if err != nil {
t.Fatalf("unexpected: %v", err)
}
expectedBuild := tc.build.DeepCopy()
tc.expectUpdate.apply(expectedBuild)

// For start/completion/duration fields, simply validate that they are set/not set
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
26 changes: 3 additions & 23 deletions pkg/build/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,24 +165,8 @@ func VersionForBuild(build *buildapi.Build) int {
return version
}

func BuildDeepCopy(build *buildapi.Build) (*buildapi.Build, error) {
objCopy, err := kapi.Scheme.DeepCopy(build)
if err != nil {
return nil, err
}
copied, ok := objCopy.(*buildapi.Build)
if !ok {
return nil, fmt.Errorf("expected Build, got %#v", objCopy)
}
return copied, nil
}

func CopyApiResourcesToV1Resources(in *kapi.ResourceRequirements) corev1.ResourceRequirements {
copied, err := kapi.Scheme.DeepCopy(in)
if err != nil {
panic(err)
}
in = copied.(*kapi.ResourceRequirements)
in = in.DeepCopy()
out := corev1.ResourceRequirements{}
if err := kapiv1.Convert_api_ResourceRequirements_To_v1_ResourceRequirements(in, &out, nil); err != nil {
panic(err)
Expand All @@ -191,14 +175,10 @@ func CopyApiResourcesToV1Resources(in *kapi.ResourceRequirements) corev1.Resourc
}

func CopyApiEnvVarToV1EnvVar(in []kapi.EnvVar) []corev1.EnvVar {
copied, err := kapi.Scheme.DeepCopy(in)
if err != nil {
panic(err)
}
in = copied.([]kapi.EnvVar)
out := make([]corev1.EnvVar, len(in))
for i := range in {
if err := kapiv1.Convert_api_EnvVar_To_v1_EnvVar(&in[i], &out[i], nil); err != nil {
item := in[i].DeepCopy()
if err := kapiv1.Convert_api_EnvVar_To_v1_EnvVar(item, &out[i], nil); err != nil {
panic(err)
}
}
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
5 changes: 1 addition & 4 deletions pkg/image/controller/imagestream_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,7 @@ func TestHandleImageStream(t *testing.T) {

for i, test := range testCases {
fake := imageclient.NewSimpleClientset()
other, err := kapi.Scheme.DeepCopy(test.stream)
if err != nil {
t.Fatal(err)
}
other := test.stream.DeepCopy()

if err := handleImageStream(test.stream, fake.Image(), nil); err != nil {
t.Errorf("%d: unexpected error: %v", i, err)
Expand Down
7 changes: 1 addition & 6 deletions pkg/image/controller/scheduled_image_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/flowcontrol"
kapi "k8s.io/kubernetes/pkg/api"

imageapi "github.com/openshift/origin/pkg/image/apis/image"
imageclient "github.com/openshift/origin/pkg/image/generated/internalclientset/typed/image/internalversion"
Expand Down Expand Up @@ -169,11 +168,7 @@ func (s *ScheduledImageStreamController) syncTimedByName(namespace, name string)
return ErrNotImportable
}

copy, err := kapi.Scheme.DeepCopy(sharedStream)
if err != nil {
return err
}
stream := copy.(*imageapi.ImageStream)
stream := sharedStream.DeepCopy()
resetScheduledTags(stream)

glog.V(3).Infof("Scheduled import of stream %s/%s...", stream.Namespace, stream.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/controller"

imageapi "github.com/openshift/origin/pkg/image/apis/image"
Expand Down Expand Up @@ -154,12 +153,7 @@ func (s *SignatureImportController) syncImageSignatures(key string) error {
return nil
}

t, err := kapi.Scheme.DeepCopy(image)
if err != nil {
return err
}
newImage := t.(*imageapi.Image)

newImage := image.DeepCopy()
shouldUpdate := false

// Only add new signatures, do not override existing stored signatures as that
Expand Down
3 changes: 2 additions & 1 deletion pkg/image/controller/trigger/image_trigger_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/golang/glog"

"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -373,5 +374,5 @@ func (c *TriggerController) syncResource(key string) error {
return nil
}

return source.Reactor.ImageChanged(obj, c.tagRetriever)
return source.Reactor.ImageChanged(obj.(runtime.Object), c.tagRetriever)
}
Loading