mirror of https://github.com/k3s-io/k3s
Merge pull request #49359 from wanghaoran1988/convert_to_table_printer
Automatic merge from submit-queue Add some table printer **What this PR does / why we need it**: Add table printer for Service resource **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: related to https://github.com/kubernetes/kubernetes/issues/48820 **Special notes for your reviewer**: **Release note**: ``` None ```pull/6/head
commit
bb48546658
|
@ -89,6 +89,7 @@ go_library(
|
||||||
"//pkg/util/slice:go_default_library",
|
"//pkg/util/slice:go_default_library",
|
||||||
"//vendor/github.com/fatih/camelcase:go_default_library",
|
"//vendor/github.com/fatih/camelcase:go_default_library",
|
||||||
"//vendor/github.com/golang/glog:go_default_library",
|
"//vendor/github.com/golang/glog:go_default_library",
|
||||||
|
"//vendor/k8s.io/api/apps/v1beta1:go_default_library",
|
||||||
"//vendor/k8s.io/api/batch/v1:go_default_library",
|
"//vendor/k8s.io/api/batch/v1:go_default_library",
|
||||||
"//vendor/k8s.io/api/batch/v2alpha1:go_default_library",
|
"//vendor/k8s.io/api/batch/v2alpha1:go_default_library",
|
||||||
"//vendor/k8s.io/api/core/v1:go_default_library",
|
"//vendor/k8s.io/api/core/v1:go_default_library",
|
||||||
|
|
|
@ -26,6 +26,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
appsv1beta1 "k8s.io/api/apps/v1beta1"
|
||||||
batchv1 "k8s.io/api/batch/v1"
|
batchv1 "k8s.io/api/batch/v1"
|
||||||
batchv2alpha1 "k8s.io/api/batch/v2alpha1"
|
batchv2alpha1 "k8s.io/api/batch/v2alpha1"
|
||||||
apiv1 "k8s.io/api/core/v1"
|
apiv1 "k8s.io/api/core/v1"
|
||||||
|
@ -60,13 +61,6 @@ const loadBalancerWidth = 16
|
||||||
// NOTE: When adding a new resource type here, please update the list
|
// NOTE: When adding a new resource type here, please update the list
|
||||||
// pkg/kubectl/cmd/get.go to reflect the new resource type.
|
// pkg/kubectl/cmd/get.go to reflect the new resource type.
|
||||||
var (
|
var (
|
||||||
serviceColumns = []string{"NAME", "TYPE", "CLUSTER-IP", "EXTERNAL-IP", "PORT(S)", "AGE"}
|
|
||||||
serviceWideColumns = []string{"SELECTOR"}
|
|
||||||
ingressColumns = []string{"NAME", "HOSTS", "ADDRESS", "PORTS", "AGE"}
|
|
||||||
statefulSetColumns = []string{"NAME", "DESIRED", "CURRENT", "AGE"}
|
|
||||||
endpointColumns = []string{"NAME", "ENDPOINTS", "AGE"}
|
|
||||||
nodeColumns = []string{"NAME", "STATUS", "AGE", "VERSION"}
|
|
||||||
nodeWideColumns = []string{"EXTERNAL-IP", "OS-IMAGE", "KERNEL-VERSION", "CONTAINER-RUNTIME"}
|
|
||||||
eventColumns = []string{"LASTSEEN", "FIRSTSEEN", "COUNT", "NAME", "KIND", "SUBOBJECT", "TYPE", "REASON", "SOURCE", "MESSAGE"}
|
eventColumns = []string{"LASTSEEN", "FIRSTSEEN", "COUNT", "NAME", "KIND", "SUBOBJECT", "TYPE", "REASON", "SOURCE", "MESSAGE"}
|
||||||
namespaceColumns = []string{"NAME", "STATUS", "AGE"}
|
namespaceColumns = []string{"NAME", "STATUS", "AGE"}
|
||||||
secretColumns = []string{"NAME", "TYPE", "DATA", "AGE"}
|
secretColumns = []string{"NAME", "TYPE", "DATA", "AGE"}
|
||||||
|
@ -196,16 +190,64 @@ func AddHandlers(h printers.PrintHandler) {
|
||||||
h.TableHandler(cronJobColumnDefinitions, printCronJob)
|
h.TableHandler(cronJobColumnDefinitions, printCronJob)
|
||||||
h.TableHandler(cronJobColumnDefinitions, printCronJobList)
|
h.TableHandler(cronJobColumnDefinitions, printCronJobList)
|
||||||
|
|
||||||
h.Handler(serviceColumns, serviceWideColumns, printService)
|
serviceColumnDefinitions := []metav1alpha1.TableColumnDefinition{
|
||||||
h.Handler(serviceColumns, serviceWideColumns, printServiceList)
|
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
|
||||||
h.Handler(ingressColumns, nil, printIngress)
|
{Name: "Type", Type: "string", Description: apiv1.ServiceSpec{}.SwaggerDoc()["type"]},
|
||||||
h.Handler(ingressColumns, nil, printIngressList)
|
{Name: "Cluster-IP", Type: "string", Description: apiv1.ServiceSpec{}.SwaggerDoc()["clusterIP"]},
|
||||||
h.Handler(statefulSetColumns, nil, printStatefulSet)
|
{Name: "External-IP", Type: "string", Description: apiv1.ServiceSpec{}.SwaggerDoc()["externalIPs"]},
|
||||||
h.Handler(statefulSetColumns, nil, printStatefulSetList)
|
{Name: "Port(s)", Type: "string", Description: apiv1.ServiceSpec{}.SwaggerDoc()["ports"]},
|
||||||
h.Handler(endpointColumns, nil, printEndpoints)
|
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
|
||||||
h.Handler(endpointColumns, nil, printEndpointsList)
|
{Name: "Selector", Type: "string", Priority: 1, Description: apiv1.ServiceSpec{}.SwaggerDoc()["selector"]},
|
||||||
h.Handler(nodeColumns, nodeWideColumns, printNode)
|
}
|
||||||
h.Handler(nodeColumns, nodeWideColumns, printNodeList)
|
|
||||||
|
h.TableHandler(serviceColumnDefinitions, printService)
|
||||||
|
h.TableHandler(serviceColumnDefinitions, printServiceList)
|
||||||
|
|
||||||
|
ingressColumnDefinitions := []metav1alpha1.TableColumnDefinition{
|
||||||
|
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
|
||||||
|
{Name: "Hosts", Type: "string", Description: "Hosts that incoming requests are matched against before the ingress rule"},
|
||||||
|
{Name: "Address", Type: "string", Description: "Address is a list containing ingress points for the load-balancer"},
|
||||||
|
{Name: "Ports", Type: "string", Description: "Ports of TLS configurations that open"},
|
||||||
|
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
|
||||||
|
}
|
||||||
|
h.TableHandler(ingressColumnDefinitions, printIngress)
|
||||||
|
h.TableHandler(ingressColumnDefinitions, printIngressList)
|
||||||
|
|
||||||
|
statefulSetColumnDefinitions := []metav1alpha1.TableColumnDefinition{
|
||||||
|
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
|
||||||
|
{Name: "Desired", Type: "string", Description: appsv1beta1.StatefulSetSpec{}.SwaggerDoc()["replicas"]},
|
||||||
|
{Name: "Current", Type: "string", Description: appsv1beta1.StatefulSetStatus{}.SwaggerDoc()["replicas"]},
|
||||||
|
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
|
||||||
|
{Name: "Containers", Type: "string", Priority: 1, Description: "Names of each container in the template."},
|
||||||
|
{Name: "Images", Type: "string", Priority: 1, Description: "Images referenced by each container in the template."},
|
||||||
|
}
|
||||||
|
h.TableHandler(statefulSetColumnDefinitions, printStatefulSet)
|
||||||
|
h.TableHandler(statefulSetColumnDefinitions, printStatefulSetList)
|
||||||
|
|
||||||
|
endpointColumnDefinitions := []metav1alpha1.TableColumnDefinition{
|
||||||
|
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
|
||||||
|
{Name: "Endpoints", Type: "string", Description: apiv1.Endpoints{}.SwaggerDoc()["subsets"]},
|
||||||
|
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
|
||||||
|
}
|
||||||
|
h.TableHandler(endpointColumnDefinitions, printEndpoints)
|
||||||
|
h.TableHandler(endpointColumnDefinitions, printEndpointsList)
|
||||||
|
|
||||||
|
//nodeColumns = []string{"NAME", "STATUS", "AGE", "VERSION"}
|
||||||
|
//nodeWideColumns = []string{"EXTERNAL-IP", "OS-IMAGE", "KERNEL-VERSION", "CONTAINER-RUNTIME"}
|
||||||
|
nodeColumnDefinitions := []metav1alpha1.TableColumnDefinition{
|
||||||
|
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
|
||||||
|
{Name: "Status", Type: "string", Description: "The status of the node"},
|
||||||
|
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
|
||||||
|
{Name: "Version", Type: "string", Description: apiv1.NodeSystemInfo{}.SwaggerDoc()["kubeletVersion"]},
|
||||||
|
{Name: "External-IP", Type: "string", Priority: 1, Description: apiv1.NodeStatus{}.SwaggerDoc()["addresses"]},
|
||||||
|
{Name: "OS-Image", Type: "string", Priority: 1, Description: apiv1.NodeSystemInfo{}.SwaggerDoc()["osImage"]},
|
||||||
|
{Name: "Kernel-Version", Type: "string", Priority: 1, Description: apiv1.NodeSystemInfo{}.SwaggerDoc()["kernelVersion"]},
|
||||||
|
{Name: "Container-Runtime", Type: "string", Priority: 1, Description: apiv1.NodeSystemInfo{}.SwaggerDoc()["containerRuntimeVersion"]},
|
||||||
|
}
|
||||||
|
|
||||||
|
h.TableHandler(nodeColumnDefinitions, printNode)
|
||||||
|
h.TableHandler(nodeColumnDefinitions, printNodeList)
|
||||||
|
|
||||||
h.Handler(eventColumns, nil, printEvent)
|
h.Handler(eventColumns, nil, printEvent)
|
||||||
h.Handler(eventColumns, nil, printEventList)
|
h.Handler(eventColumns, nil, printEventList)
|
||||||
h.Handler(namespaceColumns, nil, printNamespace)
|
h.Handler(namespaceColumns, nil, printNamespace)
|
||||||
|
@ -719,54 +761,39 @@ func makePortString(ports []api.ServicePort) string {
|
||||||
return strings.Join(pieces, ",")
|
return strings.Join(pieces, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
func printService(svc *api.Service, w io.Writer, options printers.PrintOptions) error {
|
func printService(obj *api.Service, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {
|
||||||
name := printers.FormatResourceName(options.Kind, svc.Name, options.WithKind)
|
row := metav1alpha1.TableRow{
|
||||||
namespace := svc.Namespace
|
Object: runtime.RawExtension{Object: obj},
|
||||||
svcType := svc.Spec.Type
|
}
|
||||||
internalIP := svc.Spec.ClusterIP
|
svcType := obj.Spec.Type
|
||||||
|
internalIP := obj.Spec.ClusterIP
|
||||||
if len(internalIP) == 0 {
|
if len(internalIP) == 0 {
|
||||||
internalIP = "<none>"
|
internalIP = "<none>"
|
||||||
}
|
}
|
||||||
externalIP := getServiceExternalIP(svc, options.Wide)
|
externalIP := getServiceExternalIP(obj, options.Wide)
|
||||||
svcPorts := makePortString(svc.Spec.Ports)
|
svcPorts := makePortString(obj.Spec.Ports)
|
||||||
if len(svcPorts) == 0 {
|
if len(svcPorts) == 0 {
|
||||||
svcPorts = "<none>"
|
svcPorts = "<none>"
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.WithNamespace {
|
row.Cells = append(row.Cells, obj.Name, string(svcType), internalIP, externalIP, svcPorts, translateTimestamp(obj.CreationTimestamp))
|
||||||
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%s\t%s",
|
|
||||||
name,
|
|
||||||
string(svcType),
|
|
||||||
internalIP,
|
|
||||||
externalIP,
|
|
||||||
svcPorts,
|
|
||||||
translateTimestamp(svc.CreationTimestamp),
|
|
||||||
); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if options.Wide {
|
if options.Wide {
|
||||||
if _, err := fmt.Fprintf(w, "\t%s", labels.FormatLabels(svc.Spec.Selector)); err != nil {
|
row.Cells = append(row.Cells, labels.FormatLabels(obj.Spec.Selector))
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if _, err := fmt.Fprint(w, printers.AppendLabels(svc.Labels, options.ColumnLabels)); err != nil {
|
return []metav1alpha1.TableRow{row}, nil
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, svc.Labels))
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printServiceList(list *api.ServiceList, w io.Writer, options printers.PrintOptions) error {
|
func printServiceList(list *api.ServiceList, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {
|
||||||
for _, svc := range list.Items {
|
rows := make([]metav1alpha1.TableRow, 0, len(list.Items))
|
||||||
if err := printService(&svc, w, options); err != nil {
|
for i := range list.Items {
|
||||||
return err
|
r, err := printService(&list.Items[i], options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
rows = append(rows, r...)
|
||||||
}
|
}
|
||||||
return nil
|
return rows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// backendStringer behaves just like a string interface and converts the given backend to a string.
|
// backendStringer behaves just like a string interface and converts the given backend to a string.
|
||||||
|
@ -806,91 +833,55 @@ func formatPorts(tls []extensions.IngressTLS) string {
|
||||||
return "80"
|
return "80"
|
||||||
}
|
}
|
||||||
|
|
||||||
func printIngress(ingress *extensions.Ingress, w io.Writer, options printers.PrintOptions) error {
|
func printIngress(obj *extensions.Ingress, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {
|
||||||
name := printers.FormatResourceName(options.Kind, ingress.Name, options.WithKind)
|
row := metav1alpha1.TableRow{
|
||||||
|
Object: runtime.RawExtension{Object: obj},
|
||||||
namespace := ingress.Namespace
|
|
||||||
|
|
||||||
if options.WithNamespace {
|
|
||||||
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
hosts := formatHosts(obj.Spec.Rules)
|
||||||
|
address := loadBalancerStatusStringer(obj.Status.LoadBalancer, options.Wide)
|
||||||
if _, err := fmt.Fprintf(w, "%s\t%v\t%v\t%v\t%s",
|
ports := formatPorts(obj.Spec.TLS)
|
||||||
name,
|
createTime := translateTimestamp(obj.CreationTimestamp)
|
||||||
formatHosts(ingress.Spec.Rules),
|
row.Cells = append(row.Cells, obj.Name, hosts, address, ports, createTime)
|
||||||
loadBalancerStatusStringer(ingress.Status.LoadBalancer, options.Wide),
|
return []metav1alpha1.TableRow{row}, nil
|
||||||
formatPorts(ingress.Spec.TLS),
|
|
||||||
translateTimestamp(ingress.CreationTimestamp),
|
|
||||||
); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err := fmt.Fprint(w, printers.AppendLabels(ingress.Labels, options.ColumnLabels)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, ingress.Labels)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printIngressList(ingressList *extensions.IngressList, w io.Writer, options printers.PrintOptions) error {
|
func printIngressList(list *extensions.IngressList, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {
|
||||||
for _, ingress := range ingressList.Items {
|
rows := make([]metav1alpha1.TableRow, 0, len(list.Items))
|
||||||
if err := printIngress(&ingress, w, options); err != nil {
|
for i := range list.Items {
|
||||||
return err
|
r, err := printIngress(&list.Items[i], options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
rows = append(rows, r...)
|
||||||
}
|
}
|
||||||
return nil
|
return rows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func printStatefulSet(ps *apps.StatefulSet, w io.Writer, options printers.PrintOptions) error {
|
func printStatefulSet(obj *apps.StatefulSet, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {
|
||||||
name := printers.FormatResourceName(options.Kind, ps.Name, options.WithKind)
|
row := metav1alpha1.TableRow{
|
||||||
|
Object: runtime.RawExtension{Object: obj},
|
||||||
namespace := ps.Namespace
|
|
||||||
containers := ps.Spec.Template.Spec.Containers
|
|
||||||
|
|
||||||
if options.WithNamespace {
|
|
||||||
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
desiredReplicas := ps.Spec.Replicas
|
|
||||||
currentReplicas := ps.Status.Replicas
|
|
||||||
if _, err := fmt.Fprintf(w, "%s\t%d\t%d\t%s",
|
|
||||||
name,
|
|
||||||
desiredReplicas,
|
|
||||||
currentReplicas,
|
|
||||||
translateTimestamp(ps.CreationTimestamp),
|
|
||||||
); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
desiredReplicas := obj.Spec.Replicas
|
||||||
|
currentReplicas := obj.Status.Replicas
|
||||||
|
createTime := translateTimestamp(obj.CreationTimestamp)
|
||||||
|
row.Cells = append(row.Cells, obj.Name, desiredReplicas, currentReplicas, createTime)
|
||||||
if options.Wide {
|
if options.Wide {
|
||||||
if err := layoutContainers(containers, w); err != nil {
|
names, images := layoutContainerCells(obj.Spec.Template.Spec.Containers)
|
||||||
return err
|
row.Cells = append(row.Cells, names, images)
|
||||||
}
|
}
|
||||||
if _, err := fmt.Fprintf(w, "\t%s", metav1.FormatLabelSelector(ps.Spec.Selector)); err != nil {
|
return []metav1alpha1.TableRow{row}, nil
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if _, err := fmt.Fprint(w, printers.AppendLabels(ps.Labels, options.ColumnLabels)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, ps.Labels)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printStatefulSetList(statefulSetList *apps.StatefulSetList, w io.Writer, options printers.PrintOptions) error {
|
func printStatefulSetList(list *apps.StatefulSetList, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {
|
||||||
for _, ps := range statefulSetList.Items {
|
rows := make([]metav1alpha1.TableRow, 0, len(list.Items))
|
||||||
if err := printStatefulSet(&ps, w, options); err != nil {
|
for i := range list.Items {
|
||||||
return err
|
r, err := printStatefulSet(&list.Items[i], options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
rows = append(rows, r...)
|
||||||
}
|
}
|
||||||
return nil
|
return rows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func printDaemonSet(obj *extensions.DaemonSet, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {
|
func printDaemonSet(obj *extensions.DaemonSet, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {
|
||||||
|
@ -924,33 +915,24 @@ func printDaemonSetList(list *extensions.DaemonSetList, options printers.PrintOp
|
||||||
return rows, nil
|
return rows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func printEndpoints(endpoints *api.Endpoints, w io.Writer, options printers.PrintOptions) error {
|
func printEndpoints(obj *api.Endpoints, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {
|
||||||
name := printers.FormatResourceName(options.Kind, endpoints.Name, options.WithKind)
|
row := metav1alpha1.TableRow{
|
||||||
|
Object: runtime.RawExtension{Object: obj},
|
||||||
namespace := endpoints.Namespace
|
|
||||||
|
|
||||||
if options.WithNamespace {
|
|
||||||
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
row.Cells = append(row.Cells, obj.Name, formatEndpoints(obj, nil), translateTimestamp(obj.CreationTimestamp))
|
||||||
if _, err := fmt.Fprintf(w, "%s\t%s\t%s", name, formatEndpoints(endpoints, nil), translateTimestamp(endpoints.CreationTimestamp)); err != nil {
|
return []metav1alpha1.TableRow{row}, nil
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err := fmt.Fprint(w, printers.AppendLabels(endpoints.Labels, options.ColumnLabels)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, endpoints.Labels))
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printEndpointsList(list *api.EndpointsList, w io.Writer, options printers.PrintOptions) error {
|
func printEndpointsList(list *api.EndpointsList, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {
|
||||||
for _, item := range list.Items {
|
rows := make([]metav1alpha1.TableRow, 0, len(list.Items))
|
||||||
if err := printEndpoints(&item, w, options); err != nil {
|
for i := range list.Items {
|
||||||
return err
|
r, err := printEndpoints(&list.Items[i], options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
rows = append(rows, r...)
|
||||||
}
|
}
|
||||||
return nil
|
return rows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func printNamespace(item *api.Namespace, w io.Writer, options printers.PrintOptions) error {
|
func printNamespace(item *api.Namespace, w io.Writer, options printers.PrintOptions) error {
|
||||||
|
@ -1039,16 +1021,15 @@ func printServiceAccountList(list *api.ServiceAccountList, w io.Writer, options
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func printNode(node *api.Node, w io.Writer, options printers.PrintOptions) error {
|
func printNode(obj *api.Node, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {
|
||||||
name := printers.FormatResourceName(options.Kind, node.Name, options.WithKind)
|
row := metav1alpha1.TableRow{
|
||||||
|
Object: runtime.RawExtension{Object: obj},
|
||||||
if options.WithNamespace {
|
|
||||||
return fmt.Errorf("node is not namespaced")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
conditionMap := make(map[api.NodeConditionType]*api.NodeCondition)
|
conditionMap := make(map[api.NodeConditionType]*api.NodeCondition)
|
||||||
NodeAllConditions := []api.NodeConditionType{api.NodeReady}
|
NodeAllConditions := []api.NodeConditionType{api.NodeReady}
|
||||||
for i := range node.Status.Conditions {
|
for i := range obj.Status.Conditions {
|
||||||
cond := node.Status.Conditions[i]
|
cond := obj.Status.Conditions[i]
|
||||||
conditionMap[cond.Type] = &cond
|
conditionMap[cond.Type] = &cond
|
||||||
}
|
}
|
||||||
var status []string
|
var status []string
|
||||||
|
@ -1064,16 +1045,13 @@ func printNode(node *api.Node, w io.Writer, options printers.PrintOptions) error
|
||||||
if len(status) == 0 {
|
if len(status) == 0 {
|
||||||
status = append(status, "Unknown")
|
status = append(status, "Unknown")
|
||||||
}
|
}
|
||||||
if node.Spec.Unschedulable {
|
if obj.Spec.Unschedulable {
|
||||||
status = append(status, "SchedulingDisabled")
|
status = append(status, "SchedulingDisabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s", name, strings.Join(status, ","), translateTimestamp(node.CreationTimestamp), node.Status.NodeInfo.KubeletVersion); err != nil {
|
row.Cells = append(row.Cells, obj.Name, strings.Join(status, ","), translateTimestamp(obj.CreationTimestamp), obj.Status.NodeInfo.KubeletVersion)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if options.Wide {
|
if options.Wide {
|
||||||
osImage, kernelVersion, crVersion := node.Status.NodeInfo.OSImage, node.Status.NodeInfo.KernelVersion, node.Status.NodeInfo.ContainerRuntimeVersion
|
osImage, kernelVersion, crVersion := obj.Status.NodeInfo.OSImage, obj.Status.NodeInfo.KernelVersion, obj.Status.NodeInfo.ContainerRuntimeVersion
|
||||||
if osImage == "" {
|
if osImage == "" {
|
||||||
osImage = "<unknown>"
|
osImage = "<unknown>"
|
||||||
}
|
}
|
||||||
|
@ -1083,16 +1061,10 @@ func printNode(node *api.Node, w io.Writer, options printers.PrintOptions) error
|
||||||
if crVersion == "" {
|
if crVersion == "" {
|
||||||
crVersion = "<unknown>"
|
crVersion = "<unknown>"
|
||||||
}
|
}
|
||||||
if _, err := fmt.Fprintf(w, "\t%s\t%s\t%s\t%s", getNodeExternalIP(node), osImage, kernelVersion, crVersion); err != nil {
|
row.Cells = append(row.Cells, getNodeExternalIP(obj), osImage, kernelVersion, crVersion)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// Display caller specify column labels first.
|
return []metav1alpha1.TableRow{row}, nil
|
||||||
if _, err := fmt.Fprint(w, printers.AppendLabels(node.Labels, options.ColumnLabels)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, node.Labels))
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns first external ip of the node or "<none>" if none is found.
|
// Returns first external ip of the node or "<none>" if none is found.
|
||||||
|
@ -1106,13 +1078,16 @@ func getNodeExternalIP(node *api.Node) string {
|
||||||
return "<none>"
|
return "<none>"
|
||||||
}
|
}
|
||||||
|
|
||||||
func printNodeList(list *api.NodeList, w io.Writer, options printers.PrintOptions) error {
|
func printNodeList(list *api.NodeList, options printers.PrintOptions) ([]metav1alpha1.TableRow, error) {
|
||||||
for _, node := range list.Items {
|
rows := make([]metav1alpha1.TableRow, 0, len(list.Items))
|
||||||
if err := printNode(&node, w, options); err != nil {
|
for i := range list.Items {
|
||||||
return err
|
r, err := printNode(&list.Items[i], options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
rows = append(rows, r...)
|
||||||
}
|
}
|
||||||
return nil
|
return rows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func printPersistentVolume(pv *api.PersistentVolume, w io.Writer, options printers.PrintOptions) error {
|
func printPersistentVolume(pv *api.PersistentVolume, w io.Writer, options printers.PrintOptions) error {
|
||||||
|
@ -1789,7 +1764,7 @@ func printStatus(status *metav1.Status, w io.Writer, options printers.PrintOptio
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lay out all the containers on eone line if use wide output.
|
// Lay out all the containers on one line if use wide output.
|
||||||
// DEPRECATED: convert to TableHandler and use layoutContainerCells
|
// DEPRECATED: convert to TableHandler and use layoutContainerCells
|
||||||
func layoutContainers(containers []api.Container, w io.Writer) error {
|
func layoutContainers(containers []api.Container, w io.Writer) error {
|
||||||
var namesBuffer bytes.Buffer
|
var namesBuffer bytes.Buffer
|
||||||
|
|
|
@ -1094,10 +1094,14 @@ func TestPrintHunmanReadableIngressWithColumnLabels(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
buff := bytes.Buffer{}
|
buff := bytes.NewBuffer([]byte{})
|
||||||
printIngress(&ingress, &buff, printers.PrintOptions{
|
table, err := printers.NewTablePrinter().With(AddHandlers).PrintTable(&ingress, printers.PrintOptions{ColumnLabels: []string{"app_name"}})
|
||||||
ColumnLabels: []string{"app_name"},
|
if err != nil {
|
||||||
})
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := printers.PrintTable(table, buff, printers.PrintOptions{NoHeaders: true}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
output := string(buff.Bytes())
|
output := string(buff.Bytes())
|
||||||
appName := ingress.ObjectMeta.Labels["app_name"]
|
appName := ingress.ObjectMeta.Labels["app_name"]
|
||||||
if !strings.Contains(output, appName) {
|
if !strings.Contains(output, appName) {
|
||||||
|
@ -1219,8 +1223,14 @@ func TestPrintHumanReadableService(t *testing.T) {
|
||||||
|
|
||||||
for _, svc := range tests {
|
for _, svc := range tests {
|
||||||
for _, wide := range []bool{false, true} {
|
for _, wide := range []bool{false, true} {
|
||||||
buff := bytes.Buffer{}
|
buff := bytes.NewBuffer([]byte{})
|
||||||
printService(&svc, &buff, printers.PrintOptions{Wide: wide})
|
table, err := printers.NewTablePrinter().With(AddHandlers).PrintTable(&svc, printers.PrintOptions{Wide: wide})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := printers.PrintTable(table, buff, printers.PrintOptions{NoHeaders: true}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
output := string(buff.Bytes())
|
output := string(buff.Bytes())
|
||||||
ip := svc.Spec.ClusterIP
|
ip := svc.Spec.ClusterIP
|
||||||
if !strings.Contains(output, ip) {
|
if !strings.Contains(output, ip) {
|
||||||
|
@ -2594,7 +2604,13 @@ func TestPrintService(t *testing.T) {
|
||||||
|
|
||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
printService(&test.service, buf, printers.PrintOptions{})
|
table, err := printers.NewTablePrinter().With(AddHandlers).PrintTable(&test.service, printers.PrintOptions{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := printers.PrintTable(table, buf, printers.PrintOptions{NoHeaders: true}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
// We ignore time
|
// We ignore time
|
||||||
if buf.String() != test.expect {
|
if buf.String() != test.expect {
|
||||||
t.Fatalf("Expected: %s, but got: %s", test.expect, buf.String())
|
t.Fatalf("Expected: %s, but got: %s", test.expect, buf.String())
|
||||||
|
|
Loading…
Reference in New Issue