firsthit_restmapper.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  18. )
  19. var (
  20. _ ResettableRESTMapper = &FirstHitRESTMapper{}
  21. )
  22. // FirstHitRESTMapper is a wrapper for multiple RESTMappers which returns the
  23. // first successful result for the singular requests
  24. type FirstHitRESTMapper struct {
  25. MultiRESTMapper
  26. }
  27. func (m FirstHitRESTMapper) String() string {
  28. return fmt.Sprintf("FirstHitRESTMapper{\n\t%v\n}", m.MultiRESTMapper)
  29. }
  30. func (m FirstHitRESTMapper) ResourceFor(resource schema.GroupVersionResource) (schema.GroupVersionResource, error) {
  31. errors := []error{}
  32. for _, t := range m.MultiRESTMapper {
  33. ret, err := t.ResourceFor(resource)
  34. if err == nil {
  35. return ret, nil
  36. }
  37. errors = append(errors, err)
  38. }
  39. return schema.GroupVersionResource{}, collapseAggregateErrors(errors)
  40. }
  41. func (m FirstHitRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
  42. errors := []error{}
  43. for _, t := range m.MultiRESTMapper {
  44. ret, err := t.KindFor(resource)
  45. if err == nil {
  46. return ret, nil
  47. }
  48. errors = append(errors, err)
  49. }
  50. return schema.GroupVersionKind{}, collapseAggregateErrors(errors)
  51. }
  52. // RESTMapping provides the REST mapping for the resource based on the
  53. // kind and version. This implementation supports multiple REST schemas and
  54. // return the first match.
  55. func (m FirstHitRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*RESTMapping, error) {
  56. errors := []error{}
  57. for _, t := range m.MultiRESTMapper {
  58. ret, err := t.RESTMapping(gk, versions...)
  59. if err == nil {
  60. return ret, nil
  61. }
  62. errors = append(errors, err)
  63. }
  64. return nil, collapseAggregateErrors(errors)
  65. }
  66. func (m FirstHitRESTMapper) Reset() {
  67. m.MultiRESTMapper.Reset()
  68. }
  69. // collapseAggregateErrors returns the minimal errors. it handles empty as nil, handles one item in a list
  70. // by returning the item, and collapses all NoMatchErrors to a single one (since they should all be the same)
  71. func collapseAggregateErrors(errors []error) error {
  72. if len(errors) == 0 {
  73. return nil
  74. }
  75. if len(errors) == 1 {
  76. return errors[0]
  77. }
  78. allNoMatchErrors := true
  79. for _, err := range errors {
  80. allNoMatchErrors = allNoMatchErrors && IsNoMatchError(err)
  81. }
  82. if allNoMatchErrors {
  83. return errors[0]
  84. }
  85. return utilerrors.NewAggregate(errors)
  86. }