Skip to content

Commit

Permalink
Merge pull request #7431 from wojtek-t/conversion_chains
Browse files Browse the repository at this point in the history
Prepare for chaining autogenerated conversion methods
  • Loading branch information
fgrzadkowski committed Apr 30, 2015
2 parents 6f5e081 + fc6fb09 commit 2312841
Show file tree
Hide file tree
Showing 4 changed files with 3,047 additions and 2,573 deletions.
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

0 comments on commit 2312841

Please sign in to comment.