stop pretending that we have statically known shortcuts

pull/8/head
David Eads 2018-05-07 15:25:40 -04:00
parent 8d064823bb
commit 682e028516
13 changed files with 24 additions and 226 deletions

View File

@ -98,7 +98,6 @@ go_library(
"generate.go",
"history.go",
"interfaces.go",
"kubectl.go",
"namespace.go",
"pdb.go",
"priorityclass.go",

View File

@ -359,7 +359,7 @@ func TestRunApplyViewLastApplied(t *testing.T) {
expectedErr: "error: Unexpected -o output mode: wide, the flag 'output' must be one of yaml|json\nSee 'view-last-applied -h' for help and examples.",
expectedOut: "",
selector: "",
args: []string{"rc", "test-rc"},
args: []string{"replicationcontroller", "test-rc"},
respBytes: rcBytesWithConfig,
},
{
@ -369,7 +369,7 @@ func TestRunApplyViewLastApplied(t *testing.T) {
expectedErr: "",
expectedOut: "test: 1234\n",
selector: "name=test-rc",
args: []string{"rc"},
args: []string{"replicationcontroller"},
respBytes: rcBytesWithConfig,
},
{
@ -379,7 +379,7 @@ func TestRunApplyViewLastApplied(t *testing.T) {
expectedErr: "error: no last-applied-configuration annotation found on resource: test-rc",
expectedOut: "",
selector: "",
args: []string{"rc", "test-rc"},
args: []string{"replicationcontroller", "test-rc"},
respBytes: rcBytes,
},
{
@ -389,7 +389,7 @@ func TestRunApplyViewLastApplied(t *testing.T) {
expectedErr: "Error from server (NotFound): the server could not find the requested resource (get replicationcontrollers no-match)",
expectedOut: "",
selector: "",
args: []string{"rc", "no-match"},
args: []string{"replicationcontroller", "no-match"},
respBytes: nil,
},
}

View File

@ -95,7 +95,6 @@ func NewCmdAutoscale(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *
o := NewAutoscaleOptions(ioStreams)
validArgs := []string{"deployment", "replicaset", "replicationcontroller"}
argAliases := kubectl.ResourceAliases(validArgs)
cmd := &cobra.Command{
Use: "autoscale (-f FILENAME | TYPE NAME | TYPE/NAME) [--min=MINPODS] --max=MAXPODS [--cpu-percent=CPU]",
@ -108,8 +107,7 @@ func NewCmdAutoscale(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.Run())
},
ValidArgs: validArgs,
ArgAliases: argAliases,
ValidArgs: validArgs,
}
// bind flag structs

View File

@ -136,8 +136,7 @@ func NewCmdExposeService(f cmdutil.Factory, streams genericclioptions.IOStreams)
cmdutil.CheckErr(o.Complete(f, cmd))
cmdutil.CheckErr(o.RunExpose(cmd, args))
},
ValidArgs: validArgs,
ArgAliases: kubectl.ResourceAliases(validArgs),
ValidArgs: validArgs,
}
o.RecordFlags.AddFlags(cmd)

View File

@ -21,7 +21,6 @@ import (
"io"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
@ -46,7 +45,6 @@ func NewCmdRolloutHistory(f cmdutil.Factory, out io.Writer) *cobra.Command {
options := &resource.FilenameOptions{}
validArgs := []string{"deployment", "daemonset", "statefulset"}
argAliases := kubectl.ResourceAliases(validArgs)
cmd := &cobra.Command{
Use: "history (TYPE NAME | TYPE/NAME) [flags]",
@ -57,8 +55,7 @@ func NewCmdRolloutHistory(f cmdutil.Factory, out io.Writer) *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(RunHistory(f, cmd, out, args, options))
},
ValidArgs: validArgs,
ArgAliases: argAliases,
ValidArgs: validArgs,
}
cmd.Flags().Int64("revision", 0, "See the details, including podTemplate of the revision specified")

View File

@ -24,7 +24,6 @@ import (
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/cmd/set"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
@ -69,7 +68,6 @@ func NewCmdRolloutPause(f cmdutil.Factory, streams genericclioptions.IOStreams)
}
validArgs := []string{"deployment"}
argAliases := kubectl.ResourceAliases(validArgs)
cmd := &cobra.Command{
Use: "pause RESOURCE",
@ -89,8 +87,7 @@ func NewCmdRolloutPause(f cmdutil.Factory, streams genericclioptions.IOStreams)
}
cmdutil.CheckErr(utilerrors.Flatten(utilerrors.NewAggregate(allErrs)))
},
ValidArgs: validArgs,
ArgAliases: argAliases,
ValidArgs: validArgs,
}
usage := "identifying the resource to get from a server."

View File

@ -24,7 +24,6 @@ import (
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/cmd/set"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
@ -67,7 +66,6 @@ func NewCmdRolloutResume(f cmdutil.Factory, streams genericclioptions.IOStreams)
}
validArgs := []string{"deployment"}
argAliases := kubectl.ResourceAliases(validArgs)
cmd := &cobra.Command{
Use: "resume RESOURCE",
@ -87,8 +85,7 @@ func NewCmdRolloutResume(f cmdutil.Factory, streams genericclioptions.IOStreams)
}
cmdutil.CheckErr(utilerrors.Flatten(utilerrors.NewAggregate(allErrs)))
},
ValidArgs: validArgs,
ArgAliases: argAliases,
ValidArgs: validArgs,
}
usage := "identifying the resource to get from a server."

View File

@ -77,7 +77,6 @@ func NewCmdRolloutStatus(f cmdutil.Factory, streams genericclioptions.IOStreams)
o := NewRolloutStatusOptions(streams)
validArgs := []string{"deployment", "daemonset", "statefulset"}
argAliases := kubectl.ResourceAliases(validArgs)
cmd := &cobra.Command{
Use: "status (TYPE NAME | TYPE/NAME) [flags]",
@ -90,8 +89,7 @@ func NewCmdRolloutStatus(f cmdutil.Factory, streams genericclioptions.IOStreams)
cmdutil.CheckErr(o.Validate(cmd, args))
cmdutil.CheckErr(o.Run())
},
ValidArgs: validArgs,
ArgAliases: argAliases,
ValidArgs: validArgs,
}
usage := "identifying the resource to get from a server."

View File

@ -68,7 +68,6 @@ func NewCmdRolloutUndo(f cmdutil.Factory, out io.Writer) *cobra.Command {
}
validArgs := []string{"deployment", "daemonset", "statefulset"}
argAliases := kubectl.ResourceAliases(validArgs)
cmd := &cobra.Command{
Use: "undo (TYPE NAME | TYPE/NAME) [flags]",
@ -88,8 +87,7 @@ func NewCmdRolloutUndo(f cmdutil.Factory, out io.Writer) *cobra.Command {
}
cmdutil.CheckErr(utilerrors.Flatten(utilerrors.NewAggregate(allErrs)))
},
ValidArgs: validArgs,
ArgAliases: argAliases,
ValidArgs: validArgs,
}
cmd.Flags().Int64("to-revision", 0, "The revision to rollback to. Default to 0 (last revision).")

View File

@ -114,7 +114,6 @@ func NewCmdScale(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobr
o := NewScaleOptions(ioStreams)
validArgs := []string{"deployment", "replicaset", "replicationcontroller", "statefulset"}
argAliases := kubectl.ResourceAliases(validArgs)
cmd := &cobra.Command{
Use: "scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME)",
@ -127,8 +126,7 @@ func NewCmdScale(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobr
cmdutil.CheckErr(o.Validate(cmd))
cmdutil.CheckErr(o.RunScale())
},
ValidArgs: validArgs,
ArgAliases: argAliases,
ValidArgs: validArgs,
}
o.RecordFlags.AddFlags(cmd)

View File

@ -32,7 +32,6 @@ import (
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
@ -93,7 +92,6 @@ func NewCmdTaint(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.
}
validArgs := []string{"node"}
argAliases := kubectl.ResourceAliases(validArgs)
cmd := &cobra.Command{
Use: "taint NODE NAME KEY_1=VAL_1:TAINT_EFFECT_1 ... KEY_N=VAL_N:TAINT_EFFECT_N",
@ -112,8 +110,7 @@ func NewCmdTaint(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.
cmdutil.CheckErr(err)
}
},
ValidArgs: validArgs,
ArgAliases: argAliases,
ValidArgs: validArgs,
}
options.PrintFlags.AddFlags(cmd)
@ -212,7 +209,7 @@ func (o TaintOptions) validateFlags() error {
// Validate checks to the TaintOptions to see if there is sufficient information run the command.
func (o TaintOptions) Validate() error {
resourceType := strings.ToLower(o.resources[0])
validResources, isValidResource := append(kubectl.ResourceAliases([]string{"node"}), "node"), false
validResources, isValidResource := []string{"node", "nodes"}, false
for _, validResource := range validResources {
if resourceType == validResource {
isValidResource = true

View File

@ -25,7 +25,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/kubernetes/pkg/kubectl"
)
// shortcutExpander is a RESTMapper that can be used for Kubernetes resources. It expands the resource first, then invokes the wrapped
@ -73,8 +72,8 @@ func (e shortcutExpander) RESTMappings(gk schema.GroupKind, versions ...string)
// First the list of potential resources will be taken from the API server.
// Next we will append the hardcoded list of resources - to be backward compatible with old servers.
// NOTE that the list is ordered by group priority.
func (e shortcutExpander) getShortcutMappings() ([]*metav1.APIResourceList, []kubectl.ResourceShortcuts, error) {
res := []kubectl.ResourceShortcuts{}
func (e shortcutExpander) getShortcutMappings() ([]*metav1.APIResourceList, []resourceShortcuts, error) {
res := []resourceShortcuts{}
// get server resources
// This can return an error *and* the results it was able to find. We don't need to fail on the error.
apiResList, err := e.discoveryClient.ServerResources()
@ -89,7 +88,7 @@ func (e shortcutExpander) getShortcutMappings() ([]*metav1.APIResourceList, []ku
}
for _, apiRes := range apiResources.APIResources {
for _, shortName := range apiRes.ShortNames {
rs := kubectl.ResourceShortcuts{
rs := resourceShortcuts{
ShortForm: schema.GroupResource{Group: gv.Group, Resource: shortName},
LongForm: schema.GroupResource{Group: gv.Group, Resource: apiRes.Name},
}
@ -98,8 +97,6 @@ func (e shortcutExpander) getShortcutMappings() ([]*metav1.APIResourceList, []ku
}
}
// append hardcoded short forms at the end of the list
res = append(res, kubectl.ResourcesShortcutStatic...)
return apiResList, res, nil
}
@ -158,3 +155,10 @@ func (e shortcutExpander) expandResourceShortcut(resource schema.GroupVersionRes
return resource
}
// ResourceShortcuts represents a structure that holds the information how to
// transition from resource's shortcut to its full name.
type resourceShortcuts struct {
ShortForm schema.GroupResource
LongForm schema.GroupResource
}

View File

@ -1,184 +0,0 @@
/*
Copyright 2014 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.
*/
// A set of common functions needed by cmd/kubectl and pkg/kubectl packages.
package kubectl
import (
"strings"
"k8s.io/apimachinery/pkg/runtime/schema"
)
type NamespaceInfo struct {
Namespace string
}
// ResourceShortcuts represents a structure that holds the information how to
// transition from resource's shortcut to its full name.
type ResourceShortcuts struct {
ShortForm schema.GroupResource
LongForm schema.GroupResource
}
// ResourcesShortcutStatic is the list of short names to their expanded names.
// Note that the list is ordered by group.
var ResourcesShortcutStatic = []ResourceShortcuts{
// If you add an entry here, please also take a look at pkg/kubectl/cmd/cmd.go
// and add an entry to valid_resources when appropriate.
{
ShortForm: schema.GroupResource{Resource: "cm"},
LongForm: schema.GroupResource{Resource: "configmaps"},
},
{
ShortForm: schema.GroupResource{Resource: "cs"},
LongForm: schema.GroupResource{Resource: "componentstatuses"},
},
{
ShortForm: schema.GroupResource{Resource: "ep"},
LongForm: schema.GroupResource{Resource: "endpoints"},
},
{
ShortForm: schema.GroupResource{Resource: "ev"},
LongForm: schema.GroupResource{Resource: "events"},
},
{
ShortForm: schema.GroupResource{Resource: "limits"},
LongForm: schema.GroupResource{Resource: "limitranges"},
},
{
ShortForm: schema.GroupResource{Resource: "no"},
LongForm: schema.GroupResource{Resource: "nodes"},
},
{
ShortForm: schema.GroupResource{Resource: "ns"},
LongForm: schema.GroupResource{Resource: "namespaces"},
},
{
ShortForm: schema.GroupResource{Resource: "po"},
LongForm: schema.GroupResource{Resource: "pods"},
},
{
ShortForm: schema.GroupResource{Resource: "pvc"},
LongForm: schema.GroupResource{Resource: "persistentvolumeclaims"},
},
{
ShortForm: schema.GroupResource{Resource: "pv"},
LongForm: schema.GroupResource{Resource: "persistentvolumes"},
},
{
ShortForm: schema.GroupResource{Resource: "quota"},
LongForm: schema.GroupResource{Resource: "resourcequotas"},
},
{
ShortForm: schema.GroupResource{Resource: "rc"},
LongForm: schema.GroupResource{Resource: "replicationcontrollers"},
},
{
ShortForm: schema.GroupResource{Resource: "rs"},
LongForm: schema.GroupResource{Resource: "replicasets"},
},
{
ShortForm: schema.GroupResource{Resource: "sa"},
LongForm: schema.GroupResource{Resource: "serviceaccounts"},
},
{
ShortForm: schema.GroupResource{Resource: "svc"},
LongForm: schema.GroupResource{Resource: "services"},
},
{
ShortForm: schema.GroupResource{Group: "autoscaling", Resource: "hpa"},
LongForm: schema.GroupResource{Group: "autoscaling", Resource: "horizontalpodautoscalers"},
},
{
ShortForm: schema.GroupResource{Group: "certificates.k8s.io", Resource: "csr"},
LongForm: schema.GroupResource{Group: "certificates.k8s.io", Resource: "certificatesigningrequests"},
},
{
ShortForm: schema.GroupResource{Group: "policy", Resource: "pdb"},
LongForm: schema.GroupResource{Group: "policy", Resource: "poddisruptionbudgets"},
},
{
ShortForm: schema.GroupResource{Group: "extensions", Resource: "deploy"},
LongForm: schema.GroupResource{Group: "extensions", Resource: "deployments"},
},
{
ShortForm: schema.GroupResource{Group: "extensions", Resource: "ds"},
LongForm: schema.GroupResource{Group: "extensions", Resource: "daemonsets"},
},
{
ShortForm: schema.GroupResource{Group: "extensions", Resource: "hpa"},
LongForm: schema.GroupResource{Group: "extensions", Resource: "horizontalpodautoscalers"},
},
{
ShortForm: schema.GroupResource{Group: "extensions", Resource: "ing"},
LongForm: schema.GroupResource{Group: "extensions", Resource: "ingresses"},
},
{
ShortForm: schema.GroupResource{Group: "extensions", Resource: "netpol"},
LongForm: schema.GroupResource{Group: "extensions", Resource: "networkpolicies"},
},
{
ShortForm: schema.GroupResource{Group: "extensions", Resource: "psp"},
LongForm: schema.GroupResource{Group: "extensions", Resource: "podSecurityPolicies"},
},
}
// ResourceShortFormFor looks up for a short form of resource names.
// TODO: Change the signature of this function so that it can
// make use of ResourceShortcuts.
func ResourceShortFormFor(resource string) (string, bool) {
var alias string
exists := false
for _, item := range ResourcesShortcutStatic {
if item.LongForm.Resource == resource {
alias = item.ShortForm.Resource
exists = true
break
}
}
return alias, exists
}
// ResourceAliases returns the resource shortcuts and plural forms for the given resources.
func ResourceAliases(rs []string) []string {
as := make([]string, 0, len(rs))
plurals := make(map[string]struct{}, len(rs))
for _, r := range rs {
var plural string
switch {
case r == "endpoints":
// Endpoints type itself is plural, unlike every other resource.
plural = r
case strings.HasSuffix(r, "y"):
plural = r[0:len(r)-1] + "ies"
case strings.HasSuffix(r, "s"):
plural = r + "es"
default:
plural = r + "s"
}
as = append(as, plural)
plurals[plural] = struct{}{}
}
for _, item := range ResourcesShortcutStatic {
if _, found := plurals[item.LongForm.Resource]; found {
as = append(as, item.ShortForm.Resource)
}
}
return as
}