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