Skip to content

Commit

Permalink
Changes from live testing (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinwyszynski authored Aug 19, 2023
1 parent 92560a0 commit f54705f
Show file tree
Hide file tree
Showing 27 changed files with 339 additions and 83 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Test

on: [push]

jobs:
deployment:
name: Test the code
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "1.20"

- name: Check formatting using gofmt
run: gofmt -s -l -d .

- name: Get dependencies
run: go mod download

- name: Test with coverage
run: go test ./...

release:
name: Test the release process
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "1.20"

- name: Run GoReleaser to create a snapshot release
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: --skip-publish --snapshot
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@

# Go workspace file
go.work
.env
dist
134 changes: 134 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# This file contains all available configuration options
# with their default values.

# options for analysis running
run:
# timeout for analysis, e.g. 30s, 5m, default is 1m
timeout: 5m

# include test files or not, default is true
tests: false

# default is true. Enables skipping of directories:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs-use-default: true


# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions
# default is "colored-line-number"
format: colored-line-number

# print lines of code with issue, default is true
print-issued-lines: true

# print linter name in the end of issue text, default is true
print-linter-name: true

# make issues output unique by line, default is true
uniq-by-line: true

# add a prefix to the output file references; default is no prefix
path-prefix: ""

# sorts results by: filepath, line and column
sort-results: false


linters-settings:
dupl:
threshold: 100
funlen:
lines: 100
statements: 50
gci:
sections:
- standard
- default
- prefix(github.com/spacelift-io/awsautoscalr)
goconst:
min-len: 2
min-occurrences: 2
staticcheck:
go: "1.18"
checks: [ "all", "-SA1019"]
gocritic:
enabled-tags:
- diagnostic
- performance
disabled-checks:
- ifElseChain
- wrapperFunc
- hugeParam
- rangeValCopy
- appendCombine
- commentedOutCode
- sloppyReassign
- filepathJoin
- evalOrder
- equalFold
- returnAfterHttpError
- preferStringWriter
- sprintfQuotedString
- preferFprint
goimports:
local-prefixes: github.com/spacelift-io/awsautoscalr
govet:
check-shadowing: false
nolintlint:
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
require-explanation: false # don't require an explanation for nolint directives
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
depguard:
list-type: blacklist
include-go-root: true
packages:
# The io/ioutil package has been deprecated.
# https://go.dev/doc/go1.16#ioutil
- io/ioutil


linters:
disable-all: true
enable:
- depguard
- exportloopref
- gci
- gocritic
- gofmt
- goimports
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- nolintlint
- staticcheck
- structcheck
- typecheck

severity:
# Default value is empty string.
# Set the default severity for issues. If severity rules are defined and the issues
# do not match or no severity is provided to the rule this will be the default
# severity applied. Severities should match the supported severity names of the
# selected out format.
# - Code climate: https://docs.codeclimate.com/docs/issues#issue-severity
# - Checkstyle: https://checkstyle.sourceforge.io/property_types.html#severity
# - GitHub: https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
default-severity: error

# The default value is false.
# If set to true severity-rules regular expressions become case sensitive.
case-sensitive: false

# Default value is empty list.
# When a list of severity rules are provided, severity information will be added to lint
# issues. Severity rules have the same filtering capability as exclude rules except you
# are allowed to specify one matcher per severity rule.
# Only affects out formats that support setting severity information.
rules:
- linters:
- dupl
severity: info
20 changes: 20 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
builds:
- main: ./cmd/lambda
env:
- CGO_ENABLED=0
- GOPRIVATE="github.com/spacelift-io"
mod_timestamp: '{{ .CommitTimestamp }}'
flags: [-trimpath, -v]
goos: [linux]
goarch: [amd64, arm64]
binary: '{{ .ProjectName }}_v{{ .Version }}'

archives:
- format: zip
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}'

changelog:
use: github-native

release:
draft: true
15 changes: 5 additions & 10 deletions source/cmd/internal/handle.go → cmd/internal/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"fmt"
"time"

"github.com/aws/aws-xray-sdk-go/xray"
"github.com/caarlos0/env"
"github.com/caarlos0/env/v9"
"golang.org/x/exp/slog"

"github.com/spacelift-io/awsautoscalr/internal"
Expand All @@ -19,14 +18,10 @@ func Handle(ctx context.Context, logger *slog.Logger) error {
}

logger = logger.With(
"asg_id", cfg.AutoscalingGroupName,
"asg_arn", cfg.AutoscalingGroupARN,
"worker_pool_id", cfg.SpaceliftWorkerPoolID,
)

if err := xray.Configure(xray.Config{ServiceVersion: "1.2.3"}); err != nil {
return fmt.Errorf("could not configure X-Ray: %w", err)
}

controller, err := internal.NewController(ctx, &cfg)
if err != nil {
return fmt.Errorf("could not create controller: %w", err)
Expand Down Expand Up @@ -79,7 +74,7 @@ func Handle(ctx context.Context, logger *slog.Logger) error {

// We don't want to kill too many instances at once, so let's
// return after the first successfully killed one.
logger.Info("instance successfully removed from the ASG and terminated", *instance.InstanceId)
logger.Info("instance successfully removed from the ASG and terminated")

return nil
}
Expand All @@ -94,7 +89,7 @@ func Handle(ctx context.Context, logger *slog.Logger) error {
}

if decision.ScalingDirection == internal.ScalingDirectionUp {
logger.Info("scaling up ASG by %d instances", decision.ScalingSize)
logger.With("instances", decision.ScalingSize).Info("scaling up the ASG")

if err := controller.ScaleUpASG(ctx, *asg.DesiredCapacity+int32(decision.ScalingSize)); err != nil {
return fmt.Errorf("could not scale up ASG: %w", err)
Expand All @@ -104,7 +99,7 @@ func Handle(ctx context.Context, logger *slog.Logger) error {
}

// If we got this far, we're scaling down.
logger.Info("scaling down ASG by %d instances", decision.ScalingSize)
logger.With("instances", decision.ScalingSize).Info("scaling down ASG")

idleWorkers := state.IdleWorkers()

Expand Down
6 changes: 6 additions & 0 deletions source/cmd/lambda/main.go → cmd/lambda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"context"
"fmt"
"os"

"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/lambdacontext"
"github.com/aws/aws-xray-sdk-go/xray"
"golang.org/x/exp/slog"

"github.com/spacelift-io/awsautoscalr/cmd/internal"
Expand All @@ -15,6 +17,10 @@ func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))

lambda.Start(func(ctx context.Context) error {
if err := xray.Configure(xray.Config{ServiceVersion: "1.2.3"}); err != nil {
return fmt.Errorf("could not configure X-Ray: %w", err)
}

if lc, ok := lambdacontext.FromContext(ctx); ok {
logger = logger.With("aws_request_id", lc.AwsRequestID)
}
Expand Down
30 changes: 30 additions & 0 deletions cmd/local/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"context"
"os"

"golang.org/x/exp/slog"

"github.com/aws/aws-xray-sdk-go/xray"
cmdinternal "github.com/spacelift-io/awsautoscalr/cmd/internal"
)

func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))

if err := xray.Configure(xray.Config{ServiceVersion: "1.2.3"}); err != nil {
logger.With("msg", err.Error()).Error("could not configure X-Ray")
os.Exit(1)
}

ctx, segment := xray.BeginSegment(context.Background(), "autoscaling")

if err := cmdinternal.Handle(ctx, logger); err != nil {
logger.With("msg", err.Error()).Error("could not handle request")
segment.Close(err)
os.Exit(1)
}

segment.Close(nil)
}
24 changes: 12 additions & 12 deletions source/go.mod → go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,51 @@ module github.com/spacelift-io/awsautoscalr
go 1.20

require (
github.com/aws/aws-sdk-go-v2 v1.18.1
github.com/aws/aws-lambda-go v1.41.0
github.com/aws/aws-sdk-go v1.44.288
github.com/aws/aws-sdk-go-v2/config v1.18.27
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.28.9
github.com/aws/aws-sdk-go-v2/service/ec2 v1.102.0
github.com/aws/aws-sdk-go-v2/service/ssm v1.37.4
github.com/aws/aws-xray-sdk-go v1.8.1
github.com/caarlos0/env v3.5.0+incompatible
github.com/caarlos0/env/v9 v9.0.0
github.com/franela/goblin v0.0.0-20211003143422-0a4f594942bf
github.com/onsi/gomega v1.27.8
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29
github.com/spacelift-io/spacectl v0.24.0
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/aws/aws-lambda-go v1.41.0 // indirect
github.com/aws/aws-sdk-go v1.44.288 // indirect
github.com/aws/aws-sdk-go-v2 v1.20.3 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.26 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.4 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.34 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.28 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.40 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.34 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.35 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.28 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.12.12 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.12 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.19.2 // indirect
github.com/aws/smithy-go v1.13.5 // indirect
github.com/aws/smithy-go v1.14.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/franela/goblin v0.0.0-20211003143422-0a4f594942bf // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.16.6 // indirect
github.com/onsi/gomega v1.27.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.48.0 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230525234025-438c736192d0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.56.1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
Expand Down
Loading

0 comments on commit f54705f

Please sign in to comment.