firsthit_restmapper.go 2.7 KB

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