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 meta |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | |
| 22 | "k8s.io/apimachinery/pkg/runtime/schema" |
| 23 | "k8s.io/apimachinery/pkg/util/sets" |
| 24 | ) |
| 25 | |
| 26 | // AmbiguousResourceError is returned if the RESTMapper finds multiple matches for a resource |
| 27 | type AmbiguousResourceError struct { |
| 28 | PartialResource schema.GroupVersionResource |
| 29 | |
| 30 | MatchingResources []schema.GroupVersionResource |
| 31 | MatchingKinds []schema.GroupVersionKind |
| 32 | } |
| 33 | |
| 34 | func (e *AmbiguousResourceError) Error() string { |
| 35 | switch { |
| 36 | case len(e.MatchingKinds) > 0 && len(e.MatchingResources) > 0: |
| 37 | return fmt.Sprintf("%v matches multiple resources %v and kinds %v", e.PartialResource, e.MatchingResources, e.MatchingKinds) |
| 38 | case len(e.MatchingKinds) > 0: |
| 39 | return fmt.Sprintf("%v matches multiple kinds %v", e.PartialResource, e.MatchingKinds) |
| 40 | case len(e.MatchingResources) > 0: |
| 41 | return fmt.Sprintf("%v matches multiple resources %v", e.PartialResource, e.MatchingResources) |
| 42 | } |
| 43 | return fmt.Sprintf("%v matches multiple resources or kinds", e.PartialResource) |
| 44 | } |
| 45 | |
| 46 | // AmbiguousKindError is returned if the RESTMapper finds multiple matches for a kind |
| 47 | type AmbiguousKindError struct { |
| 48 | PartialKind schema.GroupVersionKind |
| 49 | |
| 50 | MatchingResources []schema.GroupVersionResource |
| 51 | MatchingKinds []schema.GroupVersionKind |
| 52 | } |
| 53 | |
| 54 | func (e *AmbiguousKindError) Error() string { |
| 55 | switch { |
| 56 | case len(e.MatchingKinds) > 0 && len(e.MatchingResources) > 0: |
| 57 | return fmt.Sprintf("%v matches multiple resources %v and kinds %v", e.PartialKind, e.MatchingResources, e.MatchingKinds) |
| 58 | case len(e.MatchingKinds) > 0: |
| 59 | return fmt.Sprintf("%v matches multiple kinds %v", e.PartialKind, e.MatchingKinds) |
| 60 | case len(e.MatchingResources) > 0: |
| 61 | return fmt.Sprintf("%v matches multiple resources %v", e.PartialKind, e.MatchingResources) |
| 62 | } |
| 63 | return fmt.Sprintf("%v matches multiple resources or kinds", e.PartialKind) |
| 64 | } |
| 65 | |
| 66 | func IsAmbiguousError(err error) bool { |
| 67 | if err == nil { |
| 68 | return false |
| 69 | } |
| 70 | switch err.(type) { |
| 71 | case *AmbiguousResourceError, *AmbiguousKindError: |
| 72 | return true |
| 73 | default: |
| 74 | return false |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // NoResourceMatchError is returned if the RESTMapper can't find any match for a resource |
| 79 | type NoResourceMatchError struct { |
| 80 | PartialResource schema.GroupVersionResource |
| 81 | } |
| 82 | |
| 83 | func (e *NoResourceMatchError) Error() string { |
| 84 | return fmt.Sprintf("no matches for %v", e.PartialResource) |
| 85 | } |
| 86 | |
| 87 | // NoKindMatchError is returned if the RESTMapper can't find any match for a kind |
| 88 | type NoKindMatchError struct { |
| 89 | // GroupKind is the API group and kind that was searched |
| 90 | GroupKind schema.GroupKind |
| 91 | // SearchedVersions is the optional list of versions the search was restricted to |
| 92 | SearchedVersions []string |
| 93 | } |
| 94 | |
| 95 | func (e *NoKindMatchError) Error() string { |
| 96 | searchedVersions := sets.NewString() |
| 97 | for _, v := range e.SearchedVersions { |
| 98 | searchedVersions.Insert(schema.GroupVersion{Group: e.GroupKind.Group, Version: v}.String()) |
| 99 | } |
| 100 | |
| 101 | switch len(searchedVersions) { |
| 102 | case 0: |
| 103 | return fmt.Sprintf("no matches for kind %q in group %q", e.GroupKind.Kind, e.GroupKind.Group) |
| 104 | case 1: |
| 105 | return fmt.Sprintf("no matches for kind %q in version %q", e.GroupKind.Kind, searchedVersions.List()[0]) |
| 106 | default: |
| 107 | return fmt.Sprintf("no matches for kind %q in versions %q", e.GroupKind.Kind, searchedVersions.List()) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func IsNoMatchError(err error) bool { |
| 112 | if err == nil { |
| 113 | return false |
| 114 | } |
| 115 | switch err.(type) { |
| 116 | case *NoResourceMatchError, *NoKindMatchError: |
| 117 | return true |
| 118 | default: |
| 119 | return false |
| 120 | } |
| 121 | } |