Merge pull request #12423 from brendandburns/service
Make services print on a single line.
This commit is contained in:
@@ -391,11 +391,9 @@ func ExamplePrintServiceWithNamespacesAndLabels() {
|
||||
fmt.Printf("Unexpected error: %v", err)
|
||||
}
|
||||
// Output:
|
||||
// |NAMESPACE NAME LABELS SELECTOR IP(S) PORT(S) AGE L1|
|
||||
// |ns1 svc1 l1=value s=magic 10.1.1.1 53/UDP 10y value|
|
||||
// | 53/TCP |
|
||||
// |ns2 svc2 l1=dolla-bill-yall s=kazam 10.1.1.2 80/TCP 10y dolla-bill-yall|
|
||||
// | 8080/TCP |
|
||||
// |NAMESPACE NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE L1|
|
||||
// |ns1 svc1 10.1.1.1 unknown 53/UDP,53/TCP s=magic 10y value|
|
||||
// |ns2 svc2 10.1.1.2 unknown 80/TCP,8080/TCP s=kazam 10y dolla-bill-yall|
|
||||
// ||
|
||||
}
|
||||
|
||||
|
@@ -259,7 +259,7 @@ func (h *HumanReadablePrinter) HandledResources() []string {
|
||||
var podColumns = []string{"NAME", "READY", "STATUS", "RESTARTS", "AGE"}
|
||||
var podTemplateColumns = []string{"TEMPLATE", "CONTAINER(S)", "IMAGE(S)", "PODLABELS"}
|
||||
var replicationControllerColumns = []string{"CONTROLLER", "CONTAINER(S)", "IMAGE(S)", "SELECTOR", "REPLICAS", "AGE"}
|
||||
var serviceColumns = []string{"NAME", "LABELS", "SELECTOR", "IP(S)", "PORT(S)", "AGE"}
|
||||
var serviceColumns = []string{"NAME", "CLUSTER_IP", "EXTERNAL_IP", "PORT(S)", "SELECTOR", "AGE"}
|
||||
var endpointColumns = []string{"NAME", "ENDPOINTS", "AGE"}
|
||||
var nodeColumns = []string{"NAME", "LABELS", "STATUS", "AGE"}
|
||||
var eventColumns = []string{"FIRSTSEEN", "LASTSEEN", "COUNT", "NAME", "KIND", "SUBOBJECT", "REASON", "SOURCE", "MESSAGE"}
|
||||
@@ -557,29 +557,52 @@ func printReplicationControllerList(list *api.ReplicationControllerList, w io.Wr
|
||||
return nil
|
||||
}
|
||||
|
||||
func getServiceExternalIP(svc *api.Service) string {
|
||||
switch svc.Spec.Type {
|
||||
case api.ServiceTypeClusterIP:
|
||||
return "<none>"
|
||||
case api.ServiceTypeNodePort:
|
||||
return "nodes"
|
||||
case api.ServiceTypeLoadBalancer:
|
||||
ingress := svc.Status.LoadBalancer.Ingress
|
||||
result := []string{}
|
||||
for i := range ingress {
|
||||
if ingress[i].IP != "" {
|
||||
result = append(result, ingress[i].IP)
|
||||
}
|
||||
}
|
||||
return strings.Join(result, ",")
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
func makePortString(ports []api.ServicePort) string {
|
||||
pieces := make([]string, len(ports))
|
||||
for ix := range ports {
|
||||
port := &ports[ix]
|
||||
pieces[ix] = fmt.Sprintf("%d/%s", port.Port, port.Protocol)
|
||||
}
|
||||
return strings.Join(pieces, ",")
|
||||
}
|
||||
|
||||
func printService(svc *api.Service, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
|
||||
name := svc.Name
|
||||
namespace := svc.Namespace
|
||||
|
||||
ips := []string{svc.Spec.ClusterIP}
|
||||
|
||||
ingress := svc.Status.LoadBalancer.Ingress
|
||||
for i := range ingress {
|
||||
if ingress[i].IP != "" {
|
||||
ips = append(ips, ingress[i].IP)
|
||||
}
|
||||
}
|
||||
internalIP := svc.Spec.ClusterIP
|
||||
externalIP := getServiceExternalIP(svc)
|
||||
|
||||
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\t%s",
|
||||
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s",
|
||||
name,
|
||||
formatLabels(svc.Labels),
|
||||
internalIP,
|
||||
externalIP,
|
||||
makePortString(svc.Spec.Ports),
|
||||
formatLabels(svc.Spec.Selector),
|
||||
ips[0], svc.Spec.Ports[0].Port, svc.Spec.Ports[0].Protocol,
|
||||
translateTimestamp(svc.CreationTimestamp),
|
||||
); err != nil {
|
||||
return err
|
||||
@@ -587,33 +610,6 @@ func printService(svc *api.Service, w io.Writer, withNamespace bool, wide bool,
|
||||
if _, err := fmt.Fprint(w, appendLabels(svc.Labels, columnLabels)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
extraLinePrefix := "\t\t\t"
|
||||
if withNamespace {
|
||||
extraLinePrefix = "\t\t\t\t"
|
||||
}
|
||||
count := len(svc.Spec.Ports)
|
||||
if len(ips) > count {
|
||||
count = len(ips)
|
||||
}
|
||||
for i := 1; i < count; i++ {
|
||||
ip := ""
|
||||
if len(ips) > i {
|
||||
ip = ips[i]
|
||||
}
|
||||
port := ""
|
||||
if len(svc.Spec.Ports) > i {
|
||||
port = fmt.Sprintf("%d/%s", svc.Spec.Ports[i].Port, svc.Spec.Ports[i].Protocol)
|
||||
}
|
||||
// Lay out additional ports.
|
||||
if _, err := fmt.Fprintf(w, "%s%s\t%s", extraLinePrefix, ip, port); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := fmt.Fprint(w, appendLabelTabs(columnLabels)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -984,7 +980,7 @@ func appendLabels(itemLabels map[string]string, columnLabels []string) string {
|
||||
if il, ok := itemLabels[cl]; ok {
|
||||
buffer.WriteString(fmt.Sprint(il))
|
||||
} else {
|
||||
buffer.WriteString("<n/a>")
|
||||
buffer.WriteString("<none>")
|
||||
}
|
||||
}
|
||||
buffer.WriteString("\n")
|
||||
|
@@ -632,6 +632,7 @@ func TestPrintHumanReadableService(t *testing.T) {
|
||||
{
|
||||
Spec: api.ServiceSpec{
|
||||
ClusterIP: "1.2.3.4",
|
||||
Type: "LoadBalancer",
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Port: 80,
|
||||
@@ -674,6 +675,7 @@ func TestPrintHumanReadableService(t *testing.T) {
|
||||
{
|
||||
Spec: api.ServiceSpec{
|
||||
ClusterIP: "1.2.3.4",
|
||||
Type: "LoadBalancer",
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Port: 80,
|
||||
@@ -702,6 +704,7 @@ func TestPrintHumanReadableService(t *testing.T) {
|
||||
{
|
||||
Spec: api.ServiceSpec{
|
||||
ClusterIP: "1.2.3.4",
|
||||
Type: "LoadBalancer",
|
||||
Ports: []api.ServicePort{
|
||||
{
|
||||
Port: 80,
|
||||
@@ -758,13 +761,9 @@ func TestPrintHumanReadableService(t *testing.T) {
|
||||
t.Errorf("expected to contain port: %s, but doesn't: %s", portSpec, output)
|
||||
}
|
||||
}
|
||||
// Max of # ports and (# public ip + cluster ip)
|
||||
count := len(svc.Spec.Ports)
|
||||
if len(svc.Status.LoadBalancer.Ingress)+1 > count {
|
||||
count = len(svc.Status.LoadBalancer.Ingress) + 1
|
||||
}
|
||||
if count != strings.Count(output, "\n") {
|
||||
t.Errorf("expected %d newlines, found %d", count, strings.Count(output, "\n"))
|
||||
// Each service should print on one line
|
||||
if 1 != strings.Count(output, "\n") {
|
||||
t.Errorf("expected a single newline, found %d", strings.Count(output, "\n"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user