Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaler cleanups #188

Merged
merged 16 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 64 additions & 64 deletions .buildkite/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,73 @@ agents:
queue: elastic-runners

steps:
- name: ":go: Check go fmt"
key: go-fmt
command: .buildkite/steps/test-go-fmt.sh
plugins:
- docker#v5.8.0:
image: golang:1.19
workdir: /go/src/github.com/buildkite/buildkite-agent-scaler
- name: ":go: Check go fmt"
key: go-fmt
command: .buildkite/steps/test-go-fmt.sh
plugins:
- docker#v5.8.0:
image: golang:1.22
workdir: /go/src/github.com/buildkite/buildkite-agent-scaler

- name: ":golang: Run Tests"
key: test
command: .buildkite/steps/tests.sh
plugins:
- docker#v5.8.0:
image: golang:1.19
workdir: /go/src/github.com/buildkite/buildkite-agent-scaler
- name: ":golang: Run Tests"
key: test
command: .buildkite/steps/tests.sh
plugins:
- docker#v5.8.0:
image: golang:1.22
workdir: /go/src/github.com/buildkite/buildkite-agent-scaler

- name: ":lambda: Build Lambda"
key: build-lambda
depends_on:
- go-fmt
command: .buildkite/steps/build-lambda.sh
artifact_paths:
- handler.zip
- name: ":lambda: Build Lambda"
key: build-lambda
depends_on:
- go-fmt
command: .buildkite/steps/build-lambda.sh
artifact_paths:
- handler.zip

- label: ":s3: Pubish to S3 Branch Location"
key: s3-branch
depends_on:
- test
- build-lambda
command: .buildkite/steps/upload-to-s3.sh
plugins:
- aws-assume-role-with-web-identity:
role-arn: arn:aws:iam::032379705303:role/pipeline-buildkite-buildkite-agent-scaler
- label: ":s3: Pubish to S3 Branch Location"
key: s3-branch
depends_on:
- test
- build-lambda
command: .buildkite/steps/upload-to-s3.sh
plugins:
- aws-assume-role-with-web-identity:
role-arn: arn:aws:iam::032379705303:role/pipeline-buildkite-buildkite-agent-scaler

- if: build.tag =~ /^.+\$/ || build.env("RELEASE_DRY_RUN") == "true"
label: ":github: Draft GitHub Release"
key: github-release
depends_on:
- build-lambda
command: .buildkite/steps/github-release.sh
env:
BUILDKITE_AGENT_GIT_FETCH_FLAGS: -v --prune --tags
plugins:
- aws-assume-role-with-web-identity:
role-arn: arn:aws:iam::445615400570:role/pipeline-buildkite-buildkite-agent-scaler
- aws-ssm#v1.0.0:
parameters:
GITHUB_RELEASE_ACCESS_TOKEN: /pipelines/buildkite/buildkite-agent-scaler/GITHUB_RELEASE_ACCESS_TOKEN
- docker#v5.8.0:
image: alpine:3.18
propagate-environment: true
mount-buildkite-agent: true
environment:
- GITHUB_RELEASE_ACCESS_TOKEN
- BUILDKITE_AGENT_ACCESS_TOKEN
- if: build.tag =~ /^.+\$/ || build.env("RELEASE_DRY_RUN") == "true"
label: ":github: Draft GitHub Release"
key: github-release
depends_on:
- build-lambda
command: .buildkite/steps/github-release.sh
env:
BUILDKITE_AGENT_GIT_FETCH_FLAGS: -v --prune --tags
plugins:
- aws-assume-role-with-web-identity:
role-arn: arn:aws:iam::445615400570:role/pipeline-buildkite-buildkite-agent-scaler
- aws-ssm#v1.0.0:
parameters:
GITHUB_RELEASE_ACCESS_TOKEN: /pipelines/buildkite/buildkite-agent-scaler/GITHUB_RELEASE_ACCESS_TOKEN
- docker#v5.8.0:
image: alpine:3.18
propagate-environment: true
mount-buildkite-agent: true
environment:
- GITHUB_RELEASE_ACCESS_TOKEN
- BUILDKITE_AGENT_ACCESS_TOKEN

- if: build.tag =~ /^.+\$/ || build.env("RELEASE_DRY_RUN") == "true"
key: block-s3-release
block: ":rocket: Draft :github: Release Approved?"
- if: build.tag =~ /^.+\$/ || build.env("RELEASE_DRY_RUN") == "true"
key: block-s3-release
block: ":rocket: Draft :github: Release Approved?"

- if: build.tag =~ /^.+\$/ || build.env("RELEASE_DRY_RUN") == "true"
label: ":s3: Publish to S3 Release Location"
key: s3-release
depends_on:
- github-release
- block-s3-release
command: .buildkite/steps/upload-to-s3.sh release
plugins:
- aws-assume-role-with-web-identity:
role-arn: arn:aws:iam::032379705303:role/pipeline-buildkite-buildkite-agent-scaler
- if: build.tag =~ /^.+\$/ || build.env("RELEASE_DRY_RUN") == "true"
label: ":s3: Publish to S3 Release Location"
key: s3-release
depends_on:
- github-release
- block-s3-release
command: .buildkite/steps/upload-to-s3.sh release
plugins:
- aws-assume-role-with-web-identity:
role-arn: arn:aws:iam::032379705303:role/pipeline-buildkite-buildkite-agent-scaler
2 changes: 1 addition & 1 deletion .buildkite/steps/tests.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
set -euo pipefail

GO111MODULE=off go get gotest.tools/gotestsum
go install gotest.tools/gotestsum@v1.12.0

echo '+++ Running tests'
gotestsum --junitfile "junit-${OSTYPE}.xml" -- -count=1 -failfast ./...
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ bootstrap: lambda/main.go
--user $(USER) \
--volume $(PWD):/app \
--workdir /app \
--rm golang:1.19 \
--rm \
golang:1.22 \
go build -ldflags="$(LD_FLAGS)" -buildvcs="$(BUILDVSC_FLAG)" -tags lambda.norpc -o bootstrap ./lambda

lambda-sync: handler.zip
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/buildkite/buildkite-agent-scaler

go 1.19
go 1.22

require (
github.com/aws/aws-lambda-go v1.47.0
github.com/aws/aws-sdk-go v1.54.14
github.com/aws/aws-sdk-go v1.54.19
)

require github.com/jmespath/go-jmespath v0.4.0 // indirect
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
github.com/aws/aws-lambda-go v1.47.0 h1:0H8s0vumYx/YKs4sE7YM0ktwL2eWse+kfopsRI1sXVI=
github.com/aws/aws-lambda-go v1.47.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
github.com/aws/aws-sdk-go v1.54.14 h1:llJ60MzLzovyDE/rEDbUjS1cICh7krk1PwQwNlKRoeQ=
github.com/aws/aws-sdk-go v1.54.14/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/aws/aws-sdk-go v1.54.19 h1:tyWV+07jagrNiCcGRzRhdtVjQs7Vy41NwsuOcl0IbVI=
github.com/aws/aws-sdk-go v1.54.19/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand All @@ -12,7 +13,9 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
89 changes: 89 additions & 0 deletions lambda/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"log"
"os"
"strconv"
"time"
)

// RequireEnvString reads an environment variable, and if it is not set, calls
// [log.Fatalf].
func RequireEnvString(name string) string {
v := os.Getenv(name)
if v == "" {
log.Fatalf("%s is required", name)
}
return v
}

// RequireEnvInt reads an environment variable and parses it as a decimal int.
// If it is not set or does not parse, it calls [log.Fatalf].
func RequireEnvInt(name string) int {
v := RequireEnvString(name)
i, err := strconv.Atoi(v)
if err != nil {
log.Fatalf("%s must be an integer: %v", name, err)
}
return i
}

// EnvInt reads an environment variable, and if it is set, parses it with
// [strconv.Atoi].If it is not set, it returns def. If it does not parse, it calls
// [log.Fatalf].
func EnvInt(name string, def int) int {
v := os.Getenv(name)
if v == "" {
return def
}
i, err := strconv.Atoi(v)
if err != nil {
log.Fatalf("%s must be an integer: %v", name, err)
}
return i
}

// EnvDuration reads an environment variable, and if it is set, parses it as a
// [time.Duration]. If it is not set, it returns def. If parsing fails, it calls
// [log.Fatalf].
func EnvDuration(name string, def time.Duration) time.Duration {
v := os.Getenv(name)
if v == "" {
return def
}
d, err := time.ParseDuration(v)
if err != nil {
log.Fatalf("%s must be a duration: %v", name, err)
}
return d
}

// EnvFloat reads an environment variable, and if it is set, parses it using
// [strconv.ParseFloat]. If it is not set, it returns 0. If parsing fails, it
// calls [log.Fatalf].
func EnvFloat(name string) float64 {
v := os.Getenv(name)
if v == "" {
return 0
}
f, err := strconv.ParseFloat(v, 64)
if err != nil {
log.Fatalf("%s must be a number: %v", name, err)
}
return f
}

// EnvBool reads an environment variable, and if it is set, parses it using
// [strconv.ParseBool]. If it is not set, it returns false. If parsing fails, it
// calls [log.Fatalf].
func EnvBool(name string) bool {
v := os.Getenv(name)
if v == "" {
return false
}
b, err := strconv.ParseBool(v)
if err != nil {
log.Fatalf("%s must be a boolean: %v", name, err)
}
return b
}
Loading