diff --git a/.buildkite/pipeline.yml b/.buildkite/pipeline.yml index b7ec1cb..06a40e4 100644 --- a/.buildkite/pipeline.yml +++ b/.buildkite/pipeline.yml @@ -1,4 +1,12 @@ steps: + - name: ":go: go fmt" + key: test-go-fmt + command: ".buildkite/steps/test-go-fmt.sh" + plugins: + - docker#v3.1.0: + image: "golang:1.19" + workdir: /go/src/github.com/buildkite/buildkite-agent-scaler + - name: ":golang:" command: ".buildkite/steps/tests.sh" plugins: diff --git a/.buildkite/steps/test-go-fmt.sh b/.buildkite/steps/test-go-fmt.sh new file mode 100755 index 0000000..c624b76 --- /dev/null +++ b/.buildkite/steps/test-go-fmt.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +if [[ $(gofmt -l ./ | head -c 1 | wc -c) != 0 ]]; then + echo "The following files haven't been formatted with \`go fmt\`:" + gofmt -l ./ + echo + echo "Fix this by running \`go fmt ./...\` locally, and committing the result." + exit 1 +fi + +echo "Everything is formatted! 🎉" diff --git a/buildkite/buildkite.go b/buildkite/buildkite.go index 6390ca1..e84f9f7 100644 --- a/buildkite/buildkite.go +++ b/buildkite/buildkite.go @@ -14,7 +14,7 @@ import ( const ( DefaultMetricsEndpoint = "https://agent.buildkite.com/v3" - PollDurationHeader = `Buildkite-Agent-Metrics-Poll-Duration` + PollDurationHeader = "Buildkite-Agent-Metrics-Poll-Duration" ) type Client struct { diff --git a/lambda/main.go b/lambda/main.go index ad86ce7..33fdf9b 100644 --- a/lambda/main.go +++ b/lambda/main.go @@ -32,7 +32,7 @@ type LastScaleASGResult struct { } func main() { - if os.Getenv(`DEBUG`) != "" { + if os.Getenv("DEBUG") != "" { _, err := Handler(context.Background(), json.RawMessage([]byte{})) if err != nil { log.Fatal(err) @@ -69,13 +69,13 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) { disableScaleOut, disableScaleIn bool ) - if v := os.Getenv(`LAMBDA_INTERVAL`); v != "" { + if v := os.Getenv("LAMBDA_INTERVAL"); v != "" { if interval, err = time.ParseDuration(v); err != nil { return "", err } } - if v := os.Getenv(`LAMBDA_TIMEOUT`); v != "" { + if v := os.Getenv("LAMBDA_TIMEOUT"); v != "" { if timeoutDuration, err := time.ParseDuration(v); err != nil { return "", err } else { @@ -83,7 +83,7 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) { } } - if v := os.Getenv(`ASG_ACTIVITY_TIMEOUT`); v != "" { + if v := os.Getenv("ASG_ACTIVITY_TIMEOUT"); v != "" { if timeoutDuration, err := time.ParseDuration(v); err != nil { return "", err } else { @@ -92,45 +92,45 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) { } } - if v := os.Getenv(`SCALE_IN_COOLDOWN_PERIOD`); v != "" { + if v := os.Getenv("SCALE_IN_COOLDOWN_PERIOD"); v != "" { if scaleInCooldownPeriod, err = time.ParseDuration(v); err != nil { return "", err } } - if v := os.Getenv(`SCALE_IN_FACTOR`); v != "" { + if v := os.Getenv("SCALE_IN_FACTOR"); v != "" { if scaleInFactor, err = strconv.ParseFloat(v, 64); err != nil { return "", err } scaleInFactor = math.Abs(scaleInFactor) } - if v := os.Getenv(`SCALE_ONLY_AFTER_ALL_EVENT`); v != "" { + if v := os.Getenv("SCALE_ONLY_AFTER_ALL_EVENT"); v != "" { if v == "true" || v == "1" { scaleOnlyAfterAllEvent = true } } - if v := os.Getenv(`SCALE_OUT_COOLDOWN_PERIOD`); v != "" { + if v := os.Getenv("SCALE_OUT_COOLDOWN_PERIOD"); v != "" { if scaleOutCooldownPeriod, err = time.ParseDuration(v); err != nil { return "", err } } - if v := os.Getenv(`SCALE_OUT_FACTOR`); v != "" { + if v := os.Getenv("SCALE_OUT_FACTOR"); v != "" { if scaleOutFactor, err = strconv.ParseFloat(v, 64); err != nil { return "", err } scaleOutFactor = math.Abs(scaleOutFactor) } - if v := os.Getenv(`INCLUDE_WAITING`); v != "" { + if v := os.Getenv("INCLUDE_WAITING"); v != "" { if v == "true" || v == "1" { includeWaiting = true } } - if v := os.Getenv(`INSTANCE_BUFFER`); v != "" { + if v := os.Getenv("INSTANCE_BUFFER"); v != "" { if instanceBuffer, err = strconv.Atoi(v); err != nil { return "", err } @@ -144,17 +144,17 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) { } } - if m := os.Getenv(`CLOUDWATCH_METRICS`); m == `true` || m == `1` { + if m := os.Getenv("CLOUDWATCH_METRICS"); m == "true" || m == "1" { log.Printf("Publishing cloudwatch metrics") publishCloudWatchMetrics = true } - if m := os.Getenv(`DISABLE_SCALE_IN`); m == `true` || m == `1` { + if m := os.Getenv("DISABLE_SCALE_IN"); m == "true" || m == "1" { log.Printf("Disabling scale-in 🙅🏼‍") disableScaleIn = true } - if m := os.Getenv(`DISABLE_SCALE_OUT`); m == `true` || m == `1` { + if m := os.Getenv("DISABLE_SCALE_OUT"); m == "true" || m == "1" { log.Printf("Disabling scale-out 🙅🏼‍♂️") disableScaleOut = true } @@ -181,7 +181,7 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) { // set last scale in and out from asg's activities asg := &scaler.ASGDriver{ - Name: mustGetEnv(`ASG_NAME`), + Name: mustGetEnv("ASG_NAME"), Sess: sess, MaxDescribeScalingActivitiesPages: maxDescribeScalingActivitiesPages, } @@ -238,7 +238,7 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) { case <-timeout: return "", nil default: - token := os.Getenv(`BUILDKITE_AGENT_TOKEN`) + token := os.Getenv("BUILDKITE_AGENT_TOKEN") ssmTokenKey := os.Getenv("BUILDKITE_AGENT_TOKEN_SSM_KEY") if ssmTokenKey != "" { @@ -250,16 +250,14 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) { } if token == "" { - return "", errors.New( - "Must provide either BUILDKITE_AGENT_TOKEN or BUILDKITE_AGENT_TOKEN_SSM_KEY", - ) + return "", errors.New("Must provide either BUILDKITE_AGENT_TOKEN or BUILDKITE_AGENT_TOKEN_SSM_KEY") } client := buildkite.NewClient(token) params := scaler.Params{ - BuildkiteQueue: mustGetEnv(`BUILDKITE_QUEUE`), - AutoScalingGroupName: mustGetEnv(`ASG_NAME`), - AgentsPerInstance: mustGetEnvInt(`AGENTS_PER_INSTANCE`), + BuildkiteQueue: mustGetEnv("BUILDKITE_QUEUE"), + AutoScalingGroupName: mustGetEnv("ASG_NAME"), + AgentsPerInstance: mustGetEnvInt("AGENTS_PER_INSTANCE"), IncludeWaiting: includeWaiting, ScaleInParams: scaler.ScaleParams{ CooldownPeriod: scaleInCooldownPeriod, diff --git a/scaler/scaler.go b/scaler/scaler.go index def2e3d..fb63171 100644 --- a/scaler/scaler.go +++ b/scaler/scaler.go @@ -96,9 +96,9 @@ func (s *Scaler) Run() (time.Duration, error) { if s.metrics != nil { err = s.metrics.Publish(metrics.OrgSlug, metrics.Queue, map[string]int64{ - `ScheduledJobsCount`: metrics.ScheduledJobs, - `RunningJobsCount`: metrics.RunningJobs, - `WaitingJobsCount`: metrics.WaitingJobs, + "ScheduledJobsCount": metrics.ScheduledJobs, + "RunningJobsCount": metrics.RunningJobs, + "WaitingJobsCount": metrics.WaitingJobs, }) if err != nil { return metrics.PollDuration, err