De-duplication in printer

This commit is contained in:
xilabao 2017-03-14 17:11:51 +08:00
parent 35159f9c45
commit 697efd1baf
3 changed files with 93 additions and 148 deletions

View File

@ -300,7 +300,7 @@ func printUnstructured(unstructured runtime.Unstructured, w io.Writer, additiona
}
}
name := formatResourceName(options.Kind, metadata.GetName(), options.WithKind)
name := FormatResourceName(options.Kind, metadata.GetName(), options.WithKind)
if _, err := fmt.Fprintf(w, "%s\t%s", name, kind); err != nil {
return err
@ -319,10 +319,10 @@ func printUnstructured(unstructured runtime.Unstructured, w io.Writer, additiona
}
}
}
if _, err := fmt.Fprint(w, appendLabels(metadata.GetLabels(), options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, AppendLabels(metadata.GetLabels(), options.ColumnLabels)); err != nil {
return err
}
if _, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, metadata.GetLabels())); err != nil {
if _, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, metadata.GetLabels())); err != nil {
return err
}
@ -349,10 +349,9 @@ func formatShowLabelsHeader(showLabels bool, t reflect.Type) []string {
return nil
}
// formatResourceName receives a resource kind, name, and boolean specifying
// FormatResourceName receives a resource kind, name, and boolean specifying
// whether or not to update the current name to "kind/name"
// TODO: dedup this with printers/internalversions
func formatResourceName(kind, name string, withKind bool) string {
func FormatResourceName(kind, name string, withKind bool) string {
if !withKind || kind == "" {
return name
}
@ -360,8 +359,7 @@ func formatResourceName(kind, name string, withKind bool) string {
return kind + "/" + name
}
// TODO: dedup this with printers/internalversions
func appendLabels(itemLabels map[string]string, columnLabels []string) string {
func AppendLabels(itemLabels map[string]string, columnLabels []string) string {
var buffer bytes.Buffer
for _, cl := range columnLabels {
@ -378,8 +376,7 @@ func appendLabels(itemLabels map[string]string, columnLabels []string) string {
// Append all labels to a single column. We need this even when show-labels flag* is
// false, since this adds newline delimiter to the end of each row.
// TODO: dedup this with printers/internalversions
func appendAllLabels(showLabels bool, itemLabels map[string]string) string {
func AppendAllLabels(showLabels bool, itemLabels map[string]string) string {
var buffer bytes.Buffer
if showLabels {

View File

@ -196,16 +196,6 @@ func AddHandlers(h *printers.HumanReadablePrinter) {
h.Handler(statusColumns, nil, printStatus)
}
// formatResourceName receives a resource kind, name, and boolean specifying
// whether or not to update the current name to "kind/name"
func formatResourceName(kind, name string, withKind bool) string {
if !withKind || kind == "" {
return name
}
return kind + "/" + name
}
// Pass ports=nil for all ports.
func formatEndpoints(endpoints *api.Endpoints, ports sets.String) string {
if len(endpoints.Subsets) == 0 {
@ -251,7 +241,7 @@ func translateTimestamp(timestamp metav1.Time) string {
}
func printPodBase(pod *api.Pod, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, pod.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, pod.Name, options.WithKind)
namespace := pod.Namespace
restarts := 0
@ -349,10 +339,10 @@ func printPodBase(pod *api.Pod, w io.Writer, options printers.PrintOptions) erro
}
}
if _, err := fmt.Fprint(w, AppendLabels(pod.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(pod.Labels, options.ColumnLabels)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, pod.Labels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, pod.Labels)); err != nil {
return err
}
@ -360,7 +350,7 @@ func printPodBase(pod *api.Pod, w io.Writer, options printers.PrintOptions) erro
}
func printPodTemplate(pod *api.PodTemplate, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, pod.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, pod.Name, options.WithKind)
namespace := pod.Namespace
@ -380,10 +370,10 @@ func printPodTemplate(pod *api.PodTemplate, w io.Writer, options printers.PrintO
if _, err := fmt.Fprintf(w, "\t%s", labels.FormatLabels(pod.Template.Labels)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(pod.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(pod.Labels, options.ColumnLabels)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, pod.Labels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, pod.Labels)); err != nil {
return err
}
@ -401,7 +391,7 @@ func printPodTemplateList(podList *api.PodTemplateList, w io.Writer, options pri
func printPodDisruptionBudget(pdb *policy.PodDisruptionBudget, w io.Writer, options printers.PrintOptions) error {
// name, minavailable, selector
name := formatResourceName(options.Kind, pdb.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, pdb.Name, options.WithKind)
namespace := pdb.Namespace
if options.WithNamespace {
@ -432,7 +422,7 @@ func printPodDisruptionBudgetList(pdbList *policy.PodDisruptionBudgetList, w io.
// TODO(AdoHe): try to put wide output in a single method
func printReplicationController(controller *api.ReplicationController, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, controller.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, controller.Name, options.WithKind)
namespace := controller.Namespace
containers := controller.Spec.Template.Spec.Containers
@ -464,10 +454,10 @@ func printReplicationController(controller *api.ReplicationController, w io.Writ
return err
}
}
if _, err := fmt.Fprint(w, AppendLabels(controller.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(controller.Labels, options.ColumnLabels)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, controller.Labels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, controller.Labels)); err != nil {
return err
}
@ -484,7 +474,7 @@ func printReplicationControllerList(list *api.ReplicationControllerList, w io.Wr
}
func printReplicaSet(rs *extensions.ReplicaSet, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, rs.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, rs.Name, options.WithKind)
namespace := rs.Namespace
containers := rs.Spec.Template.Spec.Containers
@ -515,10 +505,10 @@ func printReplicaSet(rs *extensions.ReplicaSet, w io.Writer, options printers.Pr
return err
}
}
if _, err := fmt.Fprint(w, AppendLabels(rs.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(rs.Labels, options.ColumnLabels)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, rs.Labels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, rs.Labels)); err != nil {
return err
}
@ -535,7 +525,7 @@ func printReplicaSetList(list *extensions.ReplicaSetList, w io.Writer, options p
}
func printCluster(c *federation.Cluster, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, c.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, c.Name, options.WithKind)
var statuses []string
for _, condition := range c.Status.Conditions {
@ -568,7 +558,7 @@ func printClusterList(list *federation.ClusterList, w io.Writer, options printer
}
func printJob(job *batch.Job, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, job.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, job.Name, options.WithKind)
namespace := job.Namespace
containers := job.Spec.Template.Spec.Containers
@ -611,10 +601,10 @@ func printJob(job *batch.Job, w io.Writer, options printers.PrintOptions) error
return err
}
}
if _, err := fmt.Fprint(w, AppendLabels(job.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(job.Labels, options.ColumnLabels)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, job.Labels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, job.Labels)); err != nil {
return err
}
@ -726,7 +716,7 @@ func makePortString(ports []api.ServicePort) string {
}
func printService(svc *api.Service, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, svc.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, svc.Name, options.WithKind)
namespace := svc.Namespace
@ -752,10 +742,10 @@ func printService(svc *api.Service, w io.Writer, options printers.PrintOptions)
return err
}
}
if _, err := fmt.Fprint(w, AppendLabels(svc.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(svc.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, svc.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, svc.Labels))
return err
}
@ -806,7 +796,7 @@ func formatPorts(tls []extensions.IngressTLS) string {
}
func printIngress(ingress *extensions.Ingress, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, ingress.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, ingress.Name, options.WithKind)
namespace := ingress.Namespace
@ -825,11 +815,11 @@ func printIngress(ingress *extensions.Ingress, w io.Writer, options printers.Pri
); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(ingress.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(ingress.Labels, options.ColumnLabels)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, ingress.Labels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, ingress.Labels)); err != nil {
return err
}
return nil
@ -845,7 +835,7 @@ func printIngressList(ingressList *extensions.IngressList, w io.Writer, options
}
func printStatefulSet(ps *apps.StatefulSet, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, ps.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, ps.Name, options.WithKind)
namespace := ps.Namespace
containers := ps.Spec.Template.Spec.Containers
@ -873,10 +863,10 @@ func printStatefulSet(ps *apps.StatefulSet, w io.Writer, options printers.PrintO
return err
}
}
if _, err := fmt.Fprint(w, AppendLabels(ps.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(ps.Labels, options.ColumnLabels)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, ps.Labels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, ps.Labels)); err != nil {
return err
}
@ -893,7 +883,7 @@ func printStatefulSetList(statefulSetList *apps.StatefulSetList, w io.Writer, op
}
func printDaemonSet(ds *extensions.DaemonSet, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, ds.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, ds.Name, options.WithKind)
namespace := ds.Namespace
@ -935,10 +925,10 @@ func printDaemonSet(ds *extensions.DaemonSet, w io.Writer, options printers.Prin
return err
}
}
if _, err := fmt.Fprint(w, AppendLabels(ds.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(ds.Labels, options.ColumnLabels)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, ds.Labels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, ds.Labels)); err != nil {
return err
}
@ -955,7 +945,7 @@ func printDaemonSetList(list *extensions.DaemonSetList, w io.Writer, options pri
}
func printEndpoints(endpoints *api.Endpoints, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, endpoints.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, endpoints.Name, options.WithKind)
namespace := endpoints.Namespace
@ -967,10 +957,10 @@ func printEndpoints(endpoints *api.Endpoints, w io.Writer, options printers.Prin
if _, err := fmt.Fprintf(w, "%s\t%s\t%s", name, formatEndpoints(endpoints, nil), translateTimestamp(endpoints.CreationTimestamp)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(endpoints.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(endpoints.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, endpoints.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, endpoints.Labels))
return err
}
@ -984,7 +974,7 @@ func printEndpointsList(list *api.EndpointsList, w io.Writer, options printers.P
}
func printNamespace(item *api.Namespace, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, item.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, item.Name, options.WithKind)
if options.WithNamespace {
return fmt.Errorf("namespace is not namespaced")
@ -993,10 +983,10 @@ func printNamespace(item *api.Namespace, w io.Writer, options printers.PrintOpti
if _, err := fmt.Fprintf(w, "%s\t%s\t%s", name, item.Status.Phase, translateTimestamp(item.CreationTimestamp)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(item.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(item.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, item.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, item.Labels))
return err
}
@ -1010,7 +1000,7 @@ func printNamespaceList(list *api.NamespaceList, w io.Writer, options printers.P
}
func printSecret(item *api.Secret, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, item.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, item.Name, options.WithKind)
namespace := item.Namespace
@ -1022,10 +1012,10 @@ func printSecret(item *api.Secret, w io.Writer, options printers.PrintOptions) e
if _, err := fmt.Fprintf(w, "%s\t%s\t%v\t%s", name, item.Type, len(item.Data), translateTimestamp(item.CreationTimestamp)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(item.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(item.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, item.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, item.Labels))
return err
}
@ -1040,7 +1030,7 @@ func printSecretList(list *api.SecretList, w io.Writer, options printers.PrintOp
}
func printServiceAccount(item *api.ServiceAccount, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, item.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, item.Name, options.WithKind)
namespace := item.Namespace
@ -1052,10 +1042,10 @@ func printServiceAccount(item *api.ServiceAccount, w io.Writer, options printers
if _, err := fmt.Fprintf(w, "%s\t%d\t%s", name, len(item.Secrets), translateTimestamp(item.CreationTimestamp)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(item.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(item.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, item.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, item.Labels))
return err
}
@ -1070,7 +1060,7 @@ func printServiceAccountList(list *api.ServiceAccountList, w io.Writer, options
}
func printNode(node *api.Node, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, node.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, node.Name, options.WithKind)
if options.WithNamespace {
return fmt.Errorf("node is not namespaced")
@ -1119,10 +1109,10 @@ func printNode(node *api.Node, w io.Writer, options printers.PrintOptions) error
}
}
// Display caller specify column labels first.
if _, err := fmt.Fprint(w, AppendLabels(node.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(node.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, node.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, node.Labels))
return err
}
@ -1163,7 +1153,7 @@ func printNodeList(list *api.NodeList, w io.Writer, options printers.PrintOption
}
func printPersistentVolume(pv *api.PersistentVolume, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, pv.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, pv.Name, options.WithKind)
if options.WithNamespace {
return fmt.Errorf("persistentVolume is not namespaced")
@ -1193,10 +1183,10 @@ func printPersistentVolume(pv *api.PersistentVolume, w io.Writer, options printe
); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(pv.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(pv.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, pv.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, pv.Labels))
return err
}
@ -1210,7 +1200,7 @@ func printPersistentVolumeList(list *api.PersistentVolumeList, w io.Writer, opti
}
func printPersistentVolumeClaim(pvc *api.PersistentVolumeClaim, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, pvc.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, pvc.Name, options.WithKind)
namespace := pvc.Namespace
@ -1233,10 +1223,10 @@ func printPersistentVolumeClaim(pvc *api.PersistentVolumeClaim, w io.Writer, opt
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s", name, phase, pvc.Spec.VolumeName, capacity, accessModes, helper.GetPersistentVolumeClaimClass(pvc), translateTimestamp(pvc.CreationTimestamp)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(pvc.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(pvc.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, pvc.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, pvc.Labels))
return err
}
@ -1250,7 +1240,7 @@ func printPersistentVolumeClaimList(list *api.PersistentVolumeClaimList, w io.Wr
}
func printEvent(event *api.Event, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, event.InvolvedObject.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, event.InvolvedObject.Name, options.WithKind)
namespace := event.Namespace
if options.WithNamespace {
@ -1284,10 +1274,10 @@ func printEvent(event *api.Event, w io.Writer, options printers.PrintOptions) er
); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(event.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(event.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, event.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, event.Labels))
return err
}
@ -1318,7 +1308,7 @@ func printLimitRangeList(list *api.LimitRangeList, w io.Writer, options printers
// printObjectMeta prints the object metadata of a given resource.
func printObjectMeta(meta metav1.ObjectMeta, w io.Writer, options printers.PrintOptions, namespaced bool) error {
name := formatResourceName(options.Kind, meta.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, meta.Name, options.WithKind)
if namespaced && options.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", meta.Namespace); err != nil {
@ -1333,10 +1323,10 @@ func printObjectMeta(meta metav1.ObjectMeta, w io.Writer, options printers.Print
); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(meta.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(meta.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, meta.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, meta.Labels))
return err
}
@ -1370,7 +1360,7 @@ func printRoleList(list *rbac.RoleList, w io.Writer, options printers.PrintOptio
func printRoleBinding(roleBinding *rbac.RoleBinding, w io.Writer, options printers.PrintOptions) error {
meta := roleBinding.ObjectMeta
name := formatResourceName(options.Kind, meta.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, meta.Name, options.WithKind)
if options.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", meta.Namespace); err != nil {
@ -1399,10 +1389,10 @@ func printRoleBinding(roleBinding *rbac.RoleBinding, w io.Writer, options printe
}
}
if _, err := fmt.Fprint(w, AppendLabels(meta.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(meta.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, meta.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, meta.Labels))
return err
}
@ -1435,7 +1425,7 @@ func printClusterRoleList(list *rbac.ClusterRoleList, w io.Writer, options print
func printClusterRoleBinding(clusterRoleBinding *rbac.ClusterRoleBinding, w io.Writer, options printers.PrintOptions) error {
meta := clusterRoleBinding.ObjectMeta
name := formatResourceName(options.Kind, meta.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, meta.Name, options.WithKind)
if options.WithNamespace {
return fmt.Errorf("clusterRoleBinding is not namespaced")
@ -1462,10 +1452,10 @@ func printClusterRoleBinding(clusterRoleBinding *rbac.ClusterRoleBinding, w io.W
}
}
if _, err := fmt.Fprint(w, AppendLabels(meta.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(meta.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, meta.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, meta.Labels))
return err
}
@ -1480,7 +1470,7 @@ func printClusterRoleBindingList(list *rbac.ClusterRoleBindingList, w io.Writer,
}
func printCertificateSigningRequest(csr *certificates.CertificateSigningRequest, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, csr.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, csr.Name, options.WithKind)
meta := csr.ObjectMeta
status, err := extractCSRStatus(csr)
@ -1497,10 +1487,10 @@ func printCertificateSigningRequest(csr *certificates.CertificateSigningRequest,
); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(meta.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(meta.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err = fmt.Fprint(w, AppendAllLabels(options.ShowLabels, meta.Labels))
_, err = fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, meta.Labels))
return err
}
@ -1541,7 +1531,7 @@ func printCertificateSigningRequestList(list *certificates.CertificateSigningReq
}
func printComponentStatus(item *api.ComponentStatus, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, item.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, item.Name, options.WithKind)
if options.WithNamespace {
return fmt.Errorf("componentStatus is not namespaced")
@ -1565,10 +1555,10 @@ func printComponentStatus(item *api.ComponentStatus, w io.Writer, options printe
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s", name, status, message, error); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(item.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(item.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, item.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, item.Labels))
return err
}
@ -1583,7 +1573,7 @@ func printComponentStatusList(list *api.ComponentStatusList, w io.Writer, option
}
func printThirdPartyResource(rsrc *extensions.ThirdPartyResource, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, rsrc.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, rsrc.Name, options.WithKind)
versions := make([]string, len(rsrc.Versions))
for ix := range rsrc.Versions {
@ -1615,7 +1605,7 @@ func truncate(str string, maxLen int) string {
}
func printThirdPartyResourceData(rsrc *extensions.ThirdPartyResourceData, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, rsrc.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, rsrc.Name, options.WithKind)
l := labels.FormatLabels(rsrc.Labels)
truncateCols := 50
@ -1639,7 +1629,7 @@ func printThirdPartyResourceDataList(list *extensions.ThirdPartyResourceDataList
}
func printDeployment(deployment *extensions.Deployment, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, deployment.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, deployment.Name, options.WithKind)
if options.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", deployment.Namespace); err != nil {
@ -1671,10 +1661,10 @@ func printDeployment(deployment *extensions.Deployment, w io.Writer, options pri
}
}
if _, err := fmt.Fprint(w, AppendLabels(deployment.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(deployment.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err = fmt.Fprint(w, AppendAllLabels(options.ShowLabels, deployment.Labels))
_, err = fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, deployment.Labels))
return err
}
@ -1749,7 +1739,7 @@ func formatHPAMetrics(specs []autoscaling.MetricSpec, statuses []autoscaling.Met
func printHorizontalPodAutoscaler(hpa *autoscaling.HorizontalPodAutoscaler, w io.Writer, options printers.PrintOptions) error {
namespace := hpa.Namespace
name := formatResourceName(options.Kind, hpa.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, hpa.Name, options.WithKind)
reference := fmt.Sprintf("%s/%s",
hpa.Spec.ScaleTargetRef.Kind,
@ -1779,10 +1769,10 @@ func printHorizontalPodAutoscaler(hpa *autoscaling.HorizontalPodAutoscaler, w io
); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(hpa.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(hpa.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, hpa.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, hpa.Labels))
return err
}
@ -1796,7 +1786,7 @@ func printHorizontalPodAutoscalerList(list *autoscaling.HorizontalPodAutoscalerL
}
func printConfigMap(configMap *api.ConfigMap, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, configMap.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, configMap.Name, options.WithKind)
namespace := configMap.Namespace
@ -1808,10 +1798,10 @@ func printConfigMap(configMap *api.ConfigMap, w io.Writer, options printers.Prin
if _, err := fmt.Fprintf(w, "%s\t%v\t%s", name, len(configMap.Data), translateTimestamp(configMap.CreationTimestamp)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(configMap.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(configMap.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, configMap.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, configMap.Labels))
return err
}
@ -1825,7 +1815,7 @@ func printConfigMapList(list *api.ConfigMapList, w io.Writer, options printers.P
}
func printPodSecurityPolicy(item *extensions.PodSecurityPolicy, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, item.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, item.Name, options.WithKind)
_, err := fmt.Fprintf(w, "%s\t%t\t%v\t%s\t%s\t%s\t%s\t%t\t%v\n", name, item.Spec.Privileged,
item.Spec.AllowedCapabilities, item.Spec.SELinux.Rule,
@ -1844,7 +1834,7 @@ func printPodSecurityPolicyList(list *extensions.PodSecurityPolicyList, w io.Wri
}
func printNetworkPolicy(networkPolicy *extensions.NetworkPolicy, w io.Writer, options printers.PrintOptions) error {
name := formatResourceName(options.Kind, networkPolicy.Name, options.WithKind)
name := printers.FormatResourceName(options.Kind, networkPolicy.Name, options.WithKind)
namespace := networkPolicy.Namespace
@ -1856,10 +1846,10 @@ func printNetworkPolicy(networkPolicy *extensions.NetworkPolicy, w io.Writer, op
if _, err := fmt.Fprintf(w, "%s\t%v\t%s", name, metav1.FormatLabelSelector(&networkPolicy.Spec.PodSelector), translateTimestamp(networkPolicy.CreationTimestamp)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(networkPolicy.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(networkPolicy.Labels, options.ColumnLabels)); err != nil {
return err
}
_, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, networkPolicy.Labels))
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, networkPolicy.Labels))
return err
}
@ -1887,10 +1877,10 @@ func printStorageClass(sc *storage.StorageClass, w io.Writer, options printers.P
if _, err := fmt.Fprintf(w, "%s\t%s\t", name, provtype); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendLabels(sc.Labels, options.ColumnLabels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendLabels(sc.Labels, options.ColumnLabels)); err != nil {
return err
}
if _, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, sc.Labels)); err != nil {
if _, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, sc.Labels)); err != nil {
return err
}
@ -1914,48 +1904,6 @@ func printStatus(status *metav1.Status, w io.Writer, options printers.PrintOptio
return nil
}
func AppendLabels(itemLabels map[string]string, columnLabels []string) string {
var buffer bytes.Buffer
for _, cl := range columnLabels {
buffer.WriteString(fmt.Sprint("\t"))
if il, ok := itemLabels[cl]; ok {
buffer.WriteString(fmt.Sprint(il))
} else {
buffer.WriteString("<none>")
}
}
return buffer.String()
}
// Append all labels to a single column. We need this even when show-labels flag* is
// false, since this adds newline delimiter to the end of each row.
func AppendAllLabels(showLabels bool, itemLabels map[string]string) string {
var buffer bytes.Buffer
if showLabels {
buffer.WriteString(fmt.Sprint("\t"))
buffer.WriteString(labels.FormatLabels(itemLabels))
}
buffer.WriteString("\n")
return buffer.String()
}
// Append a set of tabs for each label column. We need this in the case where
// we have extra lines so that the tabwriter will still line things up.
func AppendLabelTabs(columnLabels []string) string {
var buffer bytes.Buffer
for range columnLabels {
buffer.WriteString("\t")
}
buffer.WriteString("\n")
return buffer.String()
}
// Lay out all the containers on one line if use wide output.
func layoutContainers(containers []api.Container, w io.Writer) error {
var namesBuffer bytes.Buffer

View File

@ -236,7 +236,7 @@ func TestFormatResourceName(t *testing.T) {
{"kind", "name", "kind/name"},
}
for _, tt := range tests {
if got := formatResourceName(tt.kind, tt.name, true); got != tt.want {
if got := printers.FormatResourceName(tt.kind, tt.name, true); got != tt.want {
t.Errorf("formatResourceName(%q, %q) = %q, want %q", tt.kind, tt.name, got, tt.want)
}
}