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

Label Cleanup, Private/Public subnets #13

Merged
merged 10 commits into from
Jun 29, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.aws-lambda-rie
dist
main
mockRepo
justfile
24 changes: 18 additions & 6 deletions cmd/cli/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/alexflint/go-arg"
"github.com/jedib0t/go-pretty/table"
"github.com/linecard/self/internal/labelgun"

"github.com/rs/zerolog/log"
)
Expand All @@ -21,6 +20,10 @@ type InspectOpts struct {
Tag string `arg:"-t,--tag,env:DEFAULT_RELEASE_BRANCH"`
}

type BusesOpts struct {
NameSpace string `arg:"-n,--namespace,env:DEFAULT_DEPLOYMENT_NAMESPACE"`
}

type FunctionScope struct {
RepoScope
Run *NullCommand `arg:"subcommand:run" help:"Deploy the function locally"`
Expand All @@ -30,7 +33,7 @@ type FunctionScope struct {
Disable *DeploymentOpts `arg:"subcommand:disable" help:"Unsubscribe deployment from buses"`
Publish *ReleaseOpts `arg:"subcommand:publish" help:"Publish a release"`
Untag *UntagOpts `arg:"subcommand:untag" help:"Untag a release"`
Buses *NullCommand `arg:"subcommand:buses" help:"List deployment bus subscriptions"`
Buses *BusesOpts `arg:"subcommand:buses" help:"List deployment bus subscriptions"`
Inspect *InspectOpts `arg:"subcommand:inspect" help:"Inspect a release"`
}

Expand Down Expand Up @@ -90,12 +93,21 @@ func (f FunctionScope) DeployLocal(ctx context.Context) {
log.Fatal().Err(err).Msg("failed to build image")
}

content, err := labelgun.DecodeLabel(cfg.Label.Policy, image.Config.Labels)
labels, err := cfg.Labels.Decode(image.Config.Labels)
if err != nil {
log.Fatal().Err(err).Msg("failed to decode policy label")
log.Fatal().Err(err).Msg("failed to decode labels")
}

for k, v := range labels {
templatedValue, err := cfg.Template(v)
if err != nil {
log.Fatal().Err(err).Msg("failed to render label template")
}

labels[k] = templatedValue
}

policy, err := cfg.Template(content)
policy, err := cfg.Template(labels[cfg.Labels.Policy.Key])
if err != nil {
log.Fatal().Err(err).Msg("failed to render policy template")
}
Expand Down Expand Up @@ -231,7 +243,7 @@ func (f FunctionScope) UntagRelease(ctx context.Context) {
}

func (f FunctionScope) ListBuses(ctx context.Context) {
deployment, err := api.Deployment.Find(ctx, cfg.Git.Branch, cfg.Function.Name)
deployment, err := api.Deployment.Find(ctx, f.Buses.NameSpace, cfg.Function.Name)
if err != nil {
log.Fatal().Err(err).Msg("failed to find deployment")
}
Expand Down
15 changes: 11 additions & 4 deletions cmd/cli/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (

"github.com/linecard/self/internal/gitlib"
"github.com/linecard/self/internal/umwelt"
"github.com/linecard/self/internal/util"
"github.com/linecard/self/pkg/convention/config"
"github.com/linecard/self/pkg/sdk"

"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/apigatewayv2"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/jedib0t/go-pretty/table"
Expand All @@ -23,6 +24,7 @@ func BeforeAll(ctx context.Context) {
var err error

log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
log.With().Caller()

tablec = table.NewWriter()
tablec.SetOutputMirror(os.Stdout)
Expand All @@ -32,21 +34,26 @@ func BeforeAll(ctx context.Context) {
log.Fatal().Err(err).Msg("failed to determine working directory")
}

awsConfig, err := awsconfig.LoadDefaultConfig(ctx)
retryLogger := util.RetryLogger{
Log: &log.Logger,
}

awsConfig, err := awsconfig.LoadDefaultConfig(ctx,
awsconfig.WithLogger(&retryLogger),
awsconfig.WithClientLogMode(aws.LogRetries))
if err != nil {
log.Fatal().Err(err).Msg("failed to load AWS configuration")
}

stsc = sts.NewFromConfig(awsConfig)
ecrc := ecr.NewFromConfig(awsConfig)
gwc := apigatewayv2.NewFromConfig(awsConfig)

git, err := gitlib.FromCwd()
if err != nil {
log.Fatal().Err(err).Msg("failed to introspect git, are you in a git repository? does it have a remote origin?")
}

here, err := umwelt.FromCwd(ctx, cwd, git, awsConfig, ecrc, gwc, stsc)
here, err := umwelt.FromCwd(ctx, cwd, git, awsConfig, ecrc, stsc)
if err != nil {
log.Fatal().Err(err).Msg("failed to introspect surrounding environment")
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package handler
import (
"context"
"fmt"
"log/slog"

"github.com/linecard/self/internal/labelgun"
"github.com/linecard/self/internal/util"
"github.com/linecard/self/pkg/convention/config"
"github.com/linecard/self/pkg/sdk"
"github.com/rs/zerolog/log"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
Expand Down Expand Up @@ -37,7 +36,7 @@ func Handler(ctx context.Context, event events.ECRImageActionEvent) error {
BeforeEach(ctx, event)

if util.ShaLike(event.Detail.ImageTag) {
slog.Warn("skipping deployment", "tag", event.Detail.ImageTag)
log.Warn().Str("function", cfg.Function.Name).Str("sha", cfg.Git.Sha).Str("branch", cfg.Git.Branch).Msg("skipping")
return nil
}

Expand All @@ -46,26 +45,27 @@ func Handler(ctx context.Context, event events.ECRImageActionEvent) error {

switch event.Detail.ActionType {
case "PUSH":
slog.Info("deploying", "function", cfg.Function.Name, "branch", cfg.Git.Branch)
log.Info().Str("function", cfg.Function.Name).Str("branch", cfg.Git.Branch).Msgf("deploying")

span.SetAttributes(
attribute.String("self.deploy.function", cfg.Function.Name),
attribute.String("self.deploy.branch", cfg.Git.Branch),
)

release, err := api.Release.Find(ctx, cfg.Git.Branch)
if err != nil {
span.SetStatus(codes.Error, err.Error())
return fmt.Errorf("failed to find release: %v", err)
}

sha, err := labelgun.DecodeLabel(cfg.Label.Sha, release.Config.Labels)
labels, err := cfg.Labels.Decode(release.Config.Labels)
if err != nil {
err := fmt.Errorf("failed to decode sha label: %v", err)
span.SetStatus(codes.Error, err.Error())
return err
return fmt.Errorf("failed to decode labels: %v", err)
}

span.SetAttributes(
attribute.String("self.deploy.sha", sha),
attribute.String("self.deploy.sha", labels[cfg.Labels.Sha.Key]),
)

deployment, err := api.Deployment.Deploy(ctx, release, cfg.Git.Branch, cfg.Function.Name)
Expand All @@ -82,7 +82,7 @@ func Handler(ctx context.Context, event events.ECRImageActionEvent) error {
}

case "DELETE":
slog.Info("destroying", "function", cfg.Function.Name, "branch", cfg.Git.Branch)
log.Info().Str("function", cfg.Function.Name).Str("branch", cfg.Git.Branch).Msg("destroying")
span.SetAttributes(
attribute.String("self.destroy.function", cfg.Function.Name),
attribute.String("self.destroy.branch", cfg.Git.Branch),
Expand Down
4 changes: 1 addition & 3 deletions cmd/handler/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/aws/aws-lambda-go/events"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/apigatewayv2"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/aws/aws-sdk-go-v2/service/sts"

Expand All @@ -26,9 +25,8 @@ func BeforeEach(ctx context.Context, event events.ECRImageActionEvent) {

stsc := sts.NewFromConfig(awsConfig)
ecrc := ecr.NewFromConfig(awsConfig)
gwc := apigatewayv2.NewFromConfig(awsConfig)

here, err := umwelt.FromEvent(ctx, event, awsConfig, ecrc, gwc, stsc)
here, err := umwelt.FromEvent(ctx, event, awsConfig, ecrc, stsc)
if err != nil {
log.Fatal().Err(err).Msg("failed to introspect surrounding environment")
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/self/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"github.com/linecard/self/cmd/handler"
"github.com/linecard/self/internal/tracing"
"github.com/linecard/self/internal/util"
"github.com/rs/zerolog"
)

func main() {
zerolog.SetGlobalLevel(zerolog.WarnLevel)
util.SetLogLevel()

ctx, tp, shutdown := tracing.InitOtel()
defer shutdown()
Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ go 1.22.0
require (
github.com/alexflint/go-arg v1.4.3
github.com/aws/aws-lambda-go v1.47.0
github.com/aws/aws-sdk-go-v2 v1.26.1
github.com/aws/aws-sdk-go-v2 v1.29.0
github.com/aws/aws-sdk-go-v2/config v1.27.11
github.com/aws/aws-sdk-go-v2/service/apigatewayv2 v1.20.4
github.com/aws/aws-sdk-go-v2/service/ec2 v1.165.0
github.com/aws/aws-sdk-go-v2/service/ecr v1.27.4
github.com/aws/aws-sdk-go-v2/service/eventbridge v1.30.4
github.com/aws/aws-sdk-go-v2/service/iam v1.32.0
Expand Down Expand Up @@ -36,12 +37,12 @@ require (
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.11 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.11 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.11 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.5 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.13 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.20.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
Expand Down
18 changes: 10 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3d
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
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-v2 v1.26.1 h1:5554eUqIYVWpU0YmeeYZ0wU64H2VLBs8TlhRB2L+EkA=
github.com/aws/aws-sdk-go-v2 v1.26.1/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM=
github.com/aws/aws-sdk-go-v2 v1.29.0 h1:uMlEecEwgp2gs6CsM6ugquNHr6mg0LHylPBR8u5Ojac=
github.com/aws/aws-sdk-go-v2 v1.29.0/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 h1:x6xsQXGSmW6frevwDA+vi/wqhp1ct18mVXYN08/93to=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2/go.mod h1:lPprDr1e6cJdyYeGXnRaJoP4Md+cDBvi2eOj00BlGmg=
github.com/aws/aws-sdk-go-v2/config v1.27.11 h1:f47rANd2LQEYHda2ddSCKYId18/8BhSRM4BULGmfgNA=
Expand All @@ -27,16 +27,18 @@ github.com/aws/aws-sdk-go-v2/credentials v1.17.11 h1:YuIB1dJNf1Re822rriUOTxopaHH
github.com/aws/aws-sdk-go-v2/credentials v1.17.11/go.mod h1:AQtFPsDH9bI2O+71anW6EKL+NcD7LG3dpKGMV4SShgo=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 h1:FVJ0r5XTHSmIHJV6KuDmdYhEpvlHpiSd38RQWhut5J4=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1/go.mod h1:zusuAeqezXzAB24LGuzuekqMAEgWkVYukBec3kr3jUg=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 h1:aw39xVGeRWlWx9EzGVnhOR4yOjQDHPQ6o6NmBlscyQg=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5/go.mod h1:FSaRudD0dXiMPK2UjknVwwTYyZMRsHv3TtkabsZih5I=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 h1:PG1F3OD1szkuQPzDw3CIQsRIrtTlUC3lP84taWzHlq0=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5/go.mod h1:jU1li6RFryMz+so64PpKtudI+QzbKoIEivqdf6LNpOc=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.11 h1:ltkhl3I9ddcRR3Dsy+7bOFFq546O8OYsfNEXVIyuOSE=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.11/go.mod h1:H4D8JoCFNJwnT7U5U8iwgG24n71Fx2I/ZP/18eYFr9g=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.11 h1:+BgX2AY7yV4ggSwa80z/yZIJX+e0jnNxjMLVyfpSXM0=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.11/go.mod h1:DlBATBSDCz30BCdRFldmyLsAzJwi2pdQ+YSdJTHhTUI=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.5 h1:81KE7vaZzrl7yHBYHVEzYB8sypz11NMOZ40YlWvPxsU=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.5/go.mod h1:LIt2rg7Mcgn09Ygbdh/RdIm0rQ+3BNkbP1gyVMFtRK0=
github.com/aws/aws-sdk-go-v2/service/apigatewayv2 v1.20.4 h1:PLfHdrvs3L32R21hoxzmp0itGKKzUASF63UMtUmRG80=
github.com/aws/aws-sdk-go-v2/service/apigatewayv2 v1.20.4/go.mod h1:PkfhkgYj7XKPO/kGyF7s4DC5ZVrxfHoWDD+rrxobLMg=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.165.0 h1:FQpJS76mmmo21FZn9FAutjAIxotNkiGXUYfUQN/RfGA=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.165.0/go.mod h1:+dDvvbkwmJCZGzsSlsqEtJ6XhyG/hD2FHjIfpqcNl+o=
github.com/aws/aws-sdk-go-v2/service/ecr v1.27.4 h1:Qr9W21mzWT3RhfYn9iAux7CeRIdbnTAqmiOlASqQgZI=
github.com/aws/aws-sdk-go-v2/service/ecr v1.27.4/go.mod h1:if7ybzzjOmDB8pat9FE35AHTY6ZxlYSy3YviSmFZv8c=
github.com/aws/aws-sdk-go-v2/service/eventbridge v1.30.4 h1:Vz4ilZcVXCR9yatX5yfMrkBldYggtkih3h7woHvzu5Q=
Expand All @@ -45,8 +47,8 @@ github.com/aws/aws-sdk-go-v2/service/iam v1.32.0 h1:ZNlfPdw849gBo/lvLFbEEvpTJMij
github.com/aws/aws-sdk-go-v2/service/iam v1.32.0/go.mod h1:aXWImQV0uTW35LM0A/T4wEg6R1/ReXUu4SM6/lUHYK0=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 h1:Ji0DY1xUsUr3I8cHps0G+XM3WWU16lP6yG8qu1GAZAs=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2/go.mod h1:5CsjAbs3NlGQyZNFACh+zztPDI7fU6eW9QsxjfnuBKg=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7 h1:ogRAwT1/gxJBcSWDMZlgyFUM962F51A5CRhDLbxLdmo=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7/go.mod h1:YCsIZhXfRPLFFCl5xxY+1T9RKzOKjCut+28JSX2DnAk=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.13 h1:3A8vxp65nZy6aMlSCBvpIyxIbAN0DOSxaPDZuzasxuU=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.13/go.mod h1:IxJ/pMQ/Y+MDFGo6pQRyqzKKwtGMHb5IWp5PXSQr8dM=
github.com/aws/aws-sdk-go-v2/service/lambda v1.54.0 h1:gazALVrZ7RIG6gJXut3c7NKtPgs9eQ8BFCA9uoliayk=
github.com/aws/aws-sdk-go-v2/service/lambda v1.54.0/go.mod h1:rFAo+jemFgeqYzDbbCbz2QWQs1Fnk1meTUK9fWkED9M=
github.com/aws/aws-sdk-go-v2/service/sso v1.20.5 h1:vN8hEbpRnL7+Hopy9dzmRle1xmDc7o8tmY0klsr175w=
Expand Down
Loading
Loading