Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit b701b0e

Browse files
committed
Add our own DeepCopy.
Signed-off-by: Lantao Liu <[email protected]>
1 parent 4985620 commit b701b0e

File tree

3 files changed

+108
-4
lines changed

3 files changed

+108
-4
lines changed

pkg/server/container_update_resources.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ import (
2828
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
2929
"github.com/opencontainers/runtime-tools/generate"
3030
"golang.org/x/net/context"
31-
"k8s.io/apimachinery/pkg/conversion"
3231
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
3332

3433
containerstore "github.com/kubernetes-incubator/cri-containerd/pkg/store/container"
34+
"github.com/kubernetes-incubator/cri-containerd/pkg/util"
3535
)
3636

3737
// UpdateContainerResources updates ContainerConfig of the container.
@@ -129,11 +129,11 @@ func updateContainerSpec(ctx context.Context, cntr containerd.Container, spec *r
129129
// updateOCILinuxResource updates container resource limit.
130130
func updateOCILinuxResource(spec *runtimespec.Spec, new *runtime.LinuxContainerResources) (*runtimespec.Spec, error) {
131131
// Copy to make sure old spec is not changed.
132-
cloned, err := conversion.NewCloner().DeepCopy(spec)
133-
if err != nil {
132+
var cloned runtimespec.Spec
133+
if err := util.DeepCopy(&cloned, spec); err != nil {
134134
return nil, fmt.Errorf("failed to deep copy: %v", err)
135135
}
136-
g := generate.NewFromSpec(cloned.(*runtimespec.Spec))
136+
g := generate.NewFromSpec(&cloned)
137137

138138
if new.GetCpuPeriod() != 0 {
139139
g.SetLinuxResourcesCPUPeriod(uint64(new.GetCpuPeriod()))

pkg/util/deep_copy.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
)
23+
24+
// DeepCopy makes a deep copy from src into dst.
25+
func DeepCopy(dst interface{}, src interface{}) error {
26+
if dst == nil {
27+
return fmt.Errorf("dst cannot be nil")
28+
}
29+
if src == nil {
30+
return fmt.Errorf("src cannot be nil")
31+
}
32+
bytes, err := json.Marshal(src)
33+
if err != nil {
34+
return fmt.Errorf("unable to marshal src: %s", err)
35+
}
36+
err = json.Unmarshal(bytes, dst)
37+
if err != nil {
38+
return fmt.Errorf("unable to unmarshal into dst: %s", err)
39+
}
40+
return nil
41+
}

pkg/util/deep_copy_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
type A struct {
26+
String string
27+
Int int
28+
Strings []string
29+
Ints map[string]int
30+
As map[string]*A
31+
}
32+
33+
func TestCopy(t *testing.T) {
34+
src := &A{
35+
String: "Hello World",
36+
Int: 5,
37+
Strings: []string{"A", "B"},
38+
Ints: map[string]int{"A": 1, "B": 2, "C": 4},
39+
As: map[string]*A{
40+
"One": {String: "2"},
41+
"Two": {String: "3"},
42+
},
43+
}
44+
dst := &A{
45+
Strings: []string{"C"},
46+
Ints: map[string]int{"B": 3, "C": 4},
47+
As: map[string]*A{"One": {String: "1", Int: 5}},
48+
}
49+
expected := &A{
50+
String: "Hello World",
51+
Int: 5,
52+
Strings: []string{"A", "B"},
53+
Ints: map[string]int{"A": 1, "B": 2, "C": 4},
54+
As: map[string]*A{
55+
"One": {String: "2"},
56+
"Two": {String: "3"},
57+
},
58+
}
59+
assert.NotEqual(t, expected, dst)
60+
err := DeepCopy(dst, src)
61+
assert.NoError(t, err)
62+
assert.Equal(t, expected, dst)
63+
}

0 commit comments

Comments
 (0)