List output with differing types should be more resilient

kubectl get can output a series of objects as a List in versioned
form, but not all API objects are available in the same schema.
Make the act of converting a []runtime.Object to api.List more
robust and add a test to verify its behavior in Get.

Makes it easier for client code to output unified objects.
This commit is contained in:
Clayton Coleman
2015-04-16 19:21:13 -04:00
parent 7f75c982ce
commit 545a5a865f
9 changed files with 255 additions and 29 deletions

View File

@@ -49,23 +49,32 @@ type externalType struct {
Name string `json:"name"`
}
func (*internalType) IsAnAPIObject() {}
func (*externalType) IsAnAPIObject() {}
type ExternalType2 struct {
Kind string `json:"kind"`
APIVersion string `json:"apiVersion"`
Name string `json:"name"`
}
func (*internalType) IsAnAPIObject() {}
func (*externalType) IsAnAPIObject() {}
func (*ExternalType2) IsAnAPIObject() {}
func newExternalScheme() (*runtime.Scheme, meta.RESTMapper, runtime.Codec) {
scheme := runtime.NewScheme()
scheme.AddKnownTypeWithName("", "Type", &internalType{})
scheme.AddKnownTypeWithName("unlikelyversion", "Type", &externalType{})
scheme.AddKnownTypeWithName("v1beta1", "Type", &ExternalType2{})
codec := runtime.CodecFor(scheme, "unlikelyversion")
mapper := meta.NewDefaultRESTMapper([]string{"unlikelyversion"}, func(version string) (*meta.VersionInterfaces, bool) {
mapper := meta.NewDefaultRESTMapper([]string{"unlikelyversion", "v1beta1"}, func(version string) (*meta.VersionInterfaces, bool) {
return &meta.VersionInterfaces{
Codec: codec,
Codec: runtime.CodecFor(scheme, version),
ObjectConvertor: scheme,
MetadataAccessor: meta.NewAccessor(),
}, (version == "unlikelyversion")
}, (version == "v1beta1" || version == "unlikelyversion")
})
for _, version := range []string{"unlikelyversion"} {
for _, version := range []string{"unlikelyversion", "v1beta1"} {
for kind := range scheme.KnownTypes(version) {
mixedCase := false
scope := meta.RESTScopeNamespace
@@ -142,6 +151,20 @@ func NewTestFactory() (*cmdutil.Factory, *testFactory, runtime.Codec) {
}, t, codec
}
func NewMixedFactory(apiClient resource.RESTClient) (*cmdutil.Factory, *testFactory, runtime.Codec) {
f, t, c := NewTestFactory()
f.Object = func() (meta.RESTMapper, runtime.ObjectTyper) {
return meta.MultiRESTMapper{t.Mapper, latest.RESTMapper}, runtime.MultiObjectTyper{t.Typer, api.Scheme}
}
f.RESTClient = func(m *meta.RESTMapping) (resource.RESTClient, error) {
if m.ObjectConvertor == api.Scheme {
return apiClient, t.Err
}
return t.Client, t.Err
}
return f, t, c
}
func NewAPIFactory() (*cmdutil.Factory, *testFactory, runtime.Codec) {
t := &testFactory{
Validator: validation.NullSchema{},