1 /* 2 Copyright 2016 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 crdserverscheme 18 19 import ( 20 "reflect" 21 22 "k8s.io/apimachinery/pkg/runtime" 23 "k8s.io/apimachinery/pkg/runtime/schema" 24 ) 25 26 // UnstructuredObjectTyper provides a runtime.ObjectTyper implementation for 27 // runtime.Unstructured object based on discovery information. 28 type UnstructuredObjectTyper struct { 29 } 30 31 // NewUnstructuredObjectTyper returns a runtime.ObjectTyper for 32 // unstructured objects based on discovery information. It accepts a list of fallback typers 33 // for handling objects that are not runtime.Unstructured. It does not delegate the Recognizes 34 // check, only ObjectKinds. 35 // TODO this only works for the apiextensions server and doesn't recognize any types. Move to point of use. 36 func NewUnstructuredObjectTyper() *UnstructuredObjectTyper { 37 dot := &UnstructuredObjectTyper{} 38 return dot 39 } 40 41 // ObjectKinds returns a slice of one element with the group,version,kind of the 42 // provided object, or an error if the object is not runtime.Unstructured or 43 // has no group,version,kind information. unversionedType will always be false 44 // because runtime.Unstructured object should always have group,version,kind 45 // information set. 46 func (d *UnstructuredObjectTyper) ObjectKinds(obj runtime.Object) (gvks []schema.GroupVersionKind, unversionedType bool, err error) { 47 if _, ok := obj.(runtime.Unstructured); ok { 48 gvk := obj.GetObjectKind().GroupVersionKind() 49 if len(gvk.Kind) == 0 { 50 return nil, false, runtime.NewMissingKindErr("object has no kind field ") 51 } 52 if len(gvk.Version) == 0 { 53 return nil, false, runtime.NewMissingVersionErr("object has no apiVersion field") 54 } 55 return []schema.GroupVersionKind{gvk}, false, nil 56 } 57 58 return nil, false, runtime.NewNotRegisteredErrForType("crdserverscheme.UnstructuredObjectTyper", reflect.TypeOf(obj)) 59 } 60 61 // Recognizes returns true if the provided group,version,kind was in the 62 // discovery information. 63 func (d *UnstructuredObjectTyper) Recognizes(gvk schema.GroupVersionKind) bool { 64 return false 65 } 66 67 var _ runtime.ObjectTyper = &UnstructuredObjectTyper{} 68