Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
6fa2777e
Commit
6fa2777e
authored
May 28, 2015
by
Wojciech Tyczynski
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #8860 from wojtek-t/replace_deep_copy
Use generated DeepCopy methods in a new API (that uses auto-generated methods)
parents
b7b89c47
33318f01
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
36 additions
and
131 deletions
+36
-131
pre-commit
hooks/pre-commit
+12
-0
copy_test.go
pkg/api/copy_test.go
+1
-2
fake_controller_source.go
pkg/controller/framework/fake_controller_source.go
+2
-3
cloner.go
pkg/conversion/cloner.go
+6
-2
deep_copy.go
pkg/conversion/deep_copy.go
+0
-114
deep_copy_test.go
pkg/conversion/deep_copy_test.go
+4
-4
rest_test.go
pkg/registry/service/rest_test.go
+1
-2
deep_copy_generator.go
pkg/runtime/deep_copy_generator.go
+5
-0
embedded_test.go
pkg/runtime/embedded_test.go
+2
-2
etcd_helper.go
pkg/tools/etcd_helper.go
+3
-2
No files found.
hooks/pre-commit
View file @
6fa2777e
...
@@ -111,4 +111,16 @@ else
...
@@ -111,4 +111,16 @@ else
fi
fi
echo
"
${
reset
}
"
echo
"
${
reset
}
"
echo
-ne
"Checking for deep-copies that need updating... "
if
!
hack/verify-generated-deep-copies.sh
>
/dev/null
;
then
echo
"
${
red
}
ERROR!"
echo
"Some deep-copy functions need regeneration."
echo
"To regenerate deep-copies, run:"
echo
" hack/update-generated-deep-copies.sh"
exit_code
=
1
else
echo
"
${
green
}
OK"
fi
echo
"
${
reset
}
"
exit
$exit_code
exit
$exit_code
pkg/api/copy_test.go
View file @
6fa2777e
...
@@ -24,7 +24,6 @@ import (
...
@@ -24,7 +24,6 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
apitesting
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testing"
apitesting
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
)
)
func
TestDeepCopyApiObjects
(
t
*
testing
.
T
)
{
func
TestDeepCopyApiObjects
(
t
*
testing
.
T
)
{
...
@@ -37,7 +36,7 @@ func TestDeepCopyApiObjects(t *testing.T) {
...
@@ -37,7 +36,7 @@ func TestDeepCopyApiObjects(t *testing.T) {
t
.
Fatalf
(
"Could not create a %s: %s"
,
kind
,
err
)
t
.
Fatalf
(
"Could not create a %s: %s"
,
kind
,
err
)
}
}
f
.
Fuzz
(
item
)
f
.
Fuzz
(
item
)
itemCopy
,
err
:=
conversion
.
DeepCopy
(
item
)
itemCopy
,
err
:=
api
.
Scheme
.
DeepCopy
(
item
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"Could not deep copy a %s: %s"
,
kind
,
err
)
t
.
Errorf
(
"Could not deep copy a %s: %s"
,
kind
,
err
)
continue
continue
...
...
pkg/controller/framework/fake_controller_source.go
View file @
6fa2777e
...
@@ -23,7 +23,6 @@ import (
...
@@ -23,7 +23,6 @@ import (
"sync"
"sync"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
...
@@ -130,7 +129,7 @@ func (f *FakeControllerSource) List() (runtime.Object, error) {
...
@@ -130,7 +129,7 @@ func (f *FakeControllerSource) List() (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 the our canonical copy (in
// will inadvertently change the our canonical copy (in
// addition to racing with other clients).
// addition to racing with other clients).
objCopy
,
err
:=
conversion
.
DeepCopy
(
obj
)
objCopy
,
err
:=
api
.
Scheme
.
DeepCopy
(
obj
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -167,7 +166,7 @@ func (f *FakeControllerSource) Watch(resourceVersion string) (watch.Interface, e
...
@@ -167,7 +166,7 @@ func (f *FakeControllerSource) Watch(resourceVersion string) (watch.Interface, e
// 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
:=
conversion
.
DeepCopy
(
c
.
Object
)
objCopy
,
err
:=
api
.
Scheme
.
DeepCopy
(
c
.
Object
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
...
pkg/conversion/cloner.go
View file @
6fa2777e
...
@@ -43,8 +43,12 @@ func NewCloner() *Cloner {
...
@@ -43,8 +43,12 @@ func NewCloner() *Cloner {
// Prevent recursing into every byte...
// Prevent recursing into every byte...
func
byteSliceDeepCopy
(
in
[]
byte
,
out
*
[]
byte
,
c
*
Cloner
)
error
{
func
byteSliceDeepCopy
(
in
[]
byte
,
out
*
[]
byte
,
c
*
Cloner
)
error
{
*
out
=
make
([]
byte
,
len
(
in
))
if
in
!=
nil
{
copy
(
*
out
,
in
)
*
out
=
make
([]
byte
,
len
(
in
))
copy
(
*
out
,
in
)
}
else
{
*
out
=
nil
}
return
nil
return
nil
}
}
...
...
pkg/conversion/deep_copy.go
deleted
100644 → 0
View file @
b7b89c47
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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.
*/
package
conversion
import
(
"fmt"
"reflect"
)
// DeepCopy makes a deep copy of source or returns an error.
func
DeepCopy
(
source
interface
{})
(
interface
{},
error
)
{
v
,
err
:=
deepCopy
(
reflect
.
ValueOf
(
source
))
return
v
.
Interface
(),
err
}
func
deepCopy
(
src
reflect
.
Value
)
(
reflect
.
Value
,
error
)
{
switch
src
.
Kind
()
{
case
reflect
.
Chan
,
reflect
.
Func
,
reflect
.
UnsafePointer
,
reflect
.
Uintptr
:
return
src
,
fmt
.
Errorf
(
"cannot deep copy kind: %s"
,
src
.
Kind
())
case
reflect
.
Array
:
dst
:=
reflect
.
New
(
src
.
Type
())
for
i
:=
0
;
i
<
src
.
Len
();
i
++
{
copyVal
,
err
:=
deepCopy
(
src
.
Index
(
i
))
if
err
!=
nil
{
return
src
,
err
}
dst
.
Elem
()
.
Index
(
i
)
.
Set
(
copyVal
)
}
return
dst
.
Elem
(),
nil
case
reflect
.
Interface
:
if
src
.
IsNil
()
{
return
src
,
nil
}
return
deepCopy
(
src
.
Elem
())
case
reflect
.
Map
:
if
src
.
IsNil
()
{
return
src
,
nil
}
dst
:=
reflect
.
MakeMap
(
src
.
Type
())
for
_
,
k
:=
range
src
.
MapKeys
()
{
copyVal
,
err
:=
deepCopy
(
src
.
MapIndex
(
k
))
if
err
!=
nil
{
return
src
,
err
}
dst
.
SetMapIndex
(
k
,
copyVal
)
}
return
dst
,
nil
case
reflect
.
Ptr
:
if
src
.
IsNil
()
{
return
src
,
nil
}
dst
:=
reflect
.
New
(
src
.
Type
()
.
Elem
())
copyVal
,
err
:=
deepCopy
(
src
.
Elem
())
if
err
!=
nil
{
return
src
,
err
}
dst
.
Elem
()
.
Set
(
copyVal
)
return
dst
,
nil
case
reflect
.
Slice
:
if
src
.
IsNil
()
{
return
src
,
nil
}
dst
:=
reflect
.
MakeSlice
(
src
.
Type
(),
0
,
src
.
Len
())
for
i
:=
0
;
i
<
src
.
Len
();
i
++
{
copyVal
,
err
:=
deepCopy
(
src
.
Index
(
i
))
if
err
!=
nil
{
return
src
,
err
}
dst
=
reflect
.
Append
(
dst
,
copyVal
)
}
return
dst
,
nil
case
reflect
.
Struct
:
dst
:=
reflect
.
New
(
src
.
Type
())
for
i
:=
0
;
i
<
src
.
NumField
();
i
++
{
if
!
dst
.
Elem
()
.
Field
(
i
)
.
CanSet
()
{
// Can't set private fields. At this point, the
// best we can do is a shallow copy. For
// example, time.Time is a value type with
// private members that can be shallow copied.
return
src
,
nil
}
copyVal
,
err
:=
deepCopy
(
src
.
Field
(
i
))
if
err
!=
nil
{
return
src
,
err
}
dst
.
Elem
()
.
Field
(
i
)
.
Set
(
copyVal
)
}
return
dst
.
Elem
(),
nil
default
:
// Value types like numbers, booleans, and strings.
return
src
,
nil
}
}
pkg/conversion/deep_copy_test.go
View file @
6fa2777e
...
@@ -40,7 +40,7 @@ func TestDeepCopy(t *testing.T) {
...
@@ -40,7 +40,7 @@ func TestDeepCopy(t *testing.T) {
}{},
}{},
}
}
for
_
,
obj
:=
range
table
{
for
_
,
obj
:=
range
table
{
obj2
,
err
:=
DeepCopy
(
obj
)
obj2
,
err
:=
NewCloner
()
.
DeepCopy
(
obj
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"Error: couldn't copy %#v"
,
obj
)
t
.
Errorf
(
"Error: couldn't copy %#v"
,
obj
)
continue
continue
...
@@ -51,7 +51,7 @@ func TestDeepCopy(t *testing.T) {
...
@@ -51,7 +51,7 @@ func TestDeepCopy(t *testing.T) {
obj3
:=
reflect
.
New
(
reflect
.
TypeOf
(
obj
))
.
Interface
()
obj3
:=
reflect
.
New
(
reflect
.
TypeOf
(
obj
))
.
Interface
()
f
.
Fuzz
(
obj3
)
f
.
Fuzz
(
obj3
)
obj4
,
err
:=
DeepCopy
(
obj3
)
obj4
,
err
:=
NewCloner
()
.
DeepCopy
(
obj3
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"Error: couldn't copy %#v"
,
obj
)
t
.
Errorf
(
"Error: couldn't copy %#v"
,
obj
)
continue
continue
...
@@ -64,7 +64,7 @@ func TestDeepCopy(t *testing.T) {
...
@@ -64,7 +64,7 @@ func TestDeepCopy(t *testing.T) {
}
}
func
copyOrDie
(
t
*
testing
.
T
,
in
interface
{})
interface
{}
{
func
copyOrDie
(
t
*
testing
.
T
,
in
interface
{})
interface
{}
{
out
,
err
:=
DeepCopy
(
in
)
out
,
err
:=
NewCloner
()
.
DeepCopy
(
in
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"DeepCopy failed: %#q: %v"
,
in
,
err
)
t
.
Fatalf
(
"DeepCopy failed: %#q: %v"
,
in
,
err
)
}
}
...
@@ -154,7 +154,7 @@ func BenchmarkDeepCopy(b *testing.B) {
...
@@ -154,7 +154,7 @@ func BenchmarkDeepCopy(b *testing.B) {
var
r
interface
{}
var
r
interface
{}
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
for
j
:=
range
table
{
for
j
:=
range
table
{
r
,
_
=
DeepCopy
(
table
[
j
])
r
,
_
=
NewCloner
()
.
DeepCopy
(
table
[
j
])
}
}
}
}
result
=
r
result
=
r
...
...
pkg/registry/service/rest_test.go
View file @
6fa2777e
...
@@ -25,7 +25,6 @@ import (
...
@@ -25,7 +25,6 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest/resttest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest/resttest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
...
@@ -60,7 +59,7 @@ func makeIPNet(t *testing.T) *net.IPNet {
...
@@ -60,7 +59,7 @@ func makeIPNet(t *testing.T) *net.IPNet {
}
}
func
deepCloneService
(
svc
*
api
.
Service
)
*
api
.
Service
{
func
deepCloneService
(
svc
*
api
.
Service
)
*
api
.
Service
{
value
,
err
:=
conversion
.
DeepCopy
(
svc
)
value
,
err
:=
api
.
Scheme
.
DeepCopy
(
svc
)
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
"couldn't copy service"
)
panic
(
"couldn't copy service"
)
}
}
...
...
pkg/runtime/deep_copy_generator.go
View file @
6fa2777e
...
@@ -27,6 +27,11 @@ import (
...
@@ -27,6 +27,11 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
)
// TODO(wojtek-t): As suggested in #8320, we should consider the strategy
// to first do the shallow copy and then recurse into things that need a
// deep copy (maps, pointers, slices). That sort of copy function would
// need one parameter - a pointer to the thing it's supposed to expand,
// and it would involve a lot less memory copying.
type
DeepCopyGenerator
interface
{
type
DeepCopyGenerator
interface
{
// Adds a type to a generator.
// Adds a type to a generator.
// If the type is non-struct, it will return an error, otherwise deep-copy
// If the type is non-struct, it will return an error, otherwise deep-copy
...
...
pkg/runtime/embedded_test.go
View file @
6fa2777e
...
@@ -21,7 +21,7 @@ import (
...
@@ -21,7 +21,7 @@ import (
"reflect"
"reflect"
"testing"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/
conversion
"
"github.com/GoogleCloudPlatform/kubernetes/pkg/
api
"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
)
...
@@ -224,7 +224,7 @@ func TestDeepCopyOfEmbeddedObject(t *testing.T) {
...
@@ -224,7 +224,7 @@ func TestDeepCopyOfEmbeddedObject(t *testing.T) {
}
}
t
.
Logf
(
"originalRole = %v
\n
"
,
string
(
originalData
))
t
.
Logf
(
"originalRole = %v
\n
"
,
string
(
originalData
))
copyOfOriginal
,
err
:=
conversion
.
DeepCopy
(
original
)
copyOfOriginal
,
err
:=
api
.
Scheme
.
DeepCopy
(
original
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
}
...
...
pkg/tools/etcd_helper.go
View file @
6fa2777e
...
@@ -27,6 +27,7 @@ import (
...
@@ -27,6 +27,7 @@ import (
"strings"
"strings"
"time"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
...
@@ -238,7 +239,7 @@ func (h *EtcdHelper) getFromCache(index uint64) (runtime.Object, bool) {
...
@@ -238,7 +239,7 @@ func (h *EtcdHelper) getFromCache(index uint64) (runtime.Object, bool) {
if
found
{
if
found
{
// We should not return the object itself to avoid poluting the cache if someone
// We should not return the object itself to avoid poluting the cache if someone
// modifies returned values.
// modifies returned values.
objCopy
,
err
:=
conversion
.
DeepCopy
(
obj
)
objCopy
,
err
:=
api
.
Scheme
.
DeepCopy
(
obj
)
trace
.
Step
(
"Deep copied"
)
trace
.
Step
(
"Deep copied"
)
if
err
!=
nil
{
if
err
!=
nil
{
glog
.
Errorf
(
"Error during DeepCopy of cached object: %q"
,
err
)
glog
.
Errorf
(
"Error during DeepCopy of cached object: %q"
,
err
)
...
@@ -256,7 +257,7 @@ func (h *EtcdHelper) addToCache(index uint64, obj runtime.Object) {
...
@@ -256,7 +257,7 @@ func (h *EtcdHelper) addToCache(index uint64, obj runtime.Object) {
defer
func
()
{
defer
func
()
{
cacheAddLatency
.
Observe
(
float64
(
time
.
Since
(
startTime
)
/
time
.
Microsecond
))
cacheAddLatency
.
Observe
(
float64
(
time
.
Since
(
startTime
)
/
time
.
Microsecond
))
}()
}()
objCopy
,
err
:=
conversion
.
DeepCopy
(
obj
)
objCopy
,
err
:=
api
.
Scheme
.
DeepCopy
(
obj
)
if
err
!=
nil
{
if
err
!=
nil
{
glog
.
Errorf
(
"Error during DeepCopy of cached object: %q"
,
err
)
glog
.
Errorf
(
"Error during DeepCopy of cached object: %q"
,
err
)
return
return
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment