Commit 8abb3ae7 authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #43269 from smarterclayton/fix_staging

Automatic merge from submit-queue protobuf generation modifies types.go, which needs to be copied out This was broken when we moved to the build container, but no one noticed. Made it so that we get a test error if a field in a registered type has a json tag with no protobuf tag. Fixes #35486
parents cdcec3f3 6d2a3bdd
...@@ -704,6 +704,7 @@ function kube::build::copy_output() { ...@@ -704,6 +704,7 @@ function kube::build::copy_output() {
--filter='+ zz_generated.*' \ --filter='+ zz_generated.*' \
--filter='+ generated.proto' \ --filter='+ generated.proto' \
--filter='+ *.pb.go' \ --filter='+ *.pb.go' \
--filter='+ types.go' \
--filter='+ */' \ --filter='+ */' \
--filter='- /**' \ --filter='- /**' \
"rsync://k8s@${KUBE_RSYNC_ADDR}/k8s/" "${KUBE_ROOT}" "rsync://k8s@${KUBE_RSYNC_ADDR}/k8s/" "${KUBE_ROOT}"
......
...@@ -19,7 +19,9 @@ package api_test ...@@ -19,7 +19,9 @@ package api_test
import ( import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"fmt"
"math/rand" "math/rand"
"reflect"
"testing" "testing"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
...@@ -59,6 +61,41 @@ func TestUniversalDeserializer(t *testing.T) { ...@@ -59,6 +61,41 @@ func TestUniversalDeserializer(t *testing.T) {
} }
} }
func TestAllFieldsHaveTags(t *testing.T) {
for gvk, obj := range api.Scheme.AllKnownTypes() {
if gvk.Version == runtime.APIVersionInternal {
// internal versions are not serialized to protobuf
continue
}
if gvk.Group == "componentconfig" {
// component config is not serialized to protobuf
continue
}
if err := fieldsHaveProtobufTags(obj); err != nil {
t.Errorf("type %s as gvk %v is missing tags: %v", obj, gvk, err)
}
}
}
func fieldsHaveProtobufTags(obj reflect.Type) error {
switch obj.Kind() {
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Array:
return fieldsHaveProtobufTags(obj.Elem())
case reflect.Struct:
for i := 0; i < obj.NumField(); i++ {
f := obj.Field(i)
if f.Name == "TypeMeta" && f.Type.Name() == "TypeMeta" {
// TypeMeta is not included in external protobuf because we use an envelope type with TypeMeta
continue
}
if len(f.Tag.Get("json")) > 0 && len(f.Tag.Get("protobuf")) == 0 {
return fmt.Errorf("field %s in %s has a 'json' tag but no protobuf tag", f.Name, obj)
}
}
}
return nil
}
func TestProtobufRoundTrip(t *testing.T) { func TestProtobufRoundTrip(t *testing.T) {
obj := &v1.Pod{} obj := &v1.Pod{}
apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t, api.Codecs), rand.NewSource(benchmarkSeed)).Fuzz(obj) apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t, api.Codecs), rand.NewSource(benchmarkSeed)).Fuzz(obj)
......
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