traefik/vendor/k8s.io/client-go/discovery/restmapper.go

332 lines
10 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 discovery
import (
"fmt"
"sync"
2018-02-14 08:56:04 +00:00
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
2017-04-07 10:49:53 +00:00
"github.com/golang/glog"
2017-02-07 21:33:23 +00:00
)
// APIGroupResources is an API group with a mapping of versions to
// resources.
type APIGroupResources struct {
2018-02-14 08:56:04 +00:00
Group metav1.APIGroup
2017-02-07 21:33:23 +00:00
// A mapping of version string to a slice of APIResources for
// that version.
2018-02-14 08:56:04 +00:00
VersionedResources map[string][]metav1.APIResource
2017-02-07 21:33:23 +00:00
}
// NewRESTMapper returns a PriorityRESTMapper based on the discovered
2017-04-07 10:49:53 +00:00
// groups and resources passed in.
2017-02-07 21:33:23 +00:00
func NewRESTMapper(groupResources []*APIGroupResources, versionInterfaces meta.VersionInterfacesFunc) meta.RESTMapper {
unionMapper := meta.MultiRESTMapper{}
var groupPriority []string
2018-02-14 08:56:04 +00:00
// /v1 is special. It should always come first
resourcePriority := []schema.GroupVersionResource{{Group: "", Version: "v1", Resource: meta.AnyResource}}
kindPriority := []schema.GroupVersionKind{{Group: "", Version: "v1", Kind: meta.AnyKind}}
2017-02-07 21:33:23 +00:00
for _, group := range groupResources {
groupPriority = append(groupPriority, group.Group.Name)
2018-02-14 08:56:04 +00:00
// Make sure the preferred version comes first
2017-02-07 21:33:23 +00:00
if len(group.Group.PreferredVersion.Version) != 0 {
2017-04-07 10:49:53 +00:00
preferred := group.Group.PreferredVersion.Version
if _, ok := group.VersionedResources[preferred]; ok {
2018-02-14 08:56:04 +00:00
resourcePriority = append(resourcePriority, schema.GroupVersionResource{
2017-02-07 21:33:23 +00:00
Group: group.Group.Name,
Version: group.Group.PreferredVersion.Version,
Resource: meta.AnyResource,
})
2018-02-14 08:56:04 +00:00
kindPriority = append(kindPriority, schema.GroupVersionKind{
2017-02-07 21:33:23 +00:00
Group: group.Group.Name,
Version: group.Group.PreferredVersion.Version,
Kind: meta.AnyKind,
})
}
}
for _, discoveryVersion := range group.Group.Versions {
resources, ok := group.VersionedResources[discoveryVersion.Version]
if !ok {
continue
}
2018-02-14 08:56:04 +00:00
// Add non-preferred versions after the preferred version, in case there are resources that only exist in those versions
if discoveryVersion.Version != group.Group.PreferredVersion.Version {
resourcePriority = append(resourcePriority, schema.GroupVersionResource{
Group: group.Group.Name,
Version: discoveryVersion.Version,
Resource: meta.AnyResource,
})
kindPriority = append(kindPriority, schema.GroupVersionKind{
Group: group.Group.Name,
Version: discoveryVersion.Version,
Kind: meta.AnyKind,
})
}
gv := schema.GroupVersion{Group: group.Group.Name, Version: discoveryVersion.Version}
versionMapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{gv}, versionInterfaces)
2017-02-07 21:33:23 +00:00
for _, resource := range resources {
scope := meta.RESTScopeNamespace
if !resource.Namespaced {
scope = meta.RESTScopeRoot
}
2018-02-14 08:56:04 +00:00
// this is for legacy resources and servers which don't list singular forms. For those we must still guess.
if len(resource.SingularName) == 0 {
versionMapper.Add(gv.WithKind(resource.Kind), scope)
// TODO this is producing unsafe guesses that don't actually work, but it matches previous behavior
versionMapper.Add(gv.WithKind(resource.Kind+"List"), scope)
continue
}
plural := gv.WithResource(resource.Name)
singular := gv.WithResource(resource.SingularName)
versionMapper.AddSpecific(gv.WithKind(resource.Kind), plural, singular, scope)
// TODO this is producing unsafe guesses that don't actually work, but it matches previous behavior
2017-02-07 21:33:23 +00:00
versionMapper.Add(gv.WithKind(resource.Kind+"List"), scope)
}
// TODO why is this type not in discovery (at least for "v1")
versionMapper.Add(gv.WithKind("List"), meta.RESTScopeRoot)
unionMapper = append(unionMapper, versionMapper)
}
}
for _, group := range groupPriority {
2018-02-14 08:56:04 +00:00
resourcePriority = append(resourcePriority, schema.GroupVersionResource{
2017-02-07 21:33:23 +00:00
Group: group,
Version: meta.AnyVersion,
Resource: meta.AnyResource,
})
2018-02-14 08:56:04 +00:00
kindPriority = append(kindPriority, schema.GroupVersionKind{
2017-02-07 21:33:23 +00:00
Group: group,
Version: meta.AnyVersion,
Kind: meta.AnyKind,
})
}
return meta.PriorityRESTMapper{
Delegate: unionMapper,
ResourcePriority: resourcePriority,
KindPriority: kindPriority,
}
}
// GetAPIGroupResources uses the provided discovery client to gather
// discovery information and populate a slice of APIGroupResources.
func GetAPIGroupResources(cl DiscoveryInterface) ([]*APIGroupResources, error) {
apiGroups, err := cl.ServerGroups()
if err != nil {
return nil, err
}
var result []*APIGroupResources
for _, group := range apiGroups.Groups {
groupResources := &APIGroupResources{
Group: group,
2018-02-14 08:56:04 +00:00
VersionedResources: make(map[string][]metav1.APIResource),
2017-02-07 21:33:23 +00:00
}
for _, version := range group.Versions {
resources, err := cl.ServerResourcesForGroupVersion(version.GroupVersion)
if err != nil {
2018-02-14 08:56:04 +00:00
// continue as best we can
// TODO track the errors and update callers to handle partial errors.
continue
2017-02-07 21:33:23 +00:00
}
groupResources.VersionedResources[version.Version] = resources.APIResources
}
result = append(result, groupResources)
}
return result, nil
}
// DeferredDiscoveryRESTMapper is a RESTMapper that will defer
// initialization of the RESTMapper until the first mapping is
// requested.
type DeferredDiscoveryRESTMapper struct {
initMu sync.Mutex
delegate meta.RESTMapper
2017-04-07 10:49:53 +00:00
cl CachedDiscoveryInterface
2017-02-07 21:33:23 +00:00
versionInterface meta.VersionInterfacesFunc
}
// NewDeferredDiscoveryRESTMapper returns a
// DeferredDiscoveryRESTMapper that will lazily query the provided
// client for discovery information to do REST mappings.
2017-04-07 10:49:53 +00:00
func NewDeferredDiscoveryRESTMapper(cl CachedDiscoveryInterface, versionInterface meta.VersionInterfacesFunc) *DeferredDiscoveryRESTMapper {
2017-02-07 21:33:23 +00:00
return &DeferredDiscoveryRESTMapper{
cl: cl,
versionInterface: versionInterface,
}
}
func (d *DeferredDiscoveryRESTMapper) getDelegate() (meta.RESTMapper, error) {
d.initMu.Lock()
defer d.initMu.Unlock()
if d.delegate != nil {
return d.delegate, nil
}
groupResources, err := GetAPIGroupResources(d.cl)
if err != nil {
return nil, err
}
d.delegate = NewRESTMapper(groupResources, d.versionInterface)
return d.delegate, err
}
// Reset resets the internally cached Discovery information and will
// cause the next mapping request to re-discover.
func (d *DeferredDiscoveryRESTMapper) Reset() {
2017-04-07 10:49:53 +00:00
glog.V(5).Info("Invalidating discovery information")
2017-02-07 21:33:23 +00:00
d.initMu.Lock()
2017-04-07 10:49:53 +00:00
defer d.initMu.Unlock()
d.cl.Invalidate()
2017-02-07 21:33:23 +00:00
d.delegate = nil
}
// KindFor takes a partial resource and returns back the single match.
// It returns an error if there are multiple matches.
2018-02-14 08:56:04 +00:00
func (d *DeferredDiscoveryRESTMapper) KindFor(resource schema.GroupVersionResource) (gvk schema.GroupVersionKind, err error) {
2017-02-07 21:33:23 +00:00
del, err := d.getDelegate()
if err != nil {
2018-02-14 08:56:04 +00:00
return schema.GroupVersionKind{}, err
2017-02-07 21:33:23 +00:00
}
2017-04-07 10:49:53 +00:00
gvk, err = del.KindFor(resource)
if err != nil && !d.cl.Fresh() {
d.Reset()
gvk, err = d.KindFor(resource)
}
return
2017-02-07 21:33:23 +00:00
}
// KindsFor takes a partial resource and returns back the list of
// potential kinds in priority order.
2018-02-14 08:56:04 +00:00
func (d *DeferredDiscoveryRESTMapper) KindsFor(resource schema.GroupVersionResource) (gvks []schema.GroupVersionKind, err error) {
2017-02-07 21:33:23 +00:00
del, err := d.getDelegate()
if err != nil {
return nil, err
}
2017-04-07 10:49:53 +00:00
gvks, err = del.KindsFor(resource)
if len(gvks) == 0 && !d.cl.Fresh() {
d.Reset()
gvks, err = d.KindsFor(resource)
}
return
2017-02-07 21:33:23 +00:00
}
// ResourceFor takes a partial resource and returns back the single
// match. It returns an error if there are multiple matches.
2018-02-14 08:56:04 +00:00
func (d *DeferredDiscoveryRESTMapper) ResourceFor(input schema.GroupVersionResource) (gvr schema.GroupVersionResource, err error) {
2017-02-07 21:33:23 +00:00
del, err := d.getDelegate()
if err != nil {
2018-02-14 08:56:04 +00:00
return schema.GroupVersionResource{}, err
2017-02-07 21:33:23 +00:00
}
2017-04-07 10:49:53 +00:00
gvr, err = del.ResourceFor(input)
if err != nil && !d.cl.Fresh() {
d.Reset()
gvr, err = d.ResourceFor(input)
}
return
2017-02-07 21:33:23 +00:00
}
// ResourcesFor takes a partial resource and returns back the list of
// potential resource in priority order.
2018-02-14 08:56:04 +00:00
func (d *DeferredDiscoveryRESTMapper) ResourcesFor(input schema.GroupVersionResource) (gvrs []schema.GroupVersionResource, err error) {
2017-02-07 21:33:23 +00:00
del, err := d.getDelegate()
if err != nil {
return nil, err
}
2017-04-07 10:49:53 +00:00
gvrs, err = del.ResourcesFor(input)
if len(gvrs) == 0 && !d.cl.Fresh() {
d.Reset()
gvrs, err = d.ResourcesFor(input)
}
return
2017-02-07 21:33:23 +00:00
}
// RESTMapping identifies a preferred resource mapping for the
// provided group kind.
2018-02-14 08:56:04 +00:00
func (d *DeferredDiscoveryRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (m *meta.RESTMapping, err error) {
2017-02-07 21:33:23 +00:00
del, err := d.getDelegate()
if err != nil {
return nil, err
}
2017-04-07 10:49:53 +00:00
m, err = del.RESTMapping(gk, versions...)
if err != nil && !d.cl.Fresh() {
d.Reset()
m, err = d.RESTMapping(gk, versions...)
}
return
2017-02-07 21:33:23 +00:00
}
// RESTMappings returns the RESTMappings for the provided group kind
// in a rough internal preferred order. If no kind is found, it will
// return a NoResourceMatchError.
2018-02-14 08:56:04 +00:00
func (d *DeferredDiscoveryRESTMapper) RESTMappings(gk schema.GroupKind, versions ...string) (ms []*meta.RESTMapping, err error) {
2017-02-07 21:33:23 +00:00
del, err := d.getDelegate()
if err != nil {
return nil, err
}
2018-02-14 08:56:04 +00:00
ms, err = del.RESTMappings(gk, versions...)
2017-04-07 10:49:53 +00:00
if len(ms) == 0 && !d.cl.Fresh() {
d.Reset()
2018-02-14 08:56:04 +00:00
ms, err = d.RESTMappings(gk, versions...)
2017-04-07 10:49:53 +00:00
}
return
2017-02-07 21:33:23 +00:00
}
// ResourceSingularizer converts a resource name from plural to
// singular (e.g., from pods to pod).
func (d *DeferredDiscoveryRESTMapper) ResourceSingularizer(resource string) (singular string, err error) {
del, err := d.getDelegate()
if err != nil {
return resource, err
}
2017-04-07 10:49:53 +00:00
singular, err = del.ResourceSingularizer(resource)
if err != nil && !d.cl.Fresh() {
d.Reset()
singular, err = d.ResourceSingularizer(resource)
}
return
2017-02-07 21:33:23 +00:00
}
func (d *DeferredDiscoveryRESTMapper) String() string {
del, err := d.getDelegate()
if err != nil {
return fmt.Sprintf("DeferredDiscoveryRESTMapper{%v}", err)
}
return fmt.Sprintf("DeferredDiscoveryRESTMapper{\n\t%v\n}", del)
}
// Make sure it satisfies the interface
var _ meta.RESTMapper = &DeferredDiscoveryRESTMapper{}