2022-05-17 14:40:51 +00:00
|
|
|
package dclient
|
2022-05-03 05:30:04 +00:00
|
|
|
|
|
|
|
import (
|
2022-09-30 08:12:21 +00:00
|
|
|
"context"
|
2022-05-03 05:30:04 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-10-05 10:42:42 +00:00
|
|
|
openapiv2 "github.com/google/gnostic/openapiv2"
|
2022-05-17 05:56:48 +00:00
|
|
|
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
|
2023-03-10 13:24:55 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/utils/wildcard"
|
2022-05-03 05:30:04 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/client-go/discovery"
|
|
|
|
)
|
|
|
|
|
2023-03-23 06:12:44 +00:00
|
|
|
// TopLevelApiDescription contains a group/version/resource/subresource reference
|
|
|
|
type TopLevelApiDescription struct {
|
2023-03-21 13:28:00 +00:00
|
|
|
schema.GroupVersion
|
|
|
|
Kind string
|
|
|
|
Resource string
|
2023-03-13 14:44:39 +00:00
|
|
|
SubResource string
|
|
|
|
}
|
|
|
|
|
2023-03-23 06:12:44 +00:00
|
|
|
func (gvrs TopLevelApiDescription) GroupVersionResource() schema.GroupVersionResource {
|
2023-03-21 13:28:00 +00:00
|
|
|
return gvrs.WithResource(gvrs.Resource)
|
|
|
|
}
|
|
|
|
|
2023-03-23 06:12:44 +00:00
|
|
|
func (gvrs TopLevelApiDescription) GroupVersionKind() schema.GroupVersionKind {
|
2023-03-21 13:28:00 +00:00
|
|
|
return gvrs.WithKind(gvrs.Kind)
|
|
|
|
}
|
|
|
|
|
2023-03-23 06:12:44 +00:00
|
|
|
func (gvrs TopLevelApiDescription) ResourceSubresource() string {
|
2023-03-13 14:44:39 +00:00
|
|
|
if gvrs.SubResource == "" {
|
|
|
|
return gvrs.Resource
|
|
|
|
}
|
|
|
|
return gvrs.Resource + "/" + gvrs.SubResource
|
|
|
|
}
|
|
|
|
|
2023-03-23 06:12:44 +00:00
|
|
|
func (gvrs TopLevelApiDescription) WithSubResource(subresource string) TopLevelApiDescription {
|
2023-03-13 14:44:39 +00:00
|
|
|
gvrs.SubResource = subresource
|
|
|
|
return gvrs
|
|
|
|
}
|
|
|
|
|
2022-05-03 05:30:04 +00:00
|
|
|
// IDiscovery provides interface to mange Kind and GVR mapping
|
|
|
|
type IDiscovery interface {
|
2023-03-23 06:12:44 +00:00
|
|
|
FindResources(group, version, kind, subresource string) (map[TopLevelApiDescription]metav1.APIResource, error)
|
2023-03-10 07:15:48 +00:00
|
|
|
// TODO: there's no mapping from GVK to GVR, this is very error prone
|
|
|
|
GetGVRFromGVK(schema.GroupVersionKind) (schema.GroupVersionResource, error)
|
2023-03-09 14:52:44 +00:00
|
|
|
GetGVKFromGVR(schema.GroupVersionResource) (schema.GroupVersionKind, error)
|
2022-05-03 05:30:04 +00:00
|
|
|
OpenAPISchema() (*openapiv2.Document, error)
|
|
|
|
DiscoveryCache() discovery.CachedDiscoveryInterface
|
2022-07-01 03:00:05 +00:00
|
|
|
DiscoveryInterface() discovery.DiscoveryInterface
|
2022-05-03 05:30:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 16:45:23 +00:00
|
|
|
// apiResourceWithListGV is a wrapper for metav1.APIResource with the group-version of its metav1.APIResourceList
|
|
|
|
type apiResourceWithListGV struct {
|
|
|
|
apiResource metav1.APIResource
|
|
|
|
listGV string
|
|
|
|
}
|
|
|
|
|
|
|
|
// serverResources stores the cachedClient instance for discovery client
|
|
|
|
type serverResources struct {
|
2022-05-03 05:30:04 +00:00
|
|
|
cachedClient discovery.CachedDiscoveryInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
// DiscoveryCache gets the discovery client cache
|
2022-12-09 16:45:23 +00:00
|
|
|
func (c serverResources) DiscoveryCache() discovery.CachedDiscoveryInterface {
|
2022-05-03 05:30:04 +00:00
|
|
|
return c.cachedClient
|
|
|
|
}
|
|
|
|
|
2022-07-01 03:00:05 +00:00
|
|
|
// DiscoveryInterface gets the discovery client
|
2022-12-09 16:45:23 +00:00
|
|
|
func (c serverResources) DiscoveryInterface() discovery.DiscoveryInterface {
|
2022-07-01 03:00:05 +00:00
|
|
|
return c.cachedClient
|
|
|
|
}
|
|
|
|
|
2022-05-03 05:30:04 +00:00
|
|
|
// Poll will keep invalidate the local cache
|
2022-12-09 16:45:23 +00:00
|
|
|
func (c serverResources) Poll(ctx context.Context, resync time.Duration) {
|
2022-05-03 08:24:30 +00:00
|
|
|
logger := logger.WithName("Poll")
|
2022-05-03 05:30:04 +00:00
|
|
|
// start a ticker
|
|
|
|
ticker := time.NewTicker(resync)
|
|
|
|
defer func() { ticker.Stop() }()
|
2022-12-09 16:45:23 +00:00
|
|
|
logger.V(6).Info("starting registered resources sync", "period", resync)
|
2022-05-03 05:30:04 +00:00
|
|
|
for {
|
|
|
|
select {
|
2022-09-30 08:12:21 +00:00
|
|
|
case <-ctx.Done():
|
2022-05-03 05:30:04 +00:00
|
|
|
logger.Info("stopping registered resources sync")
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
// set cache as stale
|
|
|
|
logger.V(6).Info("invalidating local client cache for registered resources")
|
|
|
|
c.cachedClient.Invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenAPISchema returns the API server OpenAPI schema document
|
2022-12-09 16:45:23 +00:00
|
|
|
func (c serverResources) OpenAPISchema() (*openapiv2.Document, error) {
|
2022-05-03 05:30:04 +00:00
|
|
|
return c.cachedClient.OpenAPISchema()
|
|
|
|
}
|
|
|
|
|
2023-03-10 07:15:48 +00:00
|
|
|
// GetGVRFromGVK get the Group Version Resource from APIVersion and kind
|
|
|
|
func (c serverResources) GetGVRFromGVK(gvk schema.GroupVersionKind) (schema.GroupVersionResource, error) {
|
|
|
|
_, _, gvr, err := c.FindResource(gvk.GroupVersion().String(), gvk.Kind)
|
2022-05-03 05:30:04 +00:00
|
|
|
if err != nil {
|
2023-03-10 07:15:48 +00:00
|
|
|
logger.Error(err, "schema not found", "gvk", gvk)
|
2022-05-03 05:30:04 +00:00
|
|
|
return schema.GroupVersionResource{}, err
|
|
|
|
}
|
|
|
|
return gvr, nil
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:45:23 +00:00
|
|
|
// GetGVKFromGVR returns the Group Version Kind from Group Version Resource. The groupVersion has to be specified properly
|
|
|
|
// for example, for corev1.Pod, the groupVersion has to be specified as `v1`, specifying empty groupVersion won't work.
|
2023-03-09 14:52:44 +00:00
|
|
|
func (c serverResources) GetGVKFromGVR(gvr schema.GroupVersionResource) (schema.GroupVersionKind, error) {
|
|
|
|
gvk, err := c.findResourceFromResourceName(gvr)
|
2022-05-03 05:30:04 +00:00
|
|
|
if err == nil {
|
2022-12-09 16:45:23 +00:00
|
|
|
return gvk, nil
|
2022-05-03 05:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !c.cachedClient.Fresh() {
|
|
|
|
c.cachedClient.Invalidate()
|
2023-03-09 14:52:44 +00:00
|
|
|
if gvk, err := c.findResourceFromResourceName(gvr); err == nil {
|
2022-12-09 16:45:23 +00:00
|
|
|
return gvk, nil
|
2022-05-03 05:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:45:23 +00:00
|
|
|
return schema.GroupVersionKind{}, err
|
2022-05-03 05:30:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 16:45:23 +00:00
|
|
|
// findResourceFromResourceName returns the GVK for the a particular resourceName and groupVersion
|
2023-03-09 14:52:44 +00:00
|
|
|
func (c serverResources) findResourceFromResourceName(gvr schema.GroupVersionResource) (schema.GroupVersionKind, error) {
|
2022-12-09 16:45:23 +00:00
|
|
|
_, serverGroupsAndResources, err := c.cachedClient.ServerGroupsAndResources()
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "Got empty response for") {
|
|
|
|
if discovery.IsGroupDiscoveryFailedError(err) {
|
|
|
|
logDiscoveryErrors(err)
|
2023-04-03 14:44:01 +00:00
|
|
|
} else if isServerCurrentlyUnableToHandleRequest(err) {
|
2022-12-09 16:45:23 +00:00
|
|
|
logger.V(3).Info("failed to find preferred resource version", "error", err.Error())
|
|
|
|
} else {
|
|
|
|
logger.Error(err, "failed to find preferred resource version")
|
|
|
|
return schema.GroupVersionKind{}, err
|
|
|
|
}
|
|
|
|
}
|
2023-03-09 14:52:44 +00:00
|
|
|
apiResource, err := findResourceFromResourceName(gvr, serverGroupsAndResources)
|
2022-12-09 16:45:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return schema.GroupVersionKind{}, err
|
|
|
|
}
|
|
|
|
return schema.GroupVersionKind{Group: apiResource.Group, Version: apiResource.Version, Kind: apiResource.Kind}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindResource finds an API resource that matches 'kind'. For finding subresources that have the same kind as the parent
|
|
|
|
// resource, kind has to be specified as 'ParentKind/SubresourceName'. For matching status subresource of Pod, kind has
|
|
|
|
// to be specified as `Pod/status`. If the resource is not found and the Cache is not fresh, the cache is invalidated
|
|
|
|
// and a retry is attempted
|
|
|
|
func (c serverResources) FindResource(groupVersion string, kind string) (apiResource, parentAPIResource *metav1.APIResource, gvr schema.GroupVersionResource, err error) {
|
|
|
|
r, pr, gvr, err := c.findResource(groupVersion, kind)
|
|
|
|
if err == nil {
|
|
|
|
return r, pr, gvr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.cachedClient.Fresh() {
|
|
|
|
c.cachedClient.Invalidate()
|
|
|
|
if r, pr, gvr, err = c.findResource(groupVersion, kind); err == nil {
|
|
|
|
return r, pr, gvr, nil
|
|
|
|
}
|
2022-05-03 05:30:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 16:45:23 +00:00
|
|
|
return nil, nil, schema.GroupVersionResource{}, err
|
|
|
|
}
|
|
|
|
|
2023-03-23 06:12:44 +00:00
|
|
|
func (c serverResources) FindResources(group, version, kind, subresource string) (map[TopLevelApiDescription]metav1.APIResource, error) {
|
2023-03-10 13:24:55 +00:00
|
|
|
resources, err := c.findResources(group, version, kind, subresource)
|
|
|
|
if err != nil {
|
|
|
|
if !c.cachedClient.Fresh() {
|
|
|
|
c.cachedClient.Invalidate()
|
|
|
|
return c.findResources(group, version, kind, subresource)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resources, err
|
|
|
|
}
|
|
|
|
|
2023-03-23 06:12:44 +00:00
|
|
|
func (c serverResources) findResources(group, version, kind, subresource string) (map[TopLevelApiDescription]metav1.APIResource, error) {
|
2023-03-10 13:24:55 +00:00
|
|
|
_, serverGroupsAndResources, err := c.cachedClient.ServerGroupsAndResources()
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "Got empty response for") {
|
|
|
|
if discovery.IsGroupDiscoveryFailedError(err) {
|
|
|
|
logDiscoveryErrors(err)
|
|
|
|
} else if isServerCurrentlyUnableToHandleRequest(err) {
|
|
|
|
logger.Error(err, "failed to find preferred resource version")
|
|
|
|
} else {
|
|
|
|
logger.Error(err, "failed to find preferred resource version")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getGVK := func(gv schema.GroupVersion, group, version, kind string) schema.GroupVersionKind {
|
|
|
|
if group == "" {
|
|
|
|
group = gv.Group
|
|
|
|
}
|
|
|
|
if version == "" {
|
|
|
|
version = gv.Version
|
|
|
|
}
|
|
|
|
return schema.GroupVersionKind{
|
|
|
|
Group: group,
|
|
|
|
Version: version,
|
|
|
|
Kind: kind,
|
|
|
|
}
|
|
|
|
}
|
2023-03-23 06:12:44 +00:00
|
|
|
resources := map[TopLevelApiDescription]metav1.APIResource{}
|
2023-03-10 13:24:55 +00:00
|
|
|
// first match resouces
|
|
|
|
for _, list := range serverGroupsAndResources {
|
|
|
|
gv, err := schema.ParseGroupVersion(list.GroupVersion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
for _, resource := range list.APIResources {
|
|
|
|
if !strings.Contains(resource.Name, "/") {
|
|
|
|
gvk := getGVK(gv, resource.Group, resource.Version, resource.Kind)
|
|
|
|
if wildcard.Match(group, gvk.Group) && wildcard.Match(version, gvk.Version) && wildcard.Match(kind, gvk.Kind) {
|
2023-03-23 06:12:44 +00:00
|
|
|
gvrs := TopLevelApiDescription{
|
2023-03-21 13:28:00 +00:00
|
|
|
GroupVersion: gv,
|
|
|
|
Kind: resource.Kind,
|
|
|
|
Resource: resource.Name,
|
2023-03-17 11:21:26 +00:00
|
|
|
}
|
|
|
|
resources[gvrs] = resource
|
2023-03-10 13:24:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// second match subresouces if necessary
|
2023-03-23 06:12:44 +00:00
|
|
|
subresources := map[TopLevelApiDescription]metav1.APIResource{}
|
2023-03-10 13:24:55 +00:00
|
|
|
if subresource != "" {
|
|
|
|
for _, list := range serverGroupsAndResources {
|
|
|
|
for _, resource := range list.APIResources {
|
|
|
|
for parent := range resources {
|
|
|
|
if wildcard.Match(parent.Resource+"/"+subresource, resource.Name) {
|
2023-03-13 14:44:39 +00:00
|
|
|
parts := strings.Split(resource.Name, "/")
|
2023-03-17 11:21:26 +00:00
|
|
|
subresources[parent.WithSubResource(parts[1])] = resource
|
2023-03-10 13:24:55 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if kind == "*" && subresource == "*" {
|
2023-03-17 11:21:26 +00:00
|
|
|
for key, value := range subresources {
|
|
|
|
resources[key] = value
|
|
|
|
}
|
|
|
|
return resources, nil
|
2023-03-10 13:24:55 +00:00
|
|
|
} else if subresource != "" {
|
2023-03-17 11:21:26 +00:00
|
|
|
return subresources, nil
|
2023-03-10 13:24:55 +00:00
|
|
|
}
|
2023-03-17 11:21:26 +00:00
|
|
|
return resources, nil
|
2023-03-10 13:24:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 16:45:23 +00:00
|
|
|
func (c serverResources) findResource(groupVersion string, kind string) (apiResource, parentAPIResource *metav1.APIResource,
|
|
|
|
gvr schema.GroupVersionResource, err error,
|
|
|
|
) {
|
|
|
|
serverPreferredResources, _ := c.cachedClient.ServerPreferredResources()
|
|
|
|
_, serverGroupsAndResources, err := c.cachedClient.ServerGroupsAndResources()
|
2022-07-01 03:00:05 +00:00
|
|
|
if err != nil && !strings.Contains(err.Error(), "Got empty response for") {
|
2023-04-03 14:44:01 +00:00
|
|
|
_, err := schema.ParseGroupVersion(groupVersion)
|
2023-03-09 14:52:44 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to parse group/version", "groupVersion", groupVersion)
|
|
|
|
return nil, nil, schema.GroupVersionResource{}, err
|
|
|
|
}
|
2022-05-03 05:30:04 +00:00
|
|
|
if discovery.IsGroupDiscoveryFailedError(err) {
|
2022-12-09 16:45:23 +00:00
|
|
|
logDiscoveryErrors(err)
|
2023-04-03 14:44:01 +00:00
|
|
|
} else if isServerCurrentlyUnableToHandleRequest(err) {
|
2022-05-03 08:24:30 +00:00
|
|
|
logger.V(3).Info("failed to find preferred resource version", "error", err.Error())
|
2022-05-03 05:30:04 +00:00
|
|
|
} else {
|
2022-05-03 08:24:30 +00:00
|
|
|
logger.Error(err, "failed to find preferred resource version")
|
2022-12-09 16:45:23 +00:00
|
|
|
return nil, nil, schema.GroupVersionResource{}, err
|
2022-05-03 05:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:45:23 +00:00
|
|
|
kindWithoutSubresource, subresource := kubeutils.SplitSubresource(kind)
|
|
|
|
|
2022-05-10 01:50:50 +00:00
|
|
|
if subresource != "" {
|
2022-12-09 16:45:23 +00:00
|
|
|
parentApiResource, _, _, err := c.findResource(groupVersion, kindWithoutSubresource)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "Unable to find parent resource", "kind", kind)
|
|
|
|
return nil, nil, schema.GroupVersionResource{}, err
|
|
|
|
}
|
|
|
|
parentResourceName := parentApiResource.Name
|
|
|
|
resource, gvr, err := findSubresource(groupVersion, parentResourceName, subresource, kind, serverGroupsAndResources)
|
|
|
|
return resource, parentApiResource, gvr, err
|
2022-05-10 01:50:50 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 16:45:23 +00:00
|
|
|
return findResource(groupVersion, kind, serverPreferredResources, serverGroupsAndResources)
|
|
|
|
}
|
|
|
|
|
|
|
|
// findSubresource finds the subresource for the given parent resource, groupVersion and serverResourcesList
|
|
|
|
func findSubresource(groupVersion, parentResourceName, subresource, kind string, serverResourcesList []*metav1.APIResourceList) (
|
|
|
|
apiResource *metav1.APIResource, gvr schema.GroupVersionResource, err error,
|
|
|
|
) {
|
|
|
|
for _, serverResourceList := range serverResourcesList {
|
|
|
|
if groupVersion == "" || kubeutils.GroupVersionMatches(groupVersion, serverResourceList.GroupVersion) {
|
|
|
|
for _, serverResource := range serverResourceList.APIResources {
|
|
|
|
if serverResource.Name == parentResourceName+"/"+strings.ToLower(subresource) {
|
|
|
|
logger.V(6).Info("matched API resource to kind", "apiResource", serverResource, "kind", kind)
|
2022-05-03 05:30:04 +00:00
|
|
|
|
2022-12-09 16:45:23 +00:00
|
|
|
serverResourceGv := getServerResourceGroupVersion(serverResourceList.GroupVersion, serverResource.Group, serverResource.Version)
|
|
|
|
gv, _ := schema.ParseGroupVersion(serverResourceGv)
|
|
|
|
|
|
|
|
serverResource.Group = gv.Group
|
|
|
|
serverResource.Version = gv.Version
|
|
|
|
|
|
|
|
groupVersionResource := gv.WithResource(serverResource.Name)
|
|
|
|
logger.V(6).Info("gv with resource", "gvWithResource", groupVersionResource)
|
|
|
|
return &serverResource, groupVersionResource, nil
|
2022-05-03 05:30:04 +00:00
|
|
|
}
|
2022-12-09 16:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, schema.GroupVersionResource{}, fmt.Errorf("resource not found for kind %s", kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
// findResource finds an API resource that matches 'groupVersion', 'kind', in the given serverResourcesList
|
|
|
|
func findResource(groupVersion string, kind string, serverPreferredResources, serverGroupsAndResources []*metav1.APIResourceList) (
|
|
|
|
apiResource, parentAPIResource *metav1.APIResource, gvr schema.GroupVersionResource, err error,
|
|
|
|
) {
|
|
|
|
matchingServerResources := getMatchingServerResources(groupVersion, kind, serverGroupsAndResources)
|
|
|
|
|
|
|
|
onlySubresourcePresentInMatchingResources := len(matchingServerResources) > 0
|
|
|
|
for _, matchingServerResource := range matchingServerResources {
|
|
|
|
if !kubeutils.IsSubresource(matchingServerResource.apiResource.Name) {
|
|
|
|
onlySubresourcePresentInMatchingResources = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if onlySubresourcePresentInMatchingResources {
|
|
|
|
apiResourceWithListGV := matchingServerResources[0]
|
|
|
|
matchingServerResource := apiResourceWithListGV.apiResource
|
|
|
|
logger.V(6).Info("matched API resource to kind", "apiResource", matchingServerResource, "kind", kind)
|
|
|
|
|
|
|
|
groupVersionResource := schema.GroupVersionResource{
|
|
|
|
Resource: matchingServerResource.Name,
|
|
|
|
Group: matchingServerResource.Group,
|
|
|
|
Version: matchingServerResource.Version,
|
|
|
|
}
|
|
|
|
logger.V(6).Info("gv with resource", "gvWithResource", groupVersionResource)
|
2023-03-09 14:52:44 +00:00
|
|
|
gv, err := schema.ParseGroupVersion(apiResourceWithListGV.listGV)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, schema.GroupVersionResource{}, fmt.Errorf("failed to parse group version %s: %v", apiResourceWithListGV.listGV, err)
|
|
|
|
}
|
|
|
|
parentAPIResource, err := findResourceFromResourceName(
|
|
|
|
gv.WithResource(strings.Split(matchingServerResource.Name, "/")[0]),
|
|
|
|
serverPreferredResources,
|
|
|
|
)
|
2022-12-09 16:45:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, schema.GroupVersionResource{}, fmt.Errorf("failed to find parent resource for subresource %s: %v", matchingServerResource.Name, err)
|
|
|
|
}
|
|
|
|
logger.V(6).Info("parent API resource", "parentAPIResource", parentAPIResource)
|
|
|
|
|
|
|
|
return &matchingServerResource, parentAPIResource, groupVersionResource, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if groupVersion == "" && len(matchingServerResources) > 0 {
|
|
|
|
for _, serverResourceList := range serverPreferredResources {
|
|
|
|
for _, serverResource := range serverResourceList.APIResources {
|
|
|
|
serverResourceGv := getServerResourceGroupVersion(serverResourceList.GroupVersion, serverResource.Group, serverResource.Version)
|
|
|
|
if serverResource.Kind == kind || serverResource.SingularName == kind {
|
|
|
|
gv, _ := schema.ParseGroupVersion(serverResourceGv)
|
|
|
|
serverResource.Group = gv.Group
|
|
|
|
serverResource.Version = gv.Version
|
|
|
|
groupVersionResource := gv.WithResource(serverResource.Name)
|
|
|
|
|
|
|
|
logger.V(6).Info("matched API resource to kind", "apiResource", serverResource, "kind", kind)
|
|
|
|
return &serverResource, nil, groupVersionResource, nil
|
2022-10-14 15:20:30 +00:00
|
|
|
}
|
2022-12-09 16:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, apiResourceWithListGV := range matchingServerResources {
|
|
|
|
matchingServerResource := apiResourceWithListGV.apiResource
|
|
|
|
if !kubeutils.IsSubresource(matchingServerResource.Name) {
|
|
|
|
logger.V(6).Info("matched API resource to kind", "apiResource", matchingServerResource, "kind", kind)
|
|
|
|
|
|
|
|
groupVersionResource := schema.GroupVersionResource{
|
|
|
|
Resource: matchingServerResource.Name,
|
|
|
|
Group: matchingServerResource.Group,
|
|
|
|
Version: matchingServerResource.Version,
|
2022-10-14 15:20:30 +00:00
|
|
|
}
|
2022-12-09 16:45:23 +00:00
|
|
|
logger.V(6).Info("gv with resource", "groupVersionResource", groupVersionResource)
|
|
|
|
return &matchingServerResource, nil, groupVersionResource, nil
|
2022-05-03 05:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:45:23 +00:00
|
|
|
return nil, nil, schema.GroupVersionResource{}, fmt.Errorf("kind '%s' not found in groupVersion '%s'", kind, groupVersion)
|
2022-05-03 05:30:04 +00:00
|
|
|
}
|
2022-05-10 01:50:50 +00:00
|
|
|
|
2022-12-09 16:45:23 +00:00
|
|
|
// getMatchingServerResources returns a list of API resources that match the given groupVersion and kind
|
|
|
|
func getMatchingServerResources(groupVersion string, kind string, serverGroupsAndResources []*metav1.APIResourceList) []apiResourceWithListGV {
|
|
|
|
matchingServerResources := make([]apiResourceWithListGV, 0)
|
|
|
|
for _, serverResourceList := range serverGroupsAndResources {
|
|
|
|
for _, serverResource := range serverResourceList.APIResources {
|
|
|
|
serverResourceGv := getServerResourceGroupVersion(serverResourceList.GroupVersion, serverResource.Group, serverResource.Version)
|
|
|
|
if groupVersion == "" || kubeutils.GroupVersionMatches(groupVersion, serverResourceGv) {
|
|
|
|
if serverResource.Kind == kind || serverResource.SingularName == kind {
|
|
|
|
gv, _ := schema.ParseGroupVersion(serverResourceGv)
|
|
|
|
serverResource.Group = gv.Group
|
|
|
|
serverResource.Version = gv.Version
|
|
|
|
matchingServerResources = append(matchingServerResources, apiResourceWithListGV{apiResource: serverResource, listGV: serverResourceList.GroupVersion})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-10 01:50:50 +00:00
|
|
|
}
|
2022-12-09 16:45:23 +00:00
|
|
|
return matchingServerResources
|
|
|
|
}
|
2022-05-10 01:50:50 +00:00
|
|
|
|
2022-12-09 16:45:23 +00:00
|
|
|
// findResourceFromResourceName finds an API resource that matches 'resourceName', in the given serverResourcesList
|
2023-03-09 14:52:44 +00:00
|
|
|
func findResourceFromResourceName(gvr schema.GroupVersionResource, serverGroupsAndResources []*metav1.APIResourceList) (*metav1.APIResource, error) {
|
|
|
|
for _, list := range serverGroupsAndResources {
|
|
|
|
gv, err := schema.ParseGroupVersion(list.GroupVersion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if gv.Group == gvr.Group && gv.Version == gvr.Version {
|
|
|
|
for _, resource := range list.APIResources {
|
|
|
|
if resource.Name == gvr.Resource {
|
|
|
|
// if the matched resource has group or version set we don't need to copy from the parent list
|
|
|
|
if resource.Group != "" || resource.Version != "" {
|
|
|
|
return &resource, nil
|
|
|
|
}
|
|
|
|
result := resource.DeepCopy()
|
|
|
|
result.Group = gv.Group
|
|
|
|
result.Version = gv.Version
|
|
|
|
return result, nil
|
|
|
|
}
|
2022-12-09 16:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-09 14:52:44 +00:00
|
|
|
return nil, fmt.Errorf("resource %s not found in group %s", gvr.Resource, gvr.GroupVersion())
|
2022-12-09 16:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getServerResourceGroupVersion returns the groupVersion of the serverResource from the apiResourceMetadata
|
|
|
|
func getServerResourceGroupVersion(apiResourceListGroupVersion, apiResourceGroup, apiResourceVersion string) string {
|
|
|
|
var serverResourceGroupVersion string
|
|
|
|
if apiResourceGroup == "" && apiResourceVersion == "" {
|
|
|
|
serverResourceGroupVersion = apiResourceListGroupVersion
|
|
|
|
} else {
|
|
|
|
serverResourceGroupVersion = schema.GroupVersion{
|
|
|
|
Group: apiResourceGroup,
|
|
|
|
Version: apiResourceVersion,
|
|
|
|
}.String()
|
|
|
|
}
|
|
|
|
return serverResourceGroupVersion
|
2022-05-10 01:50:50 +00:00
|
|
|
}
|