errors.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package meta
  14. import (
  15. "errors"
  16. "fmt"
  17. "k8s.io/apimachinery/pkg/runtime/schema"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. )
  20. // AmbiguousResourceError is returned if the RESTMapper finds multiple matches for a resource
  21. type AmbiguousResourceError struct {
  22. PartialResource schema.GroupVersionResource
  23. MatchingResources []schema.GroupVersionResource
  24. MatchingKinds []schema.GroupVersionKind
  25. }
  26. func (e *AmbiguousResourceError) Error() string {
  27. switch {
  28. case len(e.MatchingKinds) > 0 && len(e.MatchingResources) > 0:
  29. return fmt.Sprintf("%v matches multiple resources %v and kinds %v", e.PartialResource, e.MatchingResources, e.MatchingKinds)
  30. case len(e.MatchingKinds) > 0:
  31. return fmt.Sprintf("%v matches multiple kinds %v", e.PartialResource, e.MatchingKinds)
  32. case len(e.MatchingResources) > 0:
  33. return fmt.Sprintf("%v matches multiple resources %v", e.PartialResource, e.MatchingResources)
  34. }
  35. return fmt.Sprintf("%v matches multiple resources or kinds", e.PartialResource)
  36. }
  37. func (*AmbiguousResourceError) Is(target error) bool {
  38. _, ok := target.(*AmbiguousResourceError)
  39. return ok
  40. }
  41. // AmbiguousKindError is returned if the RESTMapper finds multiple matches for a kind
  42. type AmbiguousKindError struct {
  43. PartialKind schema.GroupVersionKind
  44. MatchingResources []schema.GroupVersionResource
  45. MatchingKinds []schema.GroupVersionKind
  46. }
  47. func (e *AmbiguousKindError) Error() string {
  48. switch {
  49. case len(e.MatchingKinds) > 0 && len(e.MatchingResources) > 0:
  50. return fmt.Sprintf("%v matches multiple resources %v and kinds %v", e.PartialKind, e.MatchingResources, e.MatchingKinds)
  51. case len(e.MatchingKinds) > 0:
  52. return fmt.Sprintf("%v matches multiple kinds %v", e.PartialKind, e.MatchingKinds)
  53. case len(e.MatchingResources) > 0:
  54. return fmt.Sprintf("%v matches multiple resources %v", e.PartialKind, e.MatchingResources)
  55. }
  56. return fmt.Sprintf("%v matches multiple resources or kinds", e.PartialKind)
  57. }
  58. func (*AmbiguousKindError) Is(target error) bool {
  59. _, ok := target.(*AmbiguousKindError)
  60. return ok
  61. }
  62. func IsAmbiguousError(err error) bool {
  63. if err == nil {
  64. return false
  65. }
  66. return errors.Is(err, &AmbiguousResourceError{}) || errors.Is(err, &AmbiguousKindError{})
  67. }
  68. // NoResourceMatchError is returned if the RESTMapper can't find any match for a resource
  69. type NoResourceMatchError struct {
  70. PartialResource schema.GroupVersionResource
  71. }
  72. func (e *NoResourceMatchError) Error() string {
  73. return fmt.Sprintf("no matches for %v", e.PartialResource)
  74. }
  75. func (*NoResourceMatchError) Is(target error) bool {
  76. _, ok := target.(*NoResourceMatchError)
  77. return ok
  78. }
  79. // NoKindMatchError is returned if the RESTMapper can't find any match for a kind
  80. type NoKindMatchError struct {
  81. // GroupKind is the API group and kind that was searched
  82. GroupKind schema.GroupKind
  83. // SearchedVersions is the optional list of versions the search was restricted to
  84. SearchedVersions []string
  85. }
  86. func (e *NoKindMatchError) Error() string {
  87. searchedVersions := sets.NewString()
  88. for _, v := range e.SearchedVersions {
  89. searchedVersions.Insert(schema.GroupVersion{Group: e.GroupKind.Group, Version: v}.String())
  90. }
  91. switch len(searchedVersions) {
  92. case 0:
  93. return fmt.Sprintf("no matches for kind %q in group %q", e.GroupKind.Kind, e.GroupKind.Group)
  94. case 1:
  95. return fmt.Sprintf("no matches for kind %q in version %q", e.GroupKind.Kind, searchedVersions.List()[0])
  96. default:
  97. return fmt.Sprintf("no matches for kind %q in versions %q", e.GroupKind.Kind, searchedVersions.List())
  98. }
  99. }
  100. func (*NoKindMatchError) Is(target error) bool {
  101. _, ok := target.(*NoKindMatchError)
  102. return ok
  103. }
  104. func IsNoMatchError(err error) bool {
  105. if err == nil {
  106. return false
  107. }
  108. return errors.Is(err, &NoResourceMatchError{}) || errors.Is(err, &NoKindMatchError{})
  109. }