Skip to content

Commit 21a4f60

Browse files
authored
csi: avoid panic in CLI for failed node attachment cleanup (#8525)
If the node API returns an attached volume that doesn't belong to an alloc (because it's failed to clean up properly), `nomad node status` will panic when rendering the response. Also, avoid empty volumes output in node status
1 parent f651278 commit 21a4f60

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

command/node_status.go

+32-25
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,6 @@ func (c *NodeStatusCommand) outputTruncatedNodeDriverInfo(node *api.Node) string
518518
}
519519

520520
func (c *NodeStatusCommand) outputNodeVolumeInfo(node *api.Node) {
521-
c.Ui.Output(c.Colorize().Color("\n[bold]Host Volumes"))
522521

523522
names := make([]string, 0, len(node.HostVolumes))
524523
for name := range node.HostVolumes {
@@ -529,15 +528,17 @@ func (c *NodeStatusCommand) outputNodeVolumeInfo(node *api.Node) {
529528
output := make([]string, 0, len(names)+1)
530529
output = append(output, "Name|ReadOnly|Source")
531530

532-
for _, volName := range names {
533-
info := node.HostVolumes[volName]
534-
output = append(output, fmt.Sprintf("%s|%v|%s", volName, info.ReadOnly, info.Path))
531+
if len(names) > 0 {
532+
c.Ui.Output(c.Colorize().Color("\n[bold]Host Volumes"))
533+
for _, volName := range names {
534+
info := node.HostVolumes[volName]
535+
output = append(output, fmt.Sprintf("%s|%v|%s", volName, info.ReadOnly, info.Path))
536+
}
537+
c.Ui.Output(formatList(output))
535538
}
536-
c.Ui.Output(formatList(output))
537539
}
538540

539541
func (c *NodeStatusCommand) outputNodeCSIVolumeInfo(client *api.Client, node *api.Node, runningAllocs []*api.Allocation) {
540-
c.Ui.Output(c.Colorize().Color("\n[bold]CSI Volumes"))
541542

542543
// Duplicate nodeCSIVolumeNames to sort by name but also index volume names to ids
543544
var names []string
@@ -563,27 +564,33 @@ func (c *NodeStatusCommand) outputNodeCSIVolumeInfo(client *api.Client, node *ap
563564
volumes := map[string]*api.CSIVolumeListStub{}
564565
vs, _ := client.Nodes().CSIVolumes(node.ID, nil)
565566
for _, v := range vs {
566-
n := requests[v.ID].Name
567-
volumes[n] = v
567+
n, ok := requests[v.ID]
568+
if ok {
569+
volumes[n.Name] = v
570+
}
568571
}
569572

570-
// Output the volumes in name order
571-
output := make([]string, 0, len(names)+1)
572-
output = append(output, "ID|Name|Plugin ID|Schedulable|Provider|Access Mode")
573-
for _, name := range names {
574-
v := volumes[name]
575-
output = append(output, fmt.Sprintf(
576-
"%s|%s|%s|%t|%s|%s",
577-
v.ID,
578-
name,
579-
v.PluginID,
580-
v.Schedulable,
581-
v.Provider,
582-
v.AccessMode,
583-
))
584-
}
585-
586-
c.Ui.Output(formatList(output))
573+
if len(names) > 0 {
574+
c.Ui.Output(c.Colorize().Color("\n[bold]CSI Volumes"))
575+
576+
// Output the volumes in name order
577+
output := make([]string, 0, len(names)+1)
578+
output = append(output, "ID|Name|Plugin ID|Schedulable|Provider|Access Mode")
579+
for _, name := range names {
580+
v := volumes[name]
581+
output = append(output, fmt.Sprintf(
582+
"%s|%s|%s|%t|%s|%s",
583+
v.ID,
584+
name,
585+
v.PluginID,
586+
v.Schedulable,
587+
v.Provider,
588+
v.AccessMode,
589+
))
590+
}
591+
592+
c.Ui.Output(formatList(output))
593+
}
587594
}
588595

589596
func (c *NodeStatusCommand) outputNodeDriverInfo(node *api.Node) {

0 commit comments

Comments
 (0)