2017-10-13 21:36:02 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-02-27 08:09:10 +00:00
|
|
|
"sort"
|
2017-10-13 21:36:02 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NamespaceStatusCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NamespaceStatusCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad namespace status [options] <namespace>
|
|
|
|
|
|
|
|
Status is used to view the status of a particular namespace.
|
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
If ACLs are enabled, this command requires a management ACL token or a token
|
|
|
|
that has a capability associated with the namespace.
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace)
|
2017-10-13 21:36:02 +00:00
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NamespaceStatusCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return c.Meta.AutocompleteFlags(FlagSetClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NamespaceStatusCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return NamespacePredictor(c.Meta.Client, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NamespaceStatusCommand) Synopsis() string {
|
|
|
|
return "Display a namespace's status"
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *NamespaceStatusCommand) Name() string { return "namespace status" }
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
func (c *NamespaceStatusCommand) Run(args []string) int {
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
2017-10-13 21:36:02 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got one arguments
|
|
|
|
args = flags.Args()
|
|
|
|
if l := len(args); l != 1 {
|
2018-04-18 17:55:51 +00:00
|
|
|
c.Ui.Error("This command takes one argument: <namespace>")
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error(commandErrorText(c))
|
2017-10-13 21:36:02 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
name := args[0]
|
|
|
|
|
|
|
|
// Get the HTTP client
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a prefix lookup
|
|
|
|
ns, possible, err := getNamespace(client.Namespaces(), name)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error retrieving namespaces: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(possible) != 0 {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Prefix matched multiple namespaces\n\n%s", formatNamespaces(possible)))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(formatNamespaceBasics(ns))
|
|
|
|
|
2022-02-27 08:09:10 +00:00
|
|
|
if len(ns.Meta) > 0 {
|
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Metadata[reset]"))
|
2022-03-04 20:59:17 +00:00
|
|
|
var meta []string
|
2022-02-27 08:09:10 +00:00
|
|
|
for k := range ns.Meta {
|
2022-03-04 20:59:17 +00:00
|
|
|
meta = append(meta, fmt.Sprintf("%s|%s", k, ns.Meta[k]))
|
2022-02-27 08:09:10 +00:00
|
|
|
}
|
2022-03-04 20:59:17 +00:00
|
|
|
sort.Strings(meta)
|
|
|
|
c.Ui.Output(formatKV(meta))
|
2022-02-27 08:09:10 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
if ns.Quota != "" {
|
|
|
|
quotas := client.Quotas()
|
|
|
|
spec, _, err := quotas.Info(ns.Quota, nil)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error retrieving quota spec: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the quota usages
|
|
|
|
usages, failures := quotaUsages(spec, quotas)
|
|
|
|
|
|
|
|
// Format the limits
|
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Quota Limits[reset]"))
|
|
|
|
c.Ui.Output(formatQuotaLimits(spec, usages))
|
|
|
|
|
|
|
|
// Display any failures
|
|
|
|
if len(failures) != 0 {
|
|
|
|
c.Ui.Error(c.Colorize().Color("\n[bold][red]Lookup Failures[reset]"))
|
|
|
|
for region, failure := range failures {
|
|
|
|
c.Ui.Error(fmt.Sprintf(" * Failed to retrieve quota usage for region %q: %v", region, failure))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatNamespaceBasics formats the basic information of the namespace
|
|
|
|
func formatNamespaceBasics(ns *api.Namespace) string {
|
2022-02-24 14:27:32 +00:00
|
|
|
enabled_drivers := "*"
|
|
|
|
disabled_drivers := ""
|
|
|
|
if ns.Capabilities != nil {
|
|
|
|
if len(ns.Capabilities.EnabledTaskDrivers) != 0 {
|
|
|
|
enabled_drivers = strings.Join(ns.Capabilities.EnabledTaskDrivers, ",")
|
|
|
|
}
|
|
|
|
if len(ns.Capabilities.DisabledTaskDrivers) != 0 {
|
|
|
|
disabled_drivers = strings.Join(ns.Capabilities.DisabledTaskDrivers, ",")
|
|
|
|
}
|
|
|
|
}
|
2017-10-13 21:36:02 +00:00
|
|
|
basic := []string{
|
|
|
|
fmt.Sprintf("Name|%s", ns.Name),
|
|
|
|
fmt.Sprintf("Description|%s", ns.Description),
|
|
|
|
fmt.Sprintf("Quota|%s", ns.Quota),
|
2022-02-24 14:27:32 +00:00
|
|
|
fmt.Sprintf("EnabledDrivers|%s", enabled_drivers),
|
|
|
|
fmt.Sprintf("DisabledDrivers|%s", disabled_drivers),
|
2017-10-13 21:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return formatKV(basic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNamespace(client *api.Namespaces, ns string) (match *api.Namespace, possible []*api.Namespace, err error) {
|
|
|
|
// Do a prefix lookup
|
|
|
|
namespaces, _, err := client.PrefixList(ns, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l := len(namespaces)
|
|
|
|
switch {
|
|
|
|
case l == 0:
|
|
|
|
return nil, nil, fmt.Errorf("Namespace %q matched no namespaces", ns)
|
|
|
|
case l == 1:
|
|
|
|
return namespaces[0], nil, nil
|
|
|
|
default:
|
2017-11-30 00:40:08 +00:00
|
|
|
// search for an exact match in the returned namespaces
|
|
|
|
for _, namespace := range namespaces {
|
2017-11-30 00:43:19 +00:00
|
|
|
if namespace.Name == ns {
|
|
|
|
return namespace, nil, nil
|
|
|
|
}
|
2017-11-30 00:40:08 +00:00
|
|
|
}
|
|
|
|
// if not found, return the fuzzy matches.
|
2017-10-13 21:36:02 +00:00
|
|
|
return nil, namespaces, nil
|
|
|
|
}
|
|
|
|
}
|