Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2014 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 runtime |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "reflect" |
| 22 | |
| 23 | "k8s.io/apimachinery/pkg/runtime/schema" |
| 24 | ) |
| 25 | |
| 26 | type notRegisteredErr struct { |
| 27 | schemeName string |
| 28 | gvk schema.GroupVersionKind |
| 29 | target GroupVersioner |
| 30 | t reflect.Type |
| 31 | } |
| 32 | |
| 33 | func NewNotRegisteredErrForKind(schemeName string, gvk schema.GroupVersionKind) error { |
| 34 | return ¬RegisteredErr{schemeName: schemeName, gvk: gvk} |
| 35 | } |
| 36 | |
| 37 | func NewNotRegisteredErrForType(schemeName string, t reflect.Type) error { |
| 38 | return ¬RegisteredErr{schemeName: schemeName, t: t} |
| 39 | } |
| 40 | |
| 41 | func NewNotRegisteredErrForTarget(schemeName string, t reflect.Type, target GroupVersioner) error { |
| 42 | return ¬RegisteredErr{schemeName: schemeName, t: t, target: target} |
| 43 | } |
| 44 | |
| 45 | func NewNotRegisteredGVKErrForTarget(schemeName string, gvk schema.GroupVersionKind, target GroupVersioner) error { |
| 46 | return ¬RegisteredErr{schemeName: schemeName, gvk: gvk, target: target} |
| 47 | } |
| 48 | |
| 49 | func (k *notRegisteredErr) Error() string { |
| 50 | if k.t != nil && k.target != nil { |
| 51 | return fmt.Sprintf("%v is not suitable for converting to %q in scheme %q", k.t, k.target, k.schemeName) |
| 52 | } |
| 53 | nullGVK := schema.GroupVersionKind{} |
| 54 | if k.gvk != nullGVK && k.target != nil { |
| 55 | return fmt.Sprintf("%q is not suitable for converting to %q in scheme %q", k.gvk.GroupVersion(), k.target, k.schemeName) |
| 56 | } |
| 57 | if k.t != nil { |
| 58 | return fmt.Sprintf("no kind is registered for the type %v in scheme %q", k.t, k.schemeName) |
| 59 | } |
| 60 | if len(k.gvk.Kind) == 0 { |
| 61 | return fmt.Sprintf("no version %q has been registered in scheme %q", k.gvk.GroupVersion(), k.schemeName) |
| 62 | } |
| 63 | if k.gvk.Version == APIVersionInternal { |
| 64 | return fmt.Sprintf("no kind %q is registered for the internal version of group %q in scheme %q", k.gvk.Kind, k.gvk.Group, k.schemeName) |
| 65 | } |
| 66 | |
| 67 | return fmt.Sprintf("no kind %q is registered for version %q in scheme %q", k.gvk.Kind, k.gvk.GroupVersion(), k.schemeName) |
| 68 | } |
| 69 | |
| 70 | // IsNotRegisteredError returns true if the error indicates the provided |
| 71 | // object or input data is not registered. |
| 72 | func IsNotRegisteredError(err error) bool { |
| 73 | if err == nil { |
| 74 | return false |
| 75 | } |
| 76 | _, ok := err.(*notRegisteredErr) |
| 77 | return ok |
| 78 | } |
| 79 | |
| 80 | type missingKindErr struct { |
| 81 | data string |
| 82 | } |
| 83 | |
| 84 | func NewMissingKindErr(data string) error { |
| 85 | return &missingKindErr{data} |
| 86 | } |
| 87 | |
| 88 | func (k *missingKindErr) Error() string { |
| 89 | return fmt.Sprintf("Object 'Kind' is missing in '%s'", k.data) |
| 90 | } |
| 91 | |
| 92 | // IsMissingKind returns true if the error indicates that the provided object |
| 93 | // is missing a 'Kind' field. |
| 94 | func IsMissingKind(err error) bool { |
| 95 | if err == nil { |
| 96 | return false |
| 97 | } |
| 98 | _, ok := err.(*missingKindErr) |
| 99 | return ok |
| 100 | } |
| 101 | |
| 102 | type missingVersionErr struct { |
| 103 | data string |
| 104 | } |
| 105 | |
| 106 | func NewMissingVersionErr(data string) error { |
| 107 | return &missingVersionErr{data} |
| 108 | } |
| 109 | |
| 110 | func (k *missingVersionErr) Error() string { |
| 111 | return fmt.Sprintf("Object 'apiVersion' is missing in '%s'", k.data) |
| 112 | } |
| 113 | |
| 114 | // IsMissingVersion returns true if the error indicates that the provided object |
| 115 | // is missing a 'Version' field. |
| 116 | func IsMissingVersion(err error) bool { |
| 117 | if err == nil { |
| 118 | return false |
| 119 | } |
| 120 | _, ok := err.(*missingVersionErr) |
| 121 | return ok |
| 122 | } |