Commit 89e860e6 authored by Wojciech Tyczynski's avatar Wojciech Tyczynski

Remove old conversion generator

parent 490c68db
/*
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 main
import (
"bytes"
"fmt"
"io"
"os"
"path"
"runtime"
"k8s.io/kubernetes/pkg/api"
_ "k8s.io/kubernetes/pkg/api/install"
"k8s.io/kubernetes/pkg/api/unversioned"
_ "k8s.io/kubernetes/pkg/apis/autoscaling/install"
_ "k8s.io/kubernetes/pkg/apis/batch/install"
_ "k8s.io/kubernetes/pkg/apis/componentconfig/install"
_ "k8s.io/kubernetes/pkg/apis/extensions/install"
_ "k8s.io/kubernetes/pkg/apis/metrics/install"
kruntime "k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/sets"
"github.com/golang/glog"
flag "github.com/spf13/pflag"
"golang.org/x/tools/imports"
)
const pkgBase = "k8s.io/kubernetes/pkg"
var (
functionDest = flag.StringP("funcDest", "f", "-", "Output for conversion functions; '-' means stdout")
groupVersion = flag.StringP("version", "v", "api/v1", "groupPath/version for conversion.")
)
// We're moving to pkg/apis/group/version. This handles new and legacy packages.
func pkgPath(group, version string) string {
if group == "" {
group = "api"
}
gv := group
if version != "" {
gv = path.Join(group, version)
}
switch {
case group == "api":
// TODO(lavalamp): remove this special case when we move api to apis/api
return path.Join(pkgBase, gv)
default:
return path.Join(pkgBase, "apis", gv)
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
var funcOut io.Writer
if *functionDest == "-" {
funcOut = os.Stdout
} else {
file, err := os.Create(*functionDest)
if err != nil {
glog.Fatalf("Couldn't open %v: %v", *functionDest, err)
}
defer file.Close()
funcOut = file
}
data := new(bytes.Buffer)
gv, err := unversioned.ParseGroupVersion(*groupVersion)
if err != nil {
glog.Fatalf("Error parsing groupversion %v: %v", *groupVersion, err)
}
_, err = data.WriteString(fmt.Sprintf("package %v\n", gv.Version))
if err != nil {
glog.Fatalf("Error while writing package line: %v", err)
}
versionPath := pkgPath(gv.Group, gv.Version)
generator := kruntime.NewConversionGenerator(api.Scheme, versionPath)
apiShort := generator.AddImport(path.Join(pkgBase, "api"))
generator.AddImport(path.Join(pkgBase, "api/resource"))
generator.AddImport(path.Join(pkgBase, "types"))
// TODO(wojtek-t): Change the overwrites to a flag.
generator.OverwritePackage(gv.Version, "")
for _, knownType := range api.Scheme.KnownTypes(gv) {
if knownType.PkgPath() != versionPath {
continue
}
if err := generator.GenerateConversionsForType(gv, knownType); err != nil {
glog.Errorf("Error while generating conversion functions for %v: %v", knownType, err)
}
}
generator.RepackImports(sets.NewString())
if err := generator.WriteImports(data); err != nil {
glog.Fatalf("Error while writing imports: %v", err)
}
if err := generator.WriteConversionFunctions(data); err != nil {
glog.Fatalf("Error while writing conversion functions: %v", err)
}
if err := generator.RegisterConversionFunctions(data, fmt.Sprintf("%s.Scheme", apiShort)); err != nil {
glog.Fatalf("Error while writing conversion functions: %v", err)
}
b, err := imports.Process("", data.Bytes(), nil)
if err != nil {
for i, s := range bytes.Split(data.Bytes(), []byte("\n")) {
glog.Infof("%d:\t%s", i, s)
}
glog.Fatalf("Error while update imports: %v\n", err)
}
if _, err := funcOut.Write(b); err != nil {
glog.Fatalf("Error while writing out the resulting file: %v", err)
}
}
#!/bin/bash
# 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.
set -o errexit
set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
genconversion=$(kube::util::find-binary "genconversion")
function generate_version() {
local group_version=$1
local TMPFILE="/tmp/conversion_generated.$(date +%s).go"
echo "Generating for ${group_version}"
sed 's/YEAR/2015/' hack/boilerplate/boilerplate.go.txt > "$TMPFILE"
cat >> "$TMPFILE" <<EOF
// DO NOT EDIT. THIS FILE IS AUTO-GENERATED BY \$KUBEROOT/hack/update-generated-conversions.sh
EOF
"${genconversion}" -v "${group_version}" -f - >> "$TMPFILE"
mv "$TMPFILE" "pkg/$(kube::util::group-version-to-pkg-path "${group_version}")/conversion_generated.go"
}
# TODO(lavalamp): get this list by listing the pkg/apis/ directory?
DEFAULT_GROUP_VERSIONS=""
VERSIONS=${VERSIONS:-$DEFAULT_GROUP_VERSIONS}
for ver in $VERSIONS; do
# Ensure that the version being processed is registered by setting
# KUBE_API_VERSIONS.
KUBE_API_VERSIONS="${ver}" generate_version "${ver}"
done
#!/bin/bash
# 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.
set -o errexit
set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
APIROOTS=${APIROOTS:-pkg/apis/authorization pkg/apis/autoscaling pkg/apis/batch pkg/apis/extensions pkg/apis/metrics}
_tmp="${KUBE_ROOT}/_tmp"
cleanup() {
rm -rf "${_tmp}"
}
trap "cleanup" EXIT SIGINT
for APIROOT in ${APIROOTS}; do
mkdir -p "${_tmp}/${APIROOT%/*}"
cp -a "${KUBE_ROOT}/${APIROOT}" "${_tmp}/${APIROOT}"
done
"${KUBE_ROOT}/hack/after-build/update-generated-conversions.sh"
for APIROOT in ${APIROOTS}; do
TMP_APIROOT="${_tmp}/${APIROOT}"
echo "diffing ${APIROOT} against freshly generated conversions"
ret=0
diff -Naupr -I 'Auto generated by' "${KUBE_ROOT}/${APIROOT}" "${TMP_APIROOT}" || ret=$?
cp -a "${TMP_APIROOT}" "${KUBE_ROOT}/${APIROOT%/*}"
if [[ $ret -eq 0 ]]; then
echo "${APIROOT} up to date."
else
echo "${APIROOT} is out of date. Please run hack/update-generated-conversions.sh"
exit 1
fi
done
...@@ -103,7 +103,6 @@ kube::golang::test_targets() { ...@@ -103,7 +103,6 @@ kube::golang::test_targets() {
cmd/genman cmd/genman
cmd/mungedocs cmd/mungedocs
cmd/genbashcomp cmd/genbashcomp
cmd/genconversion
cmd/genswaggertypedocs cmd/genswaggertypedocs
examples/k8petstore/web-server/src examples/k8petstore/web-server/src
github.com/onsi/ginkgo/ginkgo github.com/onsi/ginkgo/ginkgo
......
...@@ -51,7 +51,6 @@ if ! $ALL ; then ...@@ -51,7 +51,6 @@ if ! $ALL ; then
fi fi
BASH_TARGETS="codecgen BASH_TARGETS="codecgen
generated-conversions
generated-docs generated-docs
generated-swagger-docs generated-swagger-docs
swagger-spec swagger-spec
......
#!/bin/bash
# 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.
set -o errexit
set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
"${KUBE_ROOT}/hack/build-go.sh" cmd/genconversion
"${KUBE_ROOT}/hack/after-build/update-generated-conversions.sh" "$@"
# ex: ts=2 sw=2 et filetype=sh
#!/bin/bash
# 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.
set -o errexit
set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
"${KUBE_ROOT}/hack/build-go.sh" cmd/genconversion
"${KUBE_ROOT}/hack/after-build/verify-generated-conversions.sh" "$@"
# ex: ts=2 sw=2 et filetype=sh
...@@ -134,18 +134,6 @@ else ...@@ -134,18 +134,6 @@ else
fi fi
echo "${reset}" echo "${reset}"
echo -ne "Checking for conversions that need updating... "
if ! hack/after-build/verify-generated-conversions.sh > /dev/null; then
echo "${red}ERROR!"
echo "Some conversions functions need regeneration."
echo "To regenerate conversions, run:"
echo " hack/update-generated-conversions.sh"
exit_code=1
else
echo "${green}OK"
fi
echo "${reset}"
echo -ne "Checking for swagger type documentation that need updating... " echo -ne "Checking for swagger type documentation that need updating... "
if ! hack/after-build/verify-generated-swagger-docs.sh > /dev/null; then if ! hack/after-build/verify-generated-swagger-docs.sh > /dev/null; then
echo "${red}ERROR!" echo "${red}ERROR!"
......
/*
Copyright 2016 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 runtime
import (
"reflect"
"testing"
"k8s.io/kubernetes/pkg/api/unversioned"
)
type InternalSubtype struct {
String string
}
type Internal struct {
TypeMeta
Bool bool
Complex InternalSubtype
}
type ExternalSubtype struct {
String string
}
type External struct {
TypeMeta
Complex ExternalSubtype
}
func (obj *Internal) GetObjectKind() unversioned.ObjectKind { return &obj.TypeMeta }
func (obj *External) GetObjectKind() unversioned.ObjectKind { return &obj.TypeMeta }
func TestGenerateConversionsForStruct(t *testing.T) {
internalGV := unversioned.GroupVersion{Group: "test.group", Version: APIVersionInternal}
externalGV := unversioned.GroupVersion{Group: "test.group", Version: "external"}
scheme := NewScheme()
scheme.Log(t)
scheme.AddKnownTypeWithName(internalGV.WithKind("Complex"), &Internal{})
scheme.AddKnownTypeWithName(externalGV.WithKind("Complex"), &External{})
generator := NewConversionGenerator(scheme, "foo")
typedGenerator, ok := generator.(*conversionGenerator)
if !ok {
t.Fatalf("error converting to conversionGenerator")
}
internalType := reflect.TypeOf(Internal{})
externalType := reflect.TypeOf(External{})
err := typedGenerator.generateConversionsForStruct(internalType, externalType)
if err == nil {
t.Errorf("expected error for asymmetrical field")
}
// we are expecting Convert_runtime_InternalSubtype_To_runtime_ExternalSubtype to be generated
// even though the conversion for the parent type cannot be auto generated
if len(typedGenerator.publicFuncs) != 1 {
t.Errorf("expected to find one public conversion for the Complex type but found: %v", typedGenerator.publicFuncs)
}
}
...@@ -113,6 +113,37 @@ func fieldName(field *ast.Field) string { ...@@ -113,6 +113,37 @@ func fieldName(field *ast.Field) string {
return jsonTag return jsonTag
} }
// A buffer of lines that will be written.
type bufferedLine struct {
line string
indentation int
}
type buffer struct {
lines []bufferedLine
}
func newBuffer() *buffer {
return &buffer{
lines: make([]bufferedLine, 0),
}
}
func (b *buffer) addLine(line string, indent int) {
b.lines = append(b.lines, bufferedLine{line, indent})
}
func (b *buffer) flushLines(w io.Writer) error {
for _, line := range b.lines {
indentation := strings.Repeat("\t", line.indentation)
fullLine := fmt.Sprintf("%s%s", indentation, line.line)
if _, err := io.WriteString(w, fullLine); err != nil {
return err
}
}
return nil
}
func writeFuncHeader(b *buffer, structName string, indent int) { func writeFuncHeader(b *buffer, structName string, indent int) {
s := fmt.Sprintf("var map_%s = map[string]string {\n", structName) s := fmt.Sprintf("var map_%s = map[string]string {\n", structName)
b.addLine(s, indent) b.addLine(s, indent)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment