Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2280db

Browse files
committed
Deep-copy functions autogeneration.
1 parent 11b058c commit b2280db

14 files changed

+1049
-13
lines changed
File renamed without changes.

cmd/gendeepcopy/deep_copy.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright 2015 The Kubernetes Authors All rights reserved.
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 main
18+
19+
import (
20+
"io"
21+
"os"
22+
"runtime"
23+
"strings"
24+
25+
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
26+
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1"
27+
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
28+
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta2"
29+
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta3"
30+
pkg_runtime "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
31+
32+
"github.com/golang/glog"
33+
flag "github.com/spf13/pflag"
34+
)
35+
36+
var (
37+
functionDest = flag.StringP("func-dest", "f", "-", "Output for deep copy functions; '-' means stdout")
38+
version = flag.StringP("version", "v", "v1beta3", "Version for deep copies.")
39+
overwrites = flag.StringP("overwrites", "o", "", "Comma-separated overwrites for package names")
40+
)
41+
42+
func main() {
43+
runtime.GOMAXPROCS(runtime.NumCPU())
44+
flag.Parse()
45+
46+
var funcOut io.Writer
47+
if *functionDest == "-" {
48+
funcOut = os.Stdout
49+
} else {
50+
file, err := os.Create(*functionDest)
51+
if err != nil {
52+
glog.Fatalf("Couldn't open %v: %v", *functionDest, err)
53+
}
54+
defer file.Close()
55+
funcOut = file
56+
}
57+
58+
knownVersion := *version
59+
if knownVersion == "api" {
60+
knownVersion = api.Scheme.Raw().InternalVersion
61+
}
62+
generator := pkg_runtime.NewDeepCopyGenerator(api.Scheme.Raw())
63+
64+
for _, overwrite := range strings.Split(*overwrites, ",") {
65+
vals := strings.Split(overwrite, "=")
66+
generator.OverwritePackage(vals[0], vals[1])
67+
}
68+
for _, knownType := range api.Scheme.KnownTypes(knownVersion) {
69+
if err := generator.AddType(knownType); err != nil {
70+
glog.Errorf("error while generating deep copy functions for %v: %v", knownType, err)
71+
}
72+
}
73+
if err := generator.WriteImports(funcOut, *version); err != nil {
74+
glog.Fatalf("error while writing imports: %v", err)
75+
}
76+
if err := generator.WriteDeepCopyFunctions(funcOut); err != nil {
77+
glog.Fatalf("error while writing deep copy functions: %v", err)
78+
}
79+
if err := generator.RegisterDeepCopyFunctions(funcOut, *version); err != nil {
80+
glog.Fatalf("error while registering deep copy functions: %v", err)
81+
}
82+
}

hack/lib/golang.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ readonly KUBE_TEST_TARGETS=(
4848
cmd/gendocs
4949
cmd/genman
5050
cmd/genbashcomp
51+
cmd/genconversion
52+
cmd/gendeepcopy
5153
examples/k8petstore/web-server
5254
github.com/onsi/ginkgo/ginkgo
5355
test/e2e/e2e.test

hack/update-generated-conversions.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
// AUTO-GENERATED FUNCTIONS START HERE
4040
EOF
4141

42-
GOPATH=$(godep path):$GOPATH go run cmd/kube-conversion/conversion.go -v ${version} -f - >> $TMPFILE
42+
GOPATH=$(godep path):$GOPATH go run cmd/genconversion/conversion.go -v ${version} -f - >> $TMPFILE
4343

4444
cat >> $TMPFILE <<EOF
4545
// AUTO-GENERATED FUNCTIONS END HERE
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
# Copyright 2015 The Kubernetes Authors All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
function generate_version() {
22+
local version=$1
23+
local TMPFILE="/tmp/deep_copy_generated.$(date +%s).go"
24+
25+
echo "Generating for version ${version}"
26+
27+
sed 's/YEAR/2015/' hooks/boilerplate.go.txt > $TMPFILE
28+
cat >> $TMPFILE <<EOF
29+
package ${version}
30+
31+
// AUTO-GENERATED FUNCTIONS START HERE
32+
EOF
33+
34+
GOPATH=$(godep path):$GOPATH go run cmd/gendeepcopy/deep_copy.go -v ${version} -f - -o "${version}=" >> $TMPFILE
35+
36+
cat >> $TMPFILE <<EOF
37+
// AUTO-GENERATED FUNCTIONS END HERE
38+
EOF
39+
40+
gofmt -w -s $TMPFILE
41+
if [ "${version}" == "api" ]; then
42+
mv $TMPFILE pkg/api/deep_copy_generated.go
43+
else
44+
mv $TMPFILE pkg/api/${version}/deep_copy_generated.go
45+
fi
46+
}
47+
48+
VERSIONS="api v1beta3 v1"
49+
for ver in $VERSIONS; do
50+
generate_version "${ver}"
51+
done

hack/verify-generated-conversions.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
2222
source "${KUBE_ROOT}/hack/lib/init.sh"
2323

2424
kube::golang::setup_env
25-
"${KUBE_ROOT}/hack/build-go.sh" cmd/kube-conversion
25+
"${KUBE_ROOT}/hack/build-go.sh" cmd/genconversion
2626

27-
genconversion=$(kube::util::find-binary "kube-conversion")
27+
genconversion=$(kube::util::find-binary "genconversion")
2828

2929
if [[ ! -x "$genconversion" ]]; then
3030
{
3131
echo "It looks as if you don't have a compiled conversion binary"
3232
echo
3333
echo "If you are running from a clone of the git repo, please run"
34-
echo "'./hack/build-go.sh cmd/kube-conversion'."
34+
echo "'./hack/build-go.sh cmd/genconversion'."
3535
} >&2
3636
exit 1
3737
fi
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
3+
# Copyright 2015 The Kubernetes Authors All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
22+
source "${KUBE_ROOT}/hack/lib/init.sh"
23+
24+
kube::golang::setup_env
25+
"${KUBE_ROOT}/hack/build-go.sh" cmd/gendeepcopy
26+
27+
genconversion=$(kube::util::find-binary "gendeepcopy")
28+
29+
if [[ ! -x "$genconversion" ]]; then
30+
{
31+
echo "It looks as if you don't have a compiled conversion binary"
32+
echo
33+
echo "If you are running from a clone of the git repo, please run"
34+
echo "'./hack/build-go.sh cmd/gendeepcopy'."
35+
} >&2
36+
exit 1
37+
fi
38+
39+
APIROOT="${KUBE_ROOT}/pkg/api"
40+
TMP_APIROOT="${KUBE_ROOT}/_tmp/api"
41+
_tmp="${KUBE_ROOT}/_tmp"
42+
43+
mkdir -p "${_tmp}"
44+
cp -a "${APIROOT}" "${TMP_APIROOT}"
45+
46+
"${KUBE_ROOT}/hack/update-generated-deep-copies.sh"
47+
echo "diffing ${APIROOT} against freshly generated deep copies"
48+
ret=0
49+
diff -Naupr -I 'Auto generated by' "${APIROOT}" "${TMP_APIROOT}" || ret=$?
50+
cp -a ${TMP_APIROOT} "${KUBE_ROOT}/pkg"
51+
rm -rf "${_tmp}"
52+
if [[ $ret -eq 0 ]]
53+
then
54+
echo "${APIROOT} up to date."
55+
else
56+
echo "${APIROOT} is out of date. Please run hack/update-generated-deep-copies.sh"
57+
exit 1
58+
fi
59+
60+
# ex: ts=2 sw=2 et filetype=sh

pkg/api/deep_copy_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"testing"
2222

2323
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
24-
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
2524
)
2625

2726
func BenchmarkPodCopy(b *testing.B) {
@@ -36,7 +35,7 @@ func BenchmarkPodCopy(b *testing.B) {
3635

3736
var result *api.Pod
3837
for i := 0; i < b.N; i++ {
39-
obj, err := conversion.DeepCopy(&pod)
38+
obj, err := api.Scheme.DeepCopy(&pod)
4039
if err != nil {
4140
b.Fatalf("Unexpected error copying pod: %v", err)
4241
}
@@ -59,7 +58,7 @@ func BenchmarkNodeCopy(b *testing.B) {
5958

6059
var result *api.Node
6160
for i := 0; i < b.N; i++ {
62-
obj, err := conversion.DeepCopy(&node)
61+
obj, err := api.Scheme.DeepCopy(&node)
6362
if err != nil {
6463
b.Fatalf("Unexpected error copying node: %v", err)
6564
}
@@ -82,7 +81,7 @@ func BenchmarkReplicationControllerCopy(b *testing.B) {
8281

8382
var result *api.ReplicationController
8483
for i := 0; i < b.N; i++ {
85-
obj, err := conversion.DeepCopy(&replicationController)
84+
obj, err := api.Scheme.DeepCopy(&replicationController)
8685
if err != nil {
8786
b.Fatalf("Unexpected error copying replication controller: %v", err)
8887
}

0 commit comments

Comments
 (0)