NAMESPACE in own column: kubectl --all-namespaces

This commit is contained in:
Eric Tune 2015-07-01 13:56:31 -07:00
parent 21bbde46d4
commit 88fa471d06
3 changed files with 89 additions and 64 deletions

View File

@ -241,9 +241,9 @@ func stringBody(body string) io.ReadCloser {
// } // }
//} //}
func ExamplePrintReplicationController() { func ExamplePrintReplicationControllerWithNamespace() {
f, tf, codec := NewAPIFactory() f, tf, codec := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false, false, false, []string{}) tf.Printer = kubectl.NewHumanReadablePrinter(false, true, false, []string{})
tf.Client = &client.FakeRESTClient{ tf.Client = &client.FakeRESTClient{
Codec: codec, Codec: codec,
Client: nil, Client: nil,
@ -251,8 +251,9 @@ func ExamplePrintReplicationController() {
cmd := NewCmdRun(f, os.Stdout) cmd := NewCmdRun(f, os.Stdout)
ctrl := &api.ReplicationController{ ctrl := &api.ReplicationController{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: "foo", Name: "foo",
Labels: map[string]string{"foo": "bar"}, Namespace: "beep",
Labels: map[string]string{"foo": "bar"},
}, },
Spec: api.ReplicationControllerSpec{ Spec: api.ReplicationControllerSpec{
Replicas: 1, Replicas: 1,
@ -277,8 +278,8 @@ func ExamplePrintReplicationController() {
fmt.Printf("Unexpected error: %v", err) fmt.Printf("Unexpected error: %v", err)
} }
// Output: // Output:
// CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS // NAMESPACE CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
// foo foo someimage foo=bar 1 // beep foo foo someimage foo=bar 1
} }
func ExamplePrintPodWithWideFormat() { func ExamplePrintPodWithWideFormat() {

View File

@ -32,7 +32,6 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion" "github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/volume" "github.com/GoogleCloudPlatform/kubernetes/pkg/volume"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
@ -270,6 +269,7 @@ var serviceAccountColumns = []string{"NAME", "SECRETS"}
var persistentVolumeColumns = []string{"NAME", "LABELS", "CAPACITY", "ACCESSMODES", "STATUS", "CLAIM", "REASON"} var persistentVolumeColumns = []string{"NAME", "LABELS", "CAPACITY", "ACCESSMODES", "STATUS", "CLAIM", "REASON"}
var persistentVolumeClaimColumns = []string{"NAME", "LABELS", "STATUS", "VOLUME"} var persistentVolumeClaimColumns = []string{"NAME", "LABELS", "STATUS", "VOLUME"}
var componentStatusColumns = []string{"NAME", "STATUS", "MESSAGE", "ERROR"} var componentStatusColumns = []string{"NAME", "STATUS", "MESSAGE", "ERROR"}
var withNamespacePrefixColumns = []string{"NAMESPACE"} // TODO(erictune): print cluster name too.
// addDefaultHandlers adds print handlers for default Kubernetes types. // addDefaultHandlers adds print handlers for default Kubernetes types.
func (h *HumanReadablePrinter) addDefaultHandlers() { func (h *HumanReadablePrinter) addDefaultHandlers() {
@ -379,9 +379,7 @@ func translateTimestamp(timestamp util.Time) string {
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, columnLabels []string) error {
name := pod.Name name := pod.Name
if withNamespace { namespace := pod.Namespace
name = types.NamespacedName{Namespace: pod.Namespace, Name: pod.Name}.String()
}
restarts := 0 restarts := 0
totalContainers := len(pod.Spec.Containers) totalContainers := len(pod.Spec.Containers)
@ -411,6 +409,11 @@ func printPod(pod *api.Pod, w io.Writer, withNamespace bool, wide bool, columnLa
} }
} }
if withNamespace {
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%d/%d\t%s\t%d\t%s", if _, err := fmt.Fprintf(w, "%s\t%d/%d\t%s\t%d\t%s",
name, name,
readyContainers, readyContainers,
@ -445,12 +448,8 @@ func printPodList(podList *api.PodList, w io.Writer, withNamespace bool, wide bo
} }
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, columnLabels []string) error {
var name string name := pod.Name
if withNamespace { namespace := pod.Namespace
name = types.NamespacedName{pod.Namespace, pod.Name}.String()
} else {
name = pod.Name
}
containers := pod.Template.Spec.Containers containers := pod.Template.Spec.Containers
var firstContainer api.Container var firstContainer api.Container
@ -458,6 +457,11 @@ func printPodTemplate(pod *api.PodTemplate, w io.Writer, withNamespace bool, wid
firstContainer, containers = containers[0], containers[1:] firstContainer, containers = containers[0], containers[1:]
} }
if withNamespace {
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s", if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s",
name, name,
firstContainer.Name, firstContainer.Name,
@ -490,12 +494,8 @@ func printPodTemplateList(podList *api.PodTemplateList, w io.Writer, withNamespa
} }
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, columnLabels []string) error {
var name string name := controller.Name
if withNamespace { namespace := controller.Namespace
name = types.NamespacedName{controller.Namespace, controller.Name}.String()
} else {
name = controller.Name
}
containers := controller.Spec.Template.Spec.Containers containers := controller.Spec.Template.Spec.Containers
var firstContainer api.Container var firstContainer api.Container
@ -503,6 +503,11 @@ func printReplicationController(controller *api.ReplicationController, w io.Writ
firstContainer, containers = containers[0], containers[1:] firstContainer, containers = containers[0], containers[1:]
} }
if withNamespace {
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d", if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d",
name, name,
firstContainer.Name, firstContainer.Name,
@ -536,12 +541,8 @@ func printReplicationControllerList(list *api.ReplicationControllerList, w io.Wr
} }
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, columnLabels []string) error {
var name string name := svc.Name
if withNamespace { namespace := svc.Namespace
name = types.NamespacedName{svc.Namespace, svc.Name}.String()
} else {
name = svc.Name
}
ips := []string{svc.Spec.ClusterIP} ips := []string{svc.Spec.ClusterIP}
@ -552,6 +553,11 @@ func printService(svc *api.Service, w io.Writer, withNamespace bool, wide bool,
} }
} }
if withNamespace {
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d/%s", name, formatLabels(svc.Labels), if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d/%s", name, formatLabels(svc.Labels),
formatLabels(svc.Spec.Selector), ips[0], svc.Spec.Ports[0].Port, svc.Spec.Ports[0].Protocol); err != nil { formatLabels(svc.Spec.Selector), ips[0], svc.Spec.Ports[0].Port, svc.Spec.Ports[0].Protocol); err != nil {
return err return err
@ -592,13 +598,14 @@ func printServiceList(list *api.ServiceList, w io.Writer, withNamespace bool, wi
} }
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, columnLabels []string) error {
var name string name := endpoints.Name
if withNamespace { namespace := endpoints.Namespace
name = types.NamespacedName{endpoints.Namespace, endpoints.Name}.String()
} else {
name = endpoints.Name
}
if withNamespace {
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%s", name, formatEndpoints(endpoints, nil)); err != nil { if _, err := fmt.Fprintf(w, "%s\t%s", name, formatEndpoints(endpoints, nil)); err != nil {
return err return err
} }
@ -636,13 +643,14 @@ func printNamespaceList(list *api.NamespaceList, w io.Writer, withNamespace bool
} }
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, columnLabels []string) error {
var name string name := item.Name
if withNamespace { namespace := item.Namespace
name = types.NamespacedName{item.Namespace, item.Name}.String()
} else {
name = item.Name
}
if withNamespace {
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%v", name, item.Type, len(item.Data)); err != nil { if _, err := fmt.Fprintf(w, "%s\t%s\t%v", name, item.Type, len(item.Data)); err != nil {
return err return err
} }
@ -661,13 +669,14 @@ func printSecretList(list *api.SecretList, w io.Writer, withNamespace bool, wide
} }
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, columnLabels []string) error {
var name string name := item.Name
if withNamespace { namespace := item.Namespace
name = types.NamespacedName{item.Namespace, item.Name}.String()
} else {
name = item.Name
}
if withNamespace {
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%d", name, len(item.Secrets)); err != nil { if _, err := fmt.Fprintf(w, "%s\t%d", name, len(item.Secrets)); err != nil {
return err return err
} }
@ -729,12 +738,10 @@ func printNodeList(list *api.NodeList, w io.Writer, withNamespace bool, wide boo
} }
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, columnLabels []string) error {
var name string
if withNamespace { if withNamespace {
name = types.NamespacedName{pv.Namespace, pv.Name}.String() return fmt.Errorf("persistentVolume is not namespaced")
} else {
name = pv.Name
} }
name := pv.Name
claimRefUID := "" claimRefUID := ""
if pv.Spec.ClaimRef != nil { if pv.Spec.ClaimRef != nil {
@ -765,13 +772,20 @@ func printPersistentVolumeList(list *api.PersistentVolumeList, w io.Writer, with
} }
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, columnLabels []string) error {
var name string name := pvc.Name
namespace := pvc.Namespace
if withNamespace { if withNamespace {
name = types.NamespacedName{pvc.Namespace, pvc.Name}.String() if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
} else { return err
name = pvc.Name }
} }
if withNamespace {
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s", name, pvc.Labels, pvc.Status.Phase, pvc.Spec.VolumeName); err != nil { if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s", name, pvc.Labels, pvc.Status.Phase, pvc.Spec.VolumeName); err != nil {
return err return err
} }
@ -789,8 +803,11 @@ func printPersistentVolumeClaimList(list *api.PersistentVolumeClaimList, w io.Wr
} }
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, columnLabels []string) error {
namespace := event.Namespace
if withNamespace { if withNamespace {
return fmt.Errorf("event is not namespaced") if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
return err
}
} }
if _, err := fmt.Fprintf( if _, err := fmt.Fprintf(
w, "%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s", w, "%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s",
@ -822,11 +839,13 @@ func printEventList(list *api.EventList, w io.Writer, withNamespace bool, wide b
} }
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, columnLabels []string) error {
var name string name := limitRange.Name
namespace := limitRange.Namespace
if withNamespace { if withNamespace {
name = types.NamespacedName{limitRange.Namespace, limitRange.Name}.String() if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
} else { return err
name = limitRange.Name }
} }
if _, err := fmt.Fprintf(w, "%s", name); err != nil { if _, err := fmt.Fprintf(w, "%s", name); err != nil {
@ -847,11 +866,13 @@ func printLimitRangeList(list *api.LimitRangeList, w io.Writer, withNamespace bo
} }
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, columnLabels []string) error {
var name string name := resourceQuota.Name
namespace := resourceQuota.Namespace
if withNamespace { if withNamespace {
name = types.NamespacedName{resourceQuota.Namespace, resourceQuota.Name}.String() if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
} else { return err
name = resourceQuota.Name }
} }
if _, err := fmt.Fprintf(w, "%s", name); err != nil { if _, err := fmt.Fprintf(w, "%s", name); err != nil {
@ -952,6 +973,9 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er
if !h.noHeaders && t != h.lastType { if !h.noHeaders && t != h.lastType {
headers := append(handler.columns, formatWideHeaders(h.wide, t)...) headers := append(handler.columns, formatWideHeaders(h.wide, t)...)
headers = append(headers, formatLabelHeaders(h.columnLabels)...) headers = append(headers, formatLabelHeaders(h.columnLabels)...)
if h.withNamespace {
headers = append(withNamespacePrefixColumns, headers...)
}
h.printHeader(headers, w) h.printHeader(headers, w)
h.lastType = t h.lastType = t
} }

View File

@ -930,7 +930,7 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("An error occurred printing object: %#v", err) t.Fatalf("An error occurred printing object: %#v", err)
} }
matched := contains(strings.Fields(buffer.String()), fmt.Sprintf("%s/%s", namespaceName, name)) matched := contains(strings.Fields(buffer.String()), fmt.Sprintf("%s", namespaceName))
if !matched { if !matched {
t.Errorf("Expect printing object to contain namespace: %#v", test.obj) t.Errorf("Expect printing object to contain namespace: %#v", test.obj)
} }