2 Copyright 2014 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
22 "k8s.io/apimachinery/pkg/runtime/schema"
23 utilerrors "k8s.io/apimachinery/pkg/util/errors"
26 // FirstHitRESTMapper is a wrapper for multiple RESTMappers which returns the
27 // first successful result for the singular requests
28 type FirstHitRESTMapper struct {
32 func (m FirstHitRESTMapper) String() string {
33 return fmt.Sprintf("FirstHitRESTMapper{\n\t%v\n}", m.MultiRESTMapper)
36 func (m FirstHitRESTMapper) ResourceFor(resource schema.GroupVersionResource) (schema.GroupVersionResource, error) {
38 for _, t := range m.MultiRESTMapper {
39 ret, err := t.ResourceFor(resource)
43 errors = append(errors, err)
46 return schema.GroupVersionResource{}, collapseAggregateErrors(errors)
49 func (m FirstHitRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
51 for _, t := range m.MultiRESTMapper {
52 ret, err := t.KindFor(resource)
56 errors = append(errors, err)
59 return schema.GroupVersionKind{}, collapseAggregateErrors(errors)
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.
65 func (m FirstHitRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*RESTMapping, error) {
67 for _, t := range m.MultiRESTMapper {
68 ret, err := t.RESTMapping(gk, versions...)
72 errors = append(errors, err)
75 return nil, collapseAggregateErrors(errors)
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)
80 func collapseAggregateErrors(errors []error) error {
88 allNoMatchErrors := true
89 for _, err := range errors {
90 allNoMatchErrors = allNoMatchErrors && IsNoMatchError(err)
96 return utilerrors.NewAggregate(errors)