Introduce ObjectConvertor for conversion to known API versions

Will allow clients to transform internal objects to a version
suitable for external output.
This commit is contained in:
Clayton Coleman
2014-11-01 18:38:02 -04:00
parent 2d54dfe249
commit 778a50d00b
4 changed files with 102 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package runtime
import (
"fmt"
"reflect"
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
@@ -223,6 +224,23 @@ func (s *Scheme) Convert(in, out interface{}) error {
return s.raw.Convert(in, out)
}
// ConvertToVersion attempts to convert an input object to its matching Kind in another
// version within this scheme. Will return an error if the provided version does not
// contain the inKind (or a mapping by name defined with AddKnownTypeWithName). Will also
// return an error if the conversion does not result in a valid Object being
// returned.
func (s *Scheme) ConvertToVersion(in Object, outVersion string) (Object, error) {
unknown, err := s.raw.ConvertToVersion(in, outVersion)
if err != nil {
return nil, err
}
obj, ok := unknown.(Object)
if !ok {
return nil, fmt.Errorf("the provided object cannot be converted to a runtime.Object: %#v", unknown)
}
return obj, nil
}
// EncodeToVersion turns the given api object into an appropriate JSON string.
// Will return an error if the object doesn't have an embedded TypeMeta.
// Obj may be a pointer to a struct, or a struct. If a struct, a copy