-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9025 from hashicorp/f-gh-8649
cli: add policy list and info to new scaling cmd.
- Loading branch information
Showing
8 changed files
with
634 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package command | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/mitchellh/cli" | ||
) | ||
|
||
// Ensure ScalingCommand satisfies the cli.Command interface. | ||
var _ cli.Command = &ScalingCommand{} | ||
|
||
// ScalingCommand implements cli.Command. | ||
type ScalingCommand struct { | ||
Meta | ||
} | ||
|
||
// Help satisfies the cli.Command Help function. | ||
func (s *ScalingCommand) Help() string { | ||
helpText := ` | ||
Usage: nomad scaling <subcommand> [options] | ||
This command groups subcommands for interacting with the scaling API. | ||
Please see the individual subcommand help for detailed usage information. | ||
` | ||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
// Synopsis satisfies the cli.Command Synopsis function. | ||
func (s *ScalingCommand) Synopsis() string { | ||
return "Interact with the Nomad scaling endpoint" | ||
} | ||
|
||
// Name returns the name of this command. | ||
func (s *ScalingCommand) Name() string { return "scaling" } | ||
|
||
// Run satisfies the cli.Command Run function. | ||
func (s *ScalingCommand) Run(_ []string) int { return cli.RunResultHelp } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/mitchellh/cli" | ||
) | ||
|
||
// Ensure ScalingPolicyCommand satisfies the cli.Command interface. | ||
var _ cli.Command = &ScalingPolicyCommand{} | ||
|
||
// ScalingPolicyCommand implements cli.Command. | ||
type ScalingPolicyCommand struct { | ||
Meta | ||
} | ||
|
||
// Help satisfies the cli.Command Help function. | ||
func (s *ScalingPolicyCommand) Help() string { | ||
helpText := ` | ||
Usage: nomad scaling policy <subcommand> [options] | ||
This command groups subcommands for interacting with scaling policies. Scaling | ||
policies can be used by an external autoscaler to perform scaling actions on | ||
Nomad targets. | ||
List policies: | ||
$ nomad scaling policy list | ||
Detail an individual scaling policy: | ||
$ nomad scaling policy info <policy_id> | ||
Please see the individual subcommand help for detailed usage information. | ||
` | ||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
// Synopsis satisfies the cli.Command Synopsis function. | ||
func (s *ScalingPolicyCommand) Synopsis() string { | ||
return "Interact with Nomad scaling policies" | ||
} | ||
|
||
// Name returns the name of this command. | ||
func (s *ScalingPolicyCommand) Name() string { return "scaling policy" } | ||
|
||
// Run satisfies the cli.Command Run function. | ||
func (s *ScalingPolicyCommand) Run(_ []string) int { return cli.RunResultHelp } | ||
|
||
// formatScalingPolicyTarget is a command helper that correctly formats a | ||
// scaling policy target map into a command string output. | ||
func formatScalingPolicyTarget(t map[string]string) string { | ||
var ns, j, g string | ||
var other []string | ||
|
||
for k, v := range t { | ||
|
||
s := fmt.Sprintf("%s:%s", k, v) | ||
|
||
switch strings.ToLower(k) { | ||
case "namespace": | ||
ns = s | ||
case "job": | ||
j = s | ||
case "group": | ||
g = s | ||
default: | ||
other = append(other, s) | ||
} | ||
} | ||
|
||
out := []string{ns, j, g} | ||
|
||
if len(other) > 0 { | ||
out = append(out, other...) | ||
} | ||
return strings.Trim(strings.Join(out, ","), ",") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/mitchellh/cli" | ||
"github.com/posener/complete" | ||
) | ||
|
||
// Ensure ScalingPolicyInfoCommand satisfies the cli.Command interface. | ||
var _ cli.Command = &ScalingPolicyInfoCommand{} | ||
|
||
// ScalingPolicyListCommand implements cli.Command. | ||
type ScalingPolicyInfoCommand struct { | ||
Meta | ||
} | ||
|
||
// Help satisfies the cli.Command Help function. | ||
func (s *ScalingPolicyInfoCommand) Help() string { | ||
helpText := ` | ||
Usage: nomad scaling policy info [options] <policy_id> | ||
Info is used to read the specified scaling policy. | ||
General Options: | ||
` + generalOptionsUsage() + ` | ||
Policy Info Options: | ||
-json | ||
Output the scaling policy in its JSON format. | ||
-t | ||
Format and display the scaling policy using a Go template. | ||
` | ||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
// Synopsis satisfies the cli.Command Synopsis function. | ||
func (s *ScalingPolicyInfoCommand) Synopsis() string { | ||
return "Display an individual Nomad scaling policy" | ||
} | ||
|
||
func (s *ScalingPolicyInfoCommand) AutocompleteFlags() complete.Flags { | ||
return mergeAutocompleteFlags(s.Meta.AutocompleteFlags(FlagSetClient), | ||
complete.Flags{ | ||
"-json": complete.PredictNothing, | ||
"-t": complete.PredictAnything, | ||
}) | ||
} | ||
|
||
// Name returns the name of this command. | ||
func (s *ScalingPolicyInfoCommand) Name() string { return "scaling policy info" } | ||
|
||
// Run satisfies the cli.Command Run function. | ||
func (s *ScalingPolicyInfoCommand) Run(args []string) int { | ||
var json bool | ||
var tmpl string | ||
|
||
flags := s.Meta.FlagSet(s.Name(), FlagSetClient) | ||
flags.Usage = func() { s.Ui.Output(s.Help()) } | ||
flags.BoolVar(&json, "json", false, "") | ||
flags.StringVar(&tmpl, "t", "", "") | ||
if err := flags.Parse(args); err != nil { | ||
return 1 | ||
} | ||
|
||
if args = flags.Args(); len(args) != 1 { | ||
s.Ui.Error("This command takes one argument: <policy_id>") | ||
s.Ui.Error(commandErrorText(s)) | ||
return 1 | ||
} | ||
|
||
// Get the policy ID. | ||
policyID := args[0] | ||
|
||
// Get the HTTP client. | ||
client, err := s.Meta.Client() | ||
if err != nil { | ||
s.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) | ||
return 1 | ||
} | ||
|
||
policy, _, err := client.Scaling().GetPolicy(policyID, nil) | ||
if err != nil { | ||
s.Ui.Error(fmt.Sprintf("Error listing scaling policies: %s", err)) | ||
return 1 | ||
} | ||
|
||
// If the user has specified to output the policy as JSON or using a | ||
// template then perform this action for the entire object and exit the | ||
// command. | ||
if json || len(tmpl) > 0 { | ||
out, err := Format(json, tmpl, policy) | ||
if err != nil { | ||
s.Ui.Error(err.Error()) | ||
return 1 | ||
} | ||
s.Ui.Output(out) | ||
return 0 | ||
} | ||
|
||
// Format the policy document which is a freeform map[string]interface{} | ||
// and therefore can only be made pretty to a certain extent. Do this | ||
// before the rest of the formatting so any errors are clearly passed back | ||
// to the CLI. | ||
out, err := Format(true, "", policy.Policy) | ||
if err != nil { | ||
s.Ui.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
info := []string{ | ||
fmt.Sprintf("ID|%s", policy.ID), | ||
fmt.Sprintf("Enabled|%v", *policy.Enabled), | ||
fmt.Sprintf("Target|%s", formatScalingPolicyTarget(policy.Target)), | ||
fmt.Sprintf("Min|%v", *policy.Min), | ||
fmt.Sprintf("Max|%v", *policy.Max), | ||
} | ||
s.Ui.Output(formatKV(info)) | ||
s.Ui.Output("\nPolicy:") | ||
s.Ui.Output(out) | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/api" | ||
"github.com/hashicorp/nomad/helper" | ||
"github.com/hashicorp/nomad/testutil" | ||
"github.com/mitchellh/cli" | ||
) | ||
|
||
func TestScalingPolicyInfoCommand_Run(t *testing.T) { | ||
t.Parallel() | ||
srv, client, url := testServer(t, true, nil) | ||
defer srv.Shutdown() | ||
testutil.WaitForResult(func() (bool, error) { | ||
nodes, _, err := client.Nodes().List(nil) | ||
if err != nil { | ||
return false, err | ||
} | ||
if len(nodes) == 0 { | ||
return false, fmt.Errorf("missing node") | ||
} | ||
if _, ok := nodes[0].Drivers["mock_driver"]; !ok { | ||
return false, fmt.Errorf("mock_driver not ready") | ||
} | ||
return true, nil | ||
}, func(err error) { | ||
t.Fatalf("err: %s", err) | ||
}) | ||
|
||
ui := cli.NewMockUi() | ||
cmd := &ScalingPolicyInfoCommand{Meta: Meta{Ui: ui}} | ||
|
||
// Calling without the policyID should result in an error. | ||
if code := cmd.Run([]string{"-address=" + url}); code != 1 { | ||
t.Fatalf("expected cmd run exit code 1, got: %d", code) | ||
} | ||
if out := ui.ErrorWriter.String(); !strings.Contains(out, "This command takes one argument: <policy_id>") { | ||
t.Fatalf("expected argument error within output: %v", out) | ||
} | ||
|
||
// Perform an initial info, which should return zero results. | ||
if code := cmd.Run([]string{"-address=" + url, "scaling_policy_info"}); code != 1 { | ||
t.Fatalf("expected cmd run exit code 1, got: %d", code) | ||
} | ||
if out := ui.ErrorWriter.String(); !strings.Contains(out, "404 (policy not found)") { | ||
t.Fatalf("expected 404 not found within output: %v", out) | ||
} | ||
|
||
// Generate a test job. | ||
job := testJob("scaling_policy_info") | ||
|
||
// Generate an example scaling policy. | ||
job.TaskGroups[0].Scaling = &api.ScalingPolicy{ | ||
Enabled: helper.BoolToPtr(true), | ||
Min: helper.Int64ToPtr(1), | ||
Max: helper.Int64ToPtr(1), | ||
} | ||
|
||
// Register the job. | ||
resp, _, err := client.Jobs().Register(job, nil) | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 { | ||
t.Fatalf("expected waitForSuccess exit code 0, got: %d", code) | ||
} | ||
|
||
// Grab the generated policyID. | ||
policies, _, err := client.Scaling().ListPolicies(nil) | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
numPolicies := len(policies) | ||
if numPolicies == 0 || numPolicies > 1 { | ||
t.Fatalf("expected 1 policy return, got %v", numPolicies) | ||
} | ||
|
||
if code := cmd.Run([]string{"-address=" + url, policies[0].ID}); code != 0 { | ||
t.Fatalf("expected cmd run exit code 0, got: %d", code) | ||
} | ||
if out := ui.OutputWriter.String(); !strings.Contains(out, "Policy:") { | ||
t.Fatalf("expected policy ID within output: %v", out) | ||
} | ||
} |
Oops, something went wrong.
ca8b4f6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: