Skip to content

Commit

Permalink
feat: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Apr 26, 2024
0 parents commit c2fed66
Show file tree
Hide file tree
Showing 24 changed files with 672 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .changelog-generator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# yaml-language-server: $schema=https://gabe565.github.io/changelog-generator/config.schema.json
filters:
exclude:
- "^docs"
- "^test"
groups:
- title: Features
order: 0
regexp: "^(feat)"
- title: Fixes
order: 1
regexp: "^(fix|perf)"
- title: Others
order: 999
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.changelog-generator.yaml
.git/
.github/
.gitignore
.golangci.yaml
.idea/
.pre-commit-config.yaml
README.md
payloads/
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SLACK_API_TOKEN=
SLACK_CHANNEL=
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @clevyr/devops
16 changes: 16 additions & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"local>clevyr/renovate-config"
],
"regexManagers": [
{
"fileMatch": "Dockerfile$",
"matchStrings": [
"ARG RIE_VERSION=(?<currentValue>.*)"
],
"datasourceTemplate": "github-releases",
"depNameTemplate": "aws/aws-lambda-runtime-interface-emulator"
}
]
}
90 changes: 90 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Build

on: push

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache: false
- name: Lint
uses: golangci/golangci-lint-action@v5

test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Test
run: go test ./...

build:
name: Build Image
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
ghcr.io/clevyr/lambda-cloudwatch-slack
tags: |
type=raw,priority=1000,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,value=beta,enable=${{ github.ref_name == 'main' }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Build and Push
id: docker_build
uses: docker/build-push-action@v5
with:
context: .
pull: true
push: ${{ steps.meta.outputs.tags != '' }}
platforms: linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

release:
name: Release
runs-on: ubuntu-latest
needs: [lint, test, build]
if: startsWith(github.ref, 'refs/tags/v')
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Generate Changelog
id: changelog
uses: gabe565/changelog-generator@v0
- name: Release
uses: softprops/action-gh-release@v2
with:
body: ${{ steps.changelog.outputs.changelog }}
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Editor
.idea

# Compiled
coverage.out
dist/
lambda-cloudwatch-slack
out/

# Config
.env*
!.env.example
88 changes: 88 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
run:
timeout: 5m

issues:
max-same-issues: 50
exclude-rules:
- path: "_test\\.go"
linters:
- dupl

linters:
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- copyloopvar
- decorder
- dupl
- durationcheck
- errcheck
- errname
- errorlint
- execinquery
- exportloopref
- forbidigo
- gci
- ginkgolinter
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- gochecksumtype
- goconst
- gocritic
- gocyclo
- godox
- goerr113
- gofumpt
- goheader
- goimports
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- gosmopolitan
- govet
- importas
- inamedparam
- ineffassign
- interfacebloat
- intrange
- loggercheck
- makezero
- mirror
- musttag
- nakedret
- nilerr
- nilnil
- noctx
- nolintlint
- nonamedreturns
- nosprintfhostport
- paralleltest
- perfsprint
- prealloc
- predeclared
- promlinter
- protogetter
- reassign
- revive
- rowserrcheck
- sloglint
- spancheck
- sqlclosecheck
- staticcheck
- stylecheck
- tenv
- testableexamples
- testifylint
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- wastedassign
- whitespace
- zerologlint
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repos:
- repo: https://github.com/TekWizely/pre-commit-golang
rev: v1.0.0-rc.1
hooks:
- id: go-mod-tidy-repo
- id: golangci-lint-mod
args: [--fix]
56 changes: 56 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#syntax=docker/dockerfile:1.7

FROM --platform=$BUILDPLATFORM golang:1.22.2-alpine as builder
WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY cmd cmd
COPY internal internal
COPY *.go ./
# Set Golang build envs based on Docker platform string
ARG TARGETPLATFORM
RUN --mount=type=cache,target=/root/.cache <<EOT
set -eux

case "$TARGETPLATFORM" in
'linux/amd64') export GOARCH=amd64 ;;
'linux/arm/v6') export GOARCH=arm GOARM=6 ;;
'linux/arm/v7') export GOARCH=arm GOARM=7 ;;
'linux/arm64') export GOARCH=arm64 ;;
*) echo "Unsupported target: $TARGETPLATFORM" && exit 1 ;;
esac

export CGO_ENABLED=0
go build -ldflags='-w -s' -tags lambda.norpc -trimpath -o cloudwatch-slack-alerts .
EOT

FROM alpine:3.19 AS rie
WORKDIR /app
ARG TARGETPLATFORM
ARG RIE_VERSION=v1.18
RUN <<EOT
set -eux

case "$TARGETPLATFORM" in
'linux/amd64') export SUFFIX=x86_64 ;;
'linux/arm64') export SUFFIX=arm64 ;;
*) echo "Unsupported target: $TARGETPLATFORM" && exit 1 ;;
esac

wget \
-O aws-lambda-rie \
"https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/download/$RIE_VERSION/aws-lambda-rie-$SUFFIX"
chmod +x aws-lambda-rie
EOT

FROM alpine:3.19 AS base
WORKDIR /app
COPY --from=builder /app/cloudwatch-slack-alerts .

FROM base AS local
COPY --from=rie /app/aws-lambda-rie .

FROM base
CMD ["./cloudwatch-slack-alerts"]
29 changes: 29 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"context"

"github.com/aws/aws-lambda-go/lambda"
"github.com/clevyr/cloudwatch-slack-alerts/internal/alert"
"github.com/clevyr/cloudwatch-slack-alerts/internal/config"
"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
conf := config.New()

cmd := &cobra.Command{
Use: "cloudwatch-slack-alerts",
Short: "Send AWS CloudWatch notifications to a Slack channel using Lambda",
PreRunE: func(cmd *cobra.Command, _ []string) error {
return conf.Load(cmd)
},
Run: func(_ *cobra.Command, _ []string) {
lambda.Start(alert.Handler(conf))
},
}

conf.RegisterFlags(cmd)
cmd.SetContext(config.NewContext(context.Background(), conf))
return cmd
}
25 changes: 25 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"testing"

"github.com/clevyr/cloudwatch-slack-alerts/internal/config"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCmdDbFlags(t *testing.T) {
t.Setenv("SLACK_API_TOKEN", "123")
t.Setenv("SLACK_CHANNEL", "alerts")

rootCmd := NewCommand()
rootCmd.Run = func(_ *cobra.Command, _ []string) {}

require.NoError(t, rootCmd.Execute())

conf, ok := config.FromContext(rootCmd.Context())
require.True(t, ok)
assert.Equal(t, "123", conf.SlackAPIToken)
assert.Equal(t, "alerts", conf.SlackChannel)
}
18 changes: 18 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
services:
app:
image: ghcr.io/clevyr/cloudwatch-slack-alerts:local
build:
context: .
target: local
env_file:
- .env
entrypoint:
- ./aws-lambda-rie
command:
- ./cloudwatch-slack-alerts
ports:
- 127.0.0.1:9000:8080
develop:
watch:
- action: rebuild
path: .
Loading

0 comments on commit c2fed66

Please sign in to comment.