github.com/rohankumardubey/nomad@v0.11.8/command/plugin_status_csi.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/nomad/api"
     9  )
    10  
    11  func (c *PluginStatusCommand) csiBanner() {
    12  	if !(c.json || len(c.template) > 0) {
    13  		c.Ui.Output(c.Colorize().Color("[bold]Container Storage Interface[reset]"))
    14  	}
    15  }
    16  
    17  func (c *PluginStatusCommand) csiStatus(client *api.Client, id string) int {
    18  	if id == "" {
    19  		c.csiBanner()
    20  		plugs, _, err := client.CSIPlugins().List(nil)
    21  		if err != nil {
    22  			c.Ui.Error(fmt.Sprintf("Error querying CSI plugins: %s", err))
    23  			return 1
    24  		}
    25  
    26  		if len(plugs) == 0 {
    27  			// No output if we have no plugins
    28  			c.Ui.Error("No CSI plugins")
    29  		} else {
    30  			str, err := c.csiFormatPlugins(plugs)
    31  			if err != nil {
    32  				c.Ui.Error(fmt.Sprintf("Error formatting: %s", err))
    33  				return 1
    34  			}
    35  			c.Ui.Output(str)
    36  		}
    37  		return 0
    38  	}
    39  
    40  	// Lookup matched a single plugin
    41  	plug, _, err := client.CSIPlugins().Info(id, nil)
    42  	if err != nil {
    43  		c.Ui.Error(fmt.Sprintf("Error querying plugin: %s", err))
    44  		return 1
    45  	}
    46  
    47  	str, err := c.csiFormatPlugin(plug)
    48  	if err != nil {
    49  		c.Ui.Error(fmt.Sprintf("Error formatting plugin: %s", err))
    50  		return 1
    51  	}
    52  
    53  	c.Ui.Output(str)
    54  	return 0
    55  }
    56  
    57  func (c *PluginStatusCommand) csiFormatPlugins(plugs []*api.CSIPluginListStub) (string, error) {
    58  	// Sort the output by quota name
    59  	sort.Slice(plugs, func(i, j int) bool { return plugs[i].ID < plugs[j].ID })
    60  
    61  	if c.json || len(c.template) > 0 {
    62  		out, err := Format(c.json, c.template, plugs)
    63  		if err != nil {
    64  			return "", fmt.Errorf("format error: %v", err)
    65  		}
    66  		return out, nil
    67  	}
    68  
    69  	rows := make([]string, len(plugs)+1)
    70  	rows[0] = "ID|Provider|Controllers Healthy/Expected|Nodes Healthy/Expected"
    71  	for i, p := range plugs {
    72  		rows[i+1] = fmt.Sprintf("%s|%s|%d/%d|%d/%d",
    73  			limit(p.ID, c.length),
    74  			p.Provider,
    75  			p.ControllersHealthy,
    76  			p.ControllersExpected,
    77  			p.NodesHealthy,
    78  			p.NodesExpected,
    79  		)
    80  	}
    81  	return formatList(rows), nil
    82  }
    83  
    84  func (c *PluginStatusCommand) csiFormatPlugin(plug *api.CSIPlugin) (string, error) {
    85  	if c.json || len(c.template) > 0 {
    86  		out, err := Format(c.json, c.template, plug)
    87  		if err != nil {
    88  			return "", fmt.Errorf("format error: %v", err)
    89  		}
    90  		return out, nil
    91  	}
    92  
    93  	output := []string{
    94  		fmt.Sprintf("ID|%s", plug.ID),
    95  		fmt.Sprintf("Provider|%s", plug.Provider),
    96  		fmt.Sprintf("Version|%s", plug.Version),
    97  		fmt.Sprintf("Controllers Healthy|%d", plug.ControllersHealthy),
    98  		fmt.Sprintf("Controllers Expected|%d", len(plug.Controllers)),
    99  		fmt.Sprintf("Nodes Healthy|%d", plug.NodesHealthy),
   100  		fmt.Sprintf("Nodes Expected|%d", len(plug.Nodes)),
   101  	}
   102  
   103  	// Exit early
   104  	if c.short {
   105  		return formatKV(output), nil
   106  	}
   107  
   108  	// Format the allocs
   109  	banner := c.Colorize().Color("\n[bold]Allocations[reset]")
   110  	allocs := formatAllocListStubs(plug.Allocations, c.verbose, c.length)
   111  	full := []string{formatKV(output), banner, allocs}
   112  	return strings.Join(full, "\n"), nil
   113  }