2020-03-06 15:09:10 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-04-08 15:13:36 +00:00
|
|
|
"errors"
|
2020-03-06 15:09:10 +00:00
|
|
|
"fmt"
|
2021-04-08 15:13:36 +00:00
|
|
|
"io"
|
2020-03-06 15:09:10 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *VolumeStatusCommand) csiBanner() {
|
|
|
|
if !(c.json || len(c.template) > 0) {
|
|
|
|
c.Ui.Output(c.Colorize().Color("[bold]Container Storage Interface[reset]"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VolumeStatusCommand) csiStatus(client *api.Client, id string) int {
|
|
|
|
// Invoke list mode if no volume id
|
|
|
|
if id == "" {
|
2021-03-23 13:11:18 +00:00
|
|
|
return c.listVolumes(client)
|
2020-03-06 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 18:32:40 +00:00
|
|
|
// Prefix search for the volume
|
|
|
|
vols, _, err := client.CSIVolumes().List(&api.QueryOptions{Prefix: id})
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying volumes: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if len(vols) == 0 {
|
|
|
|
c.Ui.Error(fmt.Sprintf("No volumes(s) with prefix or ID %q found", id))
|
|
|
|
return 1
|
|
|
|
}
|
2022-04-15 18:16:30 +00:00
|
|
|
|
|
|
|
var ns string
|
|
|
|
|
|
|
|
if len(vols) == 1 {
|
|
|
|
// need to set id from the actual ID because it might be a prefix
|
|
|
|
id = vols[0].ID
|
|
|
|
ns = vols[0].Namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
// List sorts by CreateIndex, not by ID, so we need to search for
|
|
|
|
// exact matches but account for multiple exact ID matches across
|
|
|
|
// namespaces
|
2022-02-11 13:53:03 +00:00
|
|
|
if len(vols) > 1 {
|
2022-04-15 18:16:30 +00:00
|
|
|
exactMatchesCount := 0
|
|
|
|
for _, vol := range vols {
|
|
|
|
if vol.ID == id {
|
|
|
|
exactMatchesCount++
|
|
|
|
ns = vol.Namespace
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if exactMatchesCount != 1 {
|
2022-02-11 13:53:03 +00:00
|
|
|
out, err := c.csiFormatVolumes(vols)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error formatting: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
c.Ui.Error(fmt.Sprintf("Prefix matched multiple volumes\n\n%s", out))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 18:32:40 +00:00
|
|
|
|
2020-03-06 15:09:10 +00:00
|
|
|
// Try querying the volume
|
2022-04-15 18:16:30 +00:00
|
|
|
client.SetNamespace(ns)
|
2020-03-06 15:09:10 +00:00
|
|
|
vol, _, err := client.CSIVolumes().Info(id, nil)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying volume: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
str, err := c.formatBasic(vol)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error formatting volume: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
c.Ui.Output(str)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-03-23 13:11:18 +00:00
|
|
|
func (c *VolumeStatusCommand) listVolumes(client *api.Client) int {
|
|
|
|
|
|
|
|
c.csiBanner()
|
|
|
|
vols, _, err := client.CSIVolumes().List(nil)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying volumes: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(vols) == 0 {
|
|
|
|
// No output if we have no volumes
|
|
|
|
c.Ui.Error("No CSI volumes")
|
|
|
|
} else {
|
|
|
|
str, err := c.csiFormatVolumes(vols)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error formatting: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
c.Ui.Output(str)
|
|
|
|
}
|
|
|
|
if !c.verbose {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
plugins, _, err := client.CSIPlugins().List(nil)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying CSI plugins: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(plugins) == 0 {
|
|
|
|
return 0 // No more output if we have no plugins
|
|
|
|
}
|
|
|
|
|
|
|
|
var code int
|
|
|
|
q := &api.QueryOptions{PerPage: 30} // TODO: tune page size
|
|
|
|
|
2021-04-07 13:48:40 +00:00
|
|
|
NEXT_PLUGIN:
|
2021-03-23 13:11:18 +00:00
|
|
|
for _, plugin := range plugins {
|
|
|
|
if !plugin.ControllerRequired || plugin.ControllersHealthy < 1 {
|
|
|
|
continue // only controller plugins can support this query
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
externalList, _, err := client.CSIVolumes().ListExternal(plugin.ID, q)
|
2021-04-08 15:13:36 +00:00
|
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
2021-03-23 13:11:18 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error querying CSI external volumes for plugin %q: %s", plugin.ID, err))
|
|
|
|
// we'll stop querying this plugin, but there may be more to
|
|
|
|
// query, so report and set the error code but move on to the
|
|
|
|
// next plugin
|
|
|
|
code = 1
|
2021-04-07 13:48:40 +00:00
|
|
|
continue NEXT_PLUGIN
|
2021-03-23 13:11:18 +00:00
|
|
|
}
|
2021-04-08 15:13:36 +00:00
|
|
|
if externalList == nil || len(externalList.Volumes) == 0 {
|
|
|
|
// several plugins return EOF once you hit the end of the page,
|
|
|
|
// rather than an empty list
|
|
|
|
continue NEXT_PLUGIN
|
|
|
|
}
|
2021-06-25 20:17:37 +00:00
|
|
|
rows := []string{"External ID|Condition|Nodes"}
|
|
|
|
for _, v := range externalList.Volumes {
|
2021-04-08 15:13:36 +00:00
|
|
|
condition := "OK"
|
|
|
|
if v.IsAbnormal {
|
|
|
|
condition = fmt.Sprintf("Abnormal (%v)", v.Status)
|
2021-03-23 13:11:18 +00:00
|
|
|
}
|
2021-06-25 20:17:37 +00:00
|
|
|
rows = append(rows, fmt.Sprintf("%s|%s|%s",
|
2021-04-08 15:13:36 +00:00
|
|
|
limit(v.ExternalID, c.length),
|
|
|
|
limit(condition, 20),
|
|
|
|
strings.Join(v.PublishedExternalNodeIDs, ","),
|
2021-06-25 20:17:37 +00:00
|
|
|
))
|
2021-03-23 13:11:18 +00:00
|
|
|
}
|
2021-04-08 15:13:36 +00:00
|
|
|
c.Ui.Output(formatList(rows))
|
2021-03-23 13:11:18 +00:00
|
|
|
|
|
|
|
q.NextToken = externalList.NextToken
|
|
|
|
if q.NextToken == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// we can't know the shape of arbitrarily-sized lists of volumes,
|
|
|
|
// so break after each page
|
|
|
|
c.Ui.Output("...")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:09:10 +00:00
|
|
|
func (c *VolumeStatusCommand) csiFormatVolumes(vols []*api.CSIVolumeListStub) (string, error) {
|
|
|
|
// Sort the output by volume id
|
|
|
|
sort.Slice(vols, func(i, j int) bool { return vols[i].ID < vols[j].ID })
|
|
|
|
|
|
|
|
if c.json || len(c.template) > 0 {
|
|
|
|
out, err := Format(c.json, c.template, vols)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("format error: %v", err)
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2022-03-01 12:57:29 +00:00
|
|
|
return csiFormatSortedVolumes(vols)
|
2021-03-18 18:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Format the volumes, assumes that we're already sorted by volume ID
|
2022-03-01 12:57:29 +00:00
|
|
|
func csiFormatSortedVolumes(vols []*api.CSIVolumeListStub) (string, error) {
|
2020-03-06 15:09:10 +00:00
|
|
|
rows := make([]string, len(vols)+1)
|
|
|
|
rows[0] = "ID|Name|Plugin ID|Schedulable|Access Mode"
|
|
|
|
for i, v := range vols {
|
|
|
|
rows[i+1] = fmt.Sprintf("%s|%s|%s|%t|%s",
|
2022-03-01 12:57:29 +00:00
|
|
|
v.ID,
|
2020-03-06 15:09:10 +00:00
|
|
|
v.Name,
|
|
|
|
v.PluginID,
|
|
|
|
v.Schedulable,
|
|
|
|
v.AccessMode,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return formatList(rows), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *VolumeStatusCommand) formatBasic(vol *api.CSIVolume) (string, error) {
|
|
|
|
if c.json || len(c.template) > 0 {
|
|
|
|
out, err := Format(c.json, c.template, vol)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("format error: %v", err)
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
output := []string{
|
|
|
|
fmt.Sprintf("ID|%s", vol.ID),
|
|
|
|
fmt.Sprintf("Name|%s", vol.Name),
|
|
|
|
fmt.Sprintf("External ID|%s", vol.ExternalID),
|
2020-03-09 13:57:59 +00:00
|
|
|
fmt.Sprintf("Plugin ID|%s", vol.PluginID),
|
|
|
|
fmt.Sprintf("Provider|%s", vol.Provider),
|
|
|
|
fmt.Sprintf("Version|%s", vol.ProviderVersion),
|
2020-03-06 15:09:10 +00:00
|
|
|
fmt.Sprintf("Schedulable|%t", vol.Schedulable),
|
|
|
|
fmt.Sprintf("Controllers Healthy|%d", vol.ControllersHealthy),
|
|
|
|
fmt.Sprintf("Controllers Expected|%d", vol.ControllersExpected),
|
|
|
|
fmt.Sprintf("Nodes Healthy|%d", vol.NodesHealthy),
|
|
|
|
fmt.Sprintf("Nodes Expected|%d", vol.NodesExpected),
|
|
|
|
|
|
|
|
fmt.Sprintf("Access Mode|%s", vol.AccessMode),
|
|
|
|
fmt.Sprintf("Attachment Mode|%s", vol.AttachmentMode),
|
2020-03-23 17:55:26 +00:00
|
|
|
fmt.Sprintf("Mount Options|%s", csiVolMountOption(vol.MountOptions, nil)),
|
2020-03-06 15:09:10 +00:00
|
|
|
fmt.Sprintf("Namespace|%s", vol.Namespace),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit early
|
|
|
|
if c.short {
|
|
|
|
return formatKV(output), nil
|
|
|
|
}
|
|
|
|
|
2022-03-01 15:15:46 +00:00
|
|
|
full := []string{formatKV(output)}
|
|
|
|
|
|
|
|
if len(vol.Topologies) > 0 {
|
|
|
|
topoBanner := c.Colorize().Color("\n[bold]Topologies[reset]")
|
|
|
|
topo := c.formatTopology(vol)
|
|
|
|
full = append(full, topoBanner)
|
|
|
|
full = append(full, topo)
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:09:10 +00:00
|
|
|
// Format the allocs
|
|
|
|
banner := c.Colorize().Color("\n[bold]Allocations[reset]")
|
|
|
|
allocs := formatAllocListStubs(vol.Allocations, c.verbose, c.length)
|
2022-03-01 15:15:46 +00:00
|
|
|
full = append(full, banner)
|
|
|
|
full = append(full, allocs)
|
|
|
|
|
2020-03-06 15:09:10 +00:00
|
|
|
return strings.Join(full, "\n"), nil
|
|
|
|
}
|
|
|
|
|
2022-03-01 15:15:46 +00:00
|
|
|
func (c *VolumeStatusCommand) formatTopology(vol *api.CSIVolume) string {
|
|
|
|
rows := []string{"Topology|Segments"}
|
|
|
|
for i, t := range vol.Topologies {
|
2022-04-22 13:25:04 +00:00
|
|
|
if t == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-01 15:15:46 +00:00
|
|
|
segmentPairs := make([]string, 0, len(t.Segments))
|
|
|
|
for k, v := range t.Segments {
|
|
|
|
segmentPairs = append(segmentPairs, fmt.Sprintf("%s=%s", k, v))
|
2020-03-06 15:09:10 +00:00
|
|
|
}
|
2022-03-01 15:15:46 +00:00
|
|
|
// note: this looks awkward because we don't have any other
|
|
|
|
// place where we list collections of arbitrary k/v's like
|
|
|
|
// this without just dumping JSON formatted outputs. It's likely
|
|
|
|
// the spec will expand to add extra fields, in which case we'll
|
|
|
|
// add them here and drop the first column
|
|
|
|
rows = append(rows, fmt.Sprintf("%02d|%v", i, strings.Join(segmentPairs, ", ")))
|
2020-03-06 15:09:10 +00:00
|
|
|
}
|
2022-03-01 15:15:46 +00:00
|
|
|
if len(rows) == 1 {
|
|
|
|
return ""
|
2020-03-06 15:09:10 +00:00
|
|
|
}
|
2022-03-01 15:15:46 +00:00
|
|
|
return formatList(rows)
|
2020-03-06 15:09:10 +00:00
|
|
|
}
|
2020-03-23 17:55:26 +00:00
|
|
|
|
|
|
|
func csiVolMountOption(volume, request *api.CSIMountOptions) string {
|
2022-08-02 14:33:08 +00:00
|
|
|
var req, opts *api.CSIMountOptions
|
2020-03-23 17:55:26 +00:00
|
|
|
|
|
|
|
if request != nil {
|
2022-08-02 14:33:08 +00:00
|
|
|
req = &api.CSIMountOptions{
|
2020-03-23 17:55:26 +00:00
|
|
|
FSType: request.FSType,
|
|
|
|
MountFlags: request.MountFlags,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if volume == nil {
|
|
|
|
opts = req
|
|
|
|
} else {
|
2022-08-02 14:33:08 +00:00
|
|
|
opts = &api.CSIMountOptions{
|
2020-03-23 17:55:26 +00:00
|
|
|
FSType: volume.FSType,
|
|
|
|
MountFlags: volume.MountFlags,
|
|
|
|
}
|
|
|
|
opts.Merge(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts == nil {
|
|
|
|
return "<none>"
|
|
|
|
}
|
|
|
|
|
|
|
|
var out string
|
|
|
|
if opts.FSType != "" {
|
|
|
|
out = fmt.Sprintf("fs_type: %s", opts.FSType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(opts.MountFlags) > 0 {
|
|
|
|
out = fmt.Sprintf("%s flags: %s", out, strings.Join(opts.MountFlags, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|