From 78e4459446e72ae86a48428dd118bea682867733 Mon Sep 17 00:00:00 2001 From: Danielle Lancashire Date: Fri, 9 Aug 2019 12:47:09 +0200 Subject: [PATCH] command: Cleanup node-status --- command/node_status.go | 152 +++++++++++++++++++++++------------------ 1 file changed, 84 insertions(+), 68 deletions(-) diff --git a/command/node_status.go b/command/node_status.go index 54303935a1a..0199f7b242b 100644 --- a/command/node_status.go +++ b/command/node_status.go @@ -301,7 +301,7 @@ func nodeDrivers(n *api.Node) []string { func nodeVolumeNames(n *api.Node) []string { var volumes []string - for name, _ := range n.HostVolumes { + for name := range n.HostVolumes { volumes = append(volumes, name) } @@ -346,88 +346,104 @@ func (c *NodeStatusCommand) formatNode(client *api.Client, node *api.Node) int { basic = append(basic, fmt.Sprintf("Host Volumes|%s", strings.Join(nodeVolumeNames(node), ","))) basic = append(basic, fmt.Sprintf("Drivers|%s", strings.Join(nodeDrivers(node), ","))) c.Ui.Output(c.Colorize().Color(formatKV(basic))) - } else { - // Get the host stats - hostStats, nodeStatsErr := client.Nodes().Stats(node.ID, nil) - if nodeStatsErr != nil { - c.Ui.Output("") - c.Ui.Error(fmt.Sprintf("error fetching node stats: %v", nodeStatsErr)) - } - if hostStats != nil { - uptime := time.Duration(hostStats.Uptime * uint64(time.Second)) - basic = append(basic, fmt.Sprintf("Uptime|%s", uptime.String())) - } - // Emit the volume info - if !c.verbose { - basic = append(basic, fmt.Sprintf("Host Volumes|%s", strings.Join(nodeVolumeNames(node), ","))) + // Output alloc info + if err := c.outputAllocInfo(client, node); err != nil { + c.Ui.Error(fmt.Sprintf("%s", err)) + return 1 } - // Emit the driver info - if !c.verbose { - driverStatus := fmt.Sprintf("Driver Status| %s", c.outputTruncatedNodeDriverInfo(node)) - basic = append(basic, driverStatus) - } + return 0 + } - c.Ui.Output(c.Colorize().Color(formatKV(basic))) + // Get the host stats + hostStats, nodeStatsErr := client.Nodes().Stats(node.ID, nil) + if nodeStatsErr != nil { + c.Ui.Output("") + c.Ui.Error(fmt.Sprintf("error fetching node stats: %v", nodeStatsErr)) + } + if hostStats != nil { + uptime := time.Duration(hostStats.Uptime * uint64(time.Second)) + basic = append(basic, fmt.Sprintf("Uptime|%s", uptime.String())) + } - if c.verbose { - c.outputNodeVolumeInfo(node) - c.outputNodeDriverInfo(node) - } + // When we're not running in verbose mode, then also include host volumes and + // driver info in the basic output + if !c.verbose { + basic = append(basic, fmt.Sprintf("Host Volumes|%s", strings.Join(nodeVolumeNames(node), ","))) - // Emit node events - c.outputNodeStatusEvents(node) + driverStatus := fmt.Sprintf("Driver Status| %s", c.outputTruncatedNodeDriverInfo(node)) + basic = append(basic, driverStatus) + } - // Get list of running allocations on the node - runningAllocs, err := getRunningAllocs(client, node.ID) - if err != nil { - c.Ui.Error(fmt.Sprintf("Error querying node for running allocations: %s", err)) - return 1 - } + // Output the basic info + c.Ui.Output(c.Colorize().Color(formatKV(basic))) - allocatedResources := getAllocatedResources(client, runningAllocs, node) - c.Ui.Output(c.Colorize().Color("\n[bold]Allocated Resources[reset]")) - c.Ui.Output(formatList(allocatedResources)) + // If we're running in verbose mode, include full host volume and driver info + if c.verbose { + c.outputNodeVolumeInfo(node) + c.outputNodeDriverInfo(node) + } - actualResources, err := getActualResources(client, runningAllocs, node) - if err == nil { - c.Ui.Output(c.Colorize().Color("\n[bold]Allocation Resource Utilization[reset]")) - c.Ui.Output(formatList(actualResources)) - } + // Emit node events + c.outputNodeStatusEvents(node) - hostResources, err := getHostResources(hostStats, node) - if err != nil { - c.Ui.Output("") - c.Ui.Error(fmt.Sprintf("error fetching node stats: %v", err)) - } - if err == nil { - c.Ui.Output(c.Colorize().Color("\n[bold]Host Resource Utilization[reset]")) - c.Ui.Output(formatList(hostResources)) - } + // Get list of running allocations on the node + runningAllocs, err := getRunningAllocs(client, node.ID) + if err != nil { + c.Ui.Error(fmt.Sprintf("Error querying node for running allocations: %s", err)) + return 1 + } - if err == nil && node.NodeResources != nil && len(node.NodeResources.Devices) > 0 { - c.Ui.Output(c.Colorize().Color("\n[bold]Device Resource Utilization[reset]")) - c.Ui.Output(formatList(getDeviceResourcesForNode(hostStats.DeviceStats, node))) - } - if hostStats != nil && c.stats { - c.Ui.Output(c.Colorize().Color("\n[bold]CPU Stats[reset]")) - c.printCpuStats(hostStats) - c.Ui.Output(c.Colorize().Color("\n[bold]Memory Stats[reset]")) - c.printMemoryStats(hostStats) - c.Ui.Output(c.Colorize().Color("\n[bold]Disk Stats[reset]")) - c.printDiskStats(hostStats) - if len(hostStats.DeviceStats) > 0 { - c.Ui.Output(c.Colorize().Color("\n[bold]Device Stats[reset]")) - printDeviceStats(c.Ui, hostStats.DeviceStats) - } + allocatedResources := getAllocatedResources(client, runningAllocs, node) + c.Ui.Output(c.Colorize().Color("\n[bold]Allocated Resources[reset]")) + c.Ui.Output(formatList(allocatedResources)) + + actualResources, err := getActualResources(client, runningAllocs, node) + if err == nil { + c.Ui.Output(c.Colorize().Color("\n[bold]Allocation Resource Utilization[reset]")) + c.Ui.Output(formatList(actualResources)) + } + + hostResources, err := getHostResources(hostStats, node) + if err != nil { + c.Ui.Output("") + c.Ui.Error(fmt.Sprintf("error fetching node stats: %v", err)) + } + if err == nil { + c.Ui.Output(c.Colorize().Color("\n[bold]Host Resource Utilization[reset]")) + c.Ui.Output(formatList(hostResources)) + } + + if err == nil && node.NodeResources != nil && len(node.NodeResources.Devices) > 0 { + c.Ui.Output(c.Colorize().Color("\n[bold]Device Resource Utilization[reset]")) + c.Ui.Output(formatList(getDeviceResourcesForNode(hostStats.DeviceStats, node))) + } + if hostStats != nil && c.stats { + c.Ui.Output(c.Colorize().Color("\n[bold]CPU Stats[reset]")) + c.printCpuStats(hostStats) + c.Ui.Output(c.Colorize().Color("\n[bold]Memory Stats[reset]")) + c.printMemoryStats(hostStats) + c.Ui.Output(c.Colorize().Color("\n[bold]Disk Stats[reset]")) + c.printDiskStats(hostStats) + if len(hostStats.DeviceStats) > 0 { + c.Ui.Output(c.Colorize().Color("\n[bold]Device Stats[reset]")) + printDeviceStats(c.Ui, hostStats.DeviceStats) } } + if err := c.outputAllocInfo(client, node); err != nil { + c.Ui.Error(fmt.Sprintf("%s", err)) + return 1 + } + + return 0 +} + +func (c *NodeStatusCommand) outputAllocInfo(client *api.Client, node *api.Node) error { nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil) if err != nil { - c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err)) - return 1 + return fmt.Errorf("Error querying node allocations: %s", err) } c.Ui.Output(c.Colorize().Color("\n[bold]Allocations[reset]")) @@ -438,8 +454,8 @@ func (c *NodeStatusCommand) formatNode(client *api.Client, node *api.Node) int { c.formatDeviceAttributes(node) c.formatMeta(node) } - return 0 + return nil } func (c *NodeStatusCommand) outputTruncatedNodeDriverInfo(node *api.Node) string {