2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2018-03-21 21:04:54 +00:00
|
|
|
"strings"
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
"github.com/hashicorp/nomad/api/contexts"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NamespaceCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *NamespaceCommand) Help() string {
|
2018-03-21 21:04:54 +00:00
|
|
|
helpText := `
|
|
|
|
Usage: nomad namespace <subcommand> [options] [args]
|
|
|
|
|
|
|
|
This command groups subcommands for interacting with namespaces. Namespaces
|
|
|
|
allow jobs and their associated objects to be segmented from each other and
|
|
|
|
other users of the cluster. For a full guide on namespaces see:
|
2021-02-23 16:31:19 +00:00
|
|
|
https://learn.hashicorp.com/tutorials/nomad/namespaces
|
2018-03-21 21:04:54 +00:00
|
|
|
|
|
|
|
Create or update a namespace:
|
|
|
|
|
2019-09-09 17:04:41 +00:00
|
|
|
$ nomad namespace apply -description "My new namespace" <name>
|
2018-03-21 21:04:54 +00:00
|
|
|
|
|
|
|
List namespaces:
|
|
|
|
|
|
|
|
$ nomad namespace list
|
|
|
|
|
|
|
|
View the status of a namespace:
|
|
|
|
|
|
|
|
$ nomad namespace status <name>
|
|
|
|
|
|
|
|
Please see the individual subcommand help for detailed usage information.
|
|
|
|
`
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
2017-09-07 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *NamespaceCommand) Synopsis() string {
|
|
|
|
return "Interact with namespaces"
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (f *NamespaceCommand) Name() string { return "namespace" }
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
func (f *NamespaceCommand) Run(args []string) int {
|
|
|
|
return cli.RunResultHelp
|
|
|
|
}
|
|
|
|
|
|
|
|
// NamespacePredictor returns a namespace predictor that can optionally filter
|
|
|
|
// specific namespaces
|
|
|
|
func NamespacePredictor(factory ApiClientFactory, filter map[string]struct{}) complete.Predictor {
|
|
|
|
return complete.PredictFunc(func(a complete.Args) []string {
|
|
|
|
client, err := factory()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Namespaces, nil)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter the returned namespaces. We assign the unfiltered slice to the
|
|
|
|
// filtered slice but with no elements. This causes the slices to share
|
|
|
|
// the underlying array and makes the filtering allocation free.
|
|
|
|
unfiltered := resp.Matches[contexts.Namespaces]
|
|
|
|
filtered := unfiltered[:0]
|
|
|
|
for _, ns := range unfiltered {
|
|
|
|
if _, ok := filter[ns]; !ok {
|
|
|
|
filtered = append(filtered, ns)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
})
|
|
|
|
}
|