Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 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 discovery |
| 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 | typers []runtime.ObjectTyper |
| 30 | } |
| 31 | |
| 32 | // NewUnstructuredObjectTyper returns a runtime.ObjectTyper for |
| 33 | // unstructured objects based on discovery information. It accepts a list of fallback typers |
| 34 | // for handling objects that are not runtime.Unstructured. It does not delegate the Recognizes |
| 35 | // check, only ObjectKinds. |
| 36 | // TODO this only works for the apiextensions server and doesn't recognize any types. Move to point of use. |
| 37 | func NewUnstructuredObjectTyper(typers ...runtime.ObjectTyper) *UnstructuredObjectTyper { |
| 38 | dot := &UnstructuredObjectTyper{ |
| 39 | typers: typers, |
| 40 | } |
| 41 | return dot |
| 42 | } |
| 43 | |
| 44 | // ObjectKinds returns a slice of one element with the group,version,kind of the |
| 45 | // provided object, or an error if the object is not runtime.Unstructured or |
| 46 | // has no group,version,kind information. unversionedType will always be false |
| 47 | // because runtime.Unstructured object should always have group,version,kind |
| 48 | // information set. |
| 49 | func (d *UnstructuredObjectTyper) ObjectKinds(obj runtime.Object) (gvks []schema.GroupVersionKind, unversionedType bool, err error) { |
| 50 | if _, ok := obj.(runtime.Unstructured); ok { |
| 51 | gvk := obj.GetObjectKind().GroupVersionKind() |
| 52 | if len(gvk.Kind) == 0 { |
| 53 | return nil, false, runtime.NewMissingKindErr("object has no kind field ") |
| 54 | } |
| 55 | if len(gvk.Version) == 0 { |
| 56 | return nil, false, runtime.NewMissingVersionErr("object has no apiVersion field") |
| 57 | } |
| 58 | return []schema.GroupVersionKind{gvk}, false, nil |
| 59 | } |
| 60 | var lastErr error |
| 61 | for _, typer := range d.typers { |
| 62 | gvks, unversioned, err := typer.ObjectKinds(obj) |
| 63 | if err != nil { |
| 64 | lastErr = err |
| 65 | continue |
| 66 | } |
| 67 | return gvks, unversioned, nil |
| 68 | } |
| 69 | if lastErr == nil { |
| 70 | lastErr = runtime.NewNotRegisteredErrForType(reflect.TypeOf(obj)) |
| 71 | } |
| 72 | return nil, false, lastErr |
| 73 | } |
| 74 | |
| 75 | // Recognizes returns true if the provided group,version,kind was in the |
| 76 | // discovery information. |
| 77 | func (d *UnstructuredObjectTyper) Recognizes(gvk schema.GroupVersionKind) bool { |
| 78 | return false |
| 79 | } |
| 80 | |
| 81 | var _ runtime.ObjectTyper = &UnstructuredObjectTyper{} |