Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #109 from jrasell/policy-read-handle-external-checks
Browse files Browse the repository at this point in the history
cmd: update policy read to handle nomad and external check entries
  • Loading branch information
jrasell authored Nov 14, 2019
2 parents 6aff35b + 7ef3453 commit 5f4485b
Show file tree
Hide file tree
Showing 19 changed files with 1,211 additions and 7 deletions.
77 changes: 70 additions & 7 deletions cmd/policy/read/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@ package read
import (
"fmt"
"os"
"sort"
"strings"

"github.com/jrasell/sherpa/cmd/helper"
"github.com/jrasell/sherpa/pkg/api"
clientCfg "github.com/jrasell/sherpa/pkg/config/client"
"github.com/liamg/tml"
"github.com/sean-/sysexits"
"github.com/spf13/cobra"
)

const (
outputHeader = "Group|Enabled|MinCount|MaxCount|Cooldown|ScaleInCount|ScaleOutCount"
nomadCheckHeader = "CPU In|CPU Out|Memory In|Memory Out"
externalCheckHeader = "Name|Enabled|Provider|Operator|Value|Action|Query"
)

func RegisterCommand(rootCmd *cobra.Command) error {
cmd := &cobra.Command{
Use: "read",
Short: "Details the scaling policy",
Short: "Details scaling policies associated to a job",
Run: func(cmd *cobra.Command, args []string) {
runRead(cmd, args)
},
Expand Down Expand Up @@ -61,11 +64,71 @@ func runRead(_ *cobra.Command, args []string) {
os.Exit(sysexits.OK)
}

out := []string{outputHeader}
// Sort the keys so the output is ordered alphabetically by group name.
keys := []string{}
for k := range *resp {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
formatGroupOutput(k, (*resp)[k])
}
}

// formatGroupOutput format all the details of a group scaling policy and outputs this to the user
// in a readable, easy to follow fashion.
func formatGroupOutput(group string, policy *api.JobGroupPolicy) {
header := []string{
fmt.Sprintf("Group|%s", group),
fmt.Sprintf("MinCount|%v", policy.MinCount),
fmt.Sprintf("MaxCount|%v", policy.MaxCount),
fmt.Sprintf("Cooldown|%v", policy.Cooldown),
fmt.Sprintf("ScaleInCount|%v", policy.ScaleInCount),
fmt.Sprintf("ScaleOutCount|%v", policy.ScaleOutCount),
}

var nomadChecks []string
var externalChecks []string

// Check we have Nomad checks configured.
if policy.ScaleInMemoryPercentageThreshold != nil || policy.ScaleOutMemoryPercentageThreshold != nil ||
policy.ScaleInCPUPercentageThreshold != nil || policy.ScaleOutCPUPercentageThreshold != nil {

// Create the Nomad check output.
nomadChecks = append(nomadChecks, nomadCheckHeader)
nomadChecks = append(nomadChecks, fmt.Sprintf("%v%%|%v%%|%v%%|%v%%",
*policy.ScaleInCPUPercentageThreshold, *policy.ScaleOutCPUPercentageThreshold,
*policy.ScaleInMemoryPercentageThreshold, *policy.ScaleOutMemoryPercentageThreshold))
}

// Check is there are external checks configured.
if policy.ExternalChecks != nil {

// Set the header.
externalChecks = append(externalChecks, externalCheckHeader)

// Iterate the checks and format the output.
for name, check := range policy.ExternalChecks {
externalChecks = append(externalChecks, fmt.Sprintf("%s|%v|%s|%s|%v|%s|%s",
name, check.Enabled, check.Provider, check.ComparisonOperator, check.ComparisonValue, check.Action, check.Query))
}
}

// Print our top header and include the core required parameters of a group scaling policy.
tml.Println("<bold>Scaling Policy:</bold>")
fmt.Println(helper.FormatKV(header))
fmt.Println("")

if len(nomadChecks) > 0 {
fmt.Println("Nomad Checks:")
fmt.Println(helper.FormatList(nomadChecks))
fmt.Println("")
}

for group, pol := range *resp {
out = append(out, fmt.Sprintf("%s|%v|%v|%v|%v|%v|%v",
group, pol.Enabled, pol.MinCount, pol.MaxCount, pol.Cooldown, pol.ScaleInCount, pol.ScaleOutCount))
if len(externalChecks) > 0 {
fmt.Println("External Checks:")
fmt.Println(helper.FormatList(externalChecks))
fmt.Println("")
}
fmt.Println(helper.FormatList(out))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/hashicorp/nomad/api v0.0.0-20190508234936-7ba2378a159e
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/liamg/tml v0.2.0
github.com/mattn/go-isatty v0.0.7
github.com/oklog/run v1.0.0
github.com/panjf2000/ants/v2 v2.1.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/liamg/tml v0.2.0 h1:Ab4Qs+gWfy5TJ0OPk7OSVwgdPZmRD978LIHgsdqg56A=
github.com/liamg/tml v0.2.0/go.mod h1:0h4EAV/zBOsqI91EWONedjRpO8O0itjGJVd+wG5eC+E=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down
2 changes: 2 additions & 0 deletions vendor/github.com/liamg/tml/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/liamg/tml/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5f4485b

Please sign in to comment.