mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 01:40:07 +00:00
Change default output of 'label' command
Using simple, human understandable output instead of the form of 'kubectl get' command.
This commit is contained in:
parent
36987e9dce
commit
ddf090d824
@ -173,7 +173,7 @@ func RunLabel(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri
|
||||
return err
|
||||
}
|
||||
|
||||
labels, remove, err := parseLabels(labelArgs)
|
||||
lbls, remove, err := parseLabels(labelArgs)
|
||||
if err != nil {
|
||||
return cmdutil.UsageError(cmd, err.Error())
|
||||
}
|
||||
@ -204,7 +204,7 @@ func RunLabel(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri
|
||||
return err
|
||||
}
|
||||
obj, err := cmdutil.UpdateObject(info, func(obj runtime.Object) error {
|
||||
err := labelFunc(obj, overwrite, resourceVersion, labels, remove)
|
||||
err := labelFunc(obj, overwrite, resourceVersion, lbls, remove)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -214,10 +214,16 @@ func RunLabel(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri
|
||||
return err
|
||||
}
|
||||
|
||||
printer, err := f.PrinterForMapping(cmd, info.Mapping, false)
|
||||
if err != nil {
|
||||
return err
|
||||
outputFormat := cmdutil.GetFlagString(cmd, "output")
|
||||
if outputFormat == "" {
|
||||
cmdutil.PrintSuccess(mapper, false, out, info.Mapping.Resource, info.Name, "labeled")
|
||||
} else {
|
||||
printer, err := f.PrinterForMapping(cmd, info.Mapping, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return printer.PrintObj(obj, out)
|
||||
}
|
||||
return printer.PrintObj(obj, out)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
@ -325,10 +325,7 @@ func TestLabelErrors(t *testing.T) {
|
||||
|
||||
func TestLabelForResourceFromFile(t *testing.T) {
|
||||
pods, _, _ := testData()
|
||||
|
||||
f, tf, codec := NewAPIFactory()
|
||||
tf.Printer = &testPrinter{}
|
||||
|
||||
tf.Client = &client.FakeRESTClient{
|
||||
Codec: codec,
|
||||
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
||||
@ -355,7 +352,6 @@ func TestLabelForResourceFromFile(t *testing.T) {
|
||||
}
|
||||
}),
|
||||
}
|
||||
|
||||
tf.Namespace = "test"
|
||||
tf.ClientConfig = &client.Config{Version: testapi.Version()}
|
||||
|
||||
@ -363,25 +359,18 @@ func TestLabelForResourceFromFile(t *testing.T) {
|
||||
cmd := NewCmdLabel(f, buf)
|
||||
cmd.Flags().Set("filename", "../../../examples/cassandra/cassandra.yaml")
|
||||
|
||||
cmd.SetOutput(buf)
|
||||
err := RunLabel(f, buf, cmd, []string{"a=b"})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if tf.Printer.(*testPrinter).Objects == nil {
|
||||
t.Errorf("unexpected print to default printer")
|
||||
}
|
||||
if !reflect.DeepEqual(tf.Printer.(*testPrinter).Objects[0].(*api.Pod).Labels, map[string]string{"a": "b"}) {
|
||||
t.Errorf("did not set labels: %#v", string(buf.Bytes()))
|
||||
if !strings.Contains(buf.String(), "labeled") {
|
||||
t.Errorf("did not set labels: %s", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLabelMultipleObjects(t *testing.T) {
|
||||
pods, _, _ := testData()
|
||||
|
||||
f, tf, codec := NewAPIFactory()
|
||||
tf.Printer = &testPrinter{}
|
||||
tf.Client = &client.FakeRESTClient{
|
||||
Codec: codec,
|
||||
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
||||
@ -412,19 +401,15 @@ func TestLabelMultipleObjects(t *testing.T) {
|
||||
}
|
||||
tf.Namespace = "test"
|
||||
tf.ClientConfig = &client.Config{Version: testapi.Version()}
|
||||
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
|
||||
cmd := NewCmdLabel(f, buf)
|
||||
|
||||
cmd.Flags().Set("all", "true")
|
||||
|
||||
if err := RunLabel(f, buf, cmd, []string{"pods", "a=b"}); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if tf.Printer.(*testPrinter).Objects == nil {
|
||||
t.Errorf("unexpected non print to default printer")
|
||||
}
|
||||
if !reflect.DeepEqual(tf.Printer.(*testPrinter).Objects[0].(*api.Pod).Labels, map[string]string{"a": "b"}) {
|
||||
t.Errorf("did not set labels: %#v", string(buf.Bytes()))
|
||||
if strings.Count(buf.String(), "labeled") != len(pods.Items) {
|
||||
t.Errorf("not all labels are set: %s", buf.String())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user