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

Prepare for chaining autogenerated conversion methods #7431

Merged
merged 2 commits into from Apr 30, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 33 additions & 22 deletions cmd/kube-conversion/conversion.go
Expand Up @@ -20,7 +20,6 @@ import (
"io"
"os"
"runtime"
"strings"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
Expand All @@ -33,39 +32,51 @@ import (
)

var (
outputDest = flag.StringP("output", "o", "-", "Output destination; '-' means stdout")
versions = flag.StringP("versions", "v", "v1beta3", "Comma separated list of versions for conversion.")
functionDest = flag.StringP("funcDest", "f", "-", "Output for conversion functions; '-' means stdout")
namesDest = flag.StringP("nameDest", "n", "-", "Output for function names; '-' means stdout")
version = flag.StringP("version", "v", "v1beta3", "Version for conversion.")
)

func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()

var out io.Writer
if *outputDest == "-" {
out = os.Stdout
var funcOut io.Writer
if *functionDest == "-" {
funcOut = os.Stdout
} else {
file, err := os.Create(*outputDest)
file, err := os.Create(*functionDest)
if err != nil {
glog.Fatalf("Couldn't open %v: %v", *outputDest, err)
glog.Fatalf("Couldn't open %v: %v", *functionDest, err)
}
defer file.Close()
out = file
funcOut = file
}

versionsForConversion := strings.Split(*versions, ",")
for _, version := range versionsForConversion {
generator := conversion.NewGenerator(api.Scheme.Raw())
// TODO(wojtek-t): Change the overwrites to a flag.
generator.OverwritePackage(version, "")
generator.OverwritePackage("api", "newer")
for _, knownType := range api.Scheme.KnownTypes(version) {
if err := generator.GenerateConversionsForType(version, knownType); err != nil {
glog.Errorf("error while generating conversion functions for %v: %v", knownType, err)
}
var nameOut io.Writer
if *namesDest == "-" {
nameOut = os.Stdout
} else {
file, err := os.Create(*namesDest)
if err != nil {
glog.Fatalf("Couldn't open %v: %v", *functionDest, err)
}
if err := generator.WriteConversionFunctions(out); err != nil {
glog.Fatalf("Error while writing conversion functions: %v", err)
defer file.Close()
nameOut = file
}

generator := conversion.NewGenerator(api.Scheme.Raw())
// TODO(wojtek-t): Change the overwrites to a flag.
generator.OverwritePackage(*version, "")
generator.OverwritePackage("api", "newer")
for _, knownType := range api.Scheme.KnownTypes(*version) {
if err := generator.GenerateConversionsForType(*version, knownType); err != nil {
glog.Errorf("error while generating conversion functions for %v: %v", knownType, err)
}
}
if err := generator.WriteConversionFunctions(funcOut); err != nil {
glog.Fatalf("Error while writing conversion functions: %v", err)
}
if err := generator.WriteConversionFunctionNames(nameOut); err != nil {
glog.Fatalf("Error while writing conversion functions: %v", err)
}
}
19 changes: 17 additions & 2 deletions docs/devel/api_changes.md
Expand Up @@ -222,9 +222,24 @@ types, structural change in particular - you must add some logic to convert
versioned APIs to and from the internal representation. If you see errors from
the `serialization_test`, it may indicate the need for explicit conversions.

Performance of conversions very heavily influence performance of apiserver.
Thus, we are auto-generating conversion functions that are much more efficient
than the generic ones (which are based on reflections and thus are highly
inefficient).

The conversion code resides with each versioned API -
`pkg/api/<version>/conversion.go`. Unsurprisingly, this also requires you to
add tests to `pkg/api/<version>/conversion_test.go`.
`pkg/api/<version>/conversion.go`. To regenerate conversion functions:
- run
```
$ go run cmd/kube-conversion/conversion.go -v <version> -f <file1.txt> -n <file2.txt>
```
- replace all conversion functions (convert\* functions) in the above file
with the contents of \<file1.txt\>
- replace arguments of `newer.Scheme.AddGeneratedConversionFuncs`
with the contents of \<file2.txt\>

Unsurprisingly, this also requires you to add tests to
`pkg/api/<version>/conversion_test.go`.

## Update the fuzzer

Expand Down