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

genericapiserver: cut off more dependencies – episode 5 #40808

Merged
merged 8 commits into from
Feb 2, 2017
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
23 changes: 22 additions & 1 deletion cmd/libs/go2idl/conversion-gen/generators/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,27 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
skipUnsafe = customArgs.SkipUnsafe
}

// if the source path is within a /vendor/ directory (for example,
// k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/apis/meta/v1), allow
// generation to output to the proper relative path (under vendor).
// Otherwise, the generator will create the file in the wrong location
// in the output directory.
// TODO: build a more fundamental concept in gengo for dealing with modifications
// to vendored packages.
vendorless := func(pkg string) string {
if pos := strings.LastIndex(pkg, "/vendor/"); pos != -1 {
return pkg[pos+len("/vendor/"):]
}
return pkg
}
fqPkgPath := pkg.Path
if strings.Contains(pkg.SourcePath, "/vendor/") {
fqPkgPath = filepath.Join("k8s.io", "kubernetes", "vendor", pkg.Path)
}
for i := range peerPkgs {
peerPkgs[i] = vendorless(peerPkgs[i])
}

// Make sure our peer-packages are added and fully parsed.
for _, pp := range peerPkgs {
context.AddDir(pp)
Expand All @@ -236,7 +257,7 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
packages = append(packages,
&generator.DefaultPackage{
PackageName: filepath.Base(pkg.Path),
PackagePath: pkg.Path,
PackagePath: fqPkgPath,
HeaderText: header,
GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
return []generator.Generator{
Expand Down
1 change: 1 addition & 0 deletions cmd/libs/go2idl/go-to-protobuf/protobuf/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func New() *Generator {
`+k8s.io/apimachinery/pkg/runtime/schema`,
`+k8s.io/apimachinery/pkg/runtime`,
`k8s.io/apimachinery/pkg/apis/meta/v1`,
`k8s.io/apiserver/pkg/apis/example/v1`,
`k8s.io/kubernetes/pkg/api/v1`,
`k8s.io/kubernetes/pkg/apis/policy/v1beta1`,
`k8s.io/kubernetes/pkg/apis/extensions/v1beta1`,
Expand Down
103 changes: 52 additions & 51 deletions federation/apis/federation/v1beta1/generated.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions federation/apis/federation/v1beta1/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions hack/.linted_packages
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ staging/src/k8s.io/apimachinery/pkg/util/json
staging/src/k8s.io/apimachinery/pkg/util/validation/field
staging/src/k8s.io/apimachinery/pkg/version
staging/src/k8s.io/apimachinery/pkg/watch
staging/src/k8s.io/apiserver/pkg/apis/example/install
staging/src/k8s.io/apiserver/pkg/authentication/authenticator
staging/src/k8s.io/apiserver/pkg/authentication/request/union
staging/src/k8s.io/apiserver/pkg/authentication/request/x509
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

func BenchmarkPodConversion(b *testing.B) {
apiObjectFuzzer := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(b), rand.NewSource(benchmarkSeed))
apiObjectFuzzer := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(b, api.Codecs), rand.NewSource(benchmarkSeed))
items := make([]api.Pod, 4)
for i := range items {
apiObjectFuzzer.Fuzz(&items[i])
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
func TestDeepCopyApiObjects(t *testing.T) {
for i := 0; i < *fuzzIters; i++ {
for _, version := range []schema.GroupVersion{testapi.Default.InternalGroupVersion(), api.Registry.GroupOrDie(api.GroupName).GroupVersion} {
f := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t), rand.NewSource(rand.Int63()))
f := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t, api.Codecs), rand.NewSource(rand.Int63()))
for kind := range api.Scheme.KnownTypes(version) {
doDeepCopyTest(t, version.WithKind(kind), f)
}
Expand Down Expand Up @@ -84,7 +84,7 @@ func doDeepCopyTest(t *testing.T, kind schema.GroupVersionKind, f *fuzz.Fuzzer)
func TestDeepCopySingleType(t *testing.T) {
for i := 0; i < *fuzzIters; i++ {
for _, version := range []schema.GroupVersion{testapi.Default.InternalGroupVersion(), api.Registry.GroupOrDie(api.GroupName).GroupVersion} {
f := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t), rand.NewSource(rand.Int63()))
f := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t, api.Codecs), rand.NewSource(rand.Int63()))
doDeepCopyTest(t, version.WithKind("Pod"), f)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/serialization_proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestUniversalDeserializer(t *testing.T) {

func TestProtobufRoundTrip(t *testing.T) {
obj := &v1.Pod{}
apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t), rand.NewSource(benchmarkSeed)).Fuzz(obj)
apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t, api.Codecs), rand.NewSource(benchmarkSeed)).Fuzz(obj)
// InitContainers are turned into annotations by conversion.
obj.Spec.InitContainers = nil
obj.Status.InitContainerStatuses = nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var codecsToTest = []func(version schema.GroupVersion, item runtime.Object) (run
// fuzzInternalObject fuzzes an arbitrary runtime object using the appropriate
// fuzzer registered with the apitesting package.
func fuzzInternalObject(t *testing.T, forVersion schema.GroupVersion, item runtime.Object, seed int64) runtime.Object {
apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t), rand.NewSource(seed)).Fuzz(item)
apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t, api.Codecs), rand.NewSource(seed)).Fuzz(item)

j, err := meta.TypeAccessor(item)
if err != nil {
Expand Down Expand Up @@ -442,7 +442,7 @@ func TestUnversionedTypes(t *testing.T) {
// TestObjectWatchFraming establishes that a watch event can be encoded and
// decoded correctly through each of the supported RFC2046 media types.
func TestObjectWatchFraming(t *testing.T) {
f := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t), rand.NewSource(benchmarkSeed))
f := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t, api.Codecs), rand.NewSource(benchmarkSeed))
secret := &api.Secret{}
f.Fuzz(secret)
secret.Data["binary"] = []byte{0x00, 0x10, 0x30, 0x55, 0xff, 0x00}
Expand Down Expand Up @@ -524,7 +524,7 @@ func TestObjectWatchFraming(t *testing.T) {
const benchmarkSeed = 100

func benchmarkItems(b *testing.B) []v1.Pod {
apiObjectFuzzer := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(b), rand.NewSource(benchmarkSeed))
apiObjectFuzzer := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(b, api.Codecs), rand.NewSource(benchmarkSeed))
items := make([]v1.Pod, 10)
for i := range items {
var pod api.Pod
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ go_library(
"//pkg/apis/batch:go_default_library",
"//pkg/apis/certificates:go_default_library",
"//pkg/apis/extensions:go_default_library",
"//pkg/apis/extensions/v1beta1:go_default_library",
"//pkg/apis/policy:go_default_library",
"//pkg/apis/rbac:go_default_library",
"//vendor:github.com/google/gofuzz",
Expand All @@ -34,6 +35,7 @@ go_library(
"//vendor:k8s.io/apimachinery/pkg/labels",
"//vendor:k8s.io/apimachinery/pkg/runtime",
"//vendor:k8s.io/apimachinery/pkg/runtime/schema",
"//vendor:k8s.io/apimachinery/pkg/runtime/serializer",
"//vendor:k8s.io/apimachinery/pkg/util/intstr",
],
)
Expand Down
47 changes: 10 additions & 37 deletions pkg/api/testing/fuzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,24 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/apis/autoscaling"
"k8s.io/kubernetes/pkg/apis/batch"
"k8s.io/kubernetes/pkg/apis/certificates"
"k8s.io/kubernetes/pkg/apis/extensions"
extensionsv1beta1 "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
"k8s.io/kubernetes/pkg/apis/policy"
"k8s.io/kubernetes/pkg/apis/rbac"
)

// overrideGenericFuncs override some generic fuzzer funcs from k8s.io/apiserver in order to have more realistic
// values in a Kubernetes context.
func overrideGenericFuncs(t apitesting.TestingCommon) []interface{} {
func overrideGenericFuncs(t apitesting.TestingCommon, codecs runtimeserializer.CodecFactory) []interface{} {
return []interface{}{
func(j *runtime.Object, c fuzz.Continue) {
// TODO: uncomment when round trip starts from a versioned object
Expand All @@ -67,20 +70,12 @@ func overrideGenericFuncs(t apitesting.TestingCommon) []interface{} {
obj := types[c.Rand.Intn(len(types))]
c.Fuzz(obj)

// Find a codec for converting the object to raw bytes. This is necessary for the
// api version and kind to be correctly set be serialization.
var codec runtime.Codec
switch obj.(type) {
case *api.Pod:
codec = testapi.Default.Codec()
case *extensions.Deployment:
codec = testapi.Extensions.Codec()
case *api.Service:
codec = testapi.Default.Codec()
codec = apitesting.TestCodec(codecs, extensionsv1beta1.SchemeGroupVersion)
default:

t.Errorf("Failed to find codec for object type: %T", obj)
return
codec = apitesting.TestCodec(codecs, v1.SchemeGroupVersion)
}

// Convert the object to raw bytes
Expand Down Expand Up @@ -602,10 +597,10 @@ func certificateFuncs(t apitesting.TestingCommon) []interface{} {
}
}

func FuzzerFuncs(t apitesting.TestingCommon) []interface{} {
return mergeFuncLists(t,
apitesting.GenericFuzzerFuncs(t),
overrideGenericFuncs(t),
func FuzzerFuncs(t apitesting.TestingCommon, codecs runtimeserializer.CodecFactory) []interface{} {
return apitesting.MergeFuzzerFuncs(t,
apitesting.GenericFuzzerFuncs(t, codecs),
overrideGenericFuncs(t, codecs),
coreFuncs(t),
extensionFuncs(t),
batchFuncs(t),
Expand All @@ -622,25 +617,3 @@ func newBool(val bool) *bool {
*p = val
return p
}

// mergeFuncLists will merge the given funcLists, overriding early funcs with later ones if there first
// argument has the same type.
func mergeFuncLists(t apitesting.TestingCommon, funcLists ...[]interface{}) []interface{} {
funcMap := map[string]interface{}{}
for _, list := range funcLists {
for _, f := range list {
fT := reflect.TypeOf(f)
if fT.Kind() != reflect.Func || fT.NumIn() != 2 {
t.Errorf("Fuzzer func with invalid type: %v", fT)
continue
}
funcMap[fT.In(0).String()] = f
}
}

result := []interface{}{}
for _, f := range funcMap {
result = append(result, f)
}
return result
}