コード例 #1
0
func generateDeepCopies(t *testing.T, version string) bytes.Buffer {
	g := runtime.NewDeepCopyGenerator(api.Scheme.Raw())
	g.OverwritePackage(version, "")
	testedVersion := version
	if version == "api" {
		testedVersion = api.Scheme.Raw().InternalVersion
	}
	for _, knownType := range api.Scheme.KnownTypes(testedVersion) {
		if err := g.AddType(knownType); err != nil {
			glog.Errorf("error while generating deep-copy functions for %v: %v", knownType, err)
		}
	}

	var functions bytes.Buffer
	functionsWriter := bufio.NewWriter(&functions)
	if err := g.WriteImports(functionsWriter, version); err != nil {
		t.Fatalf("couldn't generate deep-copy function imports: %v", err)
	}
	if err := g.WriteDeepCopyFunctions(functionsWriter); err != nil {
		t.Fatalf("couldn't generate deep-copy functions: %v", err)
	}
	if err := g.RegisterDeepCopyFunctions(functionsWriter, version); err != nil {
		t.Fatalf("couldn't generate deep-copy function names: %v", err)
	}
	if err := functionsWriter.Flush(); err != nil {
		t.Fatalf("error while flushing writer")
	}

	return functions
}
コード例 #2
0
ファイル: deep_copy.go プロジェクト: nanit/kubernetes
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
	}

	group, version := path.Split(*groupVersion)
	group = strings.TrimRight(group, "/")
	registerTo := "api.Scheme"
	if *groupVersion == "api/" {
		registerTo = "Scheme"
	}
	versionPath := path.Join(pkgBase, group, version)
	generator := pkg_runtime.NewDeepCopyGenerator(api.Scheme.Raw(), versionPath, util.NewStringSet("k8s.io/kubernetes"))
	generator.AddImport(path.Join(pkgBase, "api"))

	if len(*overwrites) > 0 {
		for _, overwrite := range strings.Split(*overwrites, ",") {
			if !strings.Contains(overwrite, "=") {
				glog.Fatalf("Invalid overwrite syntax: %s", overwrite)
			}
			vals := strings.Split(overwrite, "=")
			generator.OverwritePackage(vals[0], vals[1])
		}
	}
	for _, knownType := range api.Scheme.KnownTypes(version) {
		if !strings.HasPrefix(knownType.PkgPath(), versionPath) {
			continue
		}
		if err := generator.AddType(knownType); err != nil {
			glog.Errorf("error while generating deep copy functions for %v: %v", knownType, err)
		}
	}
	generator.RepackImports()
	if err := generator.WriteImports(funcOut); err != nil {
		glog.Fatalf("error while writing imports: %v", err)
	}
	if err := generator.WriteDeepCopyFunctions(funcOut); err != nil {
		glog.Fatalf("error while writing deep copy functions: %v", err)
	}
	if err := generator.RegisterDeepCopyFunctions(funcOut, registerTo); err != nil {
		glog.Fatalf("error while registering deep copy functions: %v", err)
	}
}
コード例 #3
0
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
	}

	knownVersion := *version
	if knownVersion == "api" {
		knownVersion = api.Scheme.Raw().InternalVersion
	}
	generator := pkg_runtime.NewDeepCopyGenerator(api.Scheme.Raw(), "github.com/openshift/origin/pkg/api", util.NewStringSet("github.com/openshift/origin"))
	apiShort := generator.AddImport("k8s.io/kubernetes/pkg/api")
	generator.ReplaceType("k8s.io/kubernetes/pkg/util", "empty", struct{}{})

	for _, overwrite := range strings.Split(*overwrites, ",") {
		vals := strings.Split(overwrite, "=")
		generator.OverwritePackage(vals[0], vals[1])
	}
	for _, knownType := range api.Scheme.KnownTypes(knownVersion) {
		if !strings.Contains(knownType.PkgPath(), "openshift/origin") {
			continue
		}
		if err := generator.AddType(knownType); err != nil {
			glog.Errorf("error while generating deep copy functions for %v: %v", knownType, err)
		}
	}

	generator.RepackImports()
	// the repack changes the name of the import
	apiShort = generator.AddImport("k8s.io/kubernetes/pkg/api")

	if err := generator.WriteImports(funcOut); err != nil {
		glog.Fatalf("error while writing imports: %v", err)
	}
	if err := generator.WriteDeepCopyFunctions(funcOut); err != nil {
		glog.Fatalf("error while writing deep copy functions: %v", err)
	}
	if err := generator.RegisterDeepCopyFunctions(funcOut, fmt.Sprintf("%s.Scheme", apiShort)); err != nil {
		glog.Fatalf("error while registering deep copy functions: %v", err)
	}
}
コード例 #4
0
func generateDeepCopies(t *testing.T, version string) bytes.Buffer {
	testedVersion := version
	registerTo := "api.Scheme"
	if testedVersion == "api" {
		testedVersion = api.Scheme.Raw().InternalVersion
		registerTo = "Scheme"
	}

	g := runtime.NewDeepCopyGenerator(api.Scheme.Raw(), path.Join("k8s.io/kubernetes/pkg/api", testedVersion), util.NewStringSet("k8s.io/kubernetes"))
	g.AddImport("k8s.io/kubernetes/pkg/api")
	g.OverwritePackage(version, "")

	for _, knownType := range api.Scheme.KnownTypes(testedVersion) {
		if err := g.AddType(knownType); err != nil {
			glog.Errorf("error while generating deep-copy functions for %v: %v", knownType, err)
		}
	}

	var functions bytes.Buffer
	functionsWriter := bufio.NewWriter(&functions)
	g.RepackImports()
	if err := g.WriteImports(functionsWriter); err != nil {
		t.Fatalf("couldn't generate deep-copy function imports: %v", err)
	}
	if err := g.WriteDeepCopyFunctions(functionsWriter); err != nil {
		t.Fatalf("couldn't generate deep-copy functions: %v", err)
	}
	if err := g.RegisterDeepCopyFunctions(functionsWriter, registerTo); err != nil {
		t.Fatalf("couldn't generate deep-copy function names: %v", err)
	}
	if err := functionsWriter.Flush(); err != nil {
		t.Fatalf("error while flushing writer")
	}

	return functions
}
コード例 #5
0
ファイル: deep_copy.go プロジェクト: sbusso/kubernetes
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)

	group, version := path.Split(*groupVersion)
	group = strings.TrimRight(group, "/")
	registerTo := "api.Scheme"
	if *groupVersion == "api/" {
		registerTo = "Scheme"
	}
	pkgname := group
	if len(version) != 0 {
		pkgname = version
	}

	_, err := data.WriteString(fmt.Sprintf("package %s\n", pkgname))
	if err != nil {
		glog.Fatalf("error writing package line: %v", err)
	}

	versionPath := path.Join(pkgBase, group, version)
	generator := pkg_runtime.NewDeepCopyGenerator(api.Scheme.Raw(), versionPath, sets.NewString("k8s.io/kubernetes"))
	generator.AddImport(path.Join(pkgBase, "api"))

	if len(*overwrites) > 0 {
		for _, overwrite := range strings.Split(*overwrites, ",") {
			if !strings.Contains(overwrite, "=") {
				glog.Fatalf("Invalid overwrite syntax: %s", overwrite)
			}
			vals := strings.Split(overwrite, "=")
			generator.OverwritePackage(vals[0], vals[1])
		}
	}
	for _, knownType := range api.Scheme.KnownTypes(version) {
		if !strings.HasPrefix(knownType.PkgPath(), versionPath) {
			continue
		}
		if err := generator.AddType(knownType); err != nil {
			glog.Errorf("error while generating deep copy functions for %v: %v", knownType, err)
		}
	}
	generator.RepackImports()
	if err := generator.WriteImports(data); err != nil {
		glog.Fatalf("error while writing imports: %v", err)
	}
	if err := generator.WriteDeepCopyFunctions(data); err != nil {
		glog.Fatalf("error while writing deep copy functions: %v", err)
	}
	if err := generator.RegisterDeepCopyFunctions(data, registerTo); err != nil {
		glog.Fatalf("error while registering deep copy functions: %v", err)
	}
	b, err := imports.Process("", data.Bytes(), nil)
	if err != nil {
		glog.Fatalf("error while update imports: %v", err)
	}
	if _, err := funcOut.Write(b); err != nil {
		glog.Fatalf("error while writing out the resulting file: %v", err)
	}
}
コード例 #6
0
ファイル: deep_copy.go プロジェクト: ethernetdan/kubernetes
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)
	}

	registerTo := destScheme(gv)
	var pkgname string
	if gv.Group == "" {
		// the internal version of v1 is registered in package api
		pkgname = "api"
	} else {
		pkgname = gv.Group
	}
	if len(gv.Version) != 0 && gv.Version != kruntime.APIVersionInternal {
		pkgname = gv.Version
	}

	_, err = data.WriteString(fmt.Sprintf("package %s\n", pkgname))
	if err != nil {
		glog.Fatalf("Error while writing package line: %v", err)
	}

	versionPath := pkgPath(gv.Group, gv.Version)
	generator := kruntime.NewDeepCopyGenerator(api.Scheme, versionPath, sets.NewString("k8s.io/kubernetes"))
	generator.AddImport(path.Join(pkgBase, "api"))

	if len(*overwrites) > 0 {
		for _, overwrite := range strings.Split(*overwrites, ",") {
			if !strings.Contains(overwrite, "=") {
				glog.Fatalf("Invalid overwrite syntax: %s", overwrite)
			}
			vals := strings.Split(overwrite, "=")
			generator.OverwritePackage(vals[0], vals[1])
		}
	}

	for _, knownType := range api.Scheme.KnownTypes(gv) {
		if knownType.PkgPath() != versionPath {
			continue
		}
		if err := generator.AddType(knownType); err != nil {
			glog.Errorf("Error while generating deep copy functions for %v: %v", knownType, err)
		}
	}
	generator.RepackImports()
	if err := generator.WriteImports(data); err != nil {
		glog.Fatalf("Error while writing imports: %v", err)
	}
	if err := generator.WriteDeepCopyFunctions(data); err != nil {
		glog.Fatalf("Error while writing deep copy functions: %v", err)
	}
	if err := generator.RegisterDeepCopyFunctions(data, registerTo); err != nil {
		glog.Fatalf("Error while registering deep copy functions: %v", err)
	}
	b, err := imports.Process("", data.Bytes(), nil)
	if err != nil {
		glog.Fatalf("Error while update imports: %v", err)
	}
	if _, err := funcOut.Write(b); err != nil {
		glog.Fatalf("Error while writing out the resulting file: %v", err)
	}
}