1/* 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 meta 18 19import ( 20 "fmt" 21 22 "k8s.io/apimachinery/pkg/runtime/schema" 23) 24 25// AmbiguousResourceError is returned if the RESTMapper finds multiple matches for a resource 26type AmbiguousResourceError struct { 27 PartialResource schema.GroupVersionResource 28 29 MatchingResources []schema.GroupVersionResource 30 MatchingKinds []schema.GroupVersionKind 31} 32 33func (e *AmbiguousResourceError) Error() string { 34 switch { 35 case len(e.MatchingKinds) > 0 && len(e.MatchingResources) > 0: 36 return fmt.Sprintf("%v matches multiple resources %v and kinds %v", e.PartialResource, e.MatchingResources, e.MatchingKinds) 37 case len(e.MatchingKinds) > 0: 38 return fmt.Sprintf("%v matches multiple kinds %v", e.PartialResource, e.MatchingKinds) 39 case len(e.MatchingResources) > 0: 40 return fmt.Sprintf("%v matches multiple resources %v", e.PartialResource, e.MatchingResources) 41 } 42 return fmt.Sprintf("%v matches multiple resources or kinds", e.PartialResource) 43} 44 45// AmbiguousKindError is returned if the RESTMapper finds multiple matches for a kind 46type AmbiguousKindError struct { 47 PartialKind schema.GroupVersionKind 48 49 MatchingResources []schema.GroupVersionResource 50 MatchingKinds []schema.GroupVersionKind 51} 52 53func (e *AmbiguousKindError) Error() string { 54 switch { 55 case len(e.MatchingKinds) > 0 && len(e.MatchingResources) > 0: 56 return fmt.Sprintf("%v matches multiple resources %v and kinds %v", e.PartialKind, e.MatchingResources, e.MatchingKinds) 57 case len(e.MatchingKinds) > 0: 58 return fmt.Sprintf("%v matches multiple kinds %v", e.PartialKind, e.MatchingKinds) 59 case len(e.MatchingResources) > 0: 60 return fmt.Sprintf("%v matches multiple resources %v", e.PartialKind, e.MatchingResources) 61 } 62 return fmt.Sprintf("%v matches multiple resources or kinds", e.PartialKind) 63} 64 65func IsAmbiguousError(err error) bool { 66 if err == nil { 67 return false 68 } 69 switch err.(type) { 70 case *AmbiguousResourceError, *AmbiguousKindError: 71 return true 72 default: 73 return false 74 } 75} 76 77// NoResourceMatchError is returned if the RESTMapper can't find any match for a resource 78type NoResourceMatchError struct { 79 PartialResource schema.GroupVersionResource 80} 81 82func (e *NoResourceMatchError) Error() string { 83 return fmt.Sprintf("no matches for %v", e.PartialResource) 84} 85 86// NoKindMatchError is returned if the RESTMapper can't find any match for a kind 87type NoKindMatchError struct { 88 PartialKind schema.GroupVersionKind 89} 90 91func (e *NoKindMatchError) Error() string { 92 return fmt.Sprintf("no matches for %v", e.PartialKind) 93} 94 95func IsNoMatchError(err error) bool { 96 if err == nil { 97 return false 98 } 99 switch err.(type) { 100 case *NoResourceMatchError, *NoKindMatchError: 101 return true 102 default: 103 return false 104 } 105} 106