Merge pull request #12112 from JanetKuo/update-kubectl-get-pods-running

Hide terminated pods in 'kubectl' Printer by default
This commit is contained in:
Saad Ali
2015-08-19 12:51:18 -07:00
27 changed files with 402 additions and 92 deletions

View File

@@ -261,6 +261,7 @@ const (
NodeUnschedulable = "spec.unschedulable"
ObjectNameField = "metadata.name"
PodHost = "spec.nodeName"
PodStatus = "status.phase"
SecretType = "type"
EventReason = "reason"
@@ -318,7 +319,8 @@ var fieldMappings = versionToResourceToFieldMapping{
NodeUnschedulable: "spec.unschedulable",
},
"pods": clientFieldNameToAPIVersionFieldName{
PodHost: "spec.nodeName",
PodHost: "spec.nodeName",
PodStatus: "status.phase",
},
"secrets": clientFieldNameToAPIVersionFieldName{
SecretType: "type",

View File

@@ -152,7 +152,7 @@ func NewTestFactory() (*cmdutil.Factory, *testFactory, runtime.Codec) {
Describer: func(*meta.RESTMapping) (kubectl.Describer, error) {
return t.Describer, t.Err
},
Printer: func(mapping *meta.RESTMapping, noHeaders, withNamespace bool, wide bool, columnLabels []string) (kubectl.ResourcePrinter, error) {
Printer: func(mapping *meta.RESTMapping, noHeaders, withNamespace bool, wide bool, showAll bool, columnLabels []string) (kubectl.ResourcePrinter, error) {
return t.Printer, t.Err
},
Validator: func() (validation.Schema, error) {
@@ -207,7 +207,7 @@ func NewAPIFactory() (*cmdutil.Factory, *testFactory, runtime.Codec) {
Describer: func(*meta.RESTMapping) (kubectl.Describer, error) {
return t.Describer, t.Err
},
Printer: func(mapping *meta.RESTMapping, noHeaders, withNamespace bool, wide bool, columnLabels []string) (kubectl.ResourcePrinter, error) {
Printer: func(mapping *meta.RESTMapping, noHeaders, withNamespace bool, wide bool, showAll bool, columnLabels []string) (kubectl.ResourcePrinter, error) {
return t.Printer, t.Err
},
Validator: func() (validation.Schema, error) {
@@ -256,7 +256,7 @@ func stringBody(body string) io.ReadCloser {
func ExamplePrintReplicationControllerWithNamespace() {
f, tf, codec := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false, true, false, []string{})
tf.Printer = kubectl.NewHumanReadablePrinter(false, true, false, false, []string{})
tf.Client = &client.FakeRESTClient{
Codec: codec,
Client: nil,
@@ -298,7 +298,7 @@ func ExamplePrintReplicationControllerWithNamespace() {
func ExamplePrintPodWithWideFormat() {
f, tf, codec := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false, false, true, []string{})
tf.Printer = kubectl.NewHumanReadablePrinter(false, false, true, false, []string{})
tf.Client = &client.FakeRESTClient{
Codec: codec,
Client: nil,
@@ -331,9 +331,143 @@ func ExamplePrintPodWithWideFormat() {
// test1 1/2 podPhase 6 10y kubernetes-minion-abcd
}
func newAllPhasePodList() *api.PodList {
nodeName := "kubernetes-minion-abcd"
return &api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{
Name: "test1",
CreationTimestamp: util.Time{time.Now().AddDate(-10, 0, 0)},
},
Spec: api.PodSpec{
Containers: make([]api.Container, 2),
NodeName: nodeName,
},
Status: api.PodStatus{
Phase: api.PodPending,
ContainerStatuses: []api.ContainerStatus{
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
{RestartCount: 3},
},
},
},
{
ObjectMeta: api.ObjectMeta{
Name: "test2",
CreationTimestamp: util.Time{time.Now().AddDate(-10, 0, 0)},
},
Spec: api.PodSpec{
Containers: make([]api.Container, 2),
NodeName: nodeName,
},
Status: api.PodStatus{
Phase: api.PodRunning,
ContainerStatuses: []api.ContainerStatus{
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
{RestartCount: 3},
},
},
},
{
ObjectMeta: api.ObjectMeta{
Name: "test3",
CreationTimestamp: util.Time{time.Now().AddDate(-10, 0, 0)},
},
Spec: api.PodSpec{
Containers: make([]api.Container, 2),
NodeName: nodeName,
},
Status: api.PodStatus{
Phase: api.PodSucceeded,
ContainerStatuses: []api.ContainerStatus{
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
{RestartCount: 3},
},
},
},
{
ObjectMeta: api.ObjectMeta{
Name: "test4",
CreationTimestamp: util.Time{time.Now().AddDate(-10, 0, 0)},
},
Spec: api.PodSpec{
Containers: make([]api.Container, 2),
NodeName: nodeName,
},
Status: api.PodStatus{
Phase: api.PodFailed,
ContainerStatuses: []api.ContainerStatus{
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
{RestartCount: 3},
},
},
},
{
ObjectMeta: api.ObjectMeta{
Name: "test5",
CreationTimestamp: util.Time{time.Now().AddDate(-10, 0, 0)},
},
Spec: api.PodSpec{
Containers: make([]api.Container, 2),
NodeName: nodeName,
},
Status: api.PodStatus{
Phase: api.PodUnknown,
ContainerStatuses: []api.ContainerStatus{
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
{RestartCount: 3},
},
},
}},
}
}
func ExamplePrintPodHideTerminated() {
f, tf, codec := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false, false, false, false, []string{})
tf.Client = &client.FakeRESTClient{
Codec: codec,
Client: nil,
}
cmd := NewCmdRun(f, os.Stdin, os.Stdout, os.Stderr)
podList := newAllPhasePodList()
err := f.PrintObject(cmd, podList, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
// Output:
// NAME READY STATUS RESTARTS AGE
// test1 1/2 Pending 6 10y
// test2 1/2 Running 6 10y
// test5 1/2 Unknown 6 10y
}
func ExamplePrintPodShowAll() {
f, tf, codec := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false, false, false, true, []string{})
tf.Client = &client.FakeRESTClient{
Codec: codec,
Client: nil,
}
cmd := NewCmdRun(f, os.Stdin, os.Stdout, os.Stderr)
podList := newAllPhasePodList()
err := f.PrintObject(cmd, podList, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
// Output:
// NAME READY STATUS RESTARTS AGE
// test1 1/2 Pending 6 10y
// test2 1/2 Running 6 10y
// test3 1/2 Succeeded 6 10y
// test4 1/2 Failed 6 10y
// test5 1/2 Unknown 6 10y
}
func ExamplePrintServiceWithNamespacesAndLabels() {
f, tf, codec := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false, true, false, []string{"l1"})
tf.Printer = kubectl.NewHumanReadablePrinter(false, true, false, false, []string{"l1"})
tf.Client = &client.FakeRESTClient{
Codec: codec,
Client: nil,

View File

@@ -58,7 +58,7 @@ $ kubectl delete pods --all`
)
func NewCmdDelete(f *cmdutil.Factory, out io.Writer) *cobra.Command {
p := kubectl.NewHumanReadablePrinter(false, false, false, []string{})
p := kubectl.NewHumanReadablePrinter(false, false, false, false, []string{})
validArgs := p.HandledResources()
cmd := &cobra.Command{

View File

@@ -65,11 +65,11 @@ $ kubectl get rc/web service/frontend pods/web-pod-13je7`
// NewCmdGet creates a command object for the generic "get" action, which
// retrieves one or more resources from a server.
func NewCmdGet(f *cmdutil.Factory, out io.Writer) *cobra.Command {
p := kubectl.NewHumanReadablePrinter(false, false, false, []string{})
p := kubectl.NewHumanReadablePrinter(false, false, false, false, []string{})
validArgs := p.HandledResources()
cmd := &cobra.Command{
Use: "get [(-o|--output=)json|yaml|template|wide|...] (TYPE [(NAME | -l label] | TYPE/NAME ...)",
Use: "get [(-o|--output=)json|yaml|template|wide|...] (TYPE [(NAME | -l label] | TYPE/NAME ...) [flags]",
Short: "Display one or many resources",
Long: get_long,
Example: get_example,

View File

@@ -328,6 +328,33 @@ func TestGetListObjects(t *testing.T) {
}
}
func TestGetAllListObjects(t *testing.T) {
pods, _, _ := testData()
f, tf, codec := NewAPIFactory()
tf.Printer = &testPrinter{}
tf.Client = &client.FakeRESTClient{
Codec: codec,
Resp: &http.Response{StatusCode: 200, Body: objBody(codec, pods)},
}
tf.Namespace = "test"
buf := bytes.NewBuffer([]byte{})
cmd := NewCmdGet(f, buf)
cmd.SetOutput(buf)
cmd.Flags().Set("show-all", "true")
cmd.Run(cmd, []string{"pods"})
expected := []runtime.Object{pods}
actual := tf.Printer.(*testPrinter).Objects
if !reflect.DeepEqual(expected, actual) {
t.Errorf("unexpected object: %#v %#v", expected, actual)
}
if len(buf.String()) == 0 {
t.Errorf("unexpected empty output")
}
}
func TestGetListComponentStatus(t *testing.T) {
statuses := testComponentStatusData()

View File

@@ -67,7 +67,7 @@ type Factory struct {
// Returns a Describer for displaying the specified RESTMapping type or an error.
Describer func(mapping *meta.RESTMapping) (kubectl.Describer, error)
// Returns a Printer for formatting objects of the given type or an error.
Printer func(mapping *meta.RESTMapping, noHeaders, withNamespace bool, wide bool, columnLabels []string) (kubectl.ResourcePrinter, error)
Printer func(mapping *meta.RESTMapping, noHeaders, withNamespace bool, wide bool, showAll bool, columnLabels []string) (kubectl.ResourcePrinter, error)
// Returns a Scaler for changing the size of the specified RESTMapping type or an error
Scaler func(mapping *meta.RESTMapping) (kubectl.Scaler, error)
// Returns a Reaper for gracefully shutting down resources.
@@ -179,8 +179,8 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
}
return nil, fmt.Errorf("no description has been implemented for %q", mapping.Kind)
},
Printer: func(mapping *meta.RESTMapping, noHeaders, withNamespace bool, wide bool, columnLabels []string) (kubectl.ResourcePrinter, error) {
return kubectl.NewHumanReadablePrinter(noHeaders, withNamespace, wide, columnLabels), nil
Printer: func(mapping *meta.RESTMapping, noHeaders, withNamespace bool, wide bool, showAll bool, columnLabels []string) (kubectl.ResourcePrinter, error) {
return kubectl.NewHumanReadablePrinter(noHeaders, withNamespace, wide, showAll, columnLabels), nil
},
PodSelectorForObject: func(object runtime.Object) (string, error) {
// TODO: replace with a swagger schema based approach (identify pod selector via schema introspection)
@@ -461,7 +461,7 @@ func (f *Factory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMappin
if err != nil {
columnLabel = []string{}
}
printer, err = f.Printer(mapping, GetFlagBool(cmd, "no-headers"), withNamespace, GetWideFlag(cmd), columnLabel)
printer, err = f.Printer(mapping, GetFlagBool(cmd, "no-headers"), withNamespace, GetWideFlag(cmd), GetFlagBool(cmd, "show-all"), columnLabel)
if err != nil {
return nil, err
}

View File

@@ -33,6 +33,7 @@ func AddPrinterFlags(cmd *cobra.Command) {
cmd.Flags().Bool("no-headers", false, "When using the default output, don't print headers.")
cmd.Flags().StringP("template", "t", "", "Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]")
cmd.Flags().String("sort-by", "", "If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. 'ObjectMeta.Name'). The field in the API resource specified by this JSONPath expression must be an integer or a string.")
cmd.Flags().BoolP("show-all", "a", false, "When printing, show all resources (default hide terminated pods.)")
}
// AddOutputFlagsForMutation adds output related flags to a command. Used by mutations only.

View File

@@ -19,7 +19,6 @@ package resource
import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/watch"
@@ -71,14 +70,13 @@ func (m *Helper) List(namespace, apiVersion string, selector labels.Selector) (r
Get()
}
func (m *Helper) Watch(namespace, resourceVersion, apiVersion string, labelSelector labels.Selector, fieldSelector fields.Selector) (watch.Interface, error) {
func (m *Helper) Watch(namespace, resourceVersion, apiVersion string, labelSelector labels.Selector) (watch.Interface, error) {
return m.RESTClient.Get().
Prefix("watch").
NamespaceIfScoped(namespace, m.NamespaceScoped).
Resource(m.Resource).
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(labelSelector).
FieldsSelectorParam(fieldSelector).
Watch()
}

View File

@@ -21,7 +21,6 @@ import (
"k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/watch"
)
@@ -72,7 +71,7 @@ func (r *Selector) Visit(fn VisitorFunc) error {
}
func (r *Selector) Watch(resourceVersion string) (watch.Interface, error) {
return NewHelper(r.Client, r.Mapping).Watch(r.Namespace, resourceVersion, r.ResourceMapping().APIVersion, r.Selector, fields.Everything())
return NewHelper(r.Client, r.Mapping).Watch(r.Namespace, resourceVersion, r.ResourceMapping().APIVersion, r.Selector)
}
// ResourceMapping returns the mapping for this resource and implements ResourceMapping

View File

@@ -182,17 +182,19 @@ type HumanReadablePrinter struct {
noHeaders bool
withNamespace bool
wide bool
showAll bool
columnLabels []string
lastType reflect.Type
}
// NewHumanReadablePrinter creates a HumanReadablePrinter.
func NewHumanReadablePrinter(noHeaders, withNamespace bool, wide bool, columnLabels []string) *HumanReadablePrinter {
func NewHumanReadablePrinter(noHeaders, withNamespace bool, wide bool, showAll bool, columnLabels []string) *HumanReadablePrinter {
printer := &HumanReadablePrinter{
handlerMap: make(map[reflect.Type]*handlerEntry),
noHeaders: noHeaders,
withNamespace: withNamespace,
wide: wide,
showAll: showAll,
columnLabels: columnLabels,
}
printer.addDefaultHandlers()
@@ -218,22 +220,22 @@ func (h *HumanReadablePrinter) Handler(columns []string, printFunc interface{})
// validatePrintHandlerFunc validates print handler signature.
// printFunc is the function that will be called to print an object.
// It must be of the following type:
// func printFunc(object ObjectType, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error
// func printFunc(object ObjectType, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error
// where ObjectType is the type of the object that will be printed.
func (h *HumanReadablePrinter) validatePrintHandlerFunc(printFunc reflect.Value) error {
if printFunc.Kind() != reflect.Func {
return fmt.Errorf("invalid print handler. %#v is not a function", printFunc)
}
funcType := printFunc.Type()
if funcType.NumIn() != 5 || funcType.NumOut() != 1 {
if funcType.NumIn() != 6 || funcType.NumOut() != 1 {
return fmt.Errorf("invalid print handler." +
"Must accept 5 parameters and return 1 value.")
"Must accept 6 parameters and return 1 value.")
}
if funcType.In(1) != reflect.TypeOf((*io.Writer)(nil)).Elem() ||
funcType.In(4) != reflect.TypeOf((*[]string)(nil)).Elem() ||
funcType.In(5) != reflect.TypeOf((*[]string)(nil)).Elem() ||
funcType.Out(0) != reflect.TypeOf((*error)(nil)).Elem() {
return fmt.Errorf("invalid print handler. The expected signature is: "+
"func handler(obj %v, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error", funcType.In(0))
"func handler(obj %v, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error", funcType.In(0))
}
return nil
}
@@ -379,7 +381,7 @@ func translateTimestamp(timestamp util.Time) string {
return shortHumanDuration(time.Now().Sub(timestamp.Time))
}
func printPod(pod *api.Pod, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printPod(pod *api.Pod, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
name := pod.Name
namespace := pod.Namespace
@@ -388,6 +390,10 @@ func printPod(pod *api.Pod, w io.Writer, withNamespace bool, wide bool, columnLa
readyContainers := 0
reason := string(pod.Status.Phase)
// if not printing all pods, skip terminated pods (default)
if !showAll && (reason == string(api.PodSucceeded) || reason == string(api.PodFailed)) {
return nil
}
if pod.Status.Reason != "" {
reason = pod.Status.Reason
}
@@ -440,16 +446,16 @@ func printPod(pod *api.Pod, w io.Writer, withNamespace bool, wide bool, columnLa
return err
}
func printPodList(podList *api.PodList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printPodList(podList *api.PodList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
for _, pod := range podList.Items {
if err := printPod(&pod, w, withNamespace, wide, columnLabels); err != nil {
if err := printPod(&pod, w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
return nil
}
func printPodTemplate(pod *api.PodTemplate, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printPodTemplate(pod *api.PodTemplate, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
name := pod.Name
namespace := pod.Namespace
@@ -493,16 +499,16 @@ func printPodTemplate(pod *api.PodTemplate, w io.Writer, withNamespace bool, wid
return nil
}
func printPodTemplateList(podList *api.PodTemplateList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printPodTemplateList(podList *api.PodTemplateList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
for _, pod := range podList.Items {
if err := printPodTemplate(&pod, w, withNamespace, wide, columnLabels); err != nil {
if err := printPodTemplate(&pod, w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
return nil
}
func printReplicationController(controller *api.ReplicationController, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printReplicationController(controller *api.ReplicationController, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
name := controller.Name
namespace := controller.Namespace
@@ -548,9 +554,9 @@ func printReplicationController(controller *api.ReplicationController, w io.Writ
return nil
}
func printReplicationControllerList(list *api.ReplicationControllerList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printReplicationControllerList(list *api.ReplicationControllerList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
for _, controller := range list.Items {
if err := printReplicationController(&controller, w, withNamespace, wide, columnLabels); err != nil {
if err := printReplicationController(&controller, w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
@@ -585,7 +591,7 @@ func makePortString(ports []api.ServicePort) string {
return strings.Join(pieces, ",")
}
func printService(svc *api.Service, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printService(svc *api.Service, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
name := svc.Name
namespace := svc.Namespace
@@ -613,16 +619,16 @@ func printService(svc *api.Service, w io.Writer, withNamespace bool, wide bool,
return nil
}
func printServiceList(list *api.ServiceList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printServiceList(list *api.ServiceList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
for _, svc := range list.Items {
if err := printService(&svc, w, withNamespace, wide, columnLabels); err != nil {
if err := printService(&svc, w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
return nil
}
func printEndpoints(endpoints *api.Endpoints, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printEndpoints(endpoints *api.Endpoints, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
name := endpoints.Name
namespace := endpoints.Namespace
@@ -638,16 +644,16 @@ func printEndpoints(endpoints *api.Endpoints, w io.Writer, withNamespace bool, w
return err
}
func printEndpointsList(list *api.EndpointsList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printEndpointsList(list *api.EndpointsList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
for _, item := range list.Items {
if err := printEndpoints(&item, w, withNamespace, wide, columnLabels); err != nil {
if err := printEndpoints(&item, w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
return nil
}
func printNamespace(item *api.Namespace, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printNamespace(item *api.Namespace, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
if withNamespace {
return fmt.Errorf("namespace is not namespaced")
}
@@ -659,16 +665,16 @@ func printNamespace(item *api.Namespace, w io.Writer, withNamespace bool, wide b
return err
}
func printNamespaceList(list *api.NamespaceList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printNamespaceList(list *api.NamespaceList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
for _, item := range list.Items {
if err := printNamespace(&item, w, withNamespace, wide, columnLabels); err != nil {
if err := printNamespace(&item, w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
return nil
}
func printSecret(item *api.Secret, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printSecret(item *api.Secret, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
name := item.Name
namespace := item.Namespace
@@ -684,9 +690,9 @@ func printSecret(item *api.Secret, w io.Writer, withNamespace bool, wide bool, c
return err
}
func printSecretList(list *api.SecretList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printSecretList(list *api.SecretList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
for _, item := range list.Items {
if err := printSecret(&item, w, withNamespace, wide, columnLabels); err != nil {
if err := printSecret(&item, w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
@@ -694,7 +700,7 @@ func printSecretList(list *api.SecretList, w io.Writer, withNamespace bool, wide
return nil
}
func printServiceAccount(item *api.ServiceAccount, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printServiceAccount(item *api.ServiceAccount, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
name := item.Name
namespace := item.Namespace
@@ -710,9 +716,9 @@ func printServiceAccount(item *api.ServiceAccount, w io.Writer, withNamespace bo
return err
}
func printServiceAccountList(list *api.ServiceAccountList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printServiceAccountList(list *api.ServiceAccountList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
for _, item := range list.Items {
if err := printServiceAccount(&item, w, withNamespace, wide, columnLabels); err != nil {
if err := printServiceAccount(&item, w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
@@ -720,7 +726,7 @@ func printServiceAccountList(list *api.ServiceAccountList, w io.Writer, withName
return nil
}
func printNode(node *api.Node, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printNode(node *api.Node, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
if withNamespace {
return fmt.Errorf("node is not namespaced")
}
@@ -754,16 +760,16 @@ func printNode(node *api.Node, w io.Writer, withNamespace bool, wide bool, colum
return err
}
func printNodeList(list *api.NodeList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printNodeList(list *api.NodeList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
for _, node := range list.Items {
if err := printNode(&node, w, withNamespace, wide, columnLabels); err != nil {
if err := printNode(&node, w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
return nil
}
func printPersistentVolume(pv *api.PersistentVolume, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printPersistentVolume(pv *api.PersistentVolume, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
if withNamespace {
return fmt.Errorf("persistentVolume is not namespaced")
}
@@ -796,16 +802,16 @@ func printPersistentVolume(pv *api.PersistentVolume, w io.Writer, withNamespace
return err
}
func printPersistentVolumeList(list *api.PersistentVolumeList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printPersistentVolumeList(list *api.PersistentVolumeList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
for _, pv := range list.Items {
if err := printPersistentVolume(&pv, w, withNamespace, wide, columnLabels); err != nil {
if err := printPersistentVolume(&pv, w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
return nil
}
func printPersistentVolumeClaim(pvc *api.PersistentVolumeClaim, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printPersistentVolumeClaim(pvc *api.PersistentVolumeClaim, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
name := pvc.Name
namespace := pvc.Namespace
@@ -833,16 +839,16 @@ func printPersistentVolumeClaim(pvc *api.PersistentVolumeClaim, w io.Writer, wit
return err
}
func printPersistentVolumeClaimList(list *api.PersistentVolumeClaimList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printPersistentVolumeClaimList(list *api.PersistentVolumeClaimList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
for _, psd := range list.Items {
if err := printPersistentVolumeClaim(&psd, w, withNamespace, wide, columnLabels); err != nil {
if err := printPersistentVolumeClaim(&psd, w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
return nil
}
func printEvent(event *api.Event, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printEvent(event *api.Event, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
namespace := event.Namespace
if withNamespace {
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
@@ -868,17 +874,17 @@ func printEvent(event *api.Event, w io.Writer, withNamespace bool, wide bool, co
}
// Sorts and prints the EventList in a human-friendly format.
func printEventList(list *api.EventList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printEventList(list *api.EventList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
sort.Sort(SortableEvents(list.Items))
for i := range list.Items {
if err := printEvent(&list.Items[i], w, withNamespace, wide, columnLabels); err != nil {
if err := printEvent(&list.Items[i], w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
return nil
}
func printLimitRange(limitRange *api.LimitRange, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printLimitRange(limitRange *api.LimitRange, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
name := limitRange.Name
namespace := limitRange.Namespace
@@ -900,16 +906,16 @@ func printLimitRange(limitRange *api.LimitRange, w io.Writer, withNamespace bool
}
// Prints the LimitRangeList in a human-friendly format.
func printLimitRangeList(list *api.LimitRangeList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printLimitRangeList(list *api.LimitRangeList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
for i := range list.Items {
if err := printLimitRange(&list.Items[i], w, withNamespace, wide, columnLabels); err != nil {
if err := printLimitRange(&list.Items[i], w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
return nil
}
func printResourceQuota(resourceQuota *api.ResourceQuota, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printResourceQuota(resourceQuota *api.ResourceQuota, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
name := resourceQuota.Name
namespace := resourceQuota.Namespace
@@ -931,16 +937,16 @@ func printResourceQuota(resourceQuota *api.ResourceQuota, w io.Writer, withNames
}
// Prints the ResourceQuotaList in a human-friendly format.
func printResourceQuotaList(list *api.ResourceQuotaList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printResourceQuotaList(list *api.ResourceQuotaList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
for i := range list.Items {
if err := printResourceQuota(&list.Items[i], w, withNamespace, wide, columnLabels); err != nil {
if err := printResourceQuota(&list.Items[i], w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
return nil
}
func printComponentStatus(item *api.ComponentStatus, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printComponentStatus(item *api.ComponentStatus, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
if withNamespace {
return fmt.Errorf("componentStatus is not namespaced")
}
@@ -967,9 +973,9 @@ func printComponentStatus(item *api.ComponentStatus, w io.Writer, withNamespace
return err
}
func printComponentStatusList(list *api.ComponentStatusList, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func printComponentStatusList(list *api.ComponentStatusList, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
for _, item := range list.Items {
if err := printComponentStatus(&item, w, withNamespace, wide, columnLabels); err != nil {
if err := printComponentStatus(&item, w, withNamespace, wide, showAll, columnLabels); err != nil {
return err
}
}
@@ -1043,7 +1049,7 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er
h.printHeader(headers, w)
h.lastType = t
}
args := []reflect.Value{reflect.ValueOf(obj), reflect.ValueOf(w), reflect.ValueOf(h.withNamespace), reflect.ValueOf(h.wide), reflect.ValueOf(h.columnLabels)}
args := []reflect.Value{reflect.ValueOf(obj), reflect.ValueOf(w), reflect.ValueOf(h.withNamespace), reflect.ValueOf(h.wide), reflect.ValueOf(h.showAll), reflect.ValueOf(h.columnLabels)}
resultValue := handler.printFunc.Call(args)[0]
if resultValue.IsNil() {
return nil

View File

@@ -237,18 +237,18 @@ type TestUnknownType struct{}
func (*TestUnknownType) IsAnAPIObject() {}
func PrintCustomType(obj *TestPrintType, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func PrintCustomType(obj *TestPrintType, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
_, err := fmt.Fprintf(w, "%s", obj.Data)
return err
}
func ErrorPrintHandler(obj *TestPrintType, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
func ErrorPrintHandler(obj *TestPrintType, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
return fmt.Errorf("ErrorPrintHandler error")
}
func TestCustomTypePrinting(t *testing.T) {
columns := []string{"Data"}
printer := NewHumanReadablePrinter(false, false, false, []string{})
printer := NewHumanReadablePrinter(false, false, false, false, []string{})
printer.Handler(columns, PrintCustomType)
obj := TestPrintType{"test object"}
@@ -265,7 +265,7 @@ func TestCustomTypePrinting(t *testing.T) {
func TestPrintHandlerError(t *testing.T) {
columns := []string{"Data"}
printer := NewHumanReadablePrinter(false, false, false, []string{})
printer := NewHumanReadablePrinter(false, false, false, false, []string{})
printer.Handler(columns, ErrorPrintHandler)
obj := TestPrintType{"test object"}
buffer := &bytes.Buffer{}
@@ -276,7 +276,7 @@ func TestPrintHandlerError(t *testing.T) {
}
func TestUnknownTypePrinting(t *testing.T) {
printer := NewHumanReadablePrinter(false, false, false, []string{})
printer := NewHumanReadablePrinter(false, false, false, false, []string{})
buffer := &bytes.Buffer{}
err := printer.PrintObj(&TestUnknownType{}, buffer)
if err == nil {
@@ -451,8 +451,8 @@ func TestPrinters(t *testing.T) {
t.Fatal(err)
}
printers := map[string]ResourcePrinter{
"humanReadable": NewHumanReadablePrinter(true, false, false, []string{}),
"humanReadableHeaders": NewHumanReadablePrinter(false, false, false, []string{}),
"humanReadable": NewHumanReadablePrinter(true, false, false, false, []string{}),
"humanReadableHeaders": NewHumanReadablePrinter(false, false, false, false, []string{}),
"json": &JSONPrinter{},
"yaml": &YAMLPrinter{},
"template": templatePrinter,
@@ -489,7 +489,7 @@ func TestPrinters(t *testing.T) {
func TestPrintEventsResultSorted(t *testing.T) {
// Arrange
printer := NewHumanReadablePrinter(false /* noHeaders */, false, false, []string{})
printer := NewHumanReadablePrinter(false /* noHeaders */, false, false, false, []string{})
obj := api.EventList{
Items: []api.Event{
@@ -530,7 +530,7 @@ func TestPrintEventsResultSorted(t *testing.T) {
}
func TestPrintMinionStatus(t *testing.T) {
printer := NewHumanReadablePrinter(false, false, false, []string{})
printer := NewHumanReadablePrinter(false, false, false, false, []string{})
table := []struct {
minion api.Node
status string
@@ -741,7 +741,7 @@ func TestPrintHumanReadableService(t *testing.T) {
for _, svc := range tests {
buff := bytes.Buffer{}
printService(&svc, &buff, false, false, []string{})
printService(&svc, &buff, false, false, false, []string{})
output := string(buff.Bytes())
ip := svc.Spec.ClusterIP
if !strings.Contains(output, ip) {
@@ -922,7 +922,7 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
for _, test := range table {
if test.isNamespaced {
// Expect output to include namespace when requested.
printer := NewHumanReadablePrinter(false, true, false, []string{})
printer := NewHumanReadablePrinter(false, true, false, false, []string{})
buffer := &bytes.Buffer{}
err := printer.PrintObj(test.obj, buffer)
if err != nil {
@@ -934,7 +934,7 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
}
} else {
// Expect error when trying to get all namespaces for un-namespaced object.
printer := NewHumanReadablePrinter(false, true, false, []string{})
printer := NewHumanReadablePrinter(false, true, false, false, []string{})
buffer := &bytes.Buffer{}
err := printer.PrintObj(test.obj, buffer)
if err == nil {
@@ -1029,7 +1029,100 @@ func TestPrintPod(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
for _, test := range tests {
printPod(&test.pod, buf, false, false, []string{})
printPod(&test.pod, buf, false, false, true, []string{})
// We ignore time
if !strings.HasPrefix(buf.String(), test.expect) {
t.Fatalf("Expected: %s, got: %s", test.expect, buf.String())
}
buf.Reset()
}
}
func TestPrintNonTerminatedPod(t *testing.T) {
tests := []struct {
pod api.Pod
expect string
}{
{
// Test pod phase Running should be printed
api.Pod{
ObjectMeta: api.ObjectMeta{Name: "test1"},
Spec: api.PodSpec{Containers: make([]api.Container, 2)},
Status: api.PodStatus{
Phase: api.PodRunning,
ContainerStatuses: []api.ContainerStatus{
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
{RestartCount: 3},
},
},
},
"test1\t1/2\tRunning\t6\t",
},
{
// Test pod phase Pending should be printed
api.Pod{
ObjectMeta: api.ObjectMeta{Name: "test2"},
Spec: api.PodSpec{Containers: make([]api.Container, 2)},
Status: api.PodStatus{
Phase: api.PodPending,
ContainerStatuses: []api.ContainerStatus{
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
{RestartCount: 3},
},
},
},
"test2\t1/2\tPending\t6\t",
},
{
// Test pod phase Unknown should be printed
api.Pod{
ObjectMeta: api.ObjectMeta{Name: "test3"},
Spec: api.PodSpec{Containers: make([]api.Container, 2)},
Status: api.PodStatus{
Phase: api.PodUnknown,
ContainerStatuses: []api.ContainerStatus{
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
{RestartCount: 3},
},
},
},
"test3\t1/2\tUnknown\t6\t",
},
{
// Test pod phase Succeeded shouldn't be printed
api.Pod{
ObjectMeta: api.ObjectMeta{Name: "test4"},
Spec: api.PodSpec{Containers: make([]api.Container, 2)},
Status: api.PodStatus{
Phase: api.PodSucceeded,
ContainerStatuses: []api.ContainerStatus{
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
{RestartCount: 3},
},
},
},
"",
},
{
// Test pod phase Failed shouldn't be printed
api.Pod{
ObjectMeta: api.ObjectMeta{Name: "test5"},
Spec: api.PodSpec{Containers: make([]api.Container, 2)},
Status: api.PodStatus{
Phase: api.PodFailed,
ContainerStatuses: []api.ContainerStatus{
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
{Ready: true, RestartCount: 3},
},
},
},
"",
},
}
buf := bytes.NewBuffer([]byte{})
for _, test := range tests {
printPod(&test.pod, buf, false, false, false, []string{})
// We ignore time
if !strings.HasPrefix(buf.String(), test.expect) {
t.Fatalf("Expected: %s, got: %s", test.expect, buf.String())
@@ -1089,7 +1182,7 @@ func TestPrintPodWithLabels(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
for _, test := range tests {
printPod(&test.pod, buf, false, false, test.labelColumns)
printPod(&test.pod, buf, false, false, false, test.labelColumns)
// We ignore time
if !strings.HasPrefix(buf.String(), test.startsWith) || !strings.HasSuffix(buf.String(), test.endsWith) {
t.Fatalf("Expected to start with: %s and end with: %s, but got: %s", test.startsWith, test.endsWith, buf.String())