diff --git a/pkg/kubectl/resource_printer.go b/pkg/kubectl/resource_printer.go index 98c542d0d09..b2acf1ff8ec 100644 --- a/pkg/kubectl/resource_printer.go +++ b/pkg/kubectl/resource_printer.go @@ -32,6 +32,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/conversion" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" + "github.com/GoogleCloudPlatform/kubernetes/pkg/types" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/volume" "github.com/docker/docker/pkg/units" @@ -197,7 +198,7 @@ func NewHumanReadablePrinter(noHeaders, withNamespace bool) *HumanReadablePrinte // Handler adds a print handler with a given set of columns to HumanReadablePrinter instance. // 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) error +// func printFunc(object ObjectType, w io.Writer, withNamespace bool) error // where ObjectType is the type of the object that will be printed. func (h *HumanReadablePrinter) Handler(columns []string, printFunc interface{}) error { printFuncValue := reflect.ValueOf(printFunc) @@ -218,14 +219,14 @@ func (h *HumanReadablePrinter) validatePrintHandlerFunc(printFunc reflect.Value) return fmt.Errorf("invalid print handler. %#v is not a function.", printFunc) } funcType := printFunc.Type() - if funcType.NumIn() != 2 || funcType.NumOut() != 1 { + if funcType.NumIn() != 3 || funcType.NumOut() != 1 { return fmt.Errorf("invalid print handler." + - "Must accept 2 parameters and return 1 value.") + "Must accept 3 parameters and return 1 value.") } if funcType.In(1) != reflect.TypeOf((*io.Writer)(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) error", funcType.In(0)) + "func handler(obj %v, w io.Writer, withNamespace bool) error", funcType.In(0)) } return nil } @@ -393,9 +394,16 @@ func interpretContainerStatus(status *api.ContainerStatus) (string, string, stri return "", "", "", fmt.Errorf("unknown container state %#v", *state) } -func printPod(pod *api.Pod, w io.Writer) error { +func printPod(pod *api.Pod, w io.Writer, withNamespace bool) error { + var name string + if withNamespace { + name = types.NamespacedName{pod.Namespace, pod.Name}.String() + } else { + name = pod.Name + } + _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", - pod.Name, + name, pod.Status.PodIP, "", "", podHostString(pod.Spec.Host, pod.Status.HostIP), @@ -413,8 +421,8 @@ func printPod(pod *api.Pod, w io.Writer) error { // Container status has not been reported yet. Print basic information // of the containers and exit the function. for _, container := range pod.Spec.Containers { - _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", - "", "", container.Name, container.Image, "", "", "", "") + _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", + "", "", container.Name, container.Image, "", "", "", "", "") if err != nil { return err } @@ -444,23 +452,30 @@ func printPod(pod *api.Pod, w io.Writer) error { return nil } -func printPodList(podList *api.PodList, w io.Writer) error { +func printPodList(podList *api.PodList, w io.Writer, withNamespace bool) error { for _, pod := range podList.Items { - if err := printPod(&pod, w); err != nil { + if err := printPod(&pod, w, withNamespace); err != nil { return err } } return nil } -func printPodTemplate(pod *api.PodTemplate, w io.Writer) error { +func printPodTemplate(pod *api.PodTemplate, w io.Writer, withNamespace bool) error { + var name string + if withNamespace { + name = types.NamespacedName{pod.Namespace, pod.Name}.String() + } else { + name = pod.Name + } + containers := pod.Template.Spec.Containers var firstContainer api.Container if len(containers) > 0 { firstContainer, containers = containers[0], containers[1:] } _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", - pod.Name, + name, firstContainer.Name, firstContainer.Image, formatLabels(pod.Template.Labels), @@ -478,23 +493,30 @@ func printPodTemplate(pod *api.PodTemplate, w io.Writer) error { return nil } -func printPodTemplateList(podList *api.PodTemplateList, w io.Writer) error { +func printPodTemplateList(podList *api.PodTemplateList, w io.Writer, withNamespace bool) error { for _, pod := range podList.Items { - if err := printPodTemplate(&pod, w); err != nil { + if err := printPodTemplate(&pod, w, withNamespace); err != nil { return err } } return nil } -func printReplicationController(controller *api.ReplicationController, w io.Writer) error { +func printReplicationController(controller *api.ReplicationController, w io.Writer, withNamespace bool) error { + var name string + if withNamespace { + name = types.NamespacedName{controller.Namespace, controller.Name}.String() + } else { + name = controller.Name + } + containers := controller.Spec.Template.Spec.Containers var firstContainer api.Container if len(containers) > 0 { firstContainer, containers = containers[0], containers[1:] } _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\n", - controller.Name, + name, firstContainer.Name, firstContainer.Image, formatLabels(controller.Spec.Selector), @@ -512,21 +534,29 @@ func printReplicationController(controller *api.ReplicationController, w io.Writ return nil } -func printReplicationControllerList(list *api.ReplicationControllerList, w io.Writer) error { +func printReplicationControllerList(list *api.ReplicationControllerList, w io.Writer, withNamespace bool) error { for _, controller := range list.Items { - if err := printReplicationController(&controller, w); err != nil { + if err := printReplicationController(&controller, w, withNamespace); err != nil { return err } } return nil } -func printService(svc *api.Service, w io.Writer) error { +func printService(svc *api.Service, w io.Writer, withNamespace bool) error { + var name string + if withNamespace { + name = types.NamespacedName{svc.Namespace, svc.Name}.String() + } else { + name = svc.Name + } + ips := []string{svc.Spec.PortalIP} for _, publicIP := range svc.Spec.PublicIPs { ips = append(ips, publicIP) } - if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d/%s\n", svc.Name, formatLabels(svc.Labels), + + if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d/%s\n", name, formatLabels(svc.Labels), formatLabels(svc.Spec.Selector), ips[0], svc.Spec.Ports[0].Port, svc.Spec.Ports[0].Protocol); err != nil { return err } @@ -553,51 +583,64 @@ func printService(svc *api.Service, w io.Writer) error { return nil } -func printServiceList(list *api.ServiceList, w io.Writer) error { +func printServiceList(list *api.ServiceList, w io.Writer, withNamespace bool) error { for _, svc := range list.Items { - if err := printService(&svc, w); err != nil { + if err := printService(&svc, w, withNamespace); err != nil { return err } } return nil } -func printEndpoints(endpoints *api.Endpoints, w io.Writer) error { - _, err := fmt.Fprintf(w, "%s\t%s\n", endpoints.Name, formatEndpoints(endpoints, nil)) +func printEndpoints(endpoints *api.Endpoints, w io.Writer, withNamespace bool) error { + var name string + if withNamespace { + name = types.NamespacedName{endpoints.Namespace, endpoints.Name}.String() + } else { + name = endpoints.Name + } + _, err := fmt.Fprintf(w, "%s\t%s\n", name, formatEndpoints(endpoints, nil)) return err } -func printEndpointsList(list *api.EndpointsList, w io.Writer) error { +func printEndpointsList(list *api.EndpointsList, w io.Writer, withNamespace bool) error { for _, item := range list.Items { - if err := printEndpoints(&item, w); err != nil { + if err := printEndpoints(&item, w, withNamespace); err != nil { return err } } return nil } -func printNamespace(item *api.Namespace, w io.Writer) error { +func printNamespace(item *api.Namespace, w io.Writer, withNamespace bool) error { _, err := fmt.Fprintf(w, "%s\t%s\t%s\n", item.Name, formatLabels(item.Labels), item.Status.Phase) return err } -func printNamespaceList(list *api.NamespaceList, w io.Writer) error { +func printNamespaceList(list *api.NamespaceList, w io.Writer, withNamespace bool) error { for _, item := range list.Items { - if err := printNamespace(&item, w); err != nil { + if err := printNamespace(&item, w, withNamespace); err != nil { return err } } return nil } -func printSecret(item *api.Secret, w io.Writer) error { - _, err := fmt.Fprintf(w, "%s\t%s\t%v\n", item.Name, item.Type, len(item.Data)) +func printSecret(item *api.Secret, w io.Writer, withNamespace bool) error { + var name string + if withNamespace { + name = types.NamespacedName{item.Namespace, item.Name}.String() + } else { + name = item.Name + } + + _, err := fmt.Fprintf(w, "%s\t%s\t%v\n", name, item.Type, len(item.Data)) return err } -func printSecretList(list *api.SecretList, w io.Writer) error { +func printSecretList(list *api.SecretList, w io.Writer, withNamespace bool) error { for _, item := range list.Items { - if err := printSecret(&item, w); err != nil { + if err := printSecret(&item, w, withNamespace); err != nil { return err } } @@ -605,14 +648,21 @@ func printSecretList(list *api.SecretList, w io.Writer) error { return nil } -func printServiceAccount(item *api.ServiceAccount, w io.Writer) error { - _, err := fmt.Fprintf(w, "%s\t%d\n", item.Name, len(item.Secrets)) +func printServiceAccount(item *api.ServiceAccount, w io.Writer, withNamespace bool) error { + var name string + if withNamespace { + name = types.NamespacedName{item.Namespace, item.Name}.String() + } else { + name = item.Name + } + + _, err := fmt.Fprintf(w, "%s\t%d\n", name, len(item.Secrets)) return err } -func printServiceAccountList(list *api.ServiceAccountList, w io.Writer) error { +func printServiceAccountList(list *api.ServiceAccountList, w io.Writer, withNamespace bool) error { for _, item := range list.Items { - if err := printServiceAccount(&item, w); err != nil { + if err := printServiceAccount(&item, w, withNamespace); err != nil { return err } } @@ -620,7 +670,7 @@ func printServiceAccountList(list *api.ServiceAccountList, w io.Writer) error { return nil } -func printNode(node *api.Node, w io.Writer) error { +func printNode(node *api.Node, w io.Writer, withNamespace bool) error { conditionMap := make(map[api.NodeConditionType]*api.NodeCondition) NodeAllConditions := []api.NodeConditionType{api.NodeReady} for i := range node.Status.Conditions { @@ -647,16 +697,23 @@ func printNode(node *api.Node, w io.Writer) error { return err } -func printNodeList(list *api.NodeList, w io.Writer) error { +func printNodeList(list *api.NodeList, w io.Writer, withNamespace bool) error { for _, node := range list.Items { - if err := printNode(&node, w); err != nil { + if err := printNode(&node, w, withNamespace); err != nil { return err } } return nil } -func printPersistentVolume(pv *api.PersistentVolume, w io.Writer) error { +func printPersistentVolume(pv *api.PersistentVolume, w io.Writer, withNamespace bool) error { + var name string + if withNamespace { + name = types.NamespacedName{pv.Namespace, pv.Name}.String() + } else { + name = pv.Name + } + claimRefUID := "" if pv.Spec.ClaimRef != nil { claimRefUID += pv.Spec.ClaimRef.Namespace @@ -669,34 +726,41 @@ func printPersistentVolume(pv *api.PersistentVolume, w io.Writer) error { aQty := pv.Spec.Capacity[api.ResourceStorage] aSize := aQty.Value() - _, err := fmt.Fprintf(w, "%s\t%s\t%d\t%s\t%s\t%s\n", pv.Name, pv.Labels, aSize, modesStr, pv.Status.Phase, claimRefUID) + _, err := fmt.Fprintf(w, "%s\t%s\t%d\t%s\t%s\t%s\n", name, formatLabels(pv.Labels), aSize, modesStr, pv.Status.Phase, claimRefUID) return err } -func printPersistentVolumeList(list *api.PersistentVolumeList, w io.Writer) error { +func printPersistentVolumeList(list *api.PersistentVolumeList, w io.Writer, withNamespace bool) error { for _, pv := range list.Items { - if err := printPersistentVolume(&pv, w); err != nil { + if err := printPersistentVolume(&pv, w, withNamespace); err != nil { return err } } return nil } -func printPersistentVolumeClaim(pvc *api.PersistentVolumeClaim, w io.Writer) error { - _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", pvc.Name, pvc.Labels, pvc.Status.Phase, pvc.Spec.VolumeName) +func printPersistentVolumeClaim(pvc *api.PersistentVolumeClaim, w io.Writer, withNamespace bool) error { + var name string + if withNamespace { + name = types.NamespacedName{pvc.Namespace, pvc.Name}.String() + } else { + name = pvc.Name + } + + _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", name, pvc.Labels, pvc.Status.Phase, pvc.Spec.VolumeName) return err } -func printPersistentVolumeClaimList(list *api.PersistentVolumeClaimList, w io.Writer) error { +func printPersistentVolumeClaimList(list *api.PersistentVolumeClaimList, w io.Writer, withNamespace bool) error { for _, psd := range list.Items { - if err := printPersistentVolumeClaim(&psd, w); err != nil { + if err := printPersistentVolumeClaim(&psd, w, withNamespace); err != nil { return err } } return nil } -func printEvent(event *api.Event, w io.Writer) error { +func printEvent(event *api.Event, w io.Writer, withNamespace bool) error { _, err := fmt.Fprintf( w, "%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n", event.FirstTimestamp.Time.Format(time.RFC1123Z), @@ -713,53 +777,67 @@ func printEvent(event *api.Event, w io.Writer) error { } // Sorts and prints the EventList in a human-friendly format. -func printEventList(list *api.EventList, w io.Writer) error { +func printEventList(list *api.EventList, w io.Writer, withNamespace bool) error { sort.Sort(SortableEvents(list.Items)) for i := range list.Items { - if err := printEvent(&list.Items[i], w); err != nil { + if err := printEvent(&list.Items[i], w, withNamespace); err != nil { return err } } return nil } -func printLimitRange(limitRange *api.LimitRange, w io.Writer) error { +func printLimitRange(limitRange *api.LimitRange, w io.Writer, withNamespace bool) error { + var name string + if withNamespace { + name = types.NamespacedName{limitRange.Namespace, limitRange.Name}.String() + } else { + name = limitRange.Name + } + _, err := fmt.Fprintf( w, "%s\n", - limitRange.Name, + name, ) return err } // Prints the LimitRangeList in a human-friendly format. -func printLimitRangeList(list *api.LimitRangeList, w io.Writer) error { +func printLimitRangeList(list *api.LimitRangeList, w io.Writer, withNamespace bool) error { for i := range list.Items { - if err := printLimitRange(&list.Items[i], w); err != nil { + if err := printLimitRange(&list.Items[i], w, withNamespace); err != nil { return err } } return nil } -func printResourceQuota(resourceQuota *api.ResourceQuota, w io.Writer) error { +func printResourceQuota(resourceQuota *api.ResourceQuota, w io.Writer, withNamespace bool) error { + var name string + if withNamespace { + name = types.NamespacedName{resourceQuota.Namespace, resourceQuota.Name}.String() + } else { + name = resourceQuota.Name + } + _, err := fmt.Fprintf( w, "%s\n", - resourceQuota.Name, + name, ) return err } // Prints the ResourceQuotaList in a human-friendly format. -func printResourceQuotaList(list *api.ResourceQuotaList, w io.Writer) error { +func printResourceQuotaList(list *api.ResourceQuotaList, w io.Writer, withNamespace bool) error { for i := range list.Items { - if err := printResourceQuota(&list.Items[i], w); err != nil { + if err := printResourceQuota(&list.Items[i], w, withNamespace); err != nil { return err } } return nil } -func printComponentStatus(item *api.ComponentStatus, w io.Writer) error { +func printComponentStatus(item *api.ComponentStatus, w io.Writer, withNamespace bool) error { status := "Unknown" message := "" error := "" @@ -779,9 +857,9 @@ func printComponentStatus(item *api.ComponentStatus, w io.Writer) error { return err } -func printComponentStatusList(list *api.ComponentStatusList, w io.Writer) error { +func printComponentStatusList(list *api.ComponentStatusList, w io.Writer, withNamespace bool) error { for _, item := range list.Items { - if err := printComponentStatus(&item, w); err != nil { + if err := printComponentStatus(&item, w, withNamespace); err != nil { return err } } @@ -799,7 +877,7 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er h.printHeader(handler.columns, w) h.lastType = t } - args := []reflect.Value{reflect.ValueOf(obj), reflect.ValueOf(w)} + args := []reflect.Value{reflect.ValueOf(obj), reflect.ValueOf(w), reflect.ValueOf(h.withNamespace)} resultValue := handler.printFunc.Call(args)[0] if resultValue.IsNil() { return nil diff --git a/pkg/kubectl/resource_printer_test.go b/pkg/kubectl/resource_printer_test.go index 5d4230ee79d..7f520576cc8 100644 --- a/pkg/kubectl/resource_printer_test.go +++ b/pkg/kubectl/resource_printer_test.go @@ -237,12 +237,12 @@ type TestUnknownType struct{} func (*TestUnknownType) IsAnAPIObject() {} -func PrintCustomType(obj *TestPrintType, w io.Writer) error { +func PrintCustomType(obj *TestPrintType, w io.Writer, withNamespace bool) error { _, err := fmt.Fprintf(w, "%s", obj.Data) return err } -func ErrorPrintHandler(obj *TestPrintType, w io.Writer) error { +func ErrorPrintHandler(obj *TestPrintType, w io.Writer, withNamespace bool) error { return fmt.Errorf("ErrorPrintHandler error") } @@ -727,7 +727,7 @@ func TestPrintHumanReadableService(t *testing.T) { for _, svc := range tests { buff := bytes.Buffer{} - printService(&svc, &buff) + printService(&svc, &buff, false) output := string(buff.Bytes()) ip := svc.Spec.PortalIP if !strings.Contains(output, ip) { @@ -855,6 +855,179 @@ func TestInterpretContainerStatus(t *testing.T) { if msg != test.expectedMessage { t.Errorf("expected: %s, got: %s", test.expectedMessage, msg) } - + } +} + +func TestPrintHumanReadableWithNamespace(t *testing.T) { + namespaceName := "testnamespace" + name := "test" + table := []struct { + obj runtime.Object + printNamespace bool + }{ + { + obj: &api.Pod{ + ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, + }, + printNamespace: true, + }, + { + obj: &api.ReplicationController{ + ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, + Spec: api.ReplicationControllerSpec{ + Replicas: 2, + Template: &api.PodTemplateSpec{ + ObjectMeta: api.ObjectMeta{ + Labels: map[string]string{ + "name": "foo", + "type": "production", + }, + }, + Spec: api.PodSpec{ + Containers: []api.Container{ + { + Image: "foo/bar", + TerminationMessagePath: api.TerminationMessagePathDefault, + ImagePullPolicy: api.PullIfNotPresent, + }, + }, + RestartPolicy: api.RestartPolicyAlways, + DNSPolicy: api.DNSDefault, + NodeSelector: map[string]string{ + "baz": "blah", + }, + }, + }, + }, + }, + printNamespace: true, + }, + { + obj: &api.Service{ + ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, + Spec: api.ServiceSpec{ + PortalIP: "1.2.3.4", + PublicIPs: []string{ + "2.3.4.5", + }, + Ports: []api.ServicePort{ + { + Port: 80, + Protocol: "TCP", + }, + }, + }, + }, + printNamespace: true, + }, + { + obj: &api.Endpoints{ + ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, + Subsets: []api.EndpointSubset{{ + Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}, {IP: "localhost"}}, + Ports: []api.EndpointPort{{Port: 8080}}, + }, + }}, + printNamespace: true, + }, + { + obj: &api.Namespace{ + ObjectMeta: api.ObjectMeta{Name: name}, + }, + printNamespace: false, + }, + { + obj: &api.Secret{ + ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, + }, + printNamespace: true, + }, + { + obj: &api.ServiceAccount{ + ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, + Secrets: []api.ObjectReference{}, + }, + printNamespace: true, + }, + { + obj: &api.Node{ + ObjectMeta: api.ObjectMeta{Name: name}, + Status: api.NodeStatus{}, + }, + printNamespace: false, + }, + { + obj: &api.PersistentVolume{ + ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, + Spec: api.PersistentVolumeSpec{}, + }, + printNamespace: true, + }, + { + obj: &api.PersistentVolumeClaim{ + ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, + Spec: api.PersistentVolumeClaimSpec{}, + }, + printNamespace: true, + }, + { + obj: &api.Event{ + ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, + Source: api.EventSource{Component: "kubelet"}, + Message: "Item 1", + FirstTimestamp: util.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)), + LastTimestamp: util.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)), + Count: 1, + }, + printNamespace: false, + }, + { + obj: &api.LimitRange{ + ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, + }, + printNamespace: true, + }, + { + obj: &api.ResourceQuota{ + ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, + }, + printNamespace: true, + }, + { + obj: &api.ComponentStatus{ + Conditions: []api.ComponentCondition{ + {Type: api.ComponentHealthy, Status: api.ConditionTrue, Message: "ok", Error: ""}, + }, + }, + printNamespace: false, + }, + } + + printer := NewHumanReadablePrinter(false, false) + for _, test := range table { + buffer := &bytes.Buffer{} + err := printer.PrintObj(test.obj, buffer) + if err != nil { + t.Fatalf("An error occurred printing object: %#v", err) + } + matched := contains(strings.Fields(buffer.String()), fmt.Sprintf("%s/%s", namespaceName, name)) + if matched { + t.Errorf("Expect printing object not to contain namespace: %v", test.obj) + } + } + + printer = NewHumanReadablePrinter(false, true) + for _, test := range table { + buffer := &bytes.Buffer{} + err := printer.PrintObj(test.obj, buffer) + if err != nil { + t.Fatalf("An error occurred printing object: %#v", err) + } + matched := contains(strings.Fields(buffer.String()), fmt.Sprintf("%s/%s", namespaceName, name)) + if test.printNamespace && !matched { + t.Errorf("Expect printing object to contain namespace: %v", test.obj) + } else if !test.printNamespace && matched { + t.Errorf("Expect printing object not to contain namespace: %v", test.obj) + } } }