apimachinery: handle duplicated and conflicting type registration
This commit is contained in:
@@ -190,7 +190,17 @@ func (s *Scheme) AddKnownTypeWithName(gvk schema.GroupVersionKind, obj Object) {
|
||||
panic("All types must be pointers to structs.")
|
||||
}
|
||||
|
||||
if oldT, found := s.gvkToType[gvk]; found && oldT != t {
|
||||
panic(fmt.Sprintf("Double registration of different types for %v: old=%v.%v, new=%v.%v", gvk, oldT.PkgPath(), oldT.Name(), t.PkgPath(), t.Name()))
|
||||
}
|
||||
|
||||
s.gvkToType[gvk] = t
|
||||
|
||||
for _, existingGvk := range s.typeToGVK[t] {
|
||||
if existingGvk == gvk {
|
||||
return
|
||||
}
|
||||
}
|
||||
s.typeToGVK[t] = append(s.typeToGVK[t], gvk)
|
||||
}
|
||||
|
||||
|
||||
@@ -548,6 +548,79 @@ func TestKnownTypes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddKnownTypesIdemPotent(t *testing.T) {
|
||||
s := runtime.NewScheme()
|
||||
|
||||
gv := schema.GroupVersion{Group: "foo", Version: "v1"}
|
||||
s.AddKnownTypes(gv, &InternalSimple{})
|
||||
s.AddKnownTypes(gv, &InternalSimple{})
|
||||
if len(s.KnownTypes(gv)) != 1 {
|
||||
t.Errorf("expected only one %v type after double registration", gv)
|
||||
}
|
||||
if len(s.AllKnownTypes()) != 1 {
|
||||
t.Errorf("expected only one type after double registration")
|
||||
}
|
||||
|
||||
s.AddKnownTypeWithName(gv.WithKind("InternalSimple"), &InternalSimple{})
|
||||
s.AddKnownTypeWithName(gv.WithKind("InternalSimple"), &InternalSimple{})
|
||||
if len(s.KnownTypes(gv)) != 1 {
|
||||
t.Errorf("expected only one %v type after double registration with custom name", gv)
|
||||
}
|
||||
if len(s.AllKnownTypes()) != 1 {
|
||||
t.Errorf("expected only one type after double registration with custom name")
|
||||
}
|
||||
|
||||
s.AddUnversionedTypes(gv, &InternalSimple{})
|
||||
if len(s.KnownTypes(gv)) != 1 {
|
||||
t.Errorf("expected only one %v type after double registration with custom name", gv)
|
||||
}
|
||||
if len(s.AllKnownTypes()) != 1 {
|
||||
t.Errorf("expected only one type after double registration with custom name")
|
||||
}
|
||||
|
||||
kinds, _, err := s.ObjectKinds(&InternalSimple{})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if len(kinds) != 1 {
|
||||
t.Errorf("expected only one kind for InternalSimple after double registration", gv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConflictingAddKnownTypes(t *testing.T) {
|
||||
s := runtime.NewScheme()
|
||||
gv := schema.GroupVersion{Group: "foo", Version: "v1"}
|
||||
|
||||
panicked := make(chan bool)
|
||||
go func() {
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
panicked <- true
|
||||
}
|
||||
}()
|
||||
s.AddKnownTypeWithName(gv.WithKind("InternalSimple"), &InternalSimple{})
|
||||
s.AddKnownTypeWithName(gv.WithKind("InternalSimple"), &ExternalSimple{})
|
||||
panicked <- false
|
||||
}()
|
||||
if !<-panicked {
|
||||
t.Errorf("Expected AddKnownTypesWithName to panic with conflicting type registrations")
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
panicked <- true
|
||||
}
|
||||
}()
|
||||
s.AddUnversionedTypes(gv, &InternalSimple{})
|
||||
s.AddUnversionedTypes(gv, &InternalSimple{})
|
||||
panicked <- false
|
||||
}()
|
||||
if !<-panicked {
|
||||
t.Errorf("Expected AddUnversionedTypes to panic with conflicting type registrations")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertToVersionBasic(t *testing.T) {
|
||||
s := GetTestScheme()
|
||||
tt := &TestType1{A: "I'm not a pointer object"}
|
||||
|
||||
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
package apiserver
|
||||
|
||||
import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
@@ -47,6 +46,5 @@ func addKnownTypes(scheme *runtime.Scheme) error {
|
||||
scheme.AddKnownTypes(SchemeGroupVersion,
|
||||
&AdmissionConfiguration{},
|
||||
)
|
||||
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
package example
|
||||
|
||||
import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
@@ -48,6 +47,5 @@ func addKnownTypes(scheme *runtime.Scheme) error {
|
||||
scheme.AddKnownTypes(SchemeGroupVersion,
|
||||
&Pod{},
|
||||
)
|
||||
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -77,7 +77,6 @@ func testScheme(t *testing.T) (*runtime.Scheme, serializer.CodecFactory) {
|
||||
scheme := runtime.NewScheme()
|
||||
scheme.Log(t)
|
||||
scheme.AddKnownTypes(schema.GroupVersion{Version: runtime.APIVersionInternal}, &storagetesting.TestResource{})
|
||||
scheme.AddKnownTypes(schema.GroupVersion{Version: runtime.APIVersionInternal}, &storagetesting.TestResource{})
|
||||
example.AddToScheme(scheme)
|
||||
examplev1.AddToScheme(scheme)
|
||||
if err := scheme.AddConversionFuncs(
|
||||
|
||||
@@ -17,12 +17,8 @@ limitations under the License.
|
||||
package apiregistration
|
||||
|
||||
import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
|
||||
// we register here until we separate our scheme, which requires updates to client gen
|
||||
kapi "k8s.io/client-go/pkg/api"
|
||||
)
|
||||
|
||||
const GroupName = "apiregistration.k8s.io"
|
||||
@@ -50,11 +46,6 @@ func addKnownTypes(scheme *runtime.Scheme) error {
|
||||
scheme.AddKnownTypes(SchemeGroupVersion,
|
||||
&APIService{},
|
||||
&APIServiceList{},
|
||||
|
||||
&kapi.ListOptions{},
|
||||
&metav1.DeleteOptions{},
|
||||
&metav1.GetOptions{},
|
||||
)
|
||||
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
package wardle
|
||||
|
||||
import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
@@ -48,6 +47,5 @@ func addKnownTypes(scheme *runtime.Scheme) error {
|
||||
&Flunder{},
|
||||
&FlunderList{},
|
||||
)
|
||||
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user