blob: fd221002290629fcaf31d67d31e90ed2509e9fd3 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package meta
18
19import (
20 "fmt"
21
22 "k8s.io/apimachinery/pkg/runtime/schema"
23 utilerrors "k8s.io/apimachinery/pkg/util/errors"
24)
25
26// FirstHitRESTMapper is a wrapper for multiple RESTMappers which returns the
27// first successful result for the singular requests
28type FirstHitRESTMapper struct {
29 MultiRESTMapper
30}
31
32func (m FirstHitRESTMapper) String() string {
33 return fmt.Sprintf("FirstHitRESTMapper{\n\t%v\n}", m.MultiRESTMapper)
34}
35
36func (m FirstHitRESTMapper) ResourceFor(resource schema.GroupVersionResource) (schema.GroupVersionResource, error) {
37 errors := []error{}
38 for _, t := range m.MultiRESTMapper {
39 ret, err := t.ResourceFor(resource)
40 if err == nil {
41 return ret, nil
42 }
43 errors = append(errors, err)
44 }
45
46 return schema.GroupVersionResource{}, collapseAggregateErrors(errors)
47}
48
49func (m FirstHitRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
50 errors := []error{}
51 for _, t := range m.MultiRESTMapper {
52 ret, err := t.KindFor(resource)
53 if err == nil {
54 return ret, nil
55 }
56 errors = append(errors, err)
57 }
58
59 return schema.GroupVersionKind{}, collapseAggregateErrors(errors)
60}
61
62// RESTMapping provides the REST mapping for the resource based on the
63// kind and version. This implementation supports multiple REST schemas and
64// return the first match.
65func (m FirstHitRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*RESTMapping, error) {
66 errors := []error{}
67 for _, t := range m.MultiRESTMapper {
68 ret, err := t.RESTMapping(gk, versions...)
69 if err == nil {
70 return ret, nil
71 }
72 errors = append(errors, err)
73 }
74
75 return nil, collapseAggregateErrors(errors)
76}
77
78// collapseAggregateErrors returns the minimal errors. it handles empty as nil, handles one item in a list
79// by returning the item, and collapses all NoMatchErrors to a single one (since they should all be the same)
80func collapseAggregateErrors(errors []error) error {
81 if len(errors) == 0 {
82 return nil
83 }
84 if len(errors) == 1 {
85 return errors[0]
86 }
87
88 allNoMatchErrors := true
89 for _, err := range errors {
90 allNoMatchErrors = allNoMatchErrors && IsNoMatchError(err)
91 }
92 if allNoMatchErrors {
93 return errors[0]
94 }
95
96 return utilerrors.NewAggregate(errors)
97}