|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package controllerrestmapper |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + "sync" |
| 21 | + |
| 22 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 23 | + "k8s.io/apimachinery/pkg/api/meta" |
| 24 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 25 | + "k8s.io/client-go/discovery" |
| 26 | + "k8s.io/klog/v2" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 28 | +) |
| 29 | + |
| 30 | +// cache is our cache of schema information. |
| 31 | +type cache struct { |
| 32 | + mutex sync.Mutex |
| 33 | + groupVersions map[schema.GroupVersion]*cachedGroupVersion |
| 34 | +} |
| 35 | + |
| 36 | +// newCache is the constructor for a cache. |
| 37 | +func newCache() *cache { |
| 38 | + return &cache{ |
| 39 | + groupVersions: make(map[schema.GroupVersion]*cachedGroupVersion), |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// cachedGroupVersion caches (all) the resource information for a particular groupversion. |
| 44 | +type cachedGroupVersion struct { |
| 45 | + gv schema.GroupVersion |
| 46 | + mutex sync.Mutex |
| 47 | + kinds map[string]cachedGVR |
| 48 | +} |
| 49 | + |
| 50 | +// cachedGVR caches the information for a particular resource. |
| 51 | +type cachedGVR struct { |
| 52 | + Resource string |
| 53 | + Scope meta.RESTScope |
| 54 | +} |
| 55 | + |
| 56 | +// findRESTMapping returns the RESTMapping for the specified GVK, querying discovery if not cached. |
| 57 | +func (c *cache) findRESTMapping(discovery discovery.DiscoveryInterface, gv schema.GroupVersion, kind string) (*meta.RESTMapping, error) { |
| 58 | + c.mutex.Lock() |
| 59 | + cached := c.groupVersions[gv] |
| 60 | + if cached == nil { |
| 61 | + cached = &cachedGroupVersion{gv: gv} |
| 62 | + c.groupVersions[gv] = cached |
| 63 | + } |
| 64 | + c.mutex.Unlock() |
| 65 | + return cached.findRESTMapping(discovery, kind) |
| 66 | +} |
| 67 | + |
| 68 | +// findRESTMapping returns the RESTMapping for the specified GVK, querying discovery if not cached. |
| 69 | +func (c *cachedGroupVersion) findRESTMapping(discovery discovery.DiscoveryInterface, kind string) (*meta.RESTMapping, error) { |
| 70 | + kinds, err := c.fetch(discovery) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + cached, found := kinds[kind] |
| 76 | + if !found { |
| 77 | + return nil, nil |
| 78 | + } |
| 79 | + return &meta.RESTMapping{ |
| 80 | + Resource: c.gv.WithResource(cached.Resource), |
| 81 | + GroupVersionKind: c.gv.WithKind(kind), |
| 82 | + Scope: cached.Scope, |
| 83 | + }, nil |
| 84 | +} |
| 85 | + |
| 86 | +// fetch returns the metadata, fetching it if not cached. |
| 87 | +func (c *cachedGroupVersion) fetch(discovery discovery.DiscoveryInterface) (map[string]cachedGVR, error) { |
| 88 | + log := log.Log |
| 89 | + |
| 90 | + c.mutex.Lock() |
| 91 | + defer c.mutex.Unlock() |
| 92 | + |
| 93 | + if c.kinds != nil { |
| 94 | + return c.kinds, nil |
| 95 | + } |
| 96 | + |
| 97 | + log.Info("discovering server resources for group/version", "gv", c.gv.String()) |
| 98 | + resourceList, err := discovery.ServerResourcesForGroupVersion(c.gv.String()) |
| 99 | + if err != nil { |
| 100 | + // We treat "no match" as an empty result, but any other error percolates back up |
| 101 | + if meta.IsNoMatchError(err) || apierrors.IsNotFound(err) { |
| 102 | + return nil, nil |
| 103 | + } else { |
| 104 | + klog.Infof("unexpected error from ServerResourcesForGroupVersion(%v): %w", c.gv, err) |
| 105 | + return nil, fmt.Errorf("error from ServerResourcesForGroupVersion(%v): %w", c.gv, err) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + kinds := make(map[string]cachedGVR) |
| 110 | + for i := range resourceList.APIResources { |
| 111 | + resource := resourceList.APIResources[i] |
| 112 | + |
| 113 | + // if we have a slash, then this is a subresource and we shouldn't create mappings for those. |
| 114 | + if strings.Contains(resource.Name, "/") { |
| 115 | + continue |
| 116 | + } |
| 117 | + |
| 118 | + scope := meta.RESTScopeRoot |
| 119 | + if resource.Namespaced { |
| 120 | + scope = meta.RESTScopeNamespace |
| 121 | + } |
| 122 | + kinds[resource.Kind] = cachedGVR{ |
| 123 | + Resource: resource.Name, |
| 124 | + Scope: scope, |
| 125 | + } |
| 126 | + } |
| 127 | + c.kinds = kinds |
| 128 | + return kinds, nil |
| 129 | +} |
0 commit comments