Merge pull request #1457 from skriss/shortcuts-perf-issue

shortcut expander: use discovery helper's cached resource list
This commit is contained in:
Nolan Brubaker
2019-05-07 15:57:12 -04:00
committed by GitHub
2 changed files with 32 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
/*
Copyright 2017 the Velero contributors.
Copyright 2017, 2019 the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -105,12 +105,6 @@ func (h *helper) Refresh() error {
if err != nil {
return errors.WithStack(err)
}
mapper := restmapper.NewDiscoveryRESTMapper(groupResources)
shortcutExpander, err := kcmdutil.NewShortcutExpander(mapper, h.discoveryClient, h.logger)
if err != nil {
return errors.WithStack(err)
}
h.mapper = shortcutExpander
preferredResources, err := refreshServerPreferredResources(h.discoveryClient, h.logger)
if err != nil {
@@ -124,6 +118,12 @@ func (h *helper) Refresh() error {
sortResources(h.resources)
shortcutExpander, err := kcmdutil.NewShortcutExpander(restmapper.NewDiscoveryRESTMapper(groupResources), h.resources, h.logger)
if err != nil {
return errors.WithStack(err)
}
h.mapper = shortcutExpander
h.resourcesMap = make(map[schema.GroupVersionResource]metav1.APIResource)
for _, resourceGroup := range h.resources {
gv, err := schema.ParseGroupVersion(resourceGroup.GroupVersion)

View File

@@ -1,6 +1,8 @@
/*
Copyright 2016 The Kubernetes Authors.
Modifications Copyright 2019 the Velero contributors.
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
@@ -17,13 +19,12 @@ limitations under the License.
package util
import (
"errors"
"strings"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
)
// ResourceShortcuts represents a structure that holds the information how to
@@ -37,17 +38,14 @@ type ResourceShortcuts struct {
type shortcutExpander struct {
RESTMapper meta.RESTMapper
discoveryClient discovery.DiscoveryInterface
logger logrus.FieldLogger
resources []*metav1.APIResourceList
logger logrus.FieldLogger
}
var _ meta.RESTMapper = &shortcutExpander{}
func NewShortcutExpander(delegate meta.RESTMapper, client discovery.DiscoveryInterface, logger logrus.FieldLogger) (shortcutExpander, error) {
if client == nil {
return shortcutExpander{}, errors.New("Please provide discovery client to shortcut expander")
}
return shortcutExpander{RESTMapper: delegate, discoveryClient: client}, nil
func NewShortcutExpander(delegate meta.RESTMapper, resources []*metav1.APIResourceList, logger logrus.FieldLogger) (shortcutExpander, error) {
return shortcutExpander{RESTMapper: delegate, resources: resources, logger: logger}, nil
}
func (e shortcutExpander) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
@@ -88,28 +86,24 @@ func (e shortcutExpander) getShortcutMappings() ([]ResourceShortcuts, error) {
haveNetpol := false
res := []ResourceShortcuts{}
// get server resources
apiResList, err := e.discoveryClient.ServerResources()
if err == nil {
for _, apiResources := range apiResList {
for _, apiRes := range apiResources.APIResources {
for _, shortName := range apiRes.ShortNames {
gv, err := schema.ParseGroupVersion(apiResources.GroupVersion)
if err != nil {
e.logger.WithError(err).WithField("groupVersion", apiResources.GroupVersion).Error("Unable to parse groupversion")
continue
}
rs := ResourceShortcuts{
ShortForm: schema.GroupResource{Group: gv.Group, Resource: shortName},
LongForm: schema.GroupResource{Group: gv.Group, Resource: apiRes.Name},
}
res = append(res, rs)
if shortName == "svc" {
haveSvc = true
}
if shortName == "netpol" {
haveNetpol = true
}
for _, apiResources := range e.resources {
for _, apiRes := range apiResources.APIResources {
for _, shortName := range apiRes.ShortNames {
gv, err := schema.ParseGroupVersion(apiResources.GroupVersion)
if err != nil {
e.logger.WithError(err).WithField("groupVersion", apiResources.GroupVersion).Error("Unable to parse groupversion")
continue
}
rs := ResourceShortcuts{
ShortForm: schema.GroupResource{Group: gv.Group, Resource: shortName},
LongForm: schema.GroupResource{Group: gv.Group, Resource: apiRes.Name},
}
res = append(res, rs)
if shortName == "svc" {
haveSvc = true
}
if shortName == "netpol" {
haveNetpol = true
}
}
}