mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-10 13:42:02 +00:00
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:
@@ -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
|
||||
|
Reference in New Issue
Block a user