github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/runtime/serializer/yaml/meta.go (about) 1 /* 2 Copyright 2019 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package yaml 18 19 import ( 20 "fmt" 21 22 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime" 23 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema" 24 "sigs.k8s.io/yaml" 25 ) 26 27 // DefaultMetaFactory is a default factory for versioning objects in JSON or 28 // YAML. The object in memory and in the default serialization will use the 29 // "kind" and "apiVersion" fields. 30 var DefaultMetaFactory = SimpleMetaFactory{} 31 32 // SimpleMetaFactory provides default methods for retrieving the type and version of objects 33 // that are identified with an "apiVersion" and "kind" fields in their JSON 34 // serialization. It may be parameterized with the names of the fields in memory, or an 35 // optional list of base structs to search for those fields in memory. 36 type SimpleMetaFactory struct{} 37 38 // Interpret will return the APIVersion and Kind of the JSON wire-format 39 // encoding of an object, or an error. 40 func (SimpleMetaFactory) Interpret(data []byte) (*schema.GroupVersionKind, error) { 41 gvk := runtime.TypeMeta{} 42 if err := yaml.Unmarshal(data, &gvk); err != nil { 43 return nil, fmt.Errorf("could not interpret GroupVersionKind; unmarshal error: %v", err) 44 } 45 gv, err := schema.ParseGroupVersion(gvk.APIVersion) 46 if err != nil { 47 return nil, err 48 } 49 return &schema.GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: gvk.Kind}, nil 50 }