github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/meta/firsthit_restmapper.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     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
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    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.
    15  */
    16  
    17  package meta
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema"
    23  	utilerrors "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/util/errors"
    24  )
    25  
    26  var (
    27  	_ ResettableRESTMapper = &FirstHitRESTMapper{}
    28  )
    29  
    30  // FirstHitRESTMapper is a wrapper for multiple RESTMappers which returns the
    31  // first successful result for the singular requests
    32  type FirstHitRESTMapper struct {
    33  	MultiRESTMapper
    34  }
    35  
    36  func (m FirstHitRESTMapper) String() string {
    37  	return fmt.Sprintf("FirstHitRESTMapper{\n\t%v\n}", m.MultiRESTMapper)
    38  }
    39  
    40  func (m FirstHitRESTMapper) ResourceFor(resource schema.GroupVersionResource) (schema.GroupVersionResource, error) {
    41  	errors := []error{}
    42  	for _, t := range m.MultiRESTMapper {
    43  		ret, err := t.ResourceFor(resource)
    44  		if err == nil {
    45  			return ret, nil
    46  		}
    47  		errors = append(errors, err)
    48  	}
    49  
    50  	return schema.GroupVersionResource{}, collapseAggregateErrors(errors)
    51  }
    52  
    53  func (m FirstHitRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
    54  	errors := []error{}
    55  	for _, t := range m.MultiRESTMapper {
    56  		ret, err := t.KindFor(resource)
    57  		if err == nil {
    58  			return ret, nil
    59  		}
    60  		errors = append(errors, err)
    61  	}
    62  
    63  	return schema.GroupVersionKind{}, collapseAggregateErrors(errors)
    64  }
    65  
    66  // RESTMapping provides the REST mapping for the resource based on the
    67  // kind and version. This implementation supports multiple REST schemas and
    68  // return the first match.
    69  func (m FirstHitRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*RESTMapping, error) {
    70  	errors := []error{}
    71  	for _, t := range m.MultiRESTMapper {
    72  		ret, err := t.RESTMapping(gk, versions...)
    73  		if err == nil {
    74  			return ret, nil
    75  		}
    76  		errors = append(errors, err)
    77  	}
    78  
    79  	return nil, collapseAggregateErrors(errors)
    80  }
    81  
    82  func (m FirstHitRESTMapper) Reset() {
    83  	m.MultiRESTMapper.Reset()
    84  }
    85  
    86  // collapseAggregateErrors returns the minimal errors.  it handles empty as nil, handles one item in a list
    87  // by returning the item, and collapses all NoMatchErrors to a single one (since they should all be the same)
    88  func collapseAggregateErrors(errors []error) error {
    89  	if len(errors) == 0 {
    90  		return nil
    91  	}
    92  	if len(errors) == 1 {
    93  		return errors[0]
    94  	}
    95  
    96  	allNoMatchErrors := true
    97  	for _, err := range errors {
    98  		allNoMatchErrors = allNoMatchErrors && IsNoMatchError(err)
    99  	}
   100  	if allNoMatchErrors {
   101  		return errors[0]
   102  	}
   103  
   104  	return utilerrors.NewAggregate(errors)
   105  }