ingress-nginx-helm/vendor/k8s.io/kubernetes/pkg/api/meta/multirestmapper.go

201 lines
5.4 KiB
Go
Raw Normal View History

2016-02-08 17:26:38 +00:00
/*
2016-07-12 03:42:47 +00:00
Copyright 2014 The Kubernetes Authors.
2016-02-08 17:26:38 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package meta
import (
"fmt"
"strings"
"k8s.io/kubernetes/pkg/api/unversioned"
2016-03-19 23:00:11 +00:00
utilerrors "k8s.io/kubernetes/pkg/util/errors"
2016-04-17 20:19:22 +00:00
"k8s.io/kubernetes/pkg/util/sets"
2016-02-08 17:26:38 +00:00
)
// MultiRESTMapper is a wrapper for multiple RESTMappers.
type MultiRESTMapper []RESTMapper
func (m MultiRESTMapper) String() string {
nested := []string{}
for _, t := range m {
currString := fmt.Sprintf("%v", t)
splitStrings := strings.Split(currString, "\n")
nested = append(nested, strings.Join(splitStrings, "\n\t"))
}
return fmt.Sprintf("MultiRESTMapper{\n\t%s\n}", strings.Join(nested, "\n\t"))
}
// ResourceSingularizer converts a REST resource name from plural to singular (e.g., from pods to pod)
// This implementation supports multiple REST schemas and return the first match.
func (m MultiRESTMapper) ResourceSingularizer(resource string) (singular string, err error) {
for _, t := range m {
singular, err = t.ResourceSingularizer(resource)
if err == nil {
return
}
}
return
}
func (m MultiRESTMapper) ResourcesFor(resource unversioned.GroupVersionResource) ([]unversioned.GroupVersionResource, error) {
2016-03-19 23:00:11 +00:00
allGVRs := []unversioned.GroupVersionResource{}
2016-02-08 17:26:38 +00:00
for _, t := range m {
gvrs, err := t.ResourcesFor(resource)
// ignore "no match" errors, but any other error percolates back up
2016-03-19 23:00:11 +00:00
if IsNoResourceMatchError(err) {
continue
2016-02-08 17:26:38 +00:00
}
2016-03-19 23:00:11 +00:00
if err != nil {
return nil, err
}
// walk the existing values to de-dup
for _, curr := range gvrs {
found := false
for _, existing := range allGVRs {
if curr == existing {
found = true
break
}
}
if !found {
allGVRs = append(allGVRs, curr)
}
}
}
if len(allGVRs) == 0 {
return nil, &NoResourceMatchError{PartialResource: resource}
2016-02-08 17:26:38 +00:00
}
2016-03-19 23:00:11 +00:00
return allGVRs, nil
2016-02-08 17:26:38 +00:00
}
func (m MultiRESTMapper) KindsFor(resource unversioned.GroupVersionResource) (gvk []unversioned.GroupVersionKind, err error) {
2016-03-19 23:00:11 +00:00
allGVKs := []unversioned.GroupVersionKind{}
2016-02-08 17:26:38 +00:00
for _, t := range m {
gvks, err := t.KindsFor(resource)
// ignore "no match" errors, but any other error percolates back up
2016-03-19 23:00:11 +00:00
if IsNoResourceMatchError(err) {
continue
}
if err != nil {
return nil, err
}
// walk the existing values to de-dup
for _, curr := range gvks {
found := false
for _, existing := range allGVKs {
if curr == existing {
found = true
break
}
}
if !found {
allGVKs = append(allGVKs, curr)
}
2016-02-08 17:26:38 +00:00
}
}
2016-03-19 23:00:11 +00:00
if len(allGVKs) == 0 {
return nil, &NoResourceMatchError{PartialResource: resource}
}
return allGVKs, nil
2016-02-08 17:26:38 +00:00
}
func (m MultiRESTMapper) ResourceFor(resource unversioned.GroupVersionResource) (unversioned.GroupVersionResource, error) {
2016-03-19 23:00:11 +00:00
resources, err := m.ResourcesFor(resource)
if err != nil {
return unversioned.GroupVersionResource{}, err
}
if len(resources) == 1 {
return resources[0], nil
2016-02-08 17:26:38 +00:00
}
2016-03-19 23:00:11 +00:00
return unversioned.GroupVersionResource{}, &AmbiguousResourceError{PartialResource: resource, MatchingResources: resources}
2016-02-08 17:26:38 +00:00
}
func (m MultiRESTMapper) KindFor(resource unversioned.GroupVersionResource) (unversioned.GroupVersionKind, error) {
2016-03-19 23:00:11 +00:00
kinds, err := m.KindsFor(resource)
if err != nil {
return unversioned.GroupVersionKind{}, err
}
if len(kinds) == 1 {
return kinds[0], nil
2016-02-08 17:26:38 +00:00
}
2016-03-19 23:00:11 +00:00
return unversioned.GroupVersionKind{}, &AmbiguousResourceError{PartialResource: resource, MatchingKinds: kinds}
2016-02-08 17:26:38 +00:00
}
// RESTMapping provides the REST mapping for the resource based on the
// kind and version. This implementation supports multiple REST schemas and
// return the first match.
2016-03-19 23:00:11 +00:00
func (m MultiRESTMapper) RESTMapping(gk unversioned.GroupKind, versions ...string) (*RESTMapping, error) {
allMappings := []*RESTMapping{}
errors := []error{}
2016-02-08 17:26:38 +00:00
for _, t := range m {
2016-03-19 23:00:11 +00:00
currMapping, err := t.RESTMapping(gk, versions...)
// ignore "no match" errors, but any other error percolates back up
if IsNoResourceMatchError(err) {
continue
}
if err != nil {
errors = append(errors, err)
continue
2016-02-08 17:26:38 +00:00
}
2016-03-19 23:00:11 +00:00
allMappings = append(allMappings, currMapping)
2016-02-08 17:26:38 +00:00
}
2016-03-19 23:00:11 +00:00
// if we got exactly one mapping, then use it even if other requested failed
if len(allMappings) == 1 {
return allMappings[0], nil
}
2016-04-17 20:19:22 +00:00
if len(allMappings) > 1 {
return nil, fmt.Errorf("multiple matches found for %v in %v", gk, versions)
}
2016-03-19 23:00:11 +00:00
if len(errors) > 0 {
return nil, utilerrors.NewAggregate(errors)
}
2016-04-17 20:19:22 +00:00
return nil, fmt.Errorf("no match found for %v in %v", gk, versions)
2016-02-08 17:26:38 +00:00
}
// AliasesForResource finds the first alias response for the provided mappers.
2016-03-19 23:00:11 +00:00
func (m MultiRESTMapper) AliasesForResource(alias string) ([]string, bool) {
2016-04-17 20:19:22 +00:00
seenAliases := sets.NewString()
2016-03-19 23:00:11 +00:00
allAliases := []string{}
handled := false
2016-02-08 17:26:38 +00:00
for _, t := range m {
2016-03-19 23:00:11 +00:00
if currAliases, currOk := t.AliasesForResource(alias); currOk {
2016-04-17 20:19:22 +00:00
for _, currAlias := range currAliases {
if !seenAliases.Has(currAlias) {
allAliases = append(allAliases, currAlias)
seenAliases.Insert(currAlias)
}
}
2016-03-19 23:00:11 +00:00
handled = true
2016-02-08 17:26:38 +00:00
}
}
2016-03-19 23:00:11 +00:00
return allAliases, handled
2016-02-08 17:26:38 +00:00
}