blob: 81913a41423f4b1d60401f8879c5f9fa82941d3d [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package discovery
18
19import (
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.
28type 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.
37func 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.
49func (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.
77func (d *UnstructuredObjectTyper) Recognizes(gvk schema.GroupVersionKind) bool {
78 return false
79}
80
81var _ runtime.ObjectTyper = &UnstructuredObjectTyper{}