mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 08:17:26 +00:00
Add Service table printer
This commit is contained in:
parent
80582826aa
commit
9bd1529a98
@ -60,8 +60,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"}
|
ingressColumns = []string{"NAME", "HOSTS", "ADDRESS", "PORTS", "AGE"}
|
||||||
statefulSetColumns = []string{"NAME", "DESIRED", "CURRENT", "AGE"}
|
statefulSetColumns = []string{"NAME", "DESIRED", "CURRENT", "AGE"}
|
||||||
endpointColumns = []string{"NAME", "ENDPOINTS", "AGE"}
|
endpointColumns = []string{"NAME", "ENDPOINTS", "AGE"}
|
||||||
@ -196,8 +194,19 @@ 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"]},
|
||||||
|
{Name: "Type", Type: "string", Description: apiv1.ServiceSpec{}.SwaggerDoc()["type"]},
|
||||||
|
{Name: "Cluster-IP", Type: "string", Description: apiv1.ServiceSpec{}.SwaggerDoc()["clusterIP"]},
|
||||||
|
{Name: "External-IP", Type: "string", Description: apiv1.ServiceSpec{}.SwaggerDoc()["externalIPs"]},
|
||||||
|
{Name: "Port(s)", Type: "string", Description: apiv1.ServiceSpec{}.SwaggerDoc()["ports"]},
|
||||||
|
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
|
||||||
|
{Name: "Selector", Type: "string", Priority: 1, Description: apiv1.ServiceSpec{}.SwaggerDoc()["selector"]},
|
||||||
|
}
|
||||||
|
|
||||||
|
h.TableHandler(serviceColumnDefinitions, printService)
|
||||||
|
h.TableHandler(serviceColumnDefinitions, printServiceList)
|
||||||
|
|
||||||
h.Handler(ingressColumns, nil, printIngress)
|
h.Handler(ingressColumns, nil, printIngress)
|
||||||
h.Handler(ingressColumns, nil, printIngressList)
|
h.Handler(ingressColumns, nil, printIngressList)
|
||||||
h.Handler(statefulSetColumns, nil, printStatefulSet)
|
h.Handler(statefulSetColumns, nil, printStatefulSet)
|
||||||
@ -719,54 +728,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 err
|
return []metav1alpha1.TableRow{row}, nil
|
||||||
}
|
|
||||||
_, 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.
|
||||||
|
@ -1219,8 +1219,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 +2600,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
Block a user