Merge pull request #87892 from wojtek-t/manual_fake_kubectl_conversions

Register conversions for kubectl testing types

Kubernetes-commit: 64ba0bf3d6630c3565e2886d0ab923fac4ac2a66
This commit is contained in:
Kubernetes Publisher 2020-02-07 07:38:01 -08:00
commit 9f35a6cd42
4 changed files with 118 additions and 10 deletions

4
Godeps/Godeps.json generated
View File

@ -580,7 +580,7 @@
}, },
{ {
"ImportPath": "k8s.io/client-go", "ImportPath": "k8s.io/client-go",
"Rev": "473926661c44" "Rev": "5be5d5753fd2"
}, },
{ {
"ImportPath": "k8s.io/code-generator", "ImportPath": "k8s.io/code-generator",
@ -604,7 +604,7 @@
}, },
{ {
"ImportPath": "k8s.io/metrics", "ImportPath": "k8s.io/metrics",
"Rev": "ec30850af31a" "Rev": "50b91938458d"
}, },
{ {
"ImportPath": "k8s.io/utils", "ImportPath": "k8s.io/utils",

8
go.mod
View File

@ -39,11 +39,11 @@ require (
k8s.io/api v0.0.0-20200207025841-85a41f27a10c k8s.io/api v0.0.0-20200207025841-85a41f27a10c
k8s.io/apimachinery v0.0.0-20200207025655-52a338251bb2 k8s.io/apimachinery v0.0.0-20200207025655-52a338251bb2
k8s.io/cli-runtime v0.0.0-20200207031900-ea3686d36745 k8s.io/cli-runtime v0.0.0-20200207031900-ea3686d36745
k8s.io/client-go v0.0.0-20200207030105-473926661c44 k8s.io/client-go v0.0.0-20200207150107-5be5d5753fd2
k8s.io/component-base v0.0.0-20200207030544-616550b070ba k8s.io/component-base v0.0.0-20200207030544-616550b070ba
k8s.io/klog v1.0.0 k8s.io/klog v1.0.0
k8s.io/kube-openapi v0.0.0-20200121204235-bf4fb3bd569c k8s.io/kube-openapi v0.0.0-20200121204235-bf4fb3bd569c
k8s.io/metrics v0.0.0-20200207031748-ec30850af31a k8s.io/metrics v0.0.0-20200207111351-50b91938458d
k8s.io/utils v0.0.0-20191217005138-9e5e9d854fcc k8s.io/utils v0.0.0-20191217005138-9e5e9d854fcc
sigs.k8s.io/kustomize v2.0.3+incompatible sigs.k8s.io/kustomize v2.0.3+incompatible
sigs.k8s.io/yaml v1.2.0 sigs.k8s.io/yaml v1.2.0
@ -56,8 +56,8 @@ replace (
k8s.io/api => k8s.io/api v0.0.0-20200207025841-85a41f27a10c k8s.io/api => k8s.io/api v0.0.0-20200207025841-85a41f27a10c
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20200207025655-52a338251bb2 k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20200207025655-52a338251bb2
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20200207031900-ea3686d36745 k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20200207031900-ea3686d36745
k8s.io/client-go => k8s.io/client-go v0.0.0-20200207030105-473926661c44 k8s.io/client-go => k8s.io/client-go v0.0.0-20200207150107-5be5d5753fd2
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20200207025509-0fa7fdad09bb k8s.io/code-generator => k8s.io/code-generator v0.0.0-20200207025509-0fa7fdad09bb
k8s.io/component-base => k8s.io/component-base v0.0.0-20200207030544-616550b070ba k8s.io/component-base => k8s.io/component-base v0.0.0-20200207030544-616550b070ba
k8s.io/metrics => k8s.io/metrics v0.0.0-20200207031748-ec30850af31a k8s.io/metrics => k8s.io/metrics v0.0.0-20200207111351-50b91938458d
) )

4
go.sum
View File

@ -315,7 +315,7 @@ honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWh
k8s.io/api v0.0.0-20200207025841-85a41f27a10c/go.mod h1:SA5EjO7X3UBpRgCFnqrgyCg0rXvBmN8QpNZ4Bvmv9js= k8s.io/api v0.0.0-20200207025841-85a41f27a10c/go.mod h1:SA5EjO7X3UBpRgCFnqrgyCg0rXvBmN8QpNZ4Bvmv9js=
k8s.io/apimachinery v0.0.0-20200207025655-52a338251bb2/go.mod h1:4JiEu5qfVX4iOPJG4zxNOGJ0HamEJ5OmJHxtHMChmZo= k8s.io/apimachinery v0.0.0-20200207025655-52a338251bb2/go.mod h1:4JiEu5qfVX4iOPJG4zxNOGJ0HamEJ5OmJHxtHMChmZo=
k8s.io/cli-runtime v0.0.0-20200207031900-ea3686d36745/go.mod h1:8gpIw0RGF2xY0wzzX+413xlK21Jj4DDLzh6nTcmIzPE= k8s.io/cli-runtime v0.0.0-20200207031900-ea3686d36745/go.mod h1:8gpIw0RGF2xY0wzzX+413xlK21Jj4DDLzh6nTcmIzPE=
k8s.io/client-go v0.0.0-20200207030105-473926661c44/go.mod h1:ourVHU91vVd0R4YiJhW1FNlt3K+HtokZ5ImtcAbqVh4= k8s.io/client-go v0.0.0-20200207150107-5be5d5753fd2/go.mod h1:ourVHU91vVd0R4YiJhW1FNlt3K+HtokZ5ImtcAbqVh4=
k8s.io/code-generator v0.0.0-20200207025509-0fa7fdad09bb/go.mod h1:+UHX5rSbxmR8kzS+FAv7um6dtYrZokQvjHpDSYRVkTc= k8s.io/code-generator v0.0.0-20200207025509-0fa7fdad09bb/go.mod h1:+UHX5rSbxmR8kzS+FAv7um6dtYrZokQvjHpDSYRVkTc=
k8s.io/component-base v0.0.0-20200207030544-616550b070ba/go.mod h1:xqOLjWJL1P+sXGSeMoLVvN0UM/adtUwVUp/mwh5H518= k8s.io/component-base v0.0.0-20200207030544-616550b070ba/go.mod h1:xqOLjWJL1P+sXGSeMoLVvN0UM/adtUwVUp/mwh5H518=
k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0= k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
@ -326,7 +326,7 @@ k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8=
k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I= k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I=
k8s.io/kube-openapi v0.0.0-20200121204235-bf4fb3bd569c h1:/KUFqjjqAcY4Us6luF5RDNZ16KJtb49HfR3ZHB9qYXM= k8s.io/kube-openapi v0.0.0-20200121204235-bf4fb3bd569c h1:/KUFqjjqAcY4Us6luF5RDNZ16KJtb49HfR3ZHB9qYXM=
k8s.io/kube-openapi v0.0.0-20200121204235-bf4fb3bd569c/go.mod h1:GRQhZsXIAJ1xR0C9bd8UpWHZ5plfAS9fzPjJuQ6JL3E= k8s.io/kube-openapi v0.0.0-20200121204235-bf4fb3bd569c/go.mod h1:GRQhZsXIAJ1xR0C9bd8UpWHZ5plfAS9fzPjJuQ6JL3E=
k8s.io/metrics v0.0.0-20200207031748-ec30850af31a/go.mod h1:IlAqwf+NC5UU87qjUeOr3EHYOX4uNo9pMBfvFK2Uey8= k8s.io/metrics v0.0.0-20200207111351-50b91938458d/go.mod h1:+zoJP/46xQ2riSy7PeQnzMNePKGYRFaX8nC/9EFWBjA=
k8s.io/utils v0.0.0-20191217005138-9e5e9d854fcc h1:MUttqhwRgupMiA5ps5F3d2/NLkU8EZSECTGxrQxqM54= k8s.io/utils v0.0.0-20191217005138-9e5e9d854fcc h1:MUttqhwRgupMiA5ps5F3d2/NLkU8EZSECTGxrQxqM54=
k8s.io/utils v0.0.0-20191217005138-9e5e9d854fcc/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew= k8s.io/utils v0.0.0-20191217005138-9e5e9d854fcc/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew=
sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0= sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0=

View File

@ -28,9 +28,11 @@ import (
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/meta/testrestmapper" "k8s.io/apimachinery/pkg/api/meta/testrestmapper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer" "k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/resource" "k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/discovery" "k8s.io/client-go/discovery"
@ -128,6 +130,34 @@ func NewInternalType(kind, apiversion, name string) *InternalType {
return &item return &item
} }
func convertInternalTypeToExternalType(in *InternalType, out *ExternalType, s conversion.Scope) error {
out.Kind = in.Kind
out.APIVersion = in.APIVersion
out.Name = in.Name
return nil
}
func convertInternalTypeToExternalType2(in *InternalType, out *ExternalType2, s conversion.Scope) error {
out.Kind = in.Kind
out.APIVersion = in.APIVersion
out.Name = in.Name
return nil
}
func convertExternalTypeToInternalType(in *ExternalType, out *InternalType, s conversion.Scope) error {
out.Kind = in.Kind
out.APIVersion = in.APIVersion
out.Name = in.Name
return nil
}
func convertExternalType2ToInternalType(in *ExternalType2, out *InternalType, s conversion.Scope) error {
out.Kind = in.Kind
out.APIVersion = in.APIVersion
out.Name = in.Name
return nil
}
// InternalNamespacedType schema for internal namespaced types // InternalNamespacedType schema for internal namespaced types
// +k8s:deepcopy-gen=true // +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
@ -209,6 +239,38 @@ func NewInternalNamespacedType(kind, apiversion, name, namespace string) *Intern
return &item return &item
} }
func convertInternalNamespacedTypeToExternalNamespacedType(in *InternalNamespacedType, out *ExternalNamespacedType, s conversion.Scope) error {
out.Kind = in.Kind
out.APIVersion = in.APIVersion
out.Name = in.Name
out.Namespace = in.Namespace
return nil
}
func convertInternalNamespacedTypeToExternalNamespacedType2(in *InternalNamespacedType, out *ExternalNamespacedType2, s conversion.Scope) error {
out.Kind = in.Kind
out.APIVersion = in.APIVersion
out.Name = in.Name
out.Namespace = in.Namespace
return nil
}
func convertExternalNamespacedTypeToInternalNamespacedType(in *ExternalNamespacedType, out *InternalNamespacedType, s conversion.Scope) error {
out.Kind = in.Kind
out.APIVersion = in.APIVersion
out.Name = in.Name
out.Namespace = in.Namespace
return nil
}
func convertExternalNamespacedType2ToInternalNamespacedType(in *ExternalNamespacedType2, out *InternalNamespacedType, s conversion.Scope) error {
out.Kind = in.Kind
out.APIVersion = in.APIVersion
out.Name = in.Name
out.Namespace = in.Namespace
return nil
}
var errInvalidVersion = errors.New("not a version") var errInvalidVersion = errors.New("not a version")
// ValidVersion of API // ValidVersion of API
@ -230,18 +292,64 @@ func NewExternalScheme() (*runtime.Scheme, meta.RESTMapper, runtime.Codec) {
return scheme, mapper, codec return scheme, mapper, codec
} }
func registerConversions(s *runtime.Scheme) error {
if err := s.AddConversionFunc((*InternalType)(nil), (*ExternalType)(nil), func(a, b interface{}, scope conversion.Scope) error {
return convertInternalTypeToExternalType(a.(*InternalType), b.(*ExternalType), scope)
}); err != nil {
return err
}
if err := s.AddConversionFunc((*InternalType)(nil), (*ExternalType2)(nil), func(a, b interface{}, scope conversion.Scope) error {
return convertInternalTypeToExternalType2(a.(*InternalType), b.(*ExternalType2), scope)
}); err != nil {
return err
}
if err := s.AddConversionFunc((*ExternalType)(nil), (*InternalType)(nil), func(a, b interface{}, scope conversion.Scope) error {
return convertExternalTypeToInternalType(a.(*ExternalType), b.(*InternalType), scope)
}); err != nil {
return err
}
if err := s.AddConversionFunc((*ExternalType2)(nil), (*InternalType)(nil), func(a, b interface{}, scope conversion.Scope) error {
return convertExternalType2ToInternalType(a.(*ExternalType2), b.(*InternalType), scope)
}); err != nil {
return err
}
if err := s.AddConversionFunc((*InternalNamespacedType)(nil), (*ExternalNamespacedType)(nil), func(a, b interface{}, scope conversion.Scope) error {
return convertInternalNamespacedTypeToExternalNamespacedType(a.(*InternalNamespacedType), b.(*ExternalNamespacedType), scope)
}); err != nil {
return err
}
if err := s.AddConversionFunc((*InternalNamespacedType)(nil), (*ExternalNamespacedType2)(nil), func(a, b interface{}, scope conversion.Scope) error {
return convertInternalNamespacedTypeToExternalNamespacedType2(a.(*InternalNamespacedType), b.(*ExternalNamespacedType2), scope)
}); err != nil {
return err
}
if err := s.AddConversionFunc((*ExternalNamespacedType)(nil), (*InternalNamespacedType)(nil), func(a, b interface{}, scope conversion.Scope) error {
return convertExternalNamespacedTypeToInternalNamespacedType(a.(*ExternalNamespacedType), b.(*InternalNamespacedType), scope)
}); err != nil {
return err
}
if err := s.AddConversionFunc((*ExternalNamespacedType2)(nil), (*InternalNamespacedType)(nil), func(a, b interface{}, scope conversion.Scope) error {
return convertExternalNamespacedType2ToInternalNamespacedType(a.(*ExternalNamespacedType2), b.(*InternalNamespacedType), scope)
}); err != nil {
return err
}
return nil
}
// AddToScheme adds required objects into scheme // AddToScheme adds required objects into scheme
func AddToScheme(scheme *runtime.Scheme) (meta.RESTMapper, runtime.Codec) { func AddToScheme(scheme *runtime.Scheme) (meta.RESTMapper, runtime.Codec) {
scheme.AddKnownTypeWithName(InternalGV.WithKind("Type"), &InternalType{}) scheme.AddKnownTypeWithName(InternalGV.WithKind("Type"), &InternalType{})
scheme.AddKnownTypeWithName(UnlikelyGV.WithKind("Type"), &ExternalType{}) scheme.AddKnownTypeWithName(UnlikelyGV.WithKind("Type"), &ExternalType{})
//This tests that kubectl will not confuse the external scheme with the internal scheme, even when they accidentally have versions of the same name. // This tests that kubectl will not confuse the external scheme with the internal scheme, even when they accidentally have versions of the same name.
scheme.AddKnownTypeWithName(ValidVersionGV.WithKind("Type"), &ExternalType2{}) scheme.AddKnownTypeWithName(ValidVersionGV.WithKind("Type"), &ExternalType2{})
scheme.AddKnownTypeWithName(InternalGV.WithKind("NamespacedType"), &InternalNamespacedType{}) scheme.AddKnownTypeWithName(InternalGV.WithKind("NamespacedType"), &InternalNamespacedType{})
scheme.AddKnownTypeWithName(UnlikelyGV.WithKind("NamespacedType"), &ExternalNamespacedType{}) scheme.AddKnownTypeWithName(UnlikelyGV.WithKind("NamespacedType"), &ExternalNamespacedType{})
//This tests that kubectl will not confuse the external scheme with the internal scheme, even when they accidentally have versions of the same name. // This tests that kubectl will not confuse the external scheme with the internal scheme, even when they accidentally have versions of the same name.
scheme.AddKnownTypeWithName(ValidVersionGV.WithKind("NamespacedType"), &ExternalNamespacedType2{}) scheme.AddKnownTypeWithName(ValidVersionGV.WithKind("NamespacedType"), &ExternalNamespacedType2{})
utilruntime.Must(registerConversions(scheme))
codecs := serializer.NewCodecFactory(scheme) codecs := serializer.NewCodecFactory(scheme)
codec := codecs.LegacyCodec(UnlikelyGV) codec := codecs.LegacyCodec(UnlikelyGV)
mapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{UnlikelyGV, ValidVersionGV}) mapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{UnlikelyGV, ValidVersionGV})