errors.go 3.9 KB

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