blob: be0c5edc8554e1a564cfdf44ec41f0a0eceb18f2 [file] [log] [blame]
Scott Bakere7144bc2019-10-01 14:16:47 -07001/*
2Copyright 2014 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 runtime
18
19import (
20 "fmt"
21 "reflect"
22
23 "k8s.io/apimachinery/pkg/runtime/schema"
24)
25
26type notRegisteredErr struct {
27 schemeName string
28 gvk schema.GroupVersionKind
29 target GroupVersioner
30 t reflect.Type
31}
32
33func NewNotRegisteredErrForKind(schemeName string, gvk schema.GroupVersionKind) error {
34 return &notRegisteredErr{schemeName: schemeName, gvk: gvk}
35}
36
37func NewNotRegisteredErrForType(schemeName string, t reflect.Type) error {
38 return &notRegisteredErr{schemeName: schemeName, t: t}
39}
40
41func NewNotRegisteredErrForTarget(schemeName string, t reflect.Type, target GroupVersioner) error {
42 return &notRegisteredErr{schemeName: schemeName, t: t, target: target}
43}
44
45func NewNotRegisteredGVKErrForTarget(schemeName string, gvk schema.GroupVersionKind, target GroupVersioner) error {
46 return &notRegisteredErr{schemeName: schemeName, gvk: gvk, target: target}
47}
48
49func (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.
72func IsNotRegisteredError(err error) bool {
73 if err == nil {
74 return false
75 }
76 _, ok := err.(*notRegisteredErr)
77 return ok
78}
79
80type missingKindErr struct {
81 data string
82}
83
84func NewMissingKindErr(data string) error {
85 return &missingKindErr{data}
86}
87
88func (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.
94func IsMissingKind(err error) bool {
95 if err == nil {
96 return false
97 }
98 _, ok := err.(*missingKindErr)
99 return ok
100}
101
102type missingVersionErr struct {
103 data string
104}
105
106func NewMissingVersionErr(data string) error {
107 return &missingVersionErr{data}
108}
109
110func (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.
116func IsMissingVersion(err error) bool {
117 if err == nil {
118 return false
119 }
120 _, ok := err.(*missingVersionErr)
121 return ok
122}
girishke7ca43b2019-10-10 12:30:03 +0000123
124// strictDecodingError is a base error type that is returned by a strict Decoder such
125// as UniversalStrictDecoder.
126type strictDecodingError struct {
127 message string
128 data string
129}
130
131// NewStrictDecodingError creates a new strictDecodingError object.
132func NewStrictDecodingError(message string, data string) error {
133 return &strictDecodingError{
134 message: message,
135 data: data,
136 }
137}
138
139func (e *strictDecodingError) Error() string {
140 return fmt.Sprintf("strict decoder error for %s: %s", e.data, e.message)
141}
142
143// IsStrictDecodingError returns true if the error indicates that the provided object
144// strictness violations.
145func IsStrictDecodingError(err error) bool {
146 if err == nil {
147 return false
148 }
149 _, ok := err.(*strictDecodingError)
150 return ok
151}