Skip to content

Commit 7814f3a

Browse files
authored
Merge pull request kubernetes#88723 from wawa0210/kubectl_taint_short_forms
Support kubectl more flexible matching method to match whether the current resource type is node
2 parents 885e1e9 + 8fda1f3 commit 7814f3a

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/taint/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ go_library(
2323
"//staging/src/k8s.io/cli-runtime/pkg/printers:go_default_library",
2424
"//staging/src/k8s.io/cli-runtime/pkg/resource:go_default_library",
2525
"//staging/src/k8s.io/kubectl/pkg/cmd/util:go_default_library",
26+
"//staging/src/k8s.io/kubectl/pkg/explain:go_default_library",
2627
"//staging/src/k8s.io/kubectl/pkg/scheme:go_default_library",
2728
"//staging/src/k8s.io/kubectl/pkg/util/i18n:go_default_library",
2829
"//staging/src/k8s.io/kubectl/pkg/util/templates:go_default_library",

staging/src/k8s.io/kubectl/pkg/cmd/taint/taint.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/spf13/cobra"
2525
"k8s.io/klog"
26+
"k8s.io/kubectl/pkg/explain"
2627

2728
v1 "k8s.io/api/core/v1"
2829
"k8s.io/apimachinery/pkg/api/meta"
@@ -59,6 +60,8 @@ type TaintOptions struct {
5960
ClientForMapping func(*meta.RESTMapping) (resource.RESTClient, error)
6061

6162
genericclioptions.IOStreams
63+
64+
Mapper meta.RESTMapper
6265
}
6366

6467
var (
@@ -129,6 +132,11 @@ func (o *TaintOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
129132
return err
130133
}
131134

135+
o.Mapper, err = f.ToRESTMapper()
136+
if err != nil {
137+
return err
138+
}
139+
132140
o.DryRunStrategy, err = cmdutil.GetDryRunStrategy(cmd)
133141
if err != nil {
134142
return err
@@ -219,15 +227,18 @@ func (o TaintOptions) validateFlags() error {
219227
// Validate checks to the TaintOptions to see if there is sufficient information run the command.
220228
func (o TaintOptions) Validate() error {
221229
resourceType := strings.ToLower(o.resources[0])
222-
validResources, isValidResource := []string{"node", "nodes"}, false
223-
for _, validResource := range validResources {
224-
if resourceType == validResource {
225-
isValidResource = true
226-
break
227-
}
230+
fullySpecifiedGVR, _, err := explain.SplitAndParseResourceRequest(resourceType, o.Mapper)
231+
if err != nil {
232+
return err
233+
}
234+
235+
gvk, err := o.Mapper.KindFor(fullySpecifiedGVR)
236+
if err != nil {
237+
return err
228238
}
229-
if !isValidResource {
230-
return fmt.Errorf("invalid resource type %s, only %q are supported", o.resources[0], validResources)
239+
240+
if gvk.Kind != "Node" {
241+
return fmt.Errorf("invalid resource type %s, only node types are supported", resourceType)
231242
}
232243

233244
// check the format of taint args and checks removed taints aren't in the new taints list

0 commit comments

Comments
 (0)