traefik/vendor/k8s.io/apimachinery/pkg/api/meta/priority.go

223 lines
7.4 KiB
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
/*
Copyright 2016 The Kubernetes Authors.
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"
2018-02-14 08:56:04 +00:00
"k8s.io/apimachinery/pkg/runtime/schema"
2017-02-07 21:33:23 +00:00
)
const (
AnyGroup = "*"
AnyVersion = "*"
AnyResource = "*"
AnyKind = "*"
)
// PriorityRESTMapper is a wrapper for automatically choosing a particular Resource or Kind
// when multiple matches are possible
type PriorityRESTMapper struct {
// Delegate is the RESTMapper to use to locate all the Kind and Resource matches
Delegate RESTMapper
// ResourcePriority is a list of priority patterns to apply to matching resources.
// The list of all matching resources is narrowed based on the patterns until only one remains.
// A pattern with no matches is skipped. A pattern with more than one match uses its
// matches as the list to continue matching against.
2018-02-14 08:56:04 +00:00
ResourcePriority []schema.GroupVersionResource
2017-02-07 21:33:23 +00:00
// KindPriority is a list of priority patterns to apply to matching kinds.
// The list of all matching kinds is narrowed based on the patterns until only one remains.
// A pattern with no matches is skipped. A pattern with more than one match uses its
// matches as the list to continue matching against.
2018-02-14 08:56:04 +00:00
KindPriority []schema.GroupVersionKind
2017-02-07 21:33:23 +00:00
}
func (m PriorityRESTMapper) String() string {
return fmt.Sprintf("PriorityRESTMapper{\n\t%v\n\t%v\n\t%v\n}", m.ResourcePriority, m.KindPriority, m.Delegate)
}
// ResourceFor finds all resources, then passes them through the ResourcePriority patterns to find a single matching hit.
2018-02-14 08:56:04 +00:00
func (m PriorityRESTMapper) ResourceFor(partiallySpecifiedResource schema.GroupVersionResource) (schema.GroupVersionResource, error) {
originalGVRs, originalErr := m.Delegate.ResourcesFor(partiallySpecifiedResource)
if originalErr != nil && len(originalGVRs) == 0 {
return schema.GroupVersionResource{}, originalErr
2017-02-07 21:33:23 +00:00
}
if len(originalGVRs) == 1 {
return originalGVRs[0], originalErr
2017-02-07 21:33:23 +00:00
}
2018-02-14 08:56:04 +00:00
remainingGVRs := append([]schema.GroupVersionResource{}, originalGVRs...)
2017-02-07 21:33:23 +00:00
for _, pattern := range m.ResourcePriority {
2018-02-14 08:56:04 +00:00
matchedGVRs := []schema.GroupVersionResource{}
2017-02-07 21:33:23 +00:00
for _, gvr := range remainingGVRs {
if resourceMatches(pattern, gvr) {
matchedGVRs = append(matchedGVRs, gvr)
}
}
switch len(matchedGVRs) {
case 0:
// if you have no matches, then nothing matched this pattern just move to the next
continue
case 1:
// one match, return
return matchedGVRs[0], originalErr
2017-02-07 21:33:23 +00:00
default:
// more than one match, use the matched hits as the list moving to the next pattern.
// this way you can have a series of selection criteria
remainingGVRs = matchedGVRs
}
}
2018-02-14 08:56:04 +00:00
return schema.GroupVersionResource{}, &AmbiguousResourceError{PartialResource: partiallySpecifiedResource, MatchingResources: originalGVRs}
2017-02-07 21:33:23 +00:00
}
// KindFor finds all kinds, then passes them through the KindPriority patterns to find a single matching hit.
2018-02-14 08:56:04 +00:00
func (m PriorityRESTMapper) KindFor(partiallySpecifiedResource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
originalGVKs, originalErr := m.Delegate.KindsFor(partiallySpecifiedResource)
if originalErr != nil && len(originalGVKs) == 0 {
return schema.GroupVersionKind{}, originalErr
2017-02-07 21:33:23 +00:00
}
if len(originalGVKs) == 1 {
return originalGVKs[0], originalErr
2017-02-07 21:33:23 +00:00
}
2018-02-14 08:56:04 +00:00
remainingGVKs := append([]schema.GroupVersionKind{}, originalGVKs...)
2017-02-07 21:33:23 +00:00
for _, pattern := range m.KindPriority {
2018-02-14 08:56:04 +00:00
matchedGVKs := []schema.GroupVersionKind{}
2017-02-07 21:33:23 +00:00
for _, gvr := range remainingGVKs {
if kindMatches(pattern, gvr) {
matchedGVKs = append(matchedGVKs, gvr)
}
}
switch len(matchedGVKs) {
case 0:
// if you have no matches, then nothing matched this pattern just move to the next
continue
case 1:
// one match, return
return matchedGVKs[0], originalErr
2017-02-07 21:33:23 +00:00
default:
// more than one match, use the matched hits as the list moving to the next pattern.
// this way you can have a series of selection criteria
remainingGVKs = matchedGVKs
}
}
2018-02-14 08:56:04 +00:00
return schema.GroupVersionKind{}, &AmbiguousResourceError{PartialResource: partiallySpecifiedResource, MatchingKinds: originalGVKs}
2017-02-07 21:33:23 +00:00
}
2018-02-14 08:56:04 +00:00
func resourceMatches(pattern schema.GroupVersionResource, resource schema.GroupVersionResource) bool {
2017-02-07 21:33:23 +00:00
if pattern.Group != AnyGroup && pattern.Group != resource.Group {
return false
}
if pattern.Version != AnyVersion && pattern.Version != resource.Version {
return false
}
if pattern.Resource != AnyResource && pattern.Resource != resource.Resource {
return false
}
return true
}
2018-02-14 08:56:04 +00:00
func kindMatches(pattern schema.GroupVersionKind, kind schema.GroupVersionKind) bool {
2017-02-07 21:33:23 +00:00
if pattern.Group != AnyGroup && pattern.Group != kind.Group {
return false
}
if pattern.Version != AnyVersion && pattern.Version != kind.Version {
return false
}
if pattern.Kind != AnyKind && pattern.Kind != kind.Kind {
return false
}
return true
}
2018-02-14 08:56:04 +00:00
func (m PriorityRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (mapping *RESTMapping, err error) {
mappings, originalErr := m.Delegate.RESTMappings(gk, versions...)
if originalErr != nil && len(mappings) == 0 {
return nil, originalErr
2017-02-07 21:33:23 +00:00
}
// any versions the user provides take priority
priorities := m.KindPriority
if len(versions) > 0 {
2018-02-14 08:56:04 +00:00
priorities = make([]schema.GroupVersionKind, 0, len(m.KindPriority)+len(versions))
2017-02-07 21:33:23 +00:00
for _, version := range versions {
2018-02-14 08:56:04 +00:00
gv := schema.GroupVersion{
Version: version,
Group: gk.Group,
2017-02-07 21:33:23 +00:00
}
priorities = append(priorities, gv.WithKind(AnyKind))
}
priorities = append(priorities, m.KindPriority...)
}
remaining := append([]*RESTMapping{}, mappings...)
for _, pattern := range priorities {
var matching []*RESTMapping
for _, m := range remaining {
if kindMatches(pattern, m.GroupVersionKind) {
matching = append(matching, m)
}
}
switch len(matching) {
case 0:
// if you have no matches, then nothing matched this pattern just move to the next
continue
case 1:
// one match, return
return matching[0], originalErr
2017-02-07 21:33:23 +00:00
default:
// more than one match, use the matched hits as the list moving to the next pattern.
// this way you can have a series of selection criteria
remaining = matching
}
}
if len(remaining) == 1 {
return remaining[0], originalErr
2017-02-07 21:33:23 +00:00
}
2018-02-14 08:56:04 +00:00
var kinds []schema.GroupVersionKind
2017-02-07 21:33:23 +00:00
for _, m := range mappings {
kinds = append(kinds, m.GroupVersionKind)
}
return nil, &AmbiguousKindError{PartialKind: gk.WithVersion(""), MatchingKinds: kinds}
}
2018-02-14 08:56:04 +00:00
func (m PriorityRESTMapper) RESTMappings(gk schema.GroupKind, versions ...string) ([]*RESTMapping, error) {
return m.Delegate.RESTMappings(gk, versions...)
2017-02-07 21:33:23 +00:00
}
func (m PriorityRESTMapper) ResourceSingularizer(resource string) (singular string, err error) {
return m.Delegate.ResourceSingularizer(resource)
}
2018-02-14 08:56:04 +00:00
func (m PriorityRESTMapper) ResourcesFor(partiallySpecifiedResource schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
2017-02-07 21:33:23 +00:00
return m.Delegate.ResourcesFor(partiallySpecifiedResource)
}
2018-02-14 08:56:04 +00:00
func (m PriorityRESTMapper) KindsFor(partiallySpecifiedResource schema.GroupVersionResource) (gvk []schema.GroupVersionKind, err error) {
2017-02-07 21:33:23 +00:00
return m.Delegate.KindsFor(partiallySpecifiedResource)
}