collapse printing paths

This commit is contained in:
David Eads 2018-02-20 20:14:21 -05:00
parent 799a0bf410
commit be04e7c1b1
95 changed files with 594 additions and 1055 deletions

View File

@ -3804,7 +3804,7 @@ run_cmd_with_img_tests() {
# Test that a valid image reference value is provided as the value of --image in `kubectl run <name> --image`
output_message=$(kubectl run test1 --image=validname)
kube::test::if_has_string "${output_message}" 'deployments "test1" created'
kube::test::if_has_string "${output_message}" 'deployment.apps "test1" created'
kubectl delete deployments test1
# test invalid image name
output_message=$(! kubectl run test2 --image=InvalidImageName 2>&1)
@ -4482,7 +4482,7 @@ __EOF__
kube::test::if_has_string "${response}" 'must provide one or more resources'
# test=label matches our node
response=$(kubectl cordon --selector test=label)
kube::test::if_has_string "${response}" 'nodes "127.0.0.1" cordoned'
kube::test::if_has_string "${response}" 'node "127.0.0.1" cordoned'
# invalid=label does not match any nodes
response=$(kubectl cordon --selector invalid=label)
kube::test::if_has_not_string "${response}" 'cordoned'

View File

@ -216,6 +216,7 @@ go_test(
"//pkg/apis/core:go_default_library",
"//pkg/apis/extensions:go_default_library",
"//pkg/kubectl:go_default_library",
"//pkg/kubectl/cmd/resource:go_default_library",
"//pkg/kubectl/cmd/testing:go_default_library",
"//pkg/kubectl/cmd/util:go_default_library",
"//pkg/kubectl/cmd/util/openapi:go_default_library",
@ -225,7 +226,6 @@ go_test(
"//pkg/kubectl/util/i18n:go_default_library",
"//pkg/kubectl/util/term:go_default_library",
"//pkg/printers:go_default_library",
"//pkg/printers/internalversion:go_default_library",
"//pkg/util/strings:go_default_library",
"//vendor/github.com/googleapis/gnostic/OpenAPIv2:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library",
@ -235,6 +235,7 @@ go_test(
"//vendor/k8s.io/api/policy/v1beta1:go_default_library",
"//vendor/k8s.io/api/rbac/v1:go_default_library",
"//vendor/k8s.io/api/rbac/v1beta1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/equality:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library",

View File

@ -267,9 +267,9 @@ func (o AnnotateOptions) RunAnnotate(f cmdutil.Factory, cmd *cobra.Command) erro
}
if len(o.outputFormat) > 0 {
return f.PrintObject(cmd, outputObj, o.out)
return cmdutil.PrintObject(cmd, outputObj, o.out)
}
f.PrintSuccess(false, o.out, info.Mapping.Resource, info.Name, o.dryrun, "annotated")
cmdutil.PrintSuccess(false, o.out, info.Object, o.dryrun, "annotated")
return nil
})
}

View File

@ -416,7 +416,6 @@ func TestAnnotateErrors(t *testing.T) {
for k, testCase := range testCases {
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Namespace = "test"
tf.ClientConfig = defaultClientConfig()
@ -436,9 +435,6 @@ func TestAnnotateErrors(t *testing.T) {
t.Errorf("%s: unexpected error: %v", k, err)
continue
}
if tf.Printer.(*testPrinter).Objects != nil {
t.Errorf("unexpected print to default printer")
}
if buf.Len() > 0 {
t.Errorf("buffer should be empty: %s", string(buf.Bytes()))
}
@ -449,7 +445,6 @@ func TestAnnotateObject(t *testing.T) {
pods, _, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Group: "testgroup", Version: "v1"},
NegotiatedSerializer: unstructuredSerializer,
@ -500,7 +495,6 @@ func TestAnnotateObjectFromFile(t *testing.T) {
pods, _, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Group: "testgroup", Version: "v1"},
NegotiatedSerializer: unstructuredSerializer,
@ -581,7 +575,6 @@ func TestAnnotateMultipleObjects(t *testing.T) {
pods, _, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Group: "testgroup", Version: "v1"},
NegotiatedSerializer: unstructuredSerializer,

View File

@ -298,9 +298,9 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti
count++
if len(output) > 0 && !shortOutput {
return f.PrintResourceInfoForCommand(cmd, info, out)
return cmdutil.PrintObject(cmd, info.Object, out)
}
f.PrintSuccess(shortOutput, out, info.Mapping.Resource, info.Name, dryRun, "created")
cmdutil.PrintSuccess(shortOutput, out, info.Object, dryRun, "created")
return nil
}
@ -345,15 +345,15 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti
if string(patchBytes) == "{}" {
count++
f.PrintSuccess(shortOutput, out, info.Mapping.Resource, info.Name, false, "unchanged")
cmdutil.PrintSuccess(shortOutput, out, info.Object, false, "unchanged")
return nil
}
}
count++
if len(output) > 0 && !shortOutput {
return f.PrintResourceInfoForCommand(cmd, info, out)
return cmdutil.PrintObject(cmd, info.Object, out)
}
f.PrintSuccess(shortOutput, out, info.Mapping.Resource, info.Name, dryRun, "configured")
cmdutil.PrintSuccess(shortOutput, out, info.Object, dryRun, "configured")
return nil
})
@ -520,7 +520,7 @@ func (p *pruner) prune(f cmdutil.Factory, namespace string, mapping *meta.RESTMa
return err
}
}
f.PrintSuccess(shortOutput, p.out, mapping.Resource, name, p.dryRun, "pruned")
cmdutil.PrintSuccess(shortOutput, p.out, obj, p.dryRun, "pruned")
}
return nil
}

View File

@ -186,16 +186,16 @@ func (o *SetLastAppliedOptions) RunSetLastApplied(f cmdutil.Factory, cmd *cobra.
if len(o.Output) > 0 && !o.ShortOutput {
info.Refresh(patchedObj, false)
return f.PrintResourceInfoForCommand(cmd, info, o.Out)
return cmdutil.PrintObject(cmd, info.Object, o.Out)
}
f.PrintSuccess(o.ShortOutput, o.Out, info.Mapping.Resource, info.Name, o.DryRun, "configured")
cmdutil.PrintSuccess(o.ShortOutput, o.Out, info.Object, o.DryRun, "configured")
} else {
err := o.formatPrinter(o.Output, patch.Patch, o.Out)
if err != nil {
return err
}
f.PrintSuccess(o.ShortOutput, o.Out, info.Mapping.Resource, info.Name, o.DryRun, "configured")
cmdutil.PrintSuccess(o.ShortOutput, o.Out, info.Object, o.DryRun, "configured")
}
}
return nil

View File

@ -44,7 +44,6 @@ import (
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
"k8s.io/kubernetes/pkg/printers"
)
var (
@ -331,7 +330,6 @@ func TestRunApplyViewLastApplied(t *testing.T) {
}
for _, test := range tests {
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: unstructuredSerializer,
@ -387,7 +385,6 @@ func TestApplyObjectWithoutAnnotation(t *testing.T) {
pathRC := "/namespaces/test/replicationcontrollers/" + nameRC
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -415,7 +412,7 @@ func TestApplyObjectWithoutAnnotation(t *testing.T) {
cmd.Run(cmd, []string{})
// uses the name from the file, not the response
expectRC := "replicationcontrollers/" + nameRC + "\n"
expectRC := "replicationcontroller/" + nameRC + "\n"
expectWarning := fmt.Sprintf(warningNoLastAppliedConfigAnnotation, "kubectl")
if errBuf.String() != expectWarning {
t.Fatalf("unexpected non-warning: %s\nexpected: %s", errBuf.String(), expectWarning)
@ -432,7 +429,6 @@ func TestApplyObject(t *testing.T) {
for _, fn := range testingOpenAPISchemaFns {
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -461,7 +457,7 @@ func TestApplyObject(t *testing.T) {
cmd.Run(cmd, []string{})
// uses the name from the file, not the response
expectRC := "replicationcontrollers/" + nameRC + "\n"
expectRC := "replicationcontroller/" + nameRC + "\n"
if buf.String() != expectRC {
t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expectRC)
}
@ -494,7 +490,6 @@ func TestApplyObjectOutput(t *testing.T) {
for _, fn := range testingOpenAPISchemaFns {
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &printers.YAMLPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -522,8 +517,8 @@ func TestApplyObjectOutput(t *testing.T) {
cmd.Flags().Set("output", "yaml")
cmd.Run(cmd, []string{})
if !strings.Contains(buf.String(), "name: test-rc") {
t.Fatalf("unexpected output: %s\nexpected to contain: %s", buf.String(), "name: test-rc")
if !strings.Contains(buf.String(), "test-rc") {
t.Fatalf("unexpected output: %s\nexpected to contain: %s", buf.String(), "test-rc")
}
if !strings.Contains(buf.String(), "post-patch: value") {
t.Fatalf("unexpected output: %s\nexpected to contain: %s", buf.String(), "post-patch: value")
@ -544,7 +539,6 @@ func TestApplyRetry(t *testing.T) {
retry := false
getCount := 0
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -586,7 +580,7 @@ func TestApplyRetry(t *testing.T) {
}
// uses the name from the file, not the response
expectRC := "replicationcontrollers/" + nameRC + "\n"
expectRC := "replicationcontroller/" + nameRC + "\n"
if buf.String() != expectRC {
t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expectRC)
}
@ -602,7 +596,6 @@ func TestApplyNonExistObject(t *testing.T) {
pathNameRC := pathRC + "/" + nameRC
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -630,7 +623,7 @@ func TestApplyNonExistObject(t *testing.T) {
cmd.Run(cmd, []string{})
// uses the name from the file, not the response
expectRC := "replicationcontrollers/" + nameRC + "\n"
expectRC := "replicationcontroller/" + nameRC + "\n"
if buf.String() != expectRC {
t.Errorf("unexpected output: %s\nexpected: %s", buf.String(), expectRC)
}
@ -647,7 +640,6 @@ func TestApplyEmptyPatch(t *testing.T) {
var body []byte
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: unstructuredSerializer,
@ -683,7 +675,7 @@ func TestApplyEmptyPatch(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{})
expectRC := "replicationcontrollers/" + nameRC + "\n"
expectRC := "replicationcontroller/" + nameRC + "\n"
if buf.String() != expectRC {
t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expectRC)
}
@ -722,7 +714,6 @@ func testApplyMultipleObjects(t *testing.T, asList bool) {
for _, fn := range testingOpenAPISchemaFns {
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -764,8 +755,8 @@ func testApplyMultipleObjects(t *testing.T, asList bool) {
cmd.Run(cmd, []string{})
// Names should come from the REST response, NOT the files
expectRC := "replicationcontrollers/" + nameRC + "\n"
expectSVC := "services/" + nameSVC + "\n"
expectRC := "replicationcontroller/" + nameRC + "\n"
expectSVC := "service/" + nameSVC + "\n"
// Test both possible orders since output is non-deterministic.
expectOne := expectRC + expectSVC
expectTwo := expectSVC + expectRC
@ -855,7 +846,7 @@ func TestApplyNULLPreservation(t *testing.T) {
cmd.Run(cmd, []string{})
expected := "deployments/" + deploymentName + "\n"
expected := "deployment.extensions/" + deploymentName + "\n"
if buf.String() != expected {
t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expected)
}
@ -878,7 +869,6 @@ func TestUnstructuredApply(t *testing.T) {
for _, fn := range testingOpenAPISchemaFns {
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -918,7 +908,7 @@ func TestUnstructuredApply(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{})
expected := "widgets/" + name + "\n"
expected := "widget.unit-test.test.com/" + name + "\n"
if buf.String() != expected {
t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expected)
}
@ -946,7 +936,6 @@ func TestUnstructuredIdempotentApply(t *testing.T) {
for _, fn := range testingOpenAPISchemaFns {
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -1009,7 +998,7 @@ func TestUnstructuredIdempotentApply(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{})
expected := "widgets/widget\n"
expected := "widget.unit-test.test.com/widget\n"
if buf.String() != expected {
t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expected)
}
@ -1040,7 +1029,7 @@ func TestRunApplySetLastApplied(t *testing.T) {
name: "set with exist object",
filePath: filenameRC,
expectedErr: "",
expectedOut: "replicationcontrollers/test-rc\n",
expectedOut: "replicationcontroller/test-rc\n",
output: "name",
},
{
@ -1061,21 +1050,20 @@ func TestRunApplySetLastApplied(t *testing.T) {
name: "set with exist object output json",
filePath: filenameRCJSON,
expectedErr: "",
expectedOut: "replicationcontrollers/test-rc\n",
expectedOut: "replicationcontroller/test-rc\n",
output: "name",
},
{
name: "set test for a directory of files",
filePath: dirName,
expectedErr: "",
expectedOut: "replicationcontrollers/test-rc\nreplicationcontrollers/test-rc\n",
expectedOut: "replicationcontroller/test-rc\nreplicationcontroller/test-rc\n",
output: "name",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: unstructuredSerializer,
@ -1166,7 +1154,6 @@ func TestForceApply(t *testing.T) {
deleted := false
counts := map[string]int{}
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -1243,7 +1230,7 @@ func TestForceApply(t *testing.T) {
}
}
if expected := "replicationcontrollers/" + nameRC + "\n"; buf.String() != expected {
if expected := "replicationcontroller/" + nameRC + "\n"; buf.String() != expected {
t.Fatalf("unexpected output: %s\nexpected: %s", buf.String(), expected)
}
if errBuf.String() != "" {

View File

@ -119,9 +119,9 @@ func (o *ReconcileOptions) Complete(cmd *cobra.Command, f cmdutil.Factory, args
shortOutput := output == "name"
o.Print = func(info *resource.Info) error {
if len(output) > 0 && !shortOutput {
return f.PrintResourceInfoForCommand(cmd, info, o.Out)
return cmdutil.PrintObject(cmd, info.Object, o.Out)
}
f.PrintSuccess(shortOutput, o.Out, info.Mapping.Resource, info.Name, dryRun, "reconciled")
cmdutil.PrintSuccess(shortOutput, o.Out, info.Object, dryRun, "reconciled")
return nil
}

View File

@ -156,7 +156,7 @@ func RunAutoscale(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
object = hpa.Object
}
if cmdutil.GetDryRunFlag(cmd) {
return f.PrintObject(cmd, object, out)
return cmdutil.PrintObject(cmd, object, out)
}
if err := kubectl.CreateOrUpdateAnnotation(cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag), hpa, f.JSONEncoder()); err != nil {
@ -170,10 +170,10 @@ func RunAutoscale(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
count++
if len(cmdutil.GetFlagString(cmd, "output")) > 0 {
return f.PrintObject(cmd, object, out)
return cmdutil.PrintObject(cmd, object, out)
}
f.PrintSuccess(false, out, info.Mapping.Resource, info.Name, cmdutil.GetDryRunFlag(cmd), "autoscaled")
cmdutil.PrintSuccess(false, out, info.Object, cmdutil.GetDryRunFlag(cmd), "autoscaled")
return nil
})
if err != nil {

View File

@ -191,7 +191,7 @@ func (options *CertificateOptions) modifyCertificateCondition(f cmdutil.Factory,
return err
}
found++
f.PrintSuccess(options.outputStyle == "name", out, info.Mapping.Resource, info.Name, false, verb)
cmdutil.PrintSuccess(options.outputStyle == "name", out, info.Object, false, verb)
return nil
})
if found == 0 {

View File

@ -39,11 +39,11 @@ import (
"k8s.io/kubernetes/pkg/api/testapi"
apitesting "k8s.io/kubernetes/pkg/api/testing"
api "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/kubectl/cmd/resource"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/scheme"
"k8s.io/kubernetes/pkg/printers"
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
"k8s.io/kubernetes/pkg/util/strings"
)
@ -175,85 +175,8 @@ func stringBody(body string) io.ReadCloser {
return ioutil.NopCloser(bytes.NewReader([]byte(body)))
}
// TODO(jlowdermilk): refactor the Factory so we can test client versions properly,
// with different client/server version skew scenarios.
// Verify that resource.RESTClients constructed from a factory respect mapping.APIVersion
//func TestClientVersions(t *testing.T) {
// f := cmdutil.NewFactory(nil)
//
// version := testapi.Default.Version()
// mapping := &meta.RESTMapping{
// APIVersion: version,
// }
// c, err := f.ClientForMapping(mapping)
// if err != nil {
// t.Errorf("unexpected error: %v", err)
// }
// client := c.(*client.RESTClient)
// if client.APIVersion() != version {
// t.Errorf("unexpected Client APIVersion: %s %v", client.APIVersion, client)
// }
//}
func Example_printReplicationControllerWithNamespace() {
f, tf, _, ns := cmdtesting.NewAPIFactory()
p := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{
WithNamespace: true,
ColumnLabels: []string{},
})
printersinternal.AddHandlers(p)
tf.Printer = p
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
}
cmd := NewCmdRun(f, os.Stdin, os.Stdout, os.Stderr)
ctrl := &api.ReplicationController{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "beep",
Labels: map[string]string{"foo": "bar"},
CreationTimestamp: metav1.Time{Time: time.Now().AddDate(-10, 0, 0)},
},
Spec: api.ReplicationControllerSpec{
Replicas: 1,
Selector: map[string]string{"foo": "bar"},
Template: &api.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"foo": "bar"},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "foo",
Image: "someimage",
},
},
},
},
},
Status: api.ReplicationControllerStatus{
Replicas: 1,
ReadyReplicas: 1,
},
}
err := f.PrintObject(cmd, ctrl, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
// Output:
// NAMESPACE NAME DESIRED CURRENT READY AGE
// beep foo 1 1 1 10y
}
func Example_printMultiContainersReplicationControllerWithWide() {
f, tf, _, ns := cmdtesting.NewAPIFactory()
p := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{
Wide: true,
ColumnLabels: []string{},
})
printersinternal.AddHandlers(p)
tf.Printer = p
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
@ -290,7 +213,8 @@ func Example_printMultiContainersReplicationControllerWithWide() {
Replicas: 1,
},
}
err := f.PrintObject(cmd, ctrl, os.Stdout)
cmd.Flags().Set("output", "wide")
err := cmdutil.PrintObject(cmd, ctrl, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
@ -301,11 +225,6 @@ func Example_printMultiContainersReplicationControllerWithWide() {
func Example_printReplicationController() {
f, tf, _, ns := cmdtesting.NewAPIFactory()
p := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{
ColumnLabels: []string{},
})
printersinternal.AddHandlers(p)
tf.Printer = p
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
@ -342,7 +261,7 @@ func Example_printReplicationController() {
Replicas: 1,
},
}
err := f.PrintObject(cmd, ctrl, os.Stdout)
err := cmdutil.PrintObject(cmd, ctrl, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
@ -353,12 +272,6 @@ func Example_printReplicationController() {
func Example_printPodWithWideFormat() {
f, tf, _, ns := cmdtesting.NewAPIFactory()
p := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{
Wide: true,
ColumnLabels: []string{},
})
printersinternal.AddHandlers(p)
tf.Printer = p
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
@ -383,7 +296,8 @@ func Example_printPodWithWideFormat() {
PodIP: "10.1.1.3",
},
}
err := f.PrintObject(cmd, pod, os.Stdout)
cmd.Flags().Set("output", "wide")
err := cmdutil.PrintObject(cmd, pod, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
@ -394,12 +308,6 @@ func Example_printPodWithWideFormat() {
func Example_printPodWithShowLabels() {
f, tf, _, ns := cmdtesting.NewAPIFactory()
p := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{
ShowLabels: true,
ColumnLabels: []string{},
})
printersinternal.AddHandlers(p)
tf.Printer = p
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
@ -427,7 +335,8 @@ func Example_printPodWithShowLabels() {
},
},
}
err := f.PrintObject(cmd, pod, os.Stdout)
cmd.Flags().Set("show-labels", "true")
err := cmdutil.PrintObject(cmd, pod, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
@ -530,11 +439,6 @@ func newAllPhasePodList() *api.PodList {
func Example_printPodHideTerminated() {
f, tf, _, ns := cmdtesting.NewAPIFactory()
p := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{
ColumnLabels: []string{},
})
printersinternal.AddHandlers(p)
tf.Printer = p
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
@ -548,8 +452,12 @@ func Example_printPodHideTerminated() {
if errs != nil {
fmt.Printf("Unexpected filter error: %v\n", errs)
}
printer, err := cmdutil.PrinterForOptions(cmdutil.ExtractCmdPrintOptions(cmd, false))
if err != nil {
fmt.Printf("Unexpected printer get error: %v\n", errs)
}
for _, pod := range filteredPodList {
err := f.PrintObject(cmd, pod, os.Stdout)
err := printer.PrintObj(pod, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
@ -563,19 +471,13 @@ func Example_printPodHideTerminated() {
func Example_printPodShowAll() {
f, tf, _, ns := cmdtesting.NewAPIFactory()
p := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{
ShowAll: true,
ColumnLabels: []string{},
})
printersinternal.AddHandlers(p)
tf.Printer = p
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
}
cmd := NewCmdRun(f, os.Stdin, os.Stdout, os.Stderr)
podList := newAllPhasePodList()
err := f.PrintObject(cmd, podList, os.Stdout)
err := cmdutil.PrintObject(cmd, podList, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
@ -588,19 +490,13 @@ func Example_printPodShowAll() {
// test5 1/2 Unknown 6 10y
}
func Example_printServiceWithNamespacesAndLabels() {
func Example_printServiceWithLabels() {
f, tf, _, ns := cmdtesting.NewAPIFactory()
p := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{
WithNamespace: true,
ColumnLabels: []string{"l1"},
})
printersinternal.AddHandlers(p)
tf.Printer = p
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
}
cmd := NewCmdRun(f, os.Stdin, os.Stdout, os.Stderr)
cmd := resource.NewCmdGet(f, os.Stdout, os.Stderr)
svc := &api.ServiceList{
Items: []api.Service{
{
@ -650,14 +546,15 @@ func Example_printServiceWithNamespacesAndLabels() {
}
ld := strings.NewLineDelimiter(os.Stdout, "|")
defer ld.Flush()
err := f.PrintObject(cmd, svc, ld)
cmd.Flags().Set("label-columns", "l1")
err := cmdutil.PrintObject(cmd, svc, ld)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
// Output:
// |NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE L1|
// |ns1 svc1 ClusterIP 10.1.1.1 <none> 53/UDP,53/TCP 10y value|
// |ns2 svc2 ClusterIP 10.1.1.2 <none> 80/TCP,8080/TCP 10y dolla-bill-yall|
// |NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE L1|
// |svc1 ClusterIP 10.1.1.1 <none> 53/UDP,53/TCP 10y value|
// |svc2 ClusterIP 10.1.1.2 <none> 80/TCP,8080/TCP 10y dolla-bill-yall|
// ||
}

View File

@ -80,7 +80,7 @@ func NewCmdConfigView(f cmdutil.Factory, out, errOut io.Writer, ConfigAccess cli
}
printOpts := cmdutil.ExtractCmdPrintOptions(cmd, false)
printer, err := f.PrinterForOptions(printOpts)
printer, err := cmdutil.PrinterForOptions(printOpts)
cmdutil.CheckErr(err)
cmdutil.CheckErr(options.Run(out, printer))

View File

@ -156,7 +156,7 @@ func (o *ConvertOptions) Complete(f cmdutil.Factory, out io.Writer, cmd *cobra.C
cmd.Flags().Set("output", outputFormat)
}
o.encoder = f.JSONEncoder()
o.printer, err = f.PrinterForOptions(cmdutil.ExtractCmdPrintOptions(cmd, false))
o.printer, err = cmdutil.PrinterForOptions(cmdutil.ExtractCmdPrintOptions(cmd, false))
return err
}

View File

@ -24,7 +24,6 @@ import (
"k8s.io/client-go/rest/fake"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
"k8s.io/kubernetes/pkg/printers"
)
type testcase struct {
@ -116,8 +115,7 @@ func TestConvertObject(t *testing.T) {
cmd.Flags().Set("filename", tc.file)
cmd.Flags().Set("output-version", tc.outputVersion)
cmd.Flags().Set("local", "true")
cmd.Flags().Set("output", "go-template")
tf.Printer, _ = printers.NewTemplatePrinter([]byte(field.template))
cmd.Flags().Set("output", "go-template="+field.template)
cmd.Run(cmd, []string{})
if buf.String() != field.expected {
t.Errorf("unexpected output when converting %s to %q, expected: %q, but got %q", tc.file, tc.outputVersion, field.expected, buf.String())

View File

@ -211,13 +211,13 @@ func (o *CreateOptions) RunCreate(f cmdutil.Factory, cmd *cobra.Command) error {
shortOutput := output == "name"
if len(output) > 0 && !shortOutput {
return f.PrintResourceInfoForCommand(cmd, info, o.Out)
return cmdutil.PrintObject(cmd, info.Object, o.Out)
}
if !shortOutput {
f.PrintObjectSpecificMessage(info.Object, o.Out)
}
f.PrintSuccess(shortOutput, o.Out, info.Mapping.Resource, info.Name, dryRun, "created")
cmdutil.PrintSuccess(shortOutput, o.Out, info.Object, dryRun, "created")
return nil
})
if err != nil {
@ -356,9 +356,9 @@ func RunCreateSubcommand(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, o
}
if useShortOutput := options.OutputFormat == "name"; useShortOutput || len(options.OutputFormat) == 0 {
f.PrintSuccess(useShortOutput, out, mapping.Resource, info.Name, options.DryRun, "created")
cmdutil.PrintSuccess(useShortOutput, out, info.Object, options.DryRun, "created")
return nil
}
return f.PrintObject(cmd, obj, out)
return cmdutil.PrintObject(cmd, obj, out)
}

View File

@ -173,7 +173,7 @@ func (c *CreateClusterRoleOptions) RunCreateRole() error {
}
if useShortOutput := c.OutputFormat == "name"; useShortOutput || len(c.OutputFormat) == 0 {
c.PrintSuccess(useShortOutput, c.Out, "clusterroles", c.Name, c.DryRun, "created")
cmdutil.PrintSuccess(useShortOutput, c.Out, clusterRole, c.DryRun, "created")
return nil
}

View File

@ -18,44 +18,21 @@ package cmd
import (
"bytes"
"io"
"reflect"
"testing"
rbac "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest/fake"
"k8s.io/kubernetes/pkg/api/legacyscheme"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
)
type testClusterRolePrinter struct {
CachedClusterRole *rbac.ClusterRole
}
func (t *testClusterRolePrinter) PrintObj(obj runtime.Object, out io.Writer) error {
t.CachedClusterRole = obj.(*rbac.ClusterRole)
return nil
}
func (t *testClusterRolePrinter) AfterPrint(output io.Writer, res string) error {
return nil
}
func (t *testClusterRolePrinter) HandledResources() []string {
return []string{}
}
func (t *testClusterRolePrinter) IsGeneric() bool {
return true
}
func TestCreateClusterRole(t *testing.T) {
clusterRoleName := "my-cluster-role"
f, tf, _, _ := cmdtesting.NewAPIFactory()
printer := &testClusterRolePrinter{}
tf.Printer = printer
tf.Namespace = "test"
tf.Client = &fake.RESTClient{}
tf.ClientConfig = defaultClientConfig()
@ -150,7 +127,7 @@ func TestCreateClusterRole(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
cmd := NewCmdCreateClusterRole(f, buf)
cmd.Flags().Set("dry-run", "true")
cmd.Flags().Set("output", "object")
cmd.Flags().Set("output", "yaml")
cmd.Flags().Set("verb", test.verbs)
cmd.Flags().Set("resource", test.resources)
cmd.Flags().Set("non-resource-url", test.nonResourceURL)
@ -158,15 +135,19 @@ func TestCreateClusterRole(t *testing.T) {
cmd.Flags().Set("resource-name", test.resourceNames)
}
cmd.Run(cmd, []string{clusterRoleName})
if !reflect.DeepEqual(test.expectedClusterRole, printer.CachedClusterRole) {
t.Errorf("%s:\nexpected:\n%#v\nsaw:\n%#v", name, test.expectedClusterRole, printer.CachedClusterRole)
actual := &rbac.ClusterRole{}
if err := runtime.DecodeInto(legacyscheme.Codecs.UniversalDecoder(), buf.Bytes(), actual); err != nil {
t.Log(string(buf.Bytes()))
t.Fatal(err)
}
if !equality.Semantic.DeepEqual(test.expectedClusterRole, actual) {
t.Errorf("%s:\nexpected:\n%#v\nsaw:\n%#v", name, test.expectedClusterRole, actual)
}
}
}
func TestClusterRoleValidate(t *testing.T) {
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Namespace = "test"
tests := map[string]struct {

View File

@ -73,7 +73,6 @@ func TestCreateClusterRoleBinding(t *testing.T) {
decoder := ns.DecoderToVersion(info.Serializer, groupVersion)
tf.Namespace = "test"
tf.Printer = &testPrinter{}
tf.Client = &ClusterRoleBindingRESTClient{
RESTClient: &fake.RESTClient{
NegotiatedSerializer: ns,
@ -107,7 +106,7 @@ func TestCreateClusterRoleBinding(t *testing.T) {
},
}
expectedOutput := "clusterrolebindings/" + expectBinding.Name + "\n"
expectedOutput := "clusterrolebinding.rbac.authorization.k8s.io/" + expectBinding.Name + "\n"
buf := bytes.NewBuffer([]byte{})
cmd := NewCmdCreateClusterRoleBinding(f, buf)
cmd.Flags().Set("clusterrole", "fake-clusterrole")

View File

@ -31,7 +31,6 @@ func TestCreateConfigMap(t *testing.T) {
configMap := &v1.ConfigMap{}
configMap.Name = "my-configmap"
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Group: "", Version: "v1"},
NegotiatedSerializer: ns,
@ -50,7 +49,7 @@ func TestCreateConfigMap(t *testing.T) {
cmd := NewCmdCreateConfigMap(f, buf)
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{configMap.Name})
expectedOutput := "configmaps/" + configMap.Name + "\n"
expectedOutput := "configmap/" + configMap.Name + "\n"
if buf.String() != expectedOutput {
t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
}

View File

@ -84,7 +84,6 @@ func TestCreateDeployment(t *testing.T) {
}),
}
tf.ClientConfig = &restclient.Config{}
tf.Printer = &testPrinter{}
tf.Namespace = "test"
buf := bytes.NewBuffer([]byte{})
@ -93,7 +92,7 @@ func TestCreateDeployment(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Flags().Set("image", "hollywood/jonny.depp:v2")
cmd.Run(cmd, []string{depName})
expectedOutput := "deployments/" + depName + "\n"
expectedOutput := "deployment.extensions/" + depName + "\n"
if buf.String() != expectedOutput {
t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
}
@ -112,7 +111,6 @@ func TestCreateDeploymentNoImage(t *testing.T) {
}),
}
tf.ClientConfig = &restclient.Config{}
tf.Printer = &testPrinter{}
tf.Namespace = "test"
buf := bytes.NewBuffer([]byte{})

View File

@ -31,7 +31,6 @@ func TestCreateNamespace(t *testing.T) {
namespaceObject := &v1.Namespace{}
namespaceObject.Name = "my-namespace"
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: ns,
@ -49,7 +48,7 @@ func TestCreateNamespace(t *testing.T) {
cmd := NewCmdCreateNamespace(f, buf)
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{namespaceObject.Name})
expectedOutput := "namespaces/" + namespaceObject.Name + "\n"
expectedOutput := "namespace/" + namespaceObject.Name + "\n"
if buf.String() != expectedOutput {
t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
}

View File

@ -42,7 +42,6 @@ func TestCreatePdb(t *testing.T) {
}),
}
tf.ClientConfig = &restclient.Config{}
tf.Printer = &testPrinter{}
tf.Namespace = "test"
buf := bytes.NewBuffer([]byte{})
@ -52,7 +51,7 @@ func TestCreatePdb(t *testing.T) {
cmd.Flags().Set("dry-run", "true")
cmd.Flags().Set("output", "name")
CreatePodDisruptionBudget(f, buf, cmd, []string{pdbName})
expectedOutput := "poddisruptionbudgets/" + pdbName + "\n"
expectedOutput := "poddisruptionbudget.policy/" + pdbName + "\n"
if buf.String() != expectedOutput {
t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
}

View File

@ -42,7 +42,6 @@ func TestCreatePriorityClass(t *testing.T) {
}),
}
tf.ClientConfig = &restclient.Config{}
tf.Printer = &testPrinter{}
buf := bytes.NewBuffer([]byte{})
cmd := NewCmdCreatePriorityClass(f, buf)
@ -52,7 +51,7 @@ func TestCreatePriorityClass(t *testing.T) {
cmd.Flags().Set("dry-run", "true")
cmd.Flags().Set("output", "name")
CreatePriorityClass(f, buf, cmd, []string{pcName})
expectedOutput := "priorityclasses/" + pcName + "\n"
expectedOutput := "priorityclass.scheduling.k8s.io/" + pcName + "\n"
if buf.String() != expectedOutput {
t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
}

View File

@ -31,7 +31,6 @@ func TestCreateQuota(t *testing.T) {
resourceQuotaObject := &v1.ResourceQuota{}
resourceQuotaObject.Name = "my-quota"
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: ns,
@ -53,19 +52,19 @@ func TestCreateQuota(t *testing.T) {
}{
"single resource": {
flags: []string{"--hard=cpu=1"},
expectedOutput: "resourcequotas/" + resourceQuotaObject.Name + "\n",
expectedOutput: "resourcequota/" + resourceQuotaObject.Name + "\n",
},
"single resource with a scope": {
flags: []string{"--hard=cpu=1", "--scopes=BestEffort"},
expectedOutput: "resourcequotas/" + resourceQuotaObject.Name + "\n",
expectedOutput: "resourcequota/" + resourceQuotaObject.Name + "\n",
},
"multiple resources": {
flags: []string{"--hard=cpu=1,pods=42", "--scopes=BestEffort"},
expectedOutput: "resourcequotas/" + resourceQuotaObject.Name + "\n",
expectedOutput: "resourcequota/" + resourceQuotaObject.Name + "\n",
},
"single resource with multiple scopes": {
flags: []string{"--hard=cpu=1", "--scopes=BestEffort,NotTerminating"},
expectedOutput: "resourcequotas/" + resourceQuotaObject.Name + "\n",
expectedOutput: "resourcequota/" + resourceQuotaObject.Name + "\n",
},
}
for name, test := range tests {

View File

@ -112,7 +112,6 @@ type CreateRoleOptions struct {
Mapper meta.RESTMapper
Out io.Writer
PrintObject func(obj runtime.Object) error
PrintSuccess func(shortOutput bool, out io.Writer, resource, name string, dryRun bool, operation string)
}
// Role is a command to ease creating Roles.
@ -163,7 +162,6 @@ func (c *CreateRoleOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args
}
}
c.Verbs = verbs
c.PrintSuccess = f.PrintSuccess
// Support resource.group pattern. If no API Group specified, use "" as core API Group.
// e.g. --resource=pods,deployments.extensions
@ -206,7 +204,7 @@ func (c *CreateRoleOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args
}
c.PrintObject = func(obj runtime.Object) error {
return f.PrintObject(cmd, obj, c.Out)
return cmdutil.PrintObject(cmd, obj, c.Out)
}
clientset, err := f.KubernetesClientSet()
@ -295,7 +293,7 @@ func (c *CreateRoleOptions) RunCreateRole() error {
}
if useShortOutput := c.OutputFormat == "name"; useShortOutput || len(c.OutputFormat) == 0 {
c.PrintSuccess(useShortOutput, c.Out, "roles", c.Name, c.DryRun, "created")
cmdutil.PrintSuccess(useShortOutput, c.Out, role, c.DryRun, "created")
return nil
}

View File

@ -18,45 +18,23 @@ package cmd
import (
"bytes"
"io"
"reflect"
"testing"
rbac "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/client-go/rest/fake"
"k8s.io/kubernetes/pkg/api/legacyscheme"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
)
type testRolePrinter struct {
CachedRole *rbac.Role
}
func (t *testRolePrinter) PrintObj(obj runtime.Object, out io.Writer) error {
t.CachedRole = obj.(*rbac.Role)
return nil
}
func (t *testRolePrinter) AfterPrint(output io.Writer, res string) error {
return nil
}
func (t *testRolePrinter) HandledResources() []string {
return []string{}
}
func (t *testRolePrinter) IsGeneric() bool {
return true
}
func TestCreateRole(t *testing.T) {
roleName := "my-role"
f, tf, _, _ := cmdtesting.NewAPIFactory()
printer := &testRolePrinter{}
tf.Printer = printer
tf.Namespace = "test"
tf.Client = &fake.RESTClient{}
tf.ClientConfig = defaultClientConfig()
@ -148,15 +126,20 @@ func TestCreateRole(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
cmd := NewCmdCreateRole(f, buf)
cmd.Flags().Set("dry-run", "true")
cmd.Flags().Set("output", "object")
cmd.Flags().Set("output", "yaml")
cmd.Flags().Set("verb", test.verbs)
cmd.Flags().Set("resource", test.resources)
if test.resourceNames != "" {
cmd.Flags().Set("resource-name", test.resourceNames)
}
cmd.Run(cmd, []string{roleName})
if !reflect.DeepEqual(test.expectedRole, printer.CachedRole) {
t.Errorf("%s", diff.ObjectReflectDiff(test.expectedRole, printer.CachedRole))
actual := &rbac.Role{}
if err := runtime.DecodeInto(legacyscheme.Codecs.UniversalDecoder(), buf.Bytes(), actual); err != nil {
t.Log(string(buf.Bytes()))
t.Fatal(err)
}
if !equality.Semantic.DeepEqual(test.expectedRole, actual) {
t.Errorf("%s", diff.ObjectReflectDiff(test.expectedRole, actual))
}
})
}
@ -164,7 +147,6 @@ func TestCreateRole(t *testing.T) {
func TestValidate(t *testing.T) {
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Namespace = "test"
tests := map[string]struct {
@ -364,7 +346,6 @@ func TestComplete(t *testing.T) {
roleName := "my-role"
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Namespace = "test"
tf.Client = &fake.RESTClient{}
tf.ClientConfig = defaultClientConfig()

View File

@ -75,7 +75,6 @@ func TestCreateRoleBinding(t *testing.T) {
decoder := ns.DecoderToVersion(info.Serializer, groupVersion)
tf.Namespace = "test"
tf.Printer = &testPrinter{}
tf.Client = &RoleBindingRESTClient{
RESTClient: &fake.RESTClient{
NegotiatedSerializer: ns,

View File

@ -36,7 +36,6 @@ func TestCreateSecretGeneric(t *testing.T) {
}
secretObject.Name = "my-secret"
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: ns,
@ -57,7 +56,7 @@ func TestCreateSecretGeneric(t *testing.T) {
cmd.Flags().Set("from-literal", "password=includes,comma")
cmd.Flags().Set("from-literal", "username=test_user")
cmd.Run(cmd, []string{secretObject.Name})
expectedOutput := "secrets/" + secretObject.Name + "\n"
expectedOutput := "secret/" + secretObject.Name + "\n"
if buf.String() != expectedOutput {
t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
}
@ -67,7 +66,6 @@ func TestCreateSecretDockerRegistry(t *testing.T) {
secretObject := &v1.Secret{}
secretObject.Name = "my-secret"
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: ns,
@ -89,7 +87,7 @@ func TestCreateSecretDockerRegistry(t *testing.T) {
cmd.Flags().Set("docker-email", "test-email")
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{secretObject.Name})
expectedOutput := "secrets/" + secretObject.Name + "\n"
expectedOutput := "secret/" + secretObject.Name + "\n"
if buf.String() != expectedOutput {
t.Errorf("expected output: %s, but got: %s", buf.String(), expectedOutput)
}

View File

@ -31,7 +31,6 @@ func TestCreateService(t *testing.T) {
service := &v1.Service{}
service.Name = "my-service"
f, tf, codec, negSer := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: negSer,
@ -51,7 +50,7 @@ func TestCreateService(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Flags().Set("tcp", "8080:8000")
cmd.Run(cmd, []string{service.Name})
expectedOutput := "services/" + service.Name + "\n"
expectedOutput := "service/" + service.Name + "\n"
if buf.String() != expectedOutput {
t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
}
@ -61,7 +60,6 @@ func TestCreateServiceNodePort(t *testing.T) {
service := &v1.Service{}
service.Name = "my-node-port-service"
f, tf, codec, negSer := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: negSer,
@ -81,7 +79,7 @@ func TestCreateServiceNodePort(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Flags().Set("tcp", "30000:8000")
cmd.Run(cmd, []string{service.Name})
expectedOutput := "services/" + service.Name + "\n"
expectedOutput := "service/" + service.Name + "\n"
if buf.String() != expectedOutput {
t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
}
@ -91,7 +89,6 @@ func TestCreateServiceExternalName(t *testing.T) {
service := &v1.Service{}
service.Name = "my-external-name-service"
f, tf, codec, negSer := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: negSer,
@ -111,7 +108,7 @@ func TestCreateServiceExternalName(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Flags().Set("external-name", "name")
cmd.Run(cmd, []string{service.Name})
expectedOutput := "services/" + service.Name + "\n"
expectedOutput := "service/" + service.Name + "\n"
if buf.String() != expectedOutput {
t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
}

View File

@ -31,7 +31,6 @@ func TestCreateServiceAccount(t *testing.T) {
serviceAccountObject := &v1.ServiceAccount{}
serviceAccountObject.Name = "my-service-account"
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: ns,
@ -50,7 +49,7 @@ func TestCreateServiceAccount(t *testing.T) {
cmd := NewCmdCreateServiceAccount(f, buf)
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{serviceAccountObject.Name})
expectedOutput := "serviceaccounts/" + serviceAccountObject.Name + "\n"
expectedOutput := "serviceaccount/" + serviceAccountObject.Name + "\n"
if buf.String() != expectedOutput {
t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
}

View File

@ -45,7 +45,6 @@ func TestCreateObject(t *testing.T) {
rc.Items[0].Name = "redis-master-controller"
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: unstructuredSerializer,
@ -69,7 +68,7 @@ func TestCreateObject(t *testing.T) {
cmd.Run(cmd, []string{})
// uses the name from the file, not the response
if buf.String() != "replicationcontrollers/redis-master-controller\n" {
if buf.String() != "replicationcontroller/redis-master-controller\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -79,7 +78,6 @@ func TestCreateMultipleObject(t *testing.T) {
_, svc, rc := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: unstructuredSerializer,
@ -106,7 +104,7 @@ func TestCreateMultipleObject(t *testing.T) {
cmd.Run(cmd, []string{})
// Names should come from the REST response, NOT the files
if buf.String() != "replicationcontrollers/rc1\nservices/baz\n" {
if buf.String() != "replicationcontroller/rc1\nservice/baz\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -117,7 +115,6 @@ func TestCreateDirectory(t *testing.T) {
rc.Items[0].Name = "name"
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: unstructuredSerializer,
@ -140,7 +137,7 @@ func TestCreateDirectory(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{})
if buf.String() != "replicationcontrollers/name\nreplicationcontrollers/name\nreplicationcontrollers/name\n" {
if buf.String() != "replicationcontroller/name\nreplicationcontroller/name\nreplicationcontroller/name\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}

View File

@ -19,6 +19,7 @@ package cmd
import (
"fmt"
"io"
"strings"
"time"
"github.com/spf13/cobra"
@ -225,12 +226,12 @@ func (o *DeleteOptions) RunDelete() error {
shortOutput := o.Output == "name"
// By default use a reaper to delete all related resources.
if o.Cascade {
return ReapResult(o.Result, o.f, o.Out, true, o.IgnoreNotFound, o.Timeout, o.GracePeriod, o.WaitForDeletion, shortOutput, o.Mapper, false)
return ReapResult(o.Result, o.f, o.Out, true, o.IgnoreNotFound, o.Timeout, o.GracePeriod, o.WaitForDeletion, shortOutput, false)
}
return DeleteResult(o.Result, o.f, o.Out, o.IgnoreNotFound, o.GracePeriod, shortOutput, o.Mapper)
return DeleteResult(o.Result, o.Out, o.IgnoreNotFound, o.GracePeriod, shortOutput)
}
func ReapResult(r *resource.Result, f cmdutil.Factory, out io.Writer, isDefaultDelete, ignoreNotFound bool, timeout time.Duration, gracePeriod int, waitForDeletion, shortOutput bool, mapper meta.RESTMapper, quiet bool) error {
func ReapResult(r *resource.Result, f cmdutil.Factory, out io.Writer, isDefaultDelete, ignoreNotFound bool, timeout time.Duration, gracePeriod int, waitForDeletion, shortOutput bool, quiet bool) error {
found := 0
if ignoreNotFound {
r = r.IgnoreErrors(errors.IsNotFound)
@ -245,7 +246,7 @@ func ReapResult(r *resource.Result, f cmdutil.Factory, out io.Writer, isDefaultD
// If there is no reaper for this resources and the user didn't explicitly ask for stop.
if kubectl.IsNoSuchReaperError(err) && isDefaultDelete {
// No client side reaper found. Let the server do cascading deletion.
return cascadingDeleteResource(info, f, out, shortOutput, mapper)
return cascadingDeleteResource(info, out, shortOutput)
}
return cmdutil.AddSourceToErr("reaping", info.Source, err)
}
@ -262,7 +263,7 @@ func ReapResult(r *resource.Result, f cmdutil.Factory, out io.Writer, isDefaultD
}
}
if !quiet {
f.PrintSuccess(shortOutput, out, info.Mapping.Resource, info.Name, false, "deleted")
printDeletion(info, out, shortOutput)
}
return nil
})
@ -275,7 +276,7 @@ func ReapResult(r *resource.Result, f cmdutil.Factory, out io.Writer, isDefaultD
return nil
}
func DeleteResult(r *resource.Result, f cmdutil.Factory, out io.Writer, ignoreNotFound bool, gracePeriod int, shortOutput bool, mapper meta.RESTMapper) error {
func DeleteResult(r *resource.Result, out io.Writer, ignoreNotFound bool, gracePeriod int, shortOutput bool) error {
found := 0
if ignoreNotFound {
r = r.IgnoreErrors(errors.IsNotFound)
@ -293,7 +294,7 @@ func DeleteResult(r *resource.Result, f cmdutil.Factory, out io.Writer, ignoreNo
options = metav1.NewDeleteOptions(int64(gracePeriod))
}
options.OrphanDependents = &orphan
return deleteResource(info, f, out, shortOutput, mapper, options)
return deleteResource(info, out, shortOutput, options)
})
if err != nil {
return err
@ -304,20 +305,40 @@ func DeleteResult(r *resource.Result, f cmdutil.Factory, out io.Writer, ignoreNo
return nil
}
func cascadingDeleteResource(info *resource.Info, f cmdutil.Factory, out io.Writer, shortOutput bool, mapper meta.RESTMapper) error {
func cascadingDeleteResource(info *resource.Info, out io.Writer, shortOutput bool) error {
falseVar := false
deleteOptions := &metav1.DeleteOptions{OrphanDependents: &falseVar}
return deleteResource(info, f, out, shortOutput, mapper, deleteOptions)
return deleteResource(info, out, shortOutput, deleteOptions)
}
func deleteResource(info *resource.Info, f cmdutil.Factory, out io.Writer, shortOutput bool, mapper meta.RESTMapper, deleteOptions *metav1.DeleteOptions) error {
func deleteResource(info *resource.Info, out io.Writer, shortOutput bool, deleteOptions *metav1.DeleteOptions) error {
if err := resource.NewHelper(info.Client, info.Mapping).DeleteWithOptions(info.Namespace, info.Name, deleteOptions); err != nil {
return cmdutil.AddSourceToErr("deleting", info.Source, err)
}
f.PrintSuccess(shortOutput, out, info.Mapping.Resource, info.Name, false, "deleted")
printDeletion(info, out, shortOutput)
return nil
}
// deletion printing is special because they don't have an object to print. This logic mirrors PrintSuccess
func printDeletion(info *resource.Info, out io.Writer, shortOutput bool) {
operation := "deleted"
groupKind := info.Mapping.GroupVersionKind
kindString := fmt.Sprintf("%s.%s", strings.ToLower(groupKind.Kind), groupKind.Group)
if len(groupKind.Group) == 0 {
kindString = strings.ToLower(groupKind.Kind)
}
if shortOutput {
// -o name: prints resource/name
fmt.Fprintf(out, "%s/%s\n", kindString, info.Name)
return
}
// understandable output by default
fmt.Fprintf(out, "%s \"%s\" %s\n", kindString, info.Name, operation)
}
// objectDeletionWaitInterval is the interval to wait between checks for deletion.
var objectDeletionWaitInterval = time.Second

View File

@ -55,7 +55,6 @@ func TestDeleteObjectByTuple(t *testing.T) {
_, _, rc := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -84,7 +83,7 @@ func TestDeleteObjectByTuple(t *testing.T) {
cmd.Flags().Set("cascade", "false")
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{"replicationcontrollers/redis-master-controller"})
if buf.String() != "replicationcontrollers/redis-master-controller\n" {
if buf.String() != "replicationcontroller/redis-master-controller\n" {
t.Errorf("unexpected output: %s", buf.String())
}
@ -94,7 +93,7 @@ func TestDeleteObjectByTuple(t *testing.T) {
cmd.Flags().Set("namespace", "test")
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{"secrets/mysecret"})
if buf.String() != "secrets/mysecret\n" {
if buf.String() != "secret/mysecret\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -118,7 +117,6 @@ func TestOrphanDependentsInDeleteObject(t *testing.T) {
_, _, rc := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
var expectedOrphanDependents *bool
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
@ -143,7 +141,7 @@ func TestOrphanDependentsInDeleteObject(t *testing.T) {
cmd.Flags().Set("namespace", "test")
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{"secrets/mysecret"})
if buf.String() != "secrets/mysecret\n" {
if buf.String() != "secret/mysecret\n" {
t.Errorf("unexpected output: %s", buf.String())
}
@ -156,7 +154,7 @@ func TestOrphanDependentsInDeleteObject(t *testing.T) {
cmd.Flags().Set("cascade", "false")
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{"secrets/mysecret"})
if buf.String() != "secrets/mysecret\n" {
if buf.String() != "secret/mysecret\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -167,7 +165,6 @@ func TestDeleteNamedObject(t *testing.T) {
_, _, rc := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -196,7 +193,7 @@ func TestDeleteNamedObject(t *testing.T) {
cmd.Flags().Set("cascade", "false")
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{"replicationcontrollers", "redis-master-controller"})
if buf.String() != "replicationcontrollers/redis-master-controller\n" {
if buf.String() != "replicationcontroller/redis-master-controller\n" {
t.Errorf("unexpected output: %s", buf.String())
}
@ -207,7 +204,7 @@ func TestDeleteNamedObject(t *testing.T) {
cmd.Flags().Set("cascade", "false")
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{"secrets", "mysecret"})
if buf.String() != "secrets/mysecret\n" {
if buf.String() != "secret/mysecret\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -217,7 +214,6 @@ func TestDeleteObject(t *testing.T) {
_, _, rc := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -240,7 +236,7 @@ func TestDeleteObject(t *testing.T) {
cmd.Run(cmd, []string{})
// uses the name from the file, not the response
if buf.String() != "replicationcontrollers/redis-master\n" {
if buf.String() != "replicationcontroller/redis-master\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -275,7 +271,6 @@ func TestDeleteObjectGraceZero(t *testing.T) {
objectDeletionWaitInterval = time.Millisecond
count := 0
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -307,10 +302,10 @@ func TestDeleteObjectGraceZero(t *testing.T) {
cmd := NewCmdDelete(fake, buf, errBuf)
cmd.Flags().Set("output", "name")
cmd.Flags().Set("grace-period", "0")
cmd.Run(cmd, []string{"pod/nginx"})
cmd.Run(cmd, []string{"pods/nginx"})
// uses the name from the file, not the response
if buf.String() != "pods/nginx\n" {
if buf.String() != "pod/nginx\n" {
t.Errorf("unexpected output: %s\n---\n%s", buf.String(), errBuf.String())
}
if reaper.deleteOptions == nil || reaper.deleteOptions.GracePeriodSeconds == nil || *reaper.deleteOptions.GracePeriodSeconds != 1 {
@ -324,7 +319,6 @@ func TestDeleteObjectGraceZero(t *testing.T) {
func TestDeleteObjectNotFound(t *testing.T) {
initTestErrorHandler(t)
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -361,7 +355,6 @@ func TestDeleteObjectNotFound(t *testing.T) {
func TestDeleteObjectIgnoreNotFound(t *testing.T) {
initTestErrorHandler(t)
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -398,7 +391,6 @@ func TestDeleteAllNotFound(t *testing.T) {
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -447,7 +439,6 @@ func TestDeleteAllIgnoreNotFound(t *testing.T) {
svc.Items = append(svc.Items, api.Service{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
notFoundError := &errors.NewNotFound(api.Resource("services"), "foo").ErrStatus
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -473,7 +464,7 @@ func TestDeleteAllIgnoreNotFound(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{"services"})
if buf.String() != "services/baz\n" {
if buf.String() != "service/baz\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -483,7 +474,6 @@ func TestDeleteMultipleObject(t *testing.T) {
_, svc, rc := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -508,7 +498,7 @@ func TestDeleteMultipleObject(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{})
if buf.String() != "replicationcontrollers/redis-master\nservices/frontend\n" {
if buf.String() != "replicationcontroller/redis-master\nservice/frontend\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -518,7 +508,6 @@ func TestDeleteMultipleObjectContinueOnMissing(t *testing.T) {
_, svc, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -553,7 +542,7 @@ func TestDeleteMultipleObjectContinueOnMissing(t *testing.T) {
t.Errorf("unexpected error: expected NotFound, got %v", err)
}
if buf.String() != "services/frontend\n" {
if buf.String() != "service/frontend\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -562,7 +551,6 @@ func TestDeleteMultipleResourcesWithTheSameName(t *testing.T) {
initTestErrorHandler(t)
_, svc, rc := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -590,7 +578,7 @@ func TestDeleteMultipleResourcesWithTheSameName(t *testing.T) {
cmd.Flags().Set("cascade", "false")
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{"replicationcontrollers,services", "baz", "foo"})
if buf.String() != "replicationcontrollers/baz\nreplicationcontrollers/foo\nservices/baz\nservices/foo\n" {
if buf.String() != "replicationcontroller/baz\nreplicationcontroller/foo\nservice/baz\nservice/foo\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -600,7 +588,6 @@ func TestDeleteDirectory(t *testing.T) {
_, _, rc := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -622,7 +609,7 @@ func TestDeleteDirectory(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{})
if buf.String() != "replicationcontrollers/frontend\nreplicationcontrollers/redis-master\nreplicationcontrollers/redis-slave\n" {
if buf.String() != "replicationcontroller/frontend\nreplicationcontroller/redis-master\nreplicationcontroller/redis-slave\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -632,7 +619,6 @@ func TestDeleteMultipleSelector(t *testing.T) {
pods, svc, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -666,7 +652,7 @@ func TestDeleteMultipleSelector(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{"pods,services"})
if buf.String() != "pods/foo\npods/bar\nservices/baz\n" {
if buf.String() != "pod/foo\npod/bar\nservice/baz\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -697,7 +683,6 @@ func TestResourceErrors(t *testing.T) {
for k, testCase := range testCases {
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Namespace = "test"
tf.ClientConfig = defaultClientConfig()
@ -715,9 +700,6 @@ func TestResourceErrors(t *testing.T) {
continue
}
if tf.Printer.(*testPrinter).Objects != nil {
t.Errorf("unexpected print to default printer")
}
if buf.Len() > 0 {
t.Errorf("buffer should be empty: %s", string(buf.Bytes()))
}

View File

@ -295,7 +295,7 @@ func (o *DrainOptions) RunDrain() error {
}
if err == nil || o.DryRun {
drainedNodes.Insert(info.Name)
o.Factory.PrintSuccess(false, o.Out, "node", info.Name, o.DryRun, "drained")
cmdutil.PrintSuccess(false, o.Out, info.Object, o.DryRun, "drained")
} else {
fmt.Fprintf(o.ErrOut, "error: unable to drain node %q, aborting command...\n\n", info.Name)
remainingNodes := []string{}
@ -616,7 +616,7 @@ func (o *DrainOptions) waitForDelete(pods []corev1.Pod, interval, timeout time.D
for i, pod := range pods {
p, err := getPodFn(pod.Namespace, pod.Name)
if apierrors.IsNotFound(err) || (p != nil && p.ObjectMeta.UID != pod.ObjectMeta.UID) {
o.Factory.PrintSuccess(false, o.Out, "pod", pod.Name, false, verbStr)
cmdutil.PrintSuccess(false, o.Out, &pod, false, verbStr)
continue
} else if err != nil {
return false, err
@ -697,7 +697,7 @@ func (o *DrainOptions) RunCordonOrUncordon(desired bool) error {
}
unsched := node.Spec.Unschedulable
if unsched == desired {
o.Factory.PrintSuccess(false, o.Out, nodeInfo.Mapping.Resource, nodeInfo.Name, o.DryRun, already(desired))
cmdutil.PrintSuccess(false, o.Out, nodeInfo.Object, o.DryRun, already(desired))
} else {
if !o.DryRun {
helper := resource.NewHelper(o.restClient, nodeInfo.Mapping)
@ -718,10 +718,10 @@ func (o *DrainOptions) RunCordonOrUncordon(desired bool) error {
continue
}
}
o.Factory.PrintSuccess(false, o.Out, nodeInfo.Mapping.Resource, nodeInfo.Name, o.DryRun, changed(desired))
cmdutil.PrintSuccess(false, o.Out, nodeInfo.Object, o.DryRun, changed(desired))
}
} else {
o.Factory.PrintSuccess(false, o.Out, nodeInfo.Mapping.Resource, nodeInfo.Name, o.DryRun, "skipped")
cmdutil.PrintSuccess(false, o.Out, nodeInfo.Object, o.DryRun, "skipped")
}
}

View File

@ -207,7 +207,6 @@ func TestEdit(t *testing.T) {
}
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClientForMappingFunc = func(mapping *meta.RESTMapping) (resource.RESTClient, error) {
versionedAPIPath := ""
if mapping.GroupVersionKind.Group == "" {

View File

@ -255,9 +255,9 @@ func RunExpose(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri
info.Refresh(object, true)
if cmdutil.GetDryRunFlag(cmd) {
if len(cmdutil.GetFlagString(cmd, "output")) > 0 {
return f.PrintObject(cmd, object, out)
return cmdutil.PrintObject(cmd, object, out)
}
f.PrintSuccess(false, out, info.Mapping.Resource, info.Name, true, "exposed")
cmdutil.PrintSuccess(false, out, info.Object, true, "exposed")
return nil
}
if err := kubectl.CreateOrUpdateAnnotation(cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag), info, f.JSONEncoder()); err != nil {
@ -271,10 +271,10 @@ func RunExpose(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri
}
if len(cmdutil.GetFlagString(cmd, "output")) > 0 {
return f.PrintObject(cmd, object, out)
return cmdutil.PrintObject(cmd, object, out)
}
f.PrintSuccess(false, out, info.Mapping.Resource, info.Name, false, "exposed")
cmdutil.PrintSuccess(false, out, info.Object, false, "exposed")
return nil
})
if err != nil {

View File

@ -31,7 +31,6 @@ import (
api "k8s.io/kubernetes/pkg/apis/core"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
"k8s.io/kubernetes/pkg/kubectl/scheme"
"k8s.io/kubernetes/pkg/printers"
)
// This init should be removed after switching this command and its tests to user external types.
@ -79,7 +78,7 @@ func TestRunExposeService(t *testing.T) {
Selector: map[string]string{"app": "go"},
},
},
expected: "services \"foo\" exposed",
expected: "service \"foo\" exposed",
status: 200,
},
{
@ -110,7 +109,7 @@ func TestRunExposeService(t *testing.T) {
Selector: map[string]string{"func": "stream"},
},
},
expected: "services \"foo\" exposed",
expected: "service \"foo\" exposed",
status: 200,
},
{
@ -142,7 +141,7 @@ func TestRunExposeService(t *testing.T) {
Selector: map[string]string{"run": "this"},
},
},
expected: "services \"mayor\" exposed",
expected: "service \"mayor\" exposed",
status: 200,
},
{
@ -237,7 +236,7 @@ func TestRunExposeService(t *testing.T) {
ClusterIP: "10.10.10.10",
},
},
expected: "services \"foo\" exposed",
expected: "service \"foo\" exposed",
status: 200,
},
{
@ -269,7 +268,7 @@ func TestRunExposeService(t *testing.T) {
ClusterIP: api.ClusterIPNone,
},
},
expected: "services \"foo\" exposed",
expected: "service \"foo\" exposed",
status: 200,
},
{
@ -295,7 +294,7 @@ func TestRunExposeService(t *testing.T) {
ClusterIP: api.ClusterIPNone,
},
},
expected: "services \"foo\" exposed",
expected: "service \"foo\" exposed",
status: 200,
},
{
@ -353,7 +352,7 @@ func TestRunExposeService(t *testing.T) {
Selector: map[string]string{"svc": "frompod"},
},
},
expected: "services \"a-name-that-is-toooo-big-for-a-service-because-it-can-only-hand\" exposed",
expected: "service \"a-name-that-is-toooo-big-for-a-service-because-it-can-only-hand\" exposed",
status: 200,
},
{
@ -467,7 +466,6 @@ func TestRunExposeService(t *testing.T) {
for _, test := range tests {
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.Printer = &printers.JSONPrinter{}
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: ns,
@ -495,13 +493,7 @@ func TestRunExposeService(t *testing.T) {
out := buf.String()
if _, ok := test.flags["dry-run"]; ok {
buf.Reset()
if err := tf.Printer.PrintObj(test.output, buf); err != nil {
t.Errorf("%s: Unexpected error: %v", test.name, err)
continue
}
test.expected = fmt.Sprintf("services %q exposed (dry run)", test.flags["name"])
test.expected = fmt.Sprintf("service %q exposed (dry run)", test.flags["name"])
}
if !strings.Contains(out, test.expected) {

View File

@ -288,9 +288,9 @@ func (o *LabelOptions) RunLabel(f cmdutil.Factory, cmd *cobra.Command) error {
}
if len(o.outputFormat) > 0 {
return f.PrintObject(cmd, outputObj, o.out)
return cmdutil.PrintObject(cmd, outputObj, o.out)
}
f.PrintSuccess(false, o.out, info.Mapping.Resource, info.Name, o.dryrun, dataChangeMsg)
cmdutil.PrintSuccess(false, o.out, info.Object, o.dryrun, dataChangeMsg)
return nil
})
}

View File

@ -320,7 +320,6 @@ func TestLabelErrors(t *testing.T) {
for k, testCase := range testCases {
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Namespace = "test"
tf.ClientConfig = defaultClientConfig()
@ -343,9 +342,6 @@ func TestLabelErrors(t *testing.T) {
t.Errorf("%s: unexpected error: %v", k, err)
continue
}
if tf.Printer.(*testPrinter).Objects != nil {
t.Errorf("unexpected print to default printer")
}
if buf.Len() > 0 {
t.Errorf("buffer should be empty: %s", string(buf.Bytes()))
}

View File

@ -210,9 +210,9 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
}
if len(options.OutputFormat) > 0 && options.OutputFormat != "name" {
return f.PrintResourceInfoForCommand(cmd, info, out)
return cmdutil.PrintObject(cmd, info.Object, out)
}
f.PrintSuccess(options.OutputFormat == "name", out, info.Mapping.Resource, info.Name, false, dataChangedMsg)
cmdutil.PrintSuccess(options.OutputFormat == "name", out, info.Object, false, dataChangedMsg)
// if object was not successfully patched, exit with error code 1
if !didPatch {
@ -246,7 +246,7 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
if err := info.Refresh(targetObj, true); err != nil {
return err
}
return f.PrintResourceInfoForCommand(cmd, info, out)
return cmdutil.PrintObject(cmd, info.Object, out)
})
if err != nil {
return err

View File

@ -24,7 +24,6 @@ import (
"k8s.io/client-go/rest/fake"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
"k8s.io/kubernetes/pkg/printers"
)
func TestPatchObject(t *testing.T) {
@ -60,7 +59,7 @@ func TestPatchObject(t *testing.T) {
cmd.Run(cmd, []string{"services/frontend"})
// uses the name from the response
if buf.String() != "services/baz\n" {
if buf.String() != "service/baz\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -92,7 +91,7 @@ func TestPatchObjectFromFile(t *testing.T) {
cmd.Run(cmd, []string{})
// uses the name from the response
if buf.String() != "services/baz\n" {
if buf.String() != "service/baz\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -131,7 +130,7 @@ func TestPatchNoop(t *testing.T) {
cmd.Flags().Set("namespace", "test")
cmd.Flags().Set("patch", `{"metadata":{"annotations":{"foo":"bar"}}}`)
cmd.Run(cmd, []string{"services", "frontend"})
if buf.String() != "services \"baz\" patched\n" {
if buf.String() != "service \"baz\" patched\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -147,7 +146,6 @@ func TestPatchObjectFromFileOutput(t *testing.T) {
svcCopy.Labels["post-patch"] = "post-patch-value"
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &printers.YAMLPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {

View File

@ -155,7 +155,7 @@ func RunReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
info.Refresh(obj, true)
f.PrintObjectSpecificMessage(obj, out)
f.PrintSuccess(shortOutput, out, info.Mapping.Resource, info.Name, false, "replaced")
cmdutil.PrintSuccess(shortOutput, out, info.Object, false, "replaced")
return nil
})
}
@ -199,8 +199,6 @@ func forceReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
return err
}
mapper := r.Mapper().RESTMapper
//Replace will create a resource if it doesn't exist already, so ignore not found error
ignoreNotFound := true
timeout := cmdutil.GetFlagDuration(cmd, "timeout")
@ -215,9 +213,9 @@ func forceReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
// By default use a reaper to delete all related resources.
if cmdutil.GetFlagBool(cmd, "cascade") {
glog.Warningf("\"cascade\" is set, kubectl will delete and re-create all resources managed by this resource (e.g. Pods created by a ReplicationController). Consider using \"kubectl rolling-update\" if you want to update a ReplicationController together with its Pods.")
err = ReapResult(r, f, out, cmdutil.GetFlagBool(cmd, "cascade"), ignoreNotFound, timeout, gracePeriod, waitForDeletion, shortOutput, mapper, false)
err = ReapResult(r, f, out, cmdutil.GetFlagBool(cmd, "cascade"), ignoreNotFound, timeout, gracePeriod, waitForDeletion, shortOutput, false)
} else {
err = DeleteResult(r, f, out, ignoreNotFound, gracePeriod, shortOutput, mapper)
err = DeleteResult(r, out, ignoreNotFound, gracePeriod, shortOutput)
}
if err != nil {
return err
@ -279,7 +277,7 @@ func forceReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
count++
info.Refresh(obj, true)
f.PrintObjectSpecificMessage(obj, out)
f.PrintSuccess(shortOutput, out, info.Mapping.Resource, info.Name, false, "replaced")
cmdutil.PrintSuccess(shortOutput, out, info.Object, false, "replaced")
return nil
})
if err != nil {

View File

@ -31,7 +31,6 @@ func TestReplaceObject(t *testing.T) {
_, _, rc := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
deleted := false
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
@ -67,7 +66,7 @@ func TestReplaceObject(t *testing.T) {
cmd.Run(cmd, []string{})
// uses the name from the file, not the response
if buf.String() != "replicationcontrollers/rc1\n" {
if buf.String() != "replicationcontroller/rc1\n" {
t.Errorf("unexpected output: %s", buf.String())
}
@ -77,7 +76,7 @@ func TestReplaceObject(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{})
if buf.String() != "replicationcontrollers/redis-master\nreplicationcontrollers/rc1\n" {
if buf.String() != "replicationcontroller/redis-master\nreplicationcontroller/rc1\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -86,7 +85,6 @@ func TestReplaceMultipleObject(t *testing.T) {
_, svc, rc := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
redisMasterDeleted := false
frontendDeleted := false
tf.UnstructuredClient = &fake.RESTClient{
@ -136,7 +134,7 @@ func TestReplaceMultipleObject(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{})
if buf.String() != "replicationcontrollers/rc1\nservices/baz\n" {
if buf.String() != "replicationcontroller/rc1\nservice/baz\n" {
t.Errorf("unexpected output: %s", buf.String())
}
@ -146,7 +144,7 @@ func TestReplaceMultipleObject(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{})
if buf.String() != "replicationcontrollers/redis-master\nservices/frontend\nreplicationcontrollers/rc1\nservices/baz\n" {
if buf.String() != "replicationcontroller/redis-master\nservice/frontend\nreplicationcontroller/rc1\nservice/baz\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -155,7 +153,6 @@ func TestReplaceDirectory(t *testing.T) {
_, _, rc := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
created := map[string]bool{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
@ -192,7 +189,7 @@ func TestReplaceDirectory(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{})
if buf.String() != "replicationcontrollers/rc1\nreplicationcontrollers/rc1\nreplicationcontrollers/rc1\n" {
if buf.String() != "replicationcontroller/rc1\nreplicationcontroller/rc1\nreplicationcontroller/rc1\n" {
t.Errorf("unexpected output: %s", buf.String())
}
@ -201,8 +198,8 @@ func TestReplaceDirectory(t *testing.T) {
cmd.Flags().Set("cascade", "false")
cmd.Run(cmd, []string{})
if buf.String() != "replicationcontrollers/frontend\nreplicationcontrollers/redis-master\nreplicationcontrollers/redis-slave\n"+
"replicationcontrollers/rc1\nreplicationcontrollers/rc1\nreplicationcontrollers/rc1\n" {
if buf.String() != "replicationcontroller/frontend\nreplicationcontroller/redis-master\nreplicationcontroller/redis-slave\n"+
"replicationcontroller/rc1\nreplicationcontroller/rc1\nreplicationcontroller/rc1\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -211,7 +208,6 @@ func TestForceReplaceObjectNotFound(t *testing.T) {
_, _, rc := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -238,7 +234,7 @@ func TestForceReplaceObjectNotFound(t *testing.T) {
cmd.Flags().Set("output", "name")
cmd.Run(cmd, []string{})
if buf.String() != "replicationcontrollers/rc1\n" {
if buf.String() != "replicationcontroller/rc1\n" {
t.Errorf("unexpected output: %s", buf.String())
}
}

View File

@ -48,8 +48,6 @@ go_test(
"//pkg/kubectl/cmd/util/openapi:go_default_library",
"//pkg/kubectl/cmd/util/openapi/testing:go_default_library",
"//pkg/kubectl/scheme:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/equality:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",

View File

@ -261,7 +261,7 @@ func (options *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []str
}
printOpts := cmdutil.ExtractCmdPrintOptions(cmd, options.AllNamespaces)
printer, err := f.PrinterForOptions(printOpts)
printer, err := cmdutil.PrinterForOptions(printOpts)
if err != nil {
return err
}
@ -341,7 +341,7 @@ func (options *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []str
updatePrintOptionsForOpenAPI(f, mapping, printOpts)
}
printer, err = f.PrinterForMapping(printOpts)
printer, err = cmdutil.PrinterForOptions(printOpts)
if err != nil {
if !errs.Has(err.Error()) {
errs.Insert(err.Error())
@ -494,7 +494,7 @@ func (options *GetOptions) watch(f cmdutil.Factory, cmd *cobra.Command, args []s
info := infos[0]
mapping := info.ResourceMapping()
printOpts := cmdutil.ExtractCmdPrintOptions(cmd, options.AllNamespaces)
printer, err := f.PrinterForMapping(printOpts)
printer, err := cmdutil.PrinterForOptions(printOpts)
if err != nil {
return err
}

View File

@ -28,8 +28,6 @@ import (
"strings"
"testing"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
@ -197,10 +195,10 @@ func testComponentStatusData() *api.ComponentStatusList {
// Verifies that schemas that are not in the master tree of Kubernetes can be retrieved via Get.
func TestGetUnknownSchemaObject(t *testing.T) {
t.Skip("This test is completely broken. The first thing it does is add the object to the scheme!")
f, tf, _, _ := cmdtesting.NewAPIFactory()
tf.WithCustomScheme()
_, _, codec, _ := cmdtesting.NewTestFactory()
tf.Printer = &testPrinter{}
tf.OpenAPISchemaFunc = openapitesting.CreateOpenAPISchemaFunc(openapiSchemaPath)
obj := &cmdtesting.ExternalType{
@ -246,7 +244,8 @@ func TestGetUnknownSchemaObject(t *testing.T) {
cmd.Run(cmd, []string{"type", "foo"})
expected := []runtime.Object{cmdtesting.NewInternalType("", "", "foo")}
actual := tf.Printer.(*testPrinter).Objects
actual := []runtime.Object{}
//actual := tf.Printer.(*testPrinter).Objects
if len(actual) != len(expected) {
t.Fatalf("expected: %#v, but actual: %#v", expected, actual)
}
@ -276,7 +275,6 @@ func TestGetSchemaObject(t *testing.T) {
tf.Mapper = testapi.Default.RESTMapper()
tf.Typer = scheme.Scheme
codec := testapi.Default.Codec()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &api.ReplicationController{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})},
@ -289,7 +287,7 @@ func TestGetSchemaObject(t *testing.T) {
cmd := NewCmdGet(f, buf, errBuf)
cmd.Run(cmd, []string{"replicationcontrollers", "foo"})
if !strings.Contains(buf.String(), "\"foo\"") {
if !strings.Contains(buf.String(), "foo") {
t.Errorf("unexpected output: %s", buf.String())
}
}
@ -298,7 +296,6 @@ func TestGetObjectsWithOpenAPIOutputFormatPresent(t *testing.T) {
pods, _, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
// overide the openAPISchema function to return custom output
// for Pod type.
tf.OpenAPISchemaFunc = testOpenAPISchemaData
@ -315,11 +312,11 @@ func TestGetObjectsWithOpenAPIOutputFormatPresent(t *testing.T) {
cmd.Flags().Set(useOpenAPIPrintColumnFlagLabel, "true")
cmd.Run(cmd, []string{"pods", "foo"})
expected := []runtime.Object{&pods.Items[0]}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
expected := `NAME RSRC
foo 10
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -354,7 +351,6 @@ func TestGetObjects(t *testing.T) {
pods, _, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &pods.Items[0])},
@ -367,11 +363,11 @@ func TestGetObjects(t *testing.T) {
cmd.SetOutput(buf)
cmd.Run(cmd, []string{"pods", "foo"})
expected := []runtime.Object{&pods.Items[0]}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
expected := `NAME READY STATUS RESTARTS AGE
foo 0/0 0 <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -381,32 +377,29 @@ func TestGetObjectsFiltered(t *testing.T) {
pods, _, _ := testData()
pods.Items[0].Status.Phase = api.PodFailed
first := &pods.Items[0]
second := &pods.Items[1]
testCases := []struct {
args []string
resp runtime.Object
flags map[string]string
expect []runtime.Object
genericPrinter bool
args []string
resp runtime.Object
flags map[string]string
expect string
}{
{args: []string{"pods", "foo"}, resp: first, expect: []runtime.Object{first}, genericPrinter: true},
{args: []string{"pods", "foo"}, flags: map[string]string{"show-all": "false"}, resp: first, expect: []runtime.Object{first}, genericPrinter: true},
{args: []string{"pods"}, flags: map[string]string{"show-all": "true"}, resp: pods, expect: []runtime.Object{first, second}},
{args: []string{"pods/foo"}, resp: first, expect: []runtime.Object{first}, genericPrinter: true},
{args: []string{"pods"}, flags: map[string]string{"output": "yaml"}, resp: pods, expect: []runtime.Object{second}},
{args: []string{}, flags: map[string]string{"filename": "../../../../examples/storage/cassandra/cassandra-controller.yaml"}, resp: pods, expect: []runtime.Object{first, second}},
{args: []string{"pods", "foo"}, resp: first, expect: "pod/foo\n"},
{args: []string{"pods", "foo"}, flags: map[string]string{"show-all": "false"}, resp: first, expect: "pod/foo\n"},
{args: []string{"pods"}, flags: map[string]string{"show-all": "true"}, resp: pods, expect: "pod/foo\npod/bar\n"},
{args: []string{"pods/foo"}, resp: first, expect: "pod/foo\n"},
{args: []string{"pods"}, flags: map[string]string{"output": "yaml"}, resp: pods, expect: "pod/bar\n"},
{args: []string{}, flags: map[string]string{"filename": "../../../../examples/storage/cassandra/cassandra-controller.yaml"}, resp: pods, expect: "pod/foo\npod/bar\n"},
{args: []string{"pods"}, resp: pods, expect: []runtime.Object{second}},
{args: []string{"pods"}, flags: map[string]string{"show-all": "true", "output": "yaml"}, resp: pods, expect: []runtime.Object{first, second}},
{args: []string{"pods"}, flags: map[string]string{"show-all": "false"}, resp: pods, expect: []runtime.Object{second}},
{args: []string{"pods"}, resp: pods, expect: "pod/bar\n"},
{args: []string{"pods"}, flags: map[string]string{"show-all": "true", "output": "yaml"}, resp: pods, expect: "pod/foo\npod/bar\n"},
{args: []string{"pods"}, flags: map[string]string{"show-all": "false"}, resp: pods, expect: "pod/bar\n"},
}
for i, test := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.WithLegacyScheme()
tf.Printer = &testPrinter{GenericPrinter: test.genericPrinter}
tf.UnstructuredClient = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: unstructuredSerializer,
@ -421,12 +414,11 @@ func TestGetObjectsFiltered(t *testing.T) {
for k, v := range test.flags {
cmd.Flags().Lookup(k).Value.Set(v)
}
cmd.Flags().Set("output", "name")
cmd.Run(cmd, test.args)
verifyObjects(t, test.expect, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Errorf("%d: unexpected empty output", i)
if e, a := test.expect, buf.String(); e != a {
t.Errorf("expected %q, got %q", e, a)
}
})
}
@ -448,7 +440,6 @@ func TestGetObjectIgnoreNotFound(t *testing.T) {
}
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{GenericPrinter: true}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -500,7 +491,6 @@ func TestGetSortedObjects(t *testing.T) {
}
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)},
@ -518,39 +508,13 @@ func TestGetSortedObjects(t *testing.T) {
cmd.Flags().Set("sort-by", ".metadata.name")
cmd.Run(cmd, []string{"pods"})
// expect sorted: a,b,c
expected := []runtime.Object{&pods.Items[2], &pods.Items[1], &pods.Items[0]}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
}
}
func verifyObjects(t *testing.T, expected, actual []runtime.Object) {
var actualObj runtime.Object
var err error
if len(actual) != len(expected) {
t.Fatalf("expected %d, but actual %d", len(expected), len(actual))
}
for i, obj := range actual {
switch obj.(type) {
case runtime.Unstructured, *runtime.Unknown:
actualObj, err = runtime.Decode(
scheme.Codecs.UniversalDecoder(),
[]byte(runtime.EncodeOrDie(scheme.Codecs.LegacyCodec(), obj)))
default:
actualObj = obj
err = nil
}
if err != nil {
t.Fatal(err)
}
if !apiequality.Semantic.DeepEqual(expected[i], actualObj) {
t.Errorf("expected object: %#v, but actualObj:%#v\n", expected[i], actualObj)
}
expected := `NAME READY STATUS RESTARTS AGE
a 0/0 0 <unknown>
b 0/0 0 <unknown>
c 0/0 0 <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -558,7 +522,6 @@ func TestGetObjectsIdentifiedByFile(t *testing.T) {
pods, _, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{GenericPrinter: true}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &pods.Items[0])},
@ -572,11 +535,11 @@ func TestGetObjectsIdentifiedByFile(t *testing.T) {
cmd.Flags().Set("filename", "../../../../examples/storage/cassandra/cassandra-controller.yaml")
cmd.Run(cmd, []string{})
expected := []runtime.Object{&pods.Items[0]}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
expected := `NAME READY STATUS RESTARTS AGE
foo 0/0 0 <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -584,7 +547,6 @@ func TestGetListObjects(t *testing.T) {
pods, _, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)},
@ -597,34 +559,19 @@ func TestGetListObjects(t *testing.T) {
cmd.SetOutput(buf)
cmd.Run(cmd, []string{"pods"})
expected, err := extractResourceList([]runtime.Object{pods})
if err != nil {
t.Fatal(err)
expected := `NAME READY STATUS RESTARTS AGE
foo 0/0 0 <unknown>
bar 0/0 0 <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
}
}
func extractResourceList(objs []runtime.Object) ([]runtime.Object, error) {
finalObjs := []runtime.Object{}
for _, obj := range objs {
items, err := meta.ExtractList(obj)
if err != nil {
return nil, err
}
finalObjs = append(finalObjs, items...)
}
return finalObjs, nil
}
func TestGetAllListObjects(t *testing.T) {
pods, _, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)},
@ -638,14 +585,12 @@ func TestGetAllListObjects(t *testing.T) {
cmd.Flags().Set("show-all", "true")
cmd.Run(cmd, []string{"pods"})
expected, err := extractResourceList([]runtime.Object{pods})
if err != nil {
t.Fatal(err)
}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
expected := `NAME READY STATUS RESTARTS AGE
foo 0/0 0 <unknown>
bar 0/0 0 <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -653,7 +598,6 @@ func TestGetListComponentStatus(t *testing.T) {
statuses := testComponentStatusData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, statuses)},
@ -666,14 +610,13 @@ func TestGetListComponentStatus(t *testing.T) {
cmd.SetOutput(buf)
cmd.Run(cmd, []string{"componentstatuses"})
expected, err := extractResourceList([]runtime.Object{statuses})
if err != nil {
t.Fatal(err)
}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
expected := `NAME STATUS MESSAGE ERROR
servergood Healthy ok
serverbad Unhealthy bad status: 500
serverunknown Unhealthy fizzbuzz error
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -694,7 +637,6 @@ func TestGetMixedGenericObjects(t *testing.T) {
}
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{GenericPrinter: true}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -717,39 +659,25 @@ func TestGetMixedGenericObjects(t *testing.T) {
cmd.Flags().Set("output", "json")
cmd.Run(cmd, []string{"pods"})
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
}
actual := tf.Printer.(*testPrinter).Objects
fn := func(obj runtime.Object) unstructured.Unstructured {
data, err := runtime.Encode(scheme.Codecs.LegacyCodec(schema.GroupVersion{Version: "v1"}), obj)
if err != nil {
panic(err)
}
out := &unstructured.Unstructured{Object: make(map[string]interface{})}
if err := encjson.Unmarshal(data, &out.Object); err != nil {
panic(err)
}
return *out
}
expected := &unstructured.UnstructuredList{
Object: map[string]interface{}{"kind": "List", "apiVersion": "v1", "metadata": map[string]interface{}{"selfLink": "", "resourceVersion": ""}},
Items: []unstructured.Unstructured{
fn(structuredObj),
},
}
actualBytes, err := encjson.Marshal(actual[0])
if err != nil {
t.Fatal(err)
}
expectedBytes, err := encjson.Marshal(expected)
if err != nil {
t.Fatal(err)
}
if string(actualBytes) != string(expectedBytes) {
t.Errorf("expectedBytes: %s,but actualBytes: %s", expectedBytes, actualBytes)
expected := `{
"apiVersion": "v1",
"items": [
{
"apiVersion": "v1",
"kind": "Status",
"metadata": {},
"status": "Success"
}
],
"kind": "List",
"metadata": {
"resourceVersion": "",
"selfLink": ""
}
}
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -757,7 +685,6 @@ func TestGetMultipleTypeObjects(t *testing.T) {
pods, svc, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -780,14 +707,14 @@ func TestGetMultipleTypeObjects(t *testing.T) {
cmd.SetOutput(buf)
cmd.Run(cmd, []string{"pods,services"})
expected, err := extractResourceList([]runtime.Object{pods, svc})
if err != nil {
t.Fatal(err)
}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
expected := `NAME READY STATUS RESTARTS AGE
foo 0/0 0 <unknown>
bar 0/0 0 <unknown>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
baz ClusterIP <none> <none> <none> <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -795,7 +722,6 @@ func TestGetMultipleTypeObjectsAsList(t *testing.T) {
pods, svc, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{GenericPrinter: true}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -821,37 +747,74 @@ func TestGetMultipleTypeObjectsAsList(t *testing.T) {
cmd.Flags().Set("output", "json")
cmd.Run(cmd, []string{"pods,services"})
actual := tf.Printer.(*testPrinter).Objects
fn := func(obj runtime.Object) unstructured.Unstructured {
data, err := runtime.Encode(scheme.Codecs.LegacyCodec(schema.GroupVersion{Version: "v1"}), obj)
if err != nil {
t.Fatal(err)
}
out := &unstructured.Unstructured{Object: make(map[string]interface{})}
if err := encjson.Unmarshal(data, &out.Object); err != nil {
t.Fatal(err)
}
return *out
}
expected := &unstructured.UnstructuredList{
Object: map[string]interface{}{"kind": "List", "apiVersion": "v1", "metadata": map[string]interface{}{"selfLink": "", "resourceVersion": ""}},
Items: []unstructured.Unstructured{
fn(&pods.Items[0]),
fn(&pods.Items[1]),
fn(&svc.Items[0]),
},
}
actualBytes, err := encjson.Marshal(actual[0])
if err != nil {
t.Fatal(err)
}
expectedBytes, err := encjson.Marshal(expected)
if err != nil {
t.Fatal(err)
}
if string(actualBytes) != string(expectedBytes) {
t.Errorf("expectedBytes: %s,but actualBytes: %s", expectedBytes, actualBytes)
expected := `{
"apiVersion": "v1",
"items": [
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"creationTimestamp": null,
"name": "foo",
"namespace": "test",
"resourceVersion": "10"
},
"spec": {
"containers": null,
"dnsPolicy": "ClusterFirst",
"restartPolicy": "Always",
"schedulerName": "default-scheduler",
"securityContext": {},
"terminationGracePeriodSeconds": 30
},
"status": {}
},
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"creationTimestamp": null,
"name": "bar",
"namespace": "test",
"resourceVersion": "11"
},
"spec": {
"containers": null,
"dnsPolicy": "ClusterFirst",
"restartPolicy": "Always",
"schedulerName": "default-scheduler",
"securityContext": {},
"terminationGracePeriodSeconds": 30
},
"status": {}
},
{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"creationTimestamp": null,
"name": "baz",
"namespace": "test",
"resourceVersion": "12"
},
"spec": {
"sessionAffinity": "None",
"type": "ClusterIP"
},
"status": {
"loadBalancer": {}
}
}
],
"kind": "List",
"metadata": {
"resourceVersion": "",
"selfLink": ""
}
}
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -859,7 +822,6 @@ func TestGetMultipleTypeObjectsWithLabelSelector(t *testing.T) {
pods, svc, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -887,14 +849,14 @@ func TestGetMultipleTypeObjectsWithLabelSelector(t *testing.T) {
cmd.Flags().Set("selector", "a=b")
cmd.Run(cmd, []string{"pods,services"})
expected, err := extractResourceList([]runtime.Object{pods, svc})
if err != nil {
t.Fatal(err)
}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
expected := `NAME READY STATUS RESTARTS AGE
foo 0/0 0 <unknown>
bar 0/0 0 <unknown>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
baz ClusterIP <none> <none> <none> <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -902,7 +864,6 @@ func TestGetMultipleTypeObjectsWithFieldSelector(t *testing.T) {
pods, svc, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -930,14 +891,14 @@ func TestGetMultipleTypeObjectsWithFieldSelector(t *testing.T) {
cmd.Flags().Set("field-selector", "a=b")
cmd.Run(cmd, []string{"pods,services"})
expected, err := extractResourceList([]runtime.Object{pods, svc})
if err != nil {
t.Fatal(err)
}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Errorf("unexpected empty output")
expected := `NAME READY STATUS RESTARTS AGE
foo 0/0 0 <unknown>
bar 0/0 0 <unknown>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
baz ClusterIP <none> <none> <none> <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -953,7 +914,6 @@ func TestGetMultipleTypeObjectsWithDirectReference(t *testing.T) {
}
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -977,11 +937,13 @@ func TestGetMultipleTypeObjectsWithDirectReference(t *testing.T) {
cmd.Run(cmd, []string{"services/bar", "node/foo"})
expected := []runtime.Object{&svc.Items[0], node}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
expected := `NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
baz ClusterIP <none> <none> <none> <unknown>
NAME STATUS ROLES AGE VERSION
foo Unknown <none> <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -989,7 +951,6 @@ func TestGetByFormatForcesFlag(t *testing.T) {
pods, _, _ := testData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{GenericPrinter: true}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &pods.Items[0])},
@ -1083,7 +1044,6 @@ func TestWatchLabelSelector(t *testing.T) {
pods, events := watchTestData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
podList := &api.PodList{
Items: pods,
ListMeta: metav1.ListMeta{
@ -1119,11 +1079,14 @@ func TestWatchLabelSelector(t *testing.T) {
cmd.Flags().Set("selector", "a=b")
cmd.Run(cmd, []string{"pods"})
expected := []runtime.Object{&pods[0], &pods[1], events[2].Object, events[3].Object}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
expected := `NAME READY STATUS RESTARTS AGE
bar 0/0 0 <unknown>
foo 0/0 0 <unknown>
foo 0/0 0 <unknown>
foo 0/0 0 <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -1131,7 +1094,6 @@ func TestWatchFieldSelector(t *testing.T) {
pods, events := watchTestData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
podList := &api.PodList{
Items: pods,
ListMeta: metav1.ListMeta{
@ -1167,11 +1129,14 @@ func TestWatchFieldSelector(t *testing.T) {
cmd.Flags().Set("field-selector", "a=b")
cmd.Run(cmd, []string{"pods"})
expected := []runtime.Object{&pods[0], &pods[1], events[2].Object, events[3].Object}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Errorf("unexpected empty output")
expected := `NAME READY STATUS RESTARTS AGE
bar 0/0 0 <unknown>
foo 0/0 0 <unknown>
foo 0/0 0 <unknown>
foo 0/0 0 <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -1179,7 +1144,6 @@ func TestWatchResource(t *testing.T) {
pods, events := watchTestData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -1208,11 +1172,13 @@ func TestWatchResource(t *testing.T) {
cmd.Flags().Set("watch", "true")
cmd.Run(cmd, []string{"pods", "foo"})
expected := []runtime.Object{&pods[1], events[2].Object, events[3].Object}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
expected := `NAME READY STATUS RESTARTS AGE
foo 0/0 0 <unknown>
foo 0/0 0 <unknown>
foo 0/0 0 <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -1220,7 +1186,6 @@ func TestWatchResourceIdentifiedByFile(t *testing.T) {
pods, events := watchTestData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -1250,11 +1215,13 @@ func TestWatchResourceIdentifiedByFile(t *testing.T) {
cmd.Flags().Set("filename", "../../../../examples/storage/cassandra/cassandra-controller.yaml")
cmd.Run(cmd, []string{})
expected := []runtime.Object{&pods[1], events[2].Object, events[3].Object}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
expected := `NAME READY STATUS RESTARTS AGE
foo 0/0 0 <unknown>
foo 0/0 0 <unknown>
foo 0/0 0 <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -1262,7 +1229,6 @@ func TestWatchOnlyResource(t *testing.T) {
pods, events := watchTestData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: unstructuredSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -1291,11 +1257,12 @@ func TestWatchOnlyResource(t *testing.T) {
cmd.Flags().Set("watch-only", "true")
cmd.Run(cmd, []string{"pods", "foo"})
expected := []runtime.Object{events[2].Object, events[3].Object}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
expected := `NAME READY STATUS RESTARTS AGE
foo 0/0 0 <unknown>
foo 0/0 0 <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}
@ -1303,7 +1270,6 @@ func TestWatchOnlyList(t *testing.T) {
pods, events := watchTestData()
f, tf, codec, _ := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
podList := &api.PodList{
Items: pods,
ListMeta: metav1.ListMeta{
@ -1335,11 +1301,12 @@ func TestWatchOnlyList(t *testing.T) {
cmd.Flags().Set("watch-only", "true")
cmd.Run(cmd, []string{"pods"})
expected := []runtime.Object{events[2].Object, events[3].Object}
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
if len(buf.String()) == 0 {
t.Error("unexpected empty output")
expected := `NAME READY STATUS RESTARTS AGE
foo 0/0 0 <unknown>
foo 0/0 0 <unknown>
`
if e, a := expected, buf.String(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}

View File

@ -320,10 +320,10 @@ func RunRollingUpdate(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args
oldRcData.WriteString(oldRc.Name)
newRcData.WriteString(newRc.Name)
} else {
if err := f.PrintObject(cmd, oldRc, oldRcData); err != nil {
if err := cmdutil.PrintObject(cmd, oldRc, oldRcData); err != nil {
return err
}
if err := f.PrintObject(cmd, newRc, newRcData); err != nil {
if err := cmdutil.PrintObject(cmd, newRc, newRcData); err != nil {
return err
}
}
@ -368,9 +368,9 @@ func RunRollingUpdate(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args
return err
}
if outputFormat != "" {
return f.PrintObject(cmd, newRc, out)
return cmdutil.PrintObject(cmd, newRc, out)
}
f.PrintSuccess(false, out, "replicationcontrollers", oldName, dryrun, message)
cmdutil.PrintSuccess(false, out, newRc, dryrun, message)
return nil
}

View File

@ -45,8 +45,7 @@ type PauseConfig struct {
Encoder runtime.Encoder
Infos []*resource.Info
PrintSuccess func(shortOutput bool, out io.Writer, resource, name string, dryRun bool, operation string)
Out io.Writer
Out io.Writer
}
var (
@ -102,7 +101,6 @@ func (o *PauseConfig) CompletePause(f cmdutil.Factory, cmd *cobra.Command, out i
return cmdutil.UsageErrorf(cmd, "%s", cmd.Use)
}
o.PrintSuccess = f.PrintSuccess
o.Mapper, o.Typer = f.Object()
o.Encoder = f.JSONEncoder()
@ -145,7 +143,7 @@ func (o PauseConfig) RunPause() error {
}
if string(patch.Patch) == "{}" || len(patch.Patch) == 0 {
o.PrintSuccess(false, o.Out, info.Mapping.Resource, info.Name, false, "already paused")
cmdutil.PrintSuccess(false, o.Out, info.Object, false, "already paused")
continue
}
@ -156,7 +154,7 @@ func (o PauseConfig) RunPause() error {
}
info.Refresh(obj, true)
o.PrintSuccess(false, o.Out, info.Mapping.Resource, info.Name, false, "paused")
cmdutil.PrintSuccess(false, o.Out, info.Object, false, "paused")
}
return utilerrors.NewAggregate(allErrs)

View File

@ -45,8 +45,7 @@ type ResumeConfig struct {
Encoder runtime.Encoder
Infos []*resource.Info
PrintSuccess func(shortOutput bool, out io.Writer, resource, name string, dryRun bool, operation string)
Out io.Writer
Out io.Writer
}
var (
@ -100,7 +99,6 @@ func (o *ResumeConfig) CompleteResume(f cmdutil.Factory, cmd *cobra.Command, out
return cmdutil.UsageErrorf(cmd, "%s", cmd.Use)
}
o.PrintSuccess = f.PrintSuccess
o.Mapper, o.Typer = f.Object()
o.Encoder = f.JSONEncoder()
@ -150,7 +148,7 @@ func (o ResumeConfig) RunResume() error {
}
if string(patch.Patch) == "{}" || len(patch.Patch) == 0 {
o.PrintSuccess(false, o.Out, info.Mapping.Resource, info.Name, false, "already resumed")
cmdutil.PrintSuccess(false, o.Out, info.Object, false, "already resumed")
continue
}
@ -161,7 +159,7 @@ func (o ResumeConfig) RunResume() error {
}
info.Refresh(obj, true)
o.PrintSuccess(false, o.Out, info.Mapping.Resource, info.Name, false, "resumed")
cmdutil.PrintSuccess(false, o.Out, info.Object, false, "resumed")
}
return utilerrors.NewAggregate(allErrs)

View File

@ -43,8 +43,7 @@ type UndoOptions struct {
ToRevision int64
DryRun bool
PrintSuccess func(shortOutput bool, out io.Writer, resource, name string, dryRun bool, operation string)
Out io.Writer
Out io.Writer
}
var (
@ -102,7 +101,6 @@ func (o *UndoOptions) CompleteUndo(f cmdutil.Factory, cmd *cobra.Command, out io
return cmdutil.UsageErrorf(cmd, "Required resource not specified.")
}
o.PrintSuccess = f.PrintSuccess
o.ToRevision = cmdutil.GetFlagInt64(cmd, "to-revision")
o.Mapper, o.Typer = f.Object()
o.Out = out
@ -150,7 +148,7 @@ func (o *UndoOptions) RunUndo() error {
allErrs = append(allErrs, cmdutil.AddSourceToErr("undoing", info.Source, err))
continue
}
o.PrintSuccess(false, o.Out, info.Mapping.Resource, info.Name, false, result)
cmdutil.PrintSuccess(false, o.Out, info.Object, false, result)
}
return utilerrors.NewAggregate(allErrs)
}

View File

@ -91,7 +91,6 @@ var (
type RunObject struct {
Object runtime.Object
Kind string
Mapper meta.RESTMapper
Mapping *meta.RESTMapping
}
@ -370,7 +369,7 @@ func RunRun(f cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer, cmd *c
// asked for us to remove the pod (via --rm) then telling them
// its been deleted is unnecessary since that's what they asked
// for. We should only print something if the "rm" fails.
err = ReapResult(r, f, cmdOut, true, true, 0, -1, false, false, obj.Mapper, true)
err = ReapResult(r, f, cmdOut, true, true, 0, -1, false, false, true)
if err != nil {
return err
}
@ -407,9 +406,9 @@ func RunRun(f cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer, cmd *c
if runObject != nil {
outputFormat := cmdutil.GetFlagString(cmd, "output")
if outputFormat != "" || cmdutil.GetDryRunFlag(cmd) {
return f.PrintObject(cmd, runObject.Object, cmdOut)
return cmdutil.PrintObject(cmd, runObject.Object, cmdOut)
}
f.PrintSuccess(false, cmdOut, runObject.Mapping.Resource, args[0], cmdutil.GetDryRunFlag(cmd), "created")
cmdutil.PrintSuccess(false, cmdOut, runObject.Object, cmdutil.GetDryRunFlag(cmd), "created")
}
return utilerrors.NewAggregate(allErrs)
@ -557,7 +556,7 @@ func generateService(f cmdutil.Factory, cmd *cobra.Command, args []string, servi
}
if cmdutil.GetFlagString(cmd, "output") != "" || cmdutil.GetDryRunFlag(cmd) {
err := f.PrintObject(cmd, runObject.Object, out)
err := cmdutil.PrintObject(cmd, runObject.Object, out)
if err != nil {
return nil, err
}
@ -566,7 +565,7 @@ func generateService(f cmdutil.Factory, cmd *cobra.Command, args []string, servi
}
return runObject, nil
}
f.PrintSuccess(false, out, runObject.Mapping.Resource, args[0], cmdutil.GetDryRunFlag(cmd), "created")
cmdutil.PrintSuccess(false, out, runObject.Object, cmdutil.GetDryRunFlag(cmd), "created")
return runObject, nil
}
@ -640,7 +639,6 @@ func createGeneratedObject(f cmdutil.Factory, cmd *cobra.Command, generator kube
return &RunObject{
Object: obj,
Kind: groupVersionKind.Kind,
Mapper: mapper,
Mapping: mapping,
}, nil
}

View File

@ -291,7 +291,6 @@ func TestGenerateService(t *testing.T) {
sawPOST := false
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.ClientConfig = defaultClientConfig()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: "v1"},
NegotiatedSerializer: ns,
@ -432,7 +431,6 @@ func TestRunValidations(t *testing.T) {
}
for _, test := range tests {
f, tf, codec, ns := cmdtesting.NewTestFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, cmdtesting.NewInternalType("", "", ""))},

View File

@ -180,7 +180,7 @@ func RunScale(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
}
}
counter++
f.PrintSuccess(shortOutput, out, info.Mapping.Resource, info.Name, false, "scaled")
cmdutil.PrintSuccess(shortOutput, out, info.Object, false, "scaled")
return nil
})
if err != nil {

View File

@ -58,7 +58,6 @@ go_test(
],
embed = [":go_default_library"],
deps = [
"//pkg/api/legacyscheme:go_default_library",
"//pkg/api/testapi:go_default_library",
"//pkg/apis/rbac:go_default_library",
"//pkg/kubectl/categories:go_default_library",
@ -66,7 +65,6 @@ go_test(
"//pkg/kubectl/cmd/util:go_default_library",
"//pkg/kubectl/resource:go_default_library",
"//pkg/kubectl/scheme:go_default_library",
"//pkg/printers:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
"//vendor/k8s.io/api/apps/v1:go_default_library",

View File

@ -122,7 +122,6 @@ type EnvOptions struct {
Cmd *cobra.Command
UpdatePodSpecForObject func(obj runtime.Object, fn func(*v1.PodSpec) error) (bool, error)
PrintObject func(cmd *cobra.Command, obj runtime.Object, out io.Writer) error
}
// NewCmdEnv implements the OpenShift cli env command
@ -199,7 +198,6 @@ func (o *EnvOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
o.From = cmdutil.GetFlagString(cmd, "from")
o.Prefix = cmdutil.GetFlagString(cmd, "prefix")
o.DryRun = cmdutil.GetDryRunFlag(cmd)
o.PrintObject = f.PrintObject
o.EnvArgs = envArgs
o.Resources = resources
@ -413,8 +411,8 @@ func (o *EnvOptions) RunEnv(f cmdutil.Factory) error {
continue
}
if o.PrintObject != nil && (o.Local || o.DryRun) {
if err := o.PrintObject(o.Cmd, patch.Info.AsVersioned(), o.Out); err != nil {
if o.Local || o.DryRun {
if err := cmdutil.PrintObject(o.Cmd, patch.Info.AsVersioned(), o.Out); err != nil {
return err
}
continue
@ -434,13 +432,13 @@ func (o *EnvOptions) RunEnv(f cmdutil.Factory) error {
}
if len(o.Output) > 0 {
if err := o.PrintObject(o.Cmd, info.AsVersioned(), o.Out); err != nil {
if err := cmdutil.PrintObject(o.Cmd, info.AsVersioned(), o.Out); err != nil {
return err
}
continue
}
f.PrintSuccess(o.ShortOutput, o.Out, info.Mapping.Resource, info.Name, false, "env updated")
cmdutil.PrintSuccess(o.ShortOutput, o.Out, info.Object, false, "env updated")
}
return utilerrors.NewAggregate(allErrs)
}

View File

@ -38,17 +38,15 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/rest/fake"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/kubectl/categories"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/kubectl/scheme"
"k8s.io/kubernetes/pkg/printers"
)
func TestSetEnvLocal(t *testing.T) {
f, tf, codec, ns := cmdtesting.NewAPIFactory()
f, tf, _, ns := cmdtesting.NewAPIFactory()
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
NegotiatedSerializer: ns,
@ -65,8 +63,6 @@ func TestSetEnvLocal(t *testing.T) {
cmd.SetOutput(buf)
cmd.Flags().Set("output", "name")
cmd.Flags().Set("local", "true")
_, typer := f.Object()
tf.Printer = &printers.NamePrinter{Decoders: []runtime.Decoder{codec}, Typer: typer}
opts := EnvOptions{FilenameOptions: resource.FilenameOptions{
Filenames: []string{"../../../../examples/storage/cassandra/cassandra-controller.yaml"}},
@ -85,7 +81,7 @@ func TestSetEnvLocal(t *testing.T) {
}
func TestSetMultiResourcesEnvLocal(t *testing.T) {
f, tf, codec, ns := cmdtesting.NewAPIFactory()
f, tf, _, ns := cmdtesting.NewAPIFactory()
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
NegotiatedSerializer: ns,
@ -102,8 +98,6 @@ func TestSetMultiResourcesEnvLocal(t *testing.T) {
cmd.SetOutput(buf)
cmd.Flags().Set("output", "name")
cmd.Flags().Set("local", "true")
_, typer := f.Object()
tf.Printer = &printers.NamePrinter{Decoders: []runtime.Decoder{codec}, Typer: typer}
opts := EnvOptions{FilenameOptions: resource.FilenameOptions{
Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}},
@ -436,7 +430,6 @@ func TestSetEnvRemote(t *testing.T) {
testapi.Default = testapi.Groups[input.testAPIGroup]
f, tf, _, ns := cmdtesting.NewAPIFactory()
codec := scheme.Codecs.CodecForVersions(scheme.Codecs.LegacyCodec(groupVersion), scheme.Codecs.UniversalDecoder(groupVersion), groupVersion, groupVersion)
tf.Printer = printers.NewVersionedPrinter(&printers.YAMLPrinter{}, legacyscheme.Scheme, legacyscheme.Scheme, scheme.Versions...)
tf.Namespace = "test"
tf.CategoryExpander = categories.LegacyCategoryExpander
tf.Client = &fake.RESTClient{

View File

@ -52,8 +52,6 @@ type ImageOptions struct {
Cmd *cobra.Command
ResolveImage func(in string) (string, error)
PrintSuccess func(shortOutput bool, out io.Writer, resource, name string, dryRun bool, operation string)
PrintObject func(cmd *cobra.Command, obj runtime.Object, out io.Writer) error
UpdatePodSpecForObject func(obj runtime.Object, fn func(*v1.PodSpec) error) (bool, error)
Resources []string
ContainerImages map[string]string
@ -115,14 +113,12 @@ func NewCmdImage(f cmdutil.Factory, out, err io.Writer) *cobra.Command {
}
func (o *ImageOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
o.PrintSuccess = f.PrintSuccess
o.UpdatePodSpecForObject = f.UpdatePodSpecForObject
o.Encoder = f.JSONEncoder()
o.Decoder = f.Decoder(true)
o.ShortOutput = cmdutil.GetFlagString(cmd, "output") == "name"
o.Record = cmdutil.GetRecordFlag(cmd)
o.ChangeCause = f.Command(cmd, false)
o.PrintObject = f.PrintObject
o.DryRun = cmdutil.GetDryRunFlag(cmd)
o.Output = cmdutil.GetFlagString(cmd, "output")
o.ResolveImage = f.ResolveImage
@ -245,8 +241,8 @@ func (o *ImageOptions) Run() error {
continue
}
if o.PrintObject != nil && (o.Local || o.DryRun) {
if err := o.PrintObject(o.Cmd, patch.Info.AsVersioned(), o.Out); err != nil {
if o.Local || o.DryRun {
if err := cmdutil.PrintObject(o.Cmd, patch.Info.AsVersioned(), o.Out); err != nil {
return err
}
continue
@ -272,12 +268,12 @@ func (o *ImageOptions) Run() error {
info.Refresh(obj, true)
if len(o.Output) > 0 {
if err := o.PrintObject(o.Cmd, info.AsVersioned(), o.Out); err != nil {
if err := cmdutil.PrintObject(o.Cmd, info.AsVersioned(), o.Out); err != nil {
return err
}
continue
}
o.PrintSuccess(o.ShortOutput, o.Out, info.Mapping.Resource, info.Name, o.DryRun, "image updated")
cmdutil.PrintSuccess(o.ShortOutput, o.Out, info.Object, o.DryRun, "image updated")
}
return utilerrors.NewAggregate(allErrs)
}

View File

@ -37,17 +37,15 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/rest/fake"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/kubectl/categories"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/kubectl/scheme"
"k8s.io/kubernetes/pkg/printers"
)
func TestImageLocal(t *testing.T) {
f, tf, codec, ns := cmdtesting.NewAPIFactory()
f, tf, _, ns := cmdtesting.NewAPIFactory()
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
NegotiatedSerializer: ns,
@ -64,8 +62,6 @@ func TestImageLocal(t *testing.T) {
cmd.SetOutput(buf)
cmd.Flags().Set("output", "name")
cmd.Flags().Set("local", "true")
_, typer := f.Object()
tf.Printer = &printers.NamePrinter{Decoders: []runtime.Decoder{codec}, Typer: typer}
opts := ImageOptions{FilenameOptions: resource.FilenameOptions{
Filenames: []string{"../../../../examples/storage/cassandra/cassandra-controller.yaml"}},
@ -150,7 +146,7 @@ func TestSetImageValidation(t *testing.T) {
}
func TestSetMultiResourcesImageLocal(t *testing.T) {
f, tf, codec, ns := cmdtesting.NewAPIFactory()
f, tf, _, ns := cmdtesting.NewAPIFactory()
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
NegotiatedSerializer: ns,
@ -167,8 +163,6 @@ func TestSetMultiResourcesImageLocal(t *testing.T) {
cmd.SetOutput(buf)
cmd.Flags().Set("output", "name")
cmd.Flags().Set("local", "true")
_, typer := f.Object()
tf.Printer = &printers.NamePrinter{Decoders: []runtime.Decoder{codec}, Typer: typer}
opts := ImageOptions{FilenameOptions: resource.FilenameOptions{
Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}},
@ -503,7 +497,6 @@ func TestSetImageRemote(t *testing.T) {
testapi.Default = testapi.Groups[input.testAPIGroup]
f, tf, _, ns := cmdtesting.NewAPIFactory()
codec := scheme.Codecs.CodecForVersions(scheme.Codecs.LegacyCodec(groupVersion), scheme.Codecs.UniversalDecoder(groupVersion), groupVersion, groupVersion)
tf.Printer = printers.NewVersionedPrinter(&printers.YAMLPrinter{}, legacyscheme.Scheme, legacyscheme.Scheme, scheme.Versions...)
tf.Namespace = "test"
tf.CategoryExpander = categories.LegacyCategoryExpander
tf.Client = &fake.RESTClient{

View File

@ -78,8 +78,6 @@ type ResourcesOptions struct {
Requests string
ResourceRequirements v1.ResourceRequirements
PrintSuccess func(shortOutput bool, out io.Writer, resource, name string, dryRun bool, operation string)
PrintObject func(cmd *cobra.Command, obj runtime.Object, out io.Writer) error
UpdatePodSpecForObject func(obj runtime.Object, fn func(*v1.PodSpec) error) (bool, error)
Resources []string
}
@ -126,13 +124,11 @@ func NewCmdResources(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.
}
func (o *ResourcesOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
o.PrintSuccess = f.PrintSuccess
o.UpdatePodSpecForObject = f.UpdatePodSpecForObject
o.Encoder = f.JSONEncoder()
o.Output = cmdutil.GetFlagString(cmd, "output")
o.Record = cmdutil.GetRecordFlag(cmd)
o.ChangeCause = f.Command(cmd, false)
o.PrintObject = f.PrintObject
o.Cmd = cmd
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
@ -238,7 +234,7 @@ func (o *ResourcesOptions) Run() error {
}
if o.Local || cmdutil.GetDryRunFlag(o.Cmd) {
if err := o.PrintObject(o.Cmd, patch.Info.AsVersioned(), o.Out); err != nil {
if err := cmdutil.PrintObject(o.Cmd, patch.Info.AsVersioned(), o.Out); err != nil {
return err
}
continue
@ -263,12 +259,12 @@ func (o *ResourcesOptions) Run() error {
shortOutput := o.Output == "name"
if len(o.Output) > 0 && !shortOutput {
if err := o.PrintObject(o.Cmd, info.AsVersioned(), o.Out); err != nil {
if err := cmdutil.PrintObject(o.Cmd, info.AsVersioned(), o.Out); err != nil {
return err
}
continue
}
o.PrintSuccess(shortOutput, o.Out, info.Mapping.Resource, info.Name, false, "resource requirements updated")
cmdutil.PrintSuccess(shortOutput, o.Out, info.Object, false, "resource requirements updated")
}
return utilerrors.NewAggregate(allErrs)
}

View File

@ -42,11 +42,10 @@ import (
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/kubectl/scheme"
"k8s.io/kubernetes/pkg/printers"
)
func TestResourcesLocal(t *testing.T) {
f, tf, codec, ns := cmdtesting.NewAPIFactory()
f, tf, _, ns := cmdtesting.NewAPIFactory()
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
NegotiatedSerializer: ns,
@ -63,8 +62,6 @@ func TestResourcesLocal(t *testing.T) {
cmd.SetOutput(buf)
cmd.Flags().Set("output", "name")
cmd.Flags().Set("local", "true")
_, typer := f.Object()
tf.Printer = &printers.NamePrinter{Decoders: []runtime.Decoder{codec}, Typer: typer}
opts := ResourcesOptions{FilenameOptions: resource.FilenameOptions{
Filenames: []string{"../../../../examples/storage/cassandra/cassandra-controller.yaml"}},
@ -90,7 +87,7 @@ func TestResourcesLocal(t *testing.T) {
}
func TestSetMultiResourcesLimitsLocal(t *testing.T) {
f, tf, codec, ns := cmdtesting.NewAPIFactory()
f, tf, _, ns := cmdtesting.NewAPIFactory()
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
NegotiatedSerializer: ns,
@ -107,8 +104,6 @@ func TestSetMultiResourcesLimitsLocal(t *testing.T) {
cmd.SetOutput(buf)
cmd.Flags().Set("output", "name")
cmd.Flags().Set("local", "true")
_, typer := f.Object()
tf.Printer = &printers.NamePrinter{Decoders: []runtime.Decoder{codec}, Typer: typer}
opts := ResourcesOptions{FilenameOptions: resource.FilenameOptions{
Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}},
@ -448,8 +443,6 @@ func TestSetResourcesRemote(t *testing.T) {
testapi.Default = testapi.Groups[input.testAPIGroup]
f, tf, _, ns := cmdtesting.NewAPIFactory()
codec := scheme.Codecs.CodecForVersions(scheme.Codecs.LegacyCodec(groupVersion), scheme.Codecs.UniversalDecoder(groupVersion), groupVersion, groupVersion)
_, typer := f.Object()
tf.Printer = &printers.NamePrinter{Decoders: []runtime.Decoder{testapi.Default.Codec()}, Typer: typer}
tf.Namespace = "test"
tf.CategoryExpander = categories.LegacyCategoryExpander
tf.Client = &fake.RESTClient{

View File

@ -49,7 +49,6 @@ type SelectorOptions struct {
selector *metav1.LabelSelector
out io.Writer
PrintSuccess func(shortOutput bool, out io.Writer, resource, name string, dryRun bool, operation string)
PrintObject func(obj runtime.Object) error
ClientForMapping func(mapping *meta.RESTMapping) (resource.RESTClient, error)
@ -116,8 +115,6 @@ func (o *SelectorOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args [
return err
}
o.PrintSuccess = f.PrintSuccess
o.changeCause = f.Command(cmd, false)
mapper, _ := f.Object()
o.mapper = mapper
@ -152,7 +149,7 @@ func (o *SelectorOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args [
}
o.PrintObject = func(obj runtime.Object) error {
return f.PrintObject(cmd, obj, o.out)
return cmdutil.PrintObject(cmd, obj, o.out)
}
o.ClientForMapping = func(mapping *meta.RESTMapping) (resource.RESTClient, error) {
return f.ClientForMapping(mapping)
@ -218,7 +215,7 @@ func (o *SelectorOptions) RunSelector() error {
if len(o.output) > 0 && !shortOutput {
return o.PrintObject(patched)
}
o.PrintSuccess(shortOutput, o.out, info.Mapping.Resource, info.Name, o.dryrun, "selector updated")
cmdutil.PrintSuccess(shortOutput, o.out, info.Object, o.dryrun, "selector updated")
return nil
})
}

View File

@ -33,7 +33,6 @@ import (
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/rest/fake"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
"k8s.io/kubernetes/pkg/printers"
)
func TestUpdateSelectorForObjectTypes(t *testing.T) {
@ -316,7 +315,7 @@ func TestGetResourcesAndSelector(t *testing.T) {
}
func TestSelectorTest(t *testing.T) {
f, tf, codec, ns := cmdtesting.NewAPIFactory()
f, tf, _, ns := cmdtesting.NewAPIFactory()
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
NegotiatedSerializer: ns,
@ -335,8 +334,6 @@ func TestSelectorTest(t *testing.T) {
cmd.Flags().Set("local", "true")
cmd.Flags().Set("filename", "../../../../examples/storage/cassandra/cassandra-service.yaml")
_, typer := f.Object()
tf.Printer = &printers.NamePrinter{Decoders: []runtime.Decoder{codec}, Typer: typer}
cmd.Run(cmd, []string{"environment=qa"})
if !strings.Contains(buf.String(), "service/cassandra") {

View File

@ -66,9 +66,7 @@ type serviceAccountConfig struct {
output string
changeCause string
local bool
PrintObject func(cmd *cobra.Command, obj runtime.Object, out io.Writer) error
updatePodSpecForObject func(runtime.Object, func(*v1.PodSpec) error) (bool, error)
printSuccess func(shortOutput bool, out io.Writer, resource, name string, dryRun bool, operation string)
infos []*resource.Info
serviceAccountName string
}
@ -113,10 +111,8 @@ func (saConfig *serviceAccountConfig) Complete(f cmdutil.Factory, cmd *cobra.Com
saConfig.dryRun = cmdutil.GetDryRunFlag(cmd)
saConfig.output = cmdutil.GetFlagString(cmd, "output")
saConfig.updatePodSpecForObject = f.UpdatePodSpecForObject
saConfig.PrintObject = f.PrintObject
saConfig.cmd = cmd
saConfig.printSuccess = f.PrintSuccess
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
if err != nil {
return err
@ -165,7 +161,7 @@ func (saConfig *serviceAccountConfig) Run() error {
continue
}
if saConfig.local || saConfig.dryRun {
if err := saConfig.PrintObject(saConfig.cmd, patch.Info.AsVersioned(), saConfig.out); err != nil {
if err := cmdutil.PrintObject(saConfig.cmd, patch.Info.AsVersioned(), saConfig.out); err != nil {
return err
}
continue
@ -184,12 +180,12 @@ func (saConfig *serviceAccountConfig) Run() error {
}
}
if len(saConfig.output) > 0 {
if err := saConfig.PrintObject(saConfig.cmd, info.AsVersioned(), saConfig.out); err != nil {
if err := cmdutil.PrintObject(saConfig.cmd, info.AsVersioned(), saConfig.out); err != nil {
return err
}
continue
}
saConfig.printSuccess(saConfig.shortOutput, saConfig.out, info.Mapping.Resource, info.Name, saConfig.dryRun, "serviceaccount updated")
cmdutil.PrintSuccess(saConfig.shortOutput, saConfig.out, info.Object, saConfig.dryRun, "serviceaccount updated")
}
return utilerrors.NewAggregate(patchErrs)
}

View File

@ -37,13 +37,11 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/rest/fake"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/kubectl/categories"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/kubectl/scheme"
"k8s.io/kubernetes/pkg/printers"
)
const serviceAccount = "serviceaccount1"
@ -84,7 +82,6 @@ func TestSetServiceAccountLocal(t *testing.T) {
cmd.Flags().Set("output", "yaml")
cmd.Flags().Set("local", "true")
testapi.Default = testapi.Groups[input.apiGroup]
tf.Printer = printers.NewVersionedPrinter(&printers.YAMLPrinter{}, legacyscheme.Scheme, legacyscheme.Scheme, scheme.Versions...)
saConfig := serviceAccountConfig{fileNameOptions: resource.FilenameOptions{
Filenames: []string{input.yaml}},
out: out,
@ -100,7 +97,7 @@ func TestSetServiceAccountLocal(t *testing.T) {
func TestSetServiceAccountMultiLocal(t *testing.T) {
testapi.Default = testapi.Groups[""]
f, tf, codec, ns := cmdtesting.NewAPIFactory()
f, tf, _, ns := cmdtesting.NewAPIFactory()
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
NegotiatedSerializer: ns,
@ -117,8 +114,6 @@ func TestSetServiceAccountMultiLocal(t *testing.T) {
cmd.SetOutput(buf)
cmd.Flags().Set("output", "name")
cmd.Flags().Set("local", "true")
_, typer := f.Object()
tf.Printer = &printers.NamePrinter{Decoders: []runtime.Decoder{codec}, Typer: typer}
opts := serviceAccountConfig{fileNameOptions: resource.FilenameOptions{
Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}},
out: buf,
@ -318,7 +313,6 @@ func TestSetServiceAccountRemote(t *testing.T) {
testapi.Default = testapi.Groups[input.testAPIGroup]
f, tf, _, ns := cmdtesting.NewAPIFactory()
codec := scheme.Codecs.CodecForVersions(scheme.Codecs.LegacyCodec(groupVersion), scheme.Codecs.UniversalDecoder(groupVersion), groupVersion, groupVersion)
tf.Printer = printers.NewVersionedPrinter(&printers.YAMLPrinter{}, legacyscheme.Scheme, legacyscheme.Scheme, scheme.Versions...)
tf.Namespace = "test"
tf.CategoryExpander = categories.LegacyCategoryExpander
tf.Client = &fake.RESTClient{

View File

@ -112,7 +112,7 @@ func (o *SubjectOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []
o.Output = cmdutil.GetFlagString(cmd, "output")
o.DryRun = cmdutil.GetDryRunFlag(cmd)
o.PrintObject = func(obj runtime.Object, out io.Writer) error {
return f.PrintObject(cmd, obj, out)
return cmdutil.PrintObject(cmd, obj, out)
}
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
@ -255,7 +255,7 @@ func (o *SubjectOptions) Run(f cmdutil.Factory, fn updateSubjects) error {
if len(o.Output) > 0 && !shortOutput {
return o.PrintObject(info.AsVersioned(), o.Out)
}
f.PrintSuccess(shortOutput, o.Out, info.Mapping.Resource, info.Name, false, "subjects updated")
cmdutil.PrintSuccess(shortOutput, o.Out, info.Object, false, "subjects updated")
}
return utilerrors.NewAggregate(allErrs)
}

View File

@ -276,10 +276,10 @@ func (o TaintOptions) RunTaint() error {
outputFormat := cmdutil.GetFlagString(o.cmd, "output")
if outputFormat != "" {
return o.f.PrintObject(o.cmd, outputObj, o.out)
return cmdutil.PrintObject(o.cmd, outputObj, o.out)
}
o.f.PrintSuccess(false, o.out, info.Mapping.Resource, info.Name, false, operation)
cmdutil.PrintSuccess(false, o.out, info.Object, false, operation)
return nil
})
}

View File

@ -5,8 +5,8 @@ args:
- service/svc1
namespace: "myproject"
expectedStdout:
- configmaps "cm1" edited
- services "svc1" edited
- configmap "cm1" edited
- service "svc1" edited
expectedExitCode: 0
steps:
- type: request

View File

@ -5,8 +5,8 @@ args:
- service/svc1
namespace: "myproject"
expectedStdout:
- configmaps "cm1" edited
- services "svc1" edited
- configmap "cm1" edited
- service "svc1" edited
expectedExitCode: 0
steps:
- type: request

View File

@ -4,7 +4,7 @@ args:
- service/svc1
namespace: myproject
expectedStdout:
- "services \"svc1\" edited"
- "service \"svc1\" edited"
expectedExitCode: 0
steps:
- type: request

View File

@ -6,7 +6,7 @@ args:
outputFormat: yaml
namespace: myproject
expectedStdout:
- services "svc1" edited
- service "svc1" edited
expectedExitCode: 0
steps:
- type: request

View File

@ -3,7 +3,7 @@ mode: create
filename: "svc.yaml"
namespace: "edit-test"
expectedStdout:
- "services \"svc1\" created"
- "service \"svc1\" created"
expectedStderr:
- "\"svc2\" is invalid"
expectedExitCode: 1

View File

@ -3,8 +3,8 @@ mode: create
filename: "svc.yaml"
namespace: "edit-test"
expectedStdout:
- services "svc1" created
- services "svc2" created
- service "svc1" created
- service "svc2" created
expectedExitCode: 0
steps:
- type: edit

View File

@ -5,7 +5,7 @@ args:
- svc1
namespace: edit-test
expectedStdout:
- services "svc1" edited
- service "svc1" edited
expectedStderr:
- "error: services \"svc1\" is invalid"
expectedExitCode: 0

View File

@ -11,7 +11,7 @@ outputPatch: "true"
namespace: edit-test
expectedStdout:
- 'Patch: {"metadata":{"annotations":{"kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"annotations\":{},\"creationTimestamp\":\"2017-02-27T19:40:53Z\",\"labels\":{\"app\":\"svc1\",\"new-label\":\"new-value\"},\"name\":\"svc1\",\"namespace\":\"edit-test\",\"resourceVersion\":\"670\",\"selfLink\":\"/api/v1/namespaces/edit-test/services/svc1\",\"uid\":\"a6c11186-fd24-11e6-b53c-480fcf4a5275\"},\"spec\":{\"clusterIP\":\"10.0.0.204\",\"ports\":[{\"name\":\"80\",\"port\":80,\"protocol\":\"TCP\",\"targetPort\":80}],\"selector\":{\"app\":\"svc1\"},\"sessionAffinity\":\"None\",\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}\n"},"labels":{"new-label":"new-value"}}}'
- services "svc1" edited
- service "svc1" edited
expectedExitCode: 0
steps:
- type: request

View File

@ -4,8 +4,8 @@ args:
- configmaps,services
namespace: "edit-test"
expectedStdout:
- configmaps "cm1" edited
- services "svc1" edited
- configmap "cm1" edited
- service "svc1" edited
expectedExitCode: 0
steps:
- type: request

View File

@ -5,8 +5,8 @@ args:
- service/svc1
namespace: "edit-test"
expectedStdout:
- configmaps "cm1" edited
- services "svc1" edited
- configmap "cm1" edited
- service "svc1" edited
expectedExitCode: 0
steps:
- type: request

View File

@ -5,8 +5,8 @@ args:
- service/svc1
namespace: "edit-test"
expectedStdout:
- configmaps "cm1" edited
- services "svc1" edited
- configmap "cm1" edited
- service "svc1" edited
expectedExitCode: 0
steps:
- type: request

View File

@ -9,7 +9,7 @@ args:
saveConfig: "false"
namespace: edit-test
expectedStdout:
- services "svc1" edited
- service "svc1" edited
expectedExitCode: 0
steps:
- type: request

View File

@ -6,9 +6,9 @@ args:
- bars/test2
namespace: default
expectedStdout:
- "services \"kubernetes\" edited"
- "bars \"test\" edited"
- "bars \"test2\" edited"
- "service \"kubernetes\" edited"
- "bar.company.com \"test\" edited"
- "bar.company.com \"test2\" edited"
expectedExitCode: 0
steps:
- type: request

View File

@ -8,7 +8,7 @@ args:
- svc1
namespace: edit-test
expectedStdout:
- services "svc1" edited
- service "svc1" edited
expectedExitCode: 0
steps:
- type: request

View File

@ -4,7 +4,7 @@ args:
- service/kubernetes
namespace: default
expectedStdout:
- "services \"kubernetes\" edited"
- "service \"kubernetes\" edited"
expectedExitCode: 0
steps:
- type: request

View File

@ -4,7 +4,7 @@ args:
- storageclasses.v1beta1.storage.k8s.io/foo
namespace: default
expectedStdout:
- "storageclasses \"foo\" edited"
- "storageclass.storage.k8s.io \"foo\" edited"
expectedExitCode: 0
steps:
- type: request

View File

@ -4,7 +4,7 @@ args:
- storageclasses.v0.storage.k8s.io/foo
namespace: default
expectedStdout:
- "storageclasses \"foo\" edited"
- "storageclass.storage.k8s.io \"foo\" edited"
expectedExitCode: 0
steps:
- type: request

View File

@ -9,7 +9,7 @@ args:
saveConfig: "true"
namespace: edit-test
expectedStdout:
- services "svc1" edited
- service "svc1" edited
expectedExitCode: 0
steps:
- type: request

View File

@ -185,7 +185,11 @@ var ValidVersionGV = schema.GroupVersion{Group: "apitest", Version: ValidVersion
func newExternalScheme() (*runtime.Scheme, meta.RESTMapper, runtime.Codec) {
scheme := runtime.NewScheme()
mapper, codec := AddToScheme(scheme)
return scheme, mapper, codec
}
func AddToScheme(scheme *runtime.Scheme) (meta.RESTMapper, runtime.Codec) {
scheme.AddKnownTypeWithName(InternalGV.WithKind("Type"), &InternalType{})
scheme.AddKnownTypeWithName(UnlikelyGV.WithKind("Type"), &ExternalType{})
//This tests that kubectl will not confuse the external scheme with the internal scheme, even when they accidentally have versions of the same name.
@ -213,7 +217,7 @@ func newExternalScheme() (*runtime.Scheme, meta.RESTMapper, runtime.Codec) {
}
}
return scheme, mapper, codec
return mapper, codec
}
type fakeCachedDiscoveryClient struct {
@ -237,7 +241,6 @@ type TestFactory struct {
Client kubectl.RESTClient
UnstructuredClient kubectl.RESTClient
Describer printers.Describer
Printer printers.ResourcePrinter
Validator validation.Schema
Namespace string
ClientConfig *restclient.Config
@ -358,50 +361,6 @@ func (f *FakeFactory) Describer(*meta.RESTMapping) (printers.Describer, error) {
return f.tf.Describer, f.tf.Err
}
func (f *FakeFactory) PrinterForOptions(options *printers.PrintOptions) (printers.ResourcePrinter, error) {
return f.tf.Printer, f.tf.Err
}
func (f *FakeFactory) PrintResourceInfoForCommand(cmd *cobra.Command, info *resource.Info, out io.Writer) error {
printer, err := f.PrinterForOptions(&printers.PrintOptions{})
if err != nil {
return err
}
if !printer.IsGeneric() {
printer, err = f.PrinterForMapping(&printers.PrintOptions{})
if err != nil {
return err
}
}
return printer.PrintObj(info.Object, out)
}
func (f *FakeFactory) PrintSuccess(shortOutput bool, out io.Writer, resource, name string, dryRun bool, operation string) {
dryRunMsg := ""
if dryRun {
dryRunMsg = " (dry run)"
}
if shortOutput {
// -o name: prints resource/name
if len(resource) > 0 {
fmt.Fprintf(out, "%s/%s\n", resource, name)
} else {
fmt.Fprintf(out, "%s\n", name)
}
} else {
// understandable output by default
if len(resource) > 0 {
fmt.Fprintf(out, "%s \"%s\" %s%s\n", resource, name, operation, dryRunMsg)
} else {
fmt.Fprintf(out, "\"%s\" %s%s\n", name, operation, dryRunMsg)
}
}
}
func (f *FakeFactory) Printer(mapping *meta.RESTMapping, options printers.PrintOptions) (printers.ResourcePrinter, error) {
return f.tf.Printer, f.tf.Err
}
func (f *FakeFactory) Scaler(*meta.RESTMapping) (kubectl.Scaler, error) {
return nil, nil
}
@ -514,14 +473,6 @@ func (f *FakeFactory) BindFlags(flags *pflag.FlagSet) {
func (f *FakeFactory) BindExternalFlags(flags *pflag.FlagSet) {
}
func (f *FakeFactory) PrintObject(cmd *cobra.Command, obj runtime.Object, out io.Writer) error {
return nil
}
func (f *FakeFactory) PrinterForMapping(printOpts *printers.PrintOptions) (printers.ResourcePrinter, error) {
return f.tf.Printer, f.tf.Err
}
func (f *FakeFactory) NewBuilder() *resource.Builder {
mapper, typer := f.Object()
@ -753,54 +704,10 @@ func (f *fakeAPIFactory) UnstructuredClientForMapping(m *meta.RESTMapping) (reso
return f.tf.UnstructuredClient, f.tf.Err
}
func (f *fakeAPIFactory) PrinterForOptions(options *printers.PrintOptions) (printers.ResourcePrinter, error) {
return f.tf.Printer, f.tf.Err
}
func (f *fakeAPIFactory) PrintResourceInfoForCommand(cmd *cobra.Command, info *resource.Info, out io.Writer) error {
printer, err := f.PrinterForOptions(&printers.PrintOptions{})
if err != nil {
return err
}
if !printer.IsGeneric() {
printer, err = f.PrinterForMapping(&printers.PrintOptions{})
if err != nil {
return err
}
}
return printer.PrintObj(info.Object, out)
}
func (f *fakeAPIFactory) PrintSuccess(shortOutput bool, out io.Writer, resource, name string, dryRun bool, operation string) {
dryRunMsg := ""
if dryRun {
dryRunMsg = " (dry run)"
}
if shortOutput {
// -o name: prints resource/name
if len(resource) > 0 {
fmt.Fprintf(out, "%s/%s\n", resource, name)
} else {
fmt.Fprintf(out, "%s\n", name)
}
} else {
// understandable output by default
if len(resource) > 0 {
fmt.Fprintf(out, "%s \"%s\" %s%s\n", resource, name, operation, dryRunMsg)
} else {
fmt.Fprintf(out, "\"%s\" %s%s\n", name, operation, dryRunMsg)
}
}
}
func (f *fakeAPIFactory) Describer(*meta.RESTMapping) (printers.Describer, error) {
return f.tf.Describer, f.tf.Err
}
func (f *fakeAPIFactory) Printer(mapping *meta.RESTMapping, options printers.PrintOptions) (printers.ResourcePrinter, error) {
return f.tf.Printer, f.tf.Err
}
func (f *fakeAPIFactory) LogsForObject(object, options runtime.Object, timeout time.Duration) (*restclient.Request, error) {
c, err := f.ClientSet()
if err != nil {
@ -848,18 +755,6 @@ func (f *fakeAPIFactory) Generators(cmdName string) map[string]kubectl.Generator
return cmdutil.DefaultGenerators(cmdName)
}
func (f *fakeAPIFactory) PrintObject(cmd *cobra.Command, obj runtime.Object, out io.Writer) error {
printer, err := f.PrinterForMapping(&printers.PrintOptions{})
if err != nil {
return err
}
return printer.PrintObj(obj, out)
}
func (f *fakeAPIFactory) PrinterForMapping(outputOpts *printers.PrintOptions) (printers.ResourcePrinter, error) {
return f.tf.Printer, f.tf.Err
}
func (f *fakeAPIFactory) NewBuilder() *resource.Builder {
mapper, typer := f.Object()

View File

@ -48,7 +48,6 @@ func TestTopNodeAllMetrics(t *testing.T) {
expectedNodePath := fmt.Sprintf("/%s/%s/nodes", apiPrefix, apiVersion)
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -97,7 +96,6 @@ func TestTopNodeAllMetricsCustomDefaults(t *testing.T) {
expectedNodePath := fmt.Sprintf("/%s/%s/nodes", apiPrefix, apiVersion)
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -156,7 +154,6 @@ func TestTopNodeWithNameMetrics(t *testing.T) {
expectedNodePath := fmt.Sprintf("/%s/%s/nodes/%s", apiPrefix, apiVersion, expectedMetrics.Name)
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -219,7 +216,6 @@ func TestTopNodeWithLabelSelectorMetrics(t *testing.T) {
expectedNodePath := fmt.Sprintf("/%s/%s/nodes", apiPrefix, apiVersion)
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -270,7 +266,6 @@ func TestTopNodeAllMetricsFromMetricsServer(t *testing.T) {
expectedNodePath := fmt.Sprintf("/%s/%s/nodes", apiPrefix, apiVersion)
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -320,7 +315,6 @@ func TestTopNodeWithNameMetricsFromMetricsServer(t *testing.T) {
expectedNodePath := fmt.Sprintf("/%s/%s/nodes/%s", apiPrefix, apiVersion, expectedMetrics.Name)
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -380,7 +374,6 @@ func TestTopNodeWithLabelSelectorMetricsFromMetricsServer(t *testing.T) {
expectedNodePath := fmt.Sprintf("/%s/%s/nodes", apiPrefix, apiVersion)
f, tf, codec, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {

View File

@ -162,7 +162,6 @@ func TestTopPod(t *testing.T) {
}
f, tf, _, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -303,7 +302,6 @@ func TestTopPodWithMetricsServer(t *testing.T) {
}
f, tf, _, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
@ -482,7 +480,6 @@ func TestTopPodCustomDefaults(t *testing.T) {
}
f, tf, _, ns := cmdtesting.NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {

View File

@ -405,14 +405,14 @@ func (o *EditOptions) visitToApplyEditPatch(originalInfos []*resource.Info, patc
}
if reflect.DeepEqual(originalJS, editedJS) {
o.f.PrintSuccess(false, o.Out, info.Mapping.Resource, info.Name, false, "skipped")
cmdutil.PrintSuccess(false, o.Out, info.Object, false, "skipped")
return nil
} else {
err := o.annotationPatch(info)
if err != nil {
return err
}
o.f.PrintSuccess(false, o.Out, info.Mapping.Resource, info.Name, false, "edited")
cmdutil.PrintSuccess(false, o.Out, info.Object, false, "edited")
return nil
}
})
@ -531,7 +531,7 @@ func (o *EditOptions) visitToPatch(originalInfos []*resource.Info, patchVisitor
if reflect.DeepEqual(originalJS, editedJS) {
// no edit, so just skip it.
o.f.PrintSuccess(false, o.Out, info.Mapping.Resource, info.Name, false, "skipped")
cmdutil.PrintSuccess(false, o.Out, info.Object, false, "skipped")
return nil
}
@ -585,7 +585,7 @@ func (o *EditOptions) visitToPatch(originalInfos []*resource.Info, patchVisitor
return nil
}
info.Refresh(patched, true)
o.f.PrintSuccess(false, o.Out, info.Mapping.Resource, info.Name, false, "edited")
cmdutil.PrintSuccess(false, o.Out, info.Object, false, "edited")
return nil
})
return err
@ -596,7 +596,7 @@ func (o *EditOptions) visitToCreate(createVisitor resource.Visitor) error {
if err := resource.CreateAndRefresh(info); err != nil {
return err
}
o.f.PrintSuccess(false, o.Out, info.Mapping.Resource, info.Name, false, "created")
cmdutil.PrintSuccess(false, o.Out, info.Object, false, "created")
return nil
})
return err

View File

@ -150,8 +150,6 @@ type ClientAccessFactory interface {
// SuggestedPodTemplateResources returns a list of resource types that declare a pod template
SuggestedPodTemplateResources() []schema.GroupResource
// Returns a Printer for formatting objects of the given type or an error.
Printer(mapping *meta.RESTMapping, options printers.PrintOptions) (printers.ResourcePrinter, error)
// Pauser marks the object in the info as paused. Currently supported only for Deployments.
// Returns the patched object in bytes and any error that occurred during the encoding or
// in case the object is already paused.
@ -231,24 +229,6 @@ type ObjectMappingFactory interface {
// BuilderFactory holds the third level of factory methods. These functions depend upon ObjectMappingFactory and ClientAccessFactory methods.
// Generally they depend upon client mapper functions
type BuilderFactory interface {
// PrinterForCommand returns the default printer for the command. It requires that certain options
// are declared on the command (see AddPrinterFlags). Returns a printer, or an error if a printer
// could not be found.
PrinterForOptions(options *printers.PrintOptions) (printers.ResourcePrinter, error)
// PrinterForMapping returns a printer suitable for displaying the provided resource type.
// Requires that printer flags have been added to cmd (see AddPrinterFlags).
// Returns a printer, true if the printer is generic (is not internal), or
// an error if a printer could not be found.
PrinterForMapping(options *printers.PrintOptions) (printers.ResourcePrinter, error)
// PrintObject prints an api object given command line flags to modify the output format
PrintObject(cmd *cobra.Command, obj runtime.Object, out io.Writer) error
// PrintResourceInfoForCommand receives a *cobra.Command and a *resource.Info and
// attempts to print an info object based on the specified output format. If the
// object passed is non-generic, it attempts to print the object using a HumanReadablePrinter.
// Requires that printer flags have been added to cmd (see AddPrinterFlags).
PrintResourceInfoForCommand(cmd *cobra.Command, info *resource.Info, out io.Writer) error
// PrintSuccess prints message after finishing mutating operations
PrintSuccess(shortOutput bool, out io.Writer, resource, name string, dryRun bool, operation string)
// NewBuilder returns an object that assists in loading objects from both disk and the server
// and which implements the common patterns for CLI interactions with generic resources.
NewBuilder() *resource.Builder
@ -258,16 +238,6 @@ type BuilderFactory interface {
PluginRunner() plugins.PluginRunner
}
func getGroupVersionKinds(gvks []schema.GroupVersionKind, group string) []schema.GroupVersionKind {
result := []schema.GroupVersionKind{}
for ix := range gvks {
if gvks[ix].Group == group {
result = append(result, gvks[ix])
}
}
return result
}
type factory struct {
ClientAccessFactory
ObjectMappingFactory

View File

@ -19,19 +19,11 @@ limitations under the License.
package util
import (
"fmt"
"io"
"os"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/kubectl/plugins"
"k8s.io/kubernetes/pkg/kubectl/resource"
kubectlscheme "k8s.io/kubernetes/pkg/kubectl/scheme"
"k8s.io/kubernetes/pkg/printers"
)
type ring2Factory struct {
@ -48,66 +40,6 @@ func NewBuilderFactory(clientAccessFactory ClientAccessFactory, objectMappingFac
return f
}
func (f *ring2Factory) PrinterForOptions(options *printers.PrintOptions) (printers.ResourcePrinter, error) {
return printerForOptions(options)
}
func (f *ring2Factory) PrinterForMapping(options *printers.PrintOptions) (printers.ResourcePrinter, error) {
printer, err := f.PrinterForOptions(options)
if err != nil {
return nil, err
}
// wrap the printer in a versioning printer that understands when to convert and when not to convert
printer = printers.NewVersionedPrinter(printer, legacyscheme.Scheme, legacyscheme.Scheme, kubectlscheme.Versions...)
return printer, nil
}
func (f *ring2Factory) PrintSuccess(shortOutput bool, out io.Writer, resource, name string, dryRun bool, operation string) {
dryRunMsg := ""
if dryRun {
dryRunMsg = " (dry run)"
}
if shortOutput {
// -o name: prints resource/name
if len(resource) > 0 {
fmt.Fprintf(out, "%s/%s\n", resource, name)
} else {
fmt.Fprintf(out, "%s\n", name)
}
} else {
// understandable output by default
if len(resource) > 0 {
fmt.Fprintf(out, "%s \"%s\" %s%s\n", resource, name, operation, dryRunMsg)
} else {
fmt.Fprintf(out, "\"%s\" %s%s\n", name, operation, dryRunMsg)
}
}
}
func (f *ring2Factory) PrintObject(cmd *cobra.Command, obj runtime.Object, out io.Writer) error {
printer, err := f.PrinterForMapping(ExtractCmdPrintOptions(cmd, false))
if err != nil {
return err
}
return printer.PrintObj(obj, out)
}
func (f *ring2Factory) PrintResourceInfoForCommand(cmd *cobra.Command, info *resource.Info, out io.Writer) error {
printOpts := ExtractCmdPrintOptions(cmd, false)
printer, err := f.PrinterForOptions(printOpts)
if err != nil {
return err
}
if !printer.IsGeneric() {
printer, err = f.PrinterForMapping(printOpts)
if err != nil {
return err
}
}
return printer.PrintObj(info.Object, out)
}
// NewBuilder returns a new resource builder for structured api objects.
func (f *ring2Factory) NewBuilder() *resource.Builder {
clientMapperFunc := resource.ClientMapperFunc(f.objectMappingFactory.ClientForMapping)

View File

@ -62,8 +62,6 @@ import (
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/kubectl/util/transport"
"k8s.io/kubernetes/pkg/printers"
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
)
type ring0Factory struct {
@ -446,12 +444,6 @@ func (f *ring0Factory) SuggestedPodTemplateResources() []schema.GroupResource {
}
}
func (f *ring0Factory) Printer(mapping *meta.RESTMapping, options printers.PrintOptions) (printers.ResourcePrinter, error) {
p := printers.NewHumanReadablePrinter(f.JSONEncoder(), f.Decoder(true), options)
printersinternal.AddHandlers(p)
return p, nil
}
func (f *ring0Factory) Pauser(info *resource.Info) ([]byte, error) {
switch obj := info.Object.(type) {
case *extensions.Deployment:

View File

@ -18,8 +18,10 @@ package util
import (
"fmt"
"io"
"strings"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
@ -29,6 +31,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/kubernetes/pkg/api/legacyscheme"
)
// AddPrinterFlags adds printing related flags to a command (e.g. output format, no headers, template path)
@ -82,10 +85,54 @@ func ValidateOutputArgs(cmd *cobra.Command) error {
return nil
}
// printerForOptions returns the printer for the outputOptions (if given) or
// returns the default printer for the command. Requires that printer flags have
// been added to cmd (see AddPrinterFlags).
func printerForOptions(options *printers.PrintOptions) (printers.ResourcePrinter, error) {
// PrintSuccess prints a success message and can do a "-o name" as "shortOutput"
// TODO this should really just be a printer. It's got just about the exact same signature.
func PrintSuccess(shortOutput bool, out io.Writer, obj runtime.Object, dryRun bool, operation string) {
dryRunMsg := ""
if dryRun {
dryRunMsg = " (dry run)"
}
// match name printer format
name := "<unknown>"
if acc, err := meta.Accessor(obj); err == nil {
if n := acc.GetName(); len(n) > 0 {
name = n
}
}
// legacy scheme to be sure we work ok with internal types.
// TODO internal types aren't supposed to exist here
groupKind := printers.GetObjectGroupKind(obj, legacyscheme.Scheme)
kindString := fmt.Sprintf("%s.%s", strings.ToLower(groupKind.Kind), groupKind.Group)
if len(groupKind.Group) == 0 {
kindString = strings.ToLower(groupKind.Kind)
}
if shortOutput {
// -o name: prints resource/name
fmt.Fprintf(out, "%s/%s\n", kindString, name)
return
}
// understandable output by default
fmt.Fprintf(out, "%s \"%s\" %s%s\n", kindString, name, operation, dryRunMsg)
}
// PrintObject prints a single object based on the default command options
// TODO this should go away once commands can embed the PrintOptions instead
func PrintObject(cmd *cobra.Command, obj runtime.Object, out io.Writer) error {
printer, err := PrinterForOptions(ExtractCmdPrintOptions(cmd, false))
if err != nil {
return err
}
return printer.PrintObj(obj, out)
}
// PrinterForOptions returns the printer for the outputOptions (if given) or
// returns the default printer for the command.
// TODO this should become a function on the PrintOptions struct
func PrinterForOptions(options *printers.PrintOptions) (printers.ResourcePrinter, error) {
// TODO: used by the custom column implementation and the name implementation, break this dependency
decoders := []runtime.Decoder{kubectlscheme.Codecs.UniversalDecoder(), unstructured.UnstructuredJSONScheme}
encoder := kubectlscheme.Codecs.LegacyCodec(kubectlscheme.Registry.EnabledVersions()...)
@ -98,11 +145,18 @@ func printerForOptions(options *printers.PrintOptions) (printers.ResourcePrinter
// we try to convert to HumanReadablePrinter, if return ok, it must be no generic
// we execute AddHandlers() here before maybeWrapSortingPrinter so that we don't
// need to convert to delegatePrinter again then invoke AddHandlers()
// TODO this looks highly questionable. human readable printers are baked into code. This can just live in the definition of the handler itself
// TODO or be registered there
if humanReadablePrinter, ok := printer.(printers.PrintHandler); ok {
printersinternal.AddHandlers(humanReadablePrinter)
}
return maybeWrapSortingPrinter(printer, *options), nil
printer = maybeWrapSortingPrinter(printer, *options)
// wrap the printer in a versioning printer that understands when to convert and when not to convert
printer = printers.NewVersionedPrinter(printer, legacyscheme.Scheme, legacyscheme.Scheme, kubectlscheme.Versions...)
return printer, nil
}
// ExtractCmdPrintOptions parses printer specific commandline args and
@ -230,17 +284,13 @@ func ValidResourceTypeList(f ClientAccessFactory) string {
}
// Retrieve a list of handled resources from printer as valid args
// TODO: This function implementation should be replaced with a real implementation from the
// discovery service.
// TODO: This function implementation should be replaced with a real implementation from the discovery service.
func ValidArgList(f ClientAccessFactory) []string {
validArgs := []string{}
p, err := f.Printer(nil, printers.PrintOptions{
ColumnLabels: []string{},
})
CheckErr(err)
if p != nil {
validArgs = p.HandledResources()
}
humanReadablePrinter := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{})
printersinternal.AddHandlers(humanReadablePrinter)
validArgs = humanReadablePrinter.HandledResources()
return validArgs
}

View File

@ -24,6 +24,7 @@ go_library(
importpath = "k8s.io/kubernetes/pkg/printers",
deps = [
"//vendor/github.com/ghodss/yaml:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",

View File

@ -24,6 +24,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
)
@ -63,47 +64,47 @@ func (p *NamePrinter) PrintObj(obj runtime.Object, w io.Writer) error {
}
}
return printObj(w, name, GetObjectGroupKind(obj, p.Typer))
}
func GetObjectGroupKind(obj runtime.Object, typer runtime.ObjectTyper) schema.GroupKind {
if obj == nil {
return schema.GroupKind{Kind: "<unknown>"}
}
groupVersionKind := obj.GetObjectKind().GroupVersionKind()
if len(groupVersionKind.Kind) > 0 {
kind := groupVersionKind.Kind
group := groupVersionKind.Group
return printObj(w, name, group, kind)
return groupVersionKind.GroupKind()
}
if gvks, _, err := p.Typer.ObjectKinds(obj); err == nil {
if gvks, _, err := typer.ObjectKinds(obj); err == nil {
for _, gvk := range gvks {
if len(gvk.Kind) == 0 {
continue
}
return printObj(w, name, gvk.Group, gvk.Kind)
return gvk.GroupKind()
}
}
if uns, ok := obj.(*unstructured.Unstructured); ok {
group := uns.GroupVersionKind().Group
kind := uns.GroupVersionKind().Kind
if len(kind) > 0 {
return printObj(w, name, group, kind)
if len(uns.GroupVersionKind().Kind) > 0 {
return uns.GroupVersionKind().GroupKind()
}
}
fmt.Fprintf(w, "<unknown>/%s\n", name)
return nil
return schema.GroupKind{Kind: "<unknown>"}
}
func printObj(w io.Writer, name, group, kind string) error {
if len(kind) == 0 {
func printObj(w io.Writer, name string, groupKind schema.GroupKind) error {
if len(groupKind.Kind) == 0 {
return fmt.Errorf("missing kind for resource with name %v", name)
}
if len(group) == 0 {
fmt.Fprintf(w, "%s/%s\n", strings.ToLower(kind), name)
if len(groupKind.Group) == 0 {
fmt.Fprintf(w, "%s/%s\n", strings.ToLower(groupKind.Kind), name)
return nil
}
fmt.Fprintf(w, "%s.%s/%s\n", strings.ToLower(kind), group, name)
fmt.Fprintf(w, "%s.%s/%s\n", strings.ToLower(groupKind.Kind), groupKind.Group, name)
return nil
}

View File

@ -20,6 +20,7 @@ import (
"fmt"
"io"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
@ -50,17 +51,6 @@ func (p *VersionedPrinter) AfterPrint(w io.Writer, res string) error {
// PrintObj implements ResourcePrinter
func (p *VersionedPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
gvks, _, err := p.typer.ObjectKinds(obj)
if err != nil {
return err
}
needsConversion := false
for _, gvk := range gvks {
if len(gvk.Version) == 0 || gvk.Version == runtime.APIVersionInternal {
needsConversion = true
}
}
// if we're unstructured, no conversion necessary
if _, ok := obj.(*unstructured.Unstructured); ok {
return p.printer.PrintObj(obj, w)
@ -73,6 +63,18 @@ func (p *VersionedPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
}
// if we're already external, no conversion necessary
gvks, _, err := p.typer.ObjectKinds(obj)
if err != nil {
glog.V(1).Info("error determining type for %T, using passed object: %v", obj, err)
return p.printer.PrintObj(obj, w)
}
needsConversion := false
for _, gvk := range gvks {
if len(gvk.Version) == 0 || gvk.Version == runtime.APIVersionInternal {
needsConversion = true
}
}
if !needsConversion {
return p.printer.PrintObj(obj, w)
}