2016-09-15 12:52:42 +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 util
|
|
|
|
|
|
|
|
import (
|
2016-07-14 11:48:32 +00:00
|
|
|
"strings"
|
|
|
|
|
2016-09-15 12:52:42 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/meta"
|
2016-07-14 11:48:32 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/typed/discovery"
|
2016-09-15 12:52:42 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl"
|
2016-11-21 02:55:31 +00:00
|
|
|
"k8s.io/kubernetes/pkg/runtime/schema"
|
2016-09-15 12:52:42 +00:00
|
|
|
)
|
|
|
|
|
2016-10-24 10:58:47 +00:00
|
|
|
// ShortcutExpander is a RESTMapper that can be used for Kubernetes resources. It expands the resource first, then invokes the wrapped
|
2016-09-15 12:52:42 +00:00
|
|
|
type ShortcutExpander struct {
|
|
|
|
RESTMapper meta.RESTMapper
|
|
|
|
|
2016-11-21 02:55:31 +00:00
|
|
|
All []schema.GroupResource
|
2016-07-14 11:48:32 +00:00
|
|
|
|
2016-10-24 10:58:47 +00:00
|
|
|
discoveryClient discovery.DiscoveryInterface
|
2016-09-15 12:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ meta.RESTMapper = &ShortcutExpander{}
|
|
|
|
|
2016-10-24 10:58:47 +00:00
|
|
|
func NewShortcutExpander(delegate meta.RESTMapper, client discovery.DiscoveryInterface) ShortcutExpander {
|
2016-07-14 11:48:32 +00:00
|
|
|
return ShortcutExpander{All: userResources, RESTMapper: delegate, discoveryClient: client}
|
|
|
|
}
|
|
|
|
|
2016-11-21 02:55:31 +00:00
|
|
|
func (e ShortcutExpander) getAll() []schema.GroupResource {
|
2016-07-14 11:48:32 +00:00
|
|
|
if e.discoveryClient == nil {
|
|
|
|
return e.All
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have access to server resources
|
|
|
|
apiResources, err := e.discoveryClient.ServerResources()
|
|
|
|
if err != nil {
|
|
|
|
return e.All
|
|
|
|
}
|
|
|
|
|
2016-11-21 02:55:31 +00:00
|
|
|
availableResources := []schema.GroupVersionResource{}
|
2016-07-14 11:48:32 +00:00
|
|
|
for groupVersionString, resourceList := range apiResources {
|
2016-11-21 02:55:31 +00:00
|
|
|
currVersion, err := schema.ParseGroupVersion(groupVersionString)
|
2016-07-14 11:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return e.All
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, resource := range resourceList.APIResources {
|
|
|
|
availableResources = append(availableResources, currVersion.WithResource(resource.Name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-21 02:55:31 +00:00
|
|
|
availableAll := []schema.GroupResource{}
|
2016-07-14 11:48:32 +00:00
|
|
|
for _, requestedResource := range e.All {
|
|
|
|
for _, availableResource := range availableResources {
|
|
|
|
if requestedResource.Group == availableResource.Group &&
|
|
|
|
requestedResource.Resource == availableResource.Resource {
|
|
|
|
availableAll = append(availableAll, requestedResource)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return availableAll
|
2016-09-15 12:52:42 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 02:55:31 +00:00
|
|
|
func (e ShortcutExpander) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
|
2016-09-15 12:52:42 +00:00
|
|
|
return e.RESTMapper.KindFor(expandResourceShortcut(resource))
|
|
|
|
}
|
|
|
|
|
2016-11-21 02:55:31 +00:00
|
|
|
func (e ShortcutExpander) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
|
2016-09-15 12:52:42 +00:00
|
|
|
return e.RESTMapper.KindsFor(expandResourceShortcut(resource))
|
|
|
|
}
|
|
|
|
|
2016-11-21 02:55:31 +00:00
|
|
|
func (e ShortcutExpander) ResourcesFor(resource schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
|
2016-09-15 12:52:42 +00:00
|
|
|
return e.RESTMapper.ResourcesFor(expandResourceShortcut(resource))
|
|
|
|
}
|
|
|
|
|
2016-11-21 02:55:31 +00:00
|
|
|
func (e ShortcutExpander) ResourceFor(resource schema.GroupVersionResource) (schema.GroupVersionResource, error) {
|
2016-09-15 12:52:42 +00:00
|
|
|
return e.RESTMapper.ResourceFor(expandResourceShortcut(resource))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ShortcutExpander) ResourceSingularizer(resource string) (string, error) {
|
2016-11-21 02:55:31 +00:00
|
|
|
return e.RESTMapper.ResourceSingularizer(expandResourceShortcut(schema.GroupVersionResource{Resource: resource}).Resource)
|
2016-09-15 12:52:42 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 02:55:31 +00:00
|
|
|
func (e ShortcutExpander) RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error) {
|
2016-09-15 12:52:42 +00:00
|
|
|
return e.RESTMapper.RESTMapping(gk, versions...)
|
|
|
|
}
|
|
|
|
|
2016-11-21 02:55:31 +00:00
|
|
|
func (e ShortcutExpander) RESTMappings(gk schema.GroupKind) ([]*meta.RESTMapping, error) {
|
2016-09-15 12:52:42 +00:00
|
|
|
return e.RESTMapper.RESTMappings(gk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// userResources are the resource names that apply to the primary, user facing resources used by
|
|
|
|
// client tools. They are in deletion-first order - dependent resources should be last.
|
2016-11-21 02:55:31 +00:00
|
|
|
var userResources = []schema.GroupResource{
|
2016-07-14 11:48:32 +00:00
|
|
|
{Group: "", Resource: "pods"},
|
|
|
|
{Group: "", Resource: "replicationcontrollers"},
|
|
|
|
{Group: "", Resource: "services"},
|
2016-10-26 20:44:07 +00:00
|
|
|
{Group: "apps", Resource: "statefulsets"},
|
2016-07-14 11:48:32 +00:00
|
|
|
{Group: "autoscaling", Resource: "horizontalpodautoscalers"},
|
|
|
|
{Group: "extensions", Resource: "jobs"},
|
|
|
|
{Group: "extensions", Resource: "deployments"},
|
|
|
|
{Group: "extensions", Resource: "replicasets"},
|
|
|
|
}
|
2016-09-15 12:52:42 +00:00
|
|
|
|
|
|
|
// AliasesForResource returns whether a resource has an alias or not
|
|
|
|
func (e ShortcutExpander) AliasesForResource(resource string) ([]string, bool) {
|
2016-07-14 11:48:32 +00:00
|
|
|
if strings.ToLower(resource) == "all" {
|
2016-11-21 02:55:31 +00:00
|
|
|
var resources []schema.GroupResource
|
2016-07-14 11:48:32 +00:00
|
|
|
if resources = e.getAll(); len(resources) == 0 {
|
|
|
|
resources = userResources
|
|
|
|
}
|
|
|
|
aliases := []string{}
|
|
|
|
for _, r := range resources {
|
|
|
|
aliases = append(aliases, r.Resource)
|
|
|
|
}
|
|
|
|
return aliases, true
|
2016-09-15 12:52:42 +00:00
|
|
|
}
|
2016-11-21 02:55:31 +00:00
|
|
|
expanded := expandResourceShortcut(schema.GroupVersionResource{Resource: resource}).Resource
|
2016-09-15 12:52:42 +00:00
|
|
|
return []string{expanded}, (expanded != resource)
|
|
|
|
}
|
|
|
|
|
|
|
|
// expandResourceShortcut will return the expanded version of resource
|
|
|
|
// (something that a pkg/api/meta.RESTMapper can understand), if it is
|
|
|
|
// indeed a shortcut. Otherwise, will return resource unmodified.
|
2016-11-21 02:55:31 +00:00
|
|
|
func expandResourceShortcut(resource schema.GroupVersionResource) schema.GroupVersionResource {
|
2016-09-15 12:52:42 +00:00
|
|
|
if expanded, ok := kubectl.ShortForms[resource.Resource]; ok {
|
|
|
|
resource.Resource = expanded
|
|
|
|
return resource
|
|
|
|
}
|
|
|
|
return resource
|
|
|
|
}
|