2014-07-31 19:02:49 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-07-31 19:02:49 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package conversion
|
|
|
|
|
|
|
|
import (
|
2014-10-16 20:38:18 +00:00
|
|
|
"errors"
|
2014-07-31 19:02:49 +00:00
|
|
|
"fmt"
|
2015-11-10 09:20:57 +00:00
|
|
|
"net/url"
|
2015-10-20 21:33:43 +00:00
|
|
|
|
2015-10-05 13:08:39 +00:00
|
|
|
"github.com/ugorji/go/codec"
|
2015-11-17 15:07:45 +00:00
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2014-07-31 19:02:49 +00:00
|
|
|
)
|
|
|
|
|
2015-12-08 19:40:23 +00:00
|
|
|
func (s *Scheme) DecodeToVersionedObject(data []byte) (interface{}, unversioned.GroupVersionKind, error) {
|
|
|
|
kind, err := s.DataKind(data)
|
2014-07-31 19:02:49 +00:00
|
|
|
if err != nil {
|
2015-12-08 19:40:23 +00:00
|
|
|
return nil, unversioned.GroupVersionKind{}, err
|
2015-11-17 18:21:32 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 19:40:23 +00:00
|
|
|
internalGV, exists := s.InternalVersions[kind.Group]
|
2015-11-17 18:21:32 +00:00
|
|
|
if !exists {
|
2015-12-08 19:40:23 +00:00
|
|
|
return nil, unversioned.GroupVersionKind{}, fmt.Errorf("no internalVersion specified for %v", kind)
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
2015-11-17 18:21:32 +00:00
|
|
|
|
2015-12-08 19:40:23 +00:00
|
|
|
if len(kind.Group) == 0 && len(internalGV.Group) != 0 {
|
|
|
|
return nil, unversioned.GroupVersionKind{}, fmt.Errorf("group not set in '%s'", string(data))
|
2015-11-20 12:38:32 +00:00
|
|
|
}
|
2015-12-08 19:40:23 +00:00
|
|
|
if len(kind.Version) == 0 && len(internalGV.Version) != 0 {
|
|
|
|
return nil, unversioned.GroupVersionKind{}, fmt.Errorf("version not set in '%s'", string(data))
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
2015-12-08 19:40:23 +00:00
|
|
|
if kind.Kind == "" {
|
|
|
|
return nil, unversioned.GroupVersionKind{}, fmt.Errorf("kind not set in '%s'", string(data))
|
2014-11-19 01:19:58 +00:00
|
|
|
}
|
2015-11-20 12:38:32 +00:00
|
|
|
|
2015-12-08 19:40:23 +00:00
|
|
|
obj, err := s.NewObject(kind)
|
2014-07-31 19:02:49 +00:00
|
|
|
if err != nil {
|
2015-12-08 19:40:23 +00:00
|
|
|
return nil, unversioned.GroupVersionKind{}, err
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
2014-10-23 20:55:50 +00:00
|
|
|
|
2015-10-05 13:08:39 +00:00
|
|
|
if err := codec.NewDecoderBytes(data, new(codec.JsonHandle)).Decode(obj); err != nil {
|
2015-12-08 19:40:23 +00:00
|
|
|
return nil, unversioned.GroupVersionKind{}, err
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
2015-12-08 19:40:23 +00:00
|
|
|
return obj, kind, nil
|
2015-06-23 00:52:55 +00:00
|
|
|
}
|
2014-07-31 19:02:49 +00:00
|
|
|
|
2015-06-23 00:52:55 +00:00
|
|
|
// Decode converts a JSON string back into a pointer to an api object.
|
|
|
|
// Deduces the type based upon the fields added by the MetaInsertionFactory
|
|
|
|
// technique. The object will be converted, if necessary, into the
|
|
|
|
// s.InternalVersion type before being returned. Decode will not decode
|
|
|
|
// objects without version set unless InternalVersion is also "".
|
|
|
|
func (s *Scheme) Decode(data []byte) (interface{}, error) {
|
2015-11-17 18:21:32 +00:00
|
|
|
return s.DecodeToVersion(data, unversioned.GroupVersion{})
|
2015-08-07 19:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeToVersion converts a JSON string back into a pointer to an api object.
|
|
|
|
// Deduces the type based upon the fields added by the MetaInsertionFactory
|
|
|
|
// technique. The object will be converted, if necessary, into the versioned
|
|
|
|
// type before being returned. Decode will not decode objects without version
|
|
|
|
// set unless version is also "".
|
2015-11-17 18:21:32 +00:00
|
|
|
// a GroupVersion with .IsEmpty() == true is means "use the internal version for
|
|
|
|
// the object's group"
|
2015-12-08 19:40:23 +00:00
|
|
|
func (s *Scheme) DecodeToVersion(data []byte, targetVersion unversioned.GroupVersion) (interface{}, error) {
|
|
|
|
obj, sourceKind, err := s.DecodeToVersionedObject(data)
|
2015-06-23 00:52:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-07-31 19:02:49 +00:00
|
|
|
// Version and Kind should be blank in memory.
|
2015-03-17 03:43:59 +00:00
|
|
|
if err := s.SetVersionAndKind("", "", obj); err != nil {
|
2014-07-31 19:02:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-08 19:40:23 +00:00
|
|
|
// if the targetVersion is empty, then we want the internal version, but the internal version varies by
|
2015-11-17 18:21:32 +00:00
|
|
|
// group. We can lookup the group now because we have knowledge of the group
|
2015-12-08 19:40:23 +00:00
|
|
|
if targetVersion.IsEmpty() {
|
2015-11-17 18:21:32 +00:00
|
|
|
exists := false
|
2015-12-08 19:40:23 +00:00
|
|
|
targetVersion, exists = s.InternalVersions[sourceKind.Group]
|
2015-11-17 18:21:32 +00:00
|
|
|
if !exists {
|
2015-12-08 19:40:23 +00:00
|
|
|
return nil, fmt.Errorf("no internalVersion specified for %v", targetVersion)
|
2015-11-17 18:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 19:02:49 +00:00
|
|
|
// Convert if needed.
|
2015-12-08 19:40:23 +00:00
|
|
|
if targetVersion != sourceKind.GroupVersion() {
|
|
|
|
objOut, err := s.NewObject(targetVersion.WithKind(sourceKind.Kind))
|
2014-07-31 19:02:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-08 19:40:23 +00:00
|
|
|
flags, meta := s.generateConvertMeta(sourceKind.GroupVersion(), targetVersion, obj)
|
2015-03-22 00:43:52 +00:00
|
|
|
if err := s.converter.Convert(obj, objOut, flags, meta); err != nil {
|
2014-07-31 19:02:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
obj = objOut
|
|
|
|
}
|
|
|
|
return obj, nil
|
|
|
|
}
|
|
|
|
|
2015-03-17 03:43:59 +00:00
|
|
|
// DecodeInto parses a JSON string and stores it in obj. Returns an error
|
2014-07-31 19:02:49 +00:00
|
|
|
// if data.Kind is set and doesn't match the type of obj. Obj should be a
|
|
|
|
// pointer to an api type.
|
2014-08-01 21:24:12 +00:00
|
|
|
// If obj's version doesn't match that in data, an attempt will be made to convert
|
2014-07-31 19:02:49 +00:00
|
|
|
// data into obj's version.
|
|
|
|
func (s *Scheme) DecodeInto(data []byte, obj interface{}) error {
|
2015-11-17 15:07:45 +00:00
|
|
|
return s.DecodeIntoWithSpecifiedVersionKind(data, obj, unversioned.GroupVersionKind{})
|
2015-06-20 00:16:25 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 15:34:16 +00:00
|
|
|
// DecodeIntoWithSpecifiedVersionKind compares the passed in requestGroupVersionKind
|
|
|
|
// with data.Version and data.Kind, defaulting data.Version and
|
2015-06-20 00:16:25 +00:00
|
|
|
// data.Kind to the specified value if they are empty, or generating an error if
|
|
|
|
// data.Version and data.Kind are not empty and differ from the specified value.
|
|
|
|
// The function then implements the functionality of DecodeInto.
|
|
|
|
// If specifiedVersion and specifiedKind are empty, the function degenerates to
|
|
|
|
// DecodeInto.
|
2015-11-18 15:34:16 +00:00
|
|
|
func (s *Scheme) DecodeIntoWithSpecifiedVersionKind(data []byte, obj interface{}, requestedGVK unversioned.GroupVersionKind) error {
|
2014-10-16 20:38:18 +00:00
|
|
|
if len(data) == 0 {
|
|
|
|
return errors.New("empty input")
|
|
|
|
}
|
2015-12-08 19:40:23 +00:00
|
|
|
dataKind, err := s.DataKind(data)
|
2014-07-31 19:02:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-08 19:40:23 +00:00
|
|
|
if len(dataKind.Group) == 0 {
|
|
|
|
dataKind.Group = requestedGVK.Group
|
2015-11-20 12:38:32 +00:00
|
|
|
}
|
2015-12-08 19:40:23 +00:00
|
|
|
if len(dataKind.Version) == 0 {
|
|
|
|
dataKind.Version = requestedGVK.Version
|
2015-06-20 00:16:25 +00:00
|
|
|
}
|
2015-12-08 19:40:23 +00:00
|
|
|
if len(dataKind.Kind) == 0 {
|
|
|
|
dataKind.Kind = requestedGVK.Kind
|
2015-06-20 00:16:25 +00:00
|
|
|
}
|
2015-11-20 12:38:32 +00:00
|
|
|
|
2015-12-08 19:40:23 +00:00
|
|
|
if len(requestedGVK.Group) > 0 && requestedGVK.Group != dataKind.Group {
|
|
|
|
return errors.New(fmt.Sprintf("The fully qualified kind in the data (%v) does not match the specified apiVersion(%v)", dataKind, requestedGVK))
|
2015-06-20 00:16:25 +00:00
|
|
|
}
|
2015-12-08 19:40:23 +00:00
|
|
|
if len(requestedGVK.Version) > 0 && requestedGVK.Version != dataKind.Version {
|
|
|
|
return errors.New(fmt.Sprintf("The fully qualified kind in the data (%v) does not match the specified apiVersion(%v)", dataKind, requestedGVK))
|
2015-11-20 12:38:32 +00:00
|
|
|
}
|
2015-12-08 19:40:23 +00:00
|
|
|
if len(requestedGVK.Kind) > 0 && requestedGVK.Kind != dataKind.Kind {
|
|
|
|
return errors.New(fmt.Sprintf("The fully qualified kind in the data (%v) does not match the specified apiVersion(%v)", dataKind, requestedGVK))
|
2015-06-20 00:16:25 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 12:38:32 +00:00
|
|
|
objGVK, err := s.ObjectKind(obj)
|
2014-07-31 19:02:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-20 12:38:32 +00:00
|
|
|
// Assume objects with unset fields are being unmarshalled into the
|
|
|
|
// correct type.
|
2015-12-08 19:40:23 +00:00
|
|
|
if len(dataKind.Group) == 0 {
|
|
|
|
dataKind.Group = objGVK.Group
|
2015-11-20 12:38:32 +00:00
|
|
|
}
|
2015-12-08 19:40:23 +00:00
|
|
|
if len(dataKind.Version) == 0 {
|
|
|
|
dataKind.Version = objGVK.Version
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
2015-12-08 19:40:23 +00:00
|
|
|
if len(dataKind.Kind) == 0 {
|
|
|
|
dataKind.Kind = objGVK.Kind
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 19:40:23 +00:00
|
|
|
external, err := s.NewObject(dataKind)
|
2015-01-26 17:52:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-05 13:08:39 +00:00
|
|
|
if err := codec.NewDecoderBytes(data, new(codec.JsonHandle)).Decode(external); err != nil {
|
2015-01-26 17:52:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-12-08 19:40:23 +00:00
|
|
|
flags, meta := s.generateConvertMeta(dataKind.GroupVersion(), objGVK.GroupVersion(), external)
|
2015-03-22 00:43:52 +00:00
|
|
|
if err := s.converter.Convert(external, obj, flags, meta); err != nil {
|
2015-01-26 17:52:50 +00:00
|
|
|
return err
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Version and Kind should be blank in memory.
|
2014-08-01 21:24:12 +00:00
|
|
|
return s.SetVersionAndKind("", "", obj)
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
2015-11-10 09:20:57 +00:00
|
|
|
|
|
|
|
func (s *Scheme) DecodeParametersInto(parameters url.Values, obj interface{}) error {
|
|
|
|
if err := s.Convert(¶meters, obj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// TODO: Should we do any convertion here?
|
|
|
|
return nil
|
|
|
|
}
|