Skip to content

Commit

Permalink
bump: aws-sdk-v2 (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
takkyuuplayer authored Apr 10, 2024
1 parent 0d93d9b commit d153e0d
Show file tree
Hide file tree
Showing 21 changed files with 1,112 additions and 40,933 deletions.
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ version:
go run cli/cage/main.go -v | cut -f 3 -d ' '

mocks:
$(MOCKGEN) github.com/aws/aws-sdk-go/service/ec2/ec2iface EC2API > mocks/github.com/aws/aws-sdk-go/service/ec2/ec2iface/mock_interface.go
$(MOCKGEN) github.com/aws/aws-sdk-go/service/ecs/ecsiface ECSAPI > mocks/github.com/aws/aws-sdk-go/service/ecs/ecsiface/mock_interface.go
$(MOCKGEN) github.com/aws/aws-sdk-go/service/elbv2/elbv2iface ELBV2API > mocks/github.com/aws/aws-sdk-go/service/elbv2/elbv2iface/mock_interface.go
mkdir -p mocks/mock_awsiface && $(MOCKGEN) -source=./awsiface/iface.go > mocks/mock_awsiface/iface.go

.PHONY: mocks
43 changes: 43 additions & 0 deletions awsiface/iface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package awsiface

import (
"context"

"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ecs"
elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
)

type (
EcsClient interface {
CreateService(ctx context.Context, params *ecs.CreateServiceInput, optFns ...func(*ecs.Options)) (*ecs.CreateServiceOutput, error)
UpdateService(ctx context.Context, params *ecs.UpdateServiceInput, optFns ...func(*ecs.Options)) (*ecs.UpdateServiceOutput, error)
DeleteService(ctx context.Context, params *ecs.DeleteServiceInput, optFns ...func(*ecs.Options)) (*ecs.DeleteServiceOutput, error)
StartTask(ctx context.Context, params *ecs.StartTaskInput, optFns ...func(*ecs.Options)) (*ecs.StartTaskOutput, error)
RegisterTaskDefinition(ctx context.Context, params *ecs.RegisterTaskDefinitionInput, optFns ...func(*ecs.Options)) (*ecs.RegisterTaskDefinitionOutput, error)
DescribeServices(ctx context.Context, params *ecs.DescribeServicesInput, optFns ...func(*ecs.Options)) (*ecs.DescribeServicesOutput, error)
DescribeTasks(ctx context.Context, params *ecs.DescribeTasksInput, optFns ...func(*ecs.Options)) (*ecs.DescribeTasksOutput, error)
DescribeContainerInstances(ctx context.Context, params *ecs.DescribeContainerInstancesInput, optFns ...func(*ecs.Options)) (*ecs.DescribeContainerInstancesOutput, error)
ListTasks(ctx context.Context, params *ecs.ListTasksInput, optFns ...func(*ecs.Options)) (*ecs.ListTasksOutput, error)
RunTask(ctx context.Context, params *ecs.RunTaskInput, optFns ...func(*ecs.Options)) (*ecs.RunTaskOutput, error)
StopTask(ctx context.Context, params *ecs.StopTaskInput, optFns ...func(*ecs.Options)) (*ecs.StopTaskOutput, error)
ListAttributes(ctx context.Context, params *ecs.ListAttributesInput, optFns ...func(*ecs.Options)) (*ecs.ListAttributesOutput, error)
PutAttributes(ctx context.Context, params *ecs.PutAttributesInput, optFns ...func(*ecs.Options)) (*ecs.PutAttributesOutput, error)
DescribeTaskDefinition(ctx context.Context, params *ecs.DescribeTaskDefinitionInput, optFns ...func(*ecs.Options)) (*ecs.DescribeTaskDefinitionOutput, error)
}
AlbClient interface {
DescribeTargetGroups(ctx context.Context, params *elbv2.DescribeTargetGroupsInput, optFns ...func(*elbv2.Options)) (*elbv2.DescribeTargetGroupsOutput, error)
DescribeTargetHealth(ctx context.Context, params *elbv2.DescribeTargetHealthInput, optFns ...func(*elbv2.Options)) (*elbv2.DescribeTargetHealthOutput, error)
DescribeTargetGroupAttributes(ctx context.Context, params *elbv2.DescribeTargetGroupAttributesInput, optFns ...func(*elbv2.Options)) (*elbv2.DescribeTargetGroupAttributesOutput, error)
RegisterTargets(ctx context.Context, params *elbv2.RegisterTargetsInput, optFns ...func(*elbv2.Options)) (*elbv2.RegisterTargetsOutput, error)
DeregisterTargets(ctx context.Context, params *elbv2.DeregisterTargetsInput, optFns ...func(*elbv2.Options)) (*elbv2.DeregisterTargetsOutput, error)
}
Ec2Client interface {
DescribeSubnets(ctx context.Context, params *ec2.DescribeSubnetsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeSubnetsOutput, error)
DescribeInstances(ctx context.Context, params *ec2.DescribeInstancesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeInstancesOutput, error)
}
)

var _ EcsClient = (*ecs.Client)(nil)
var _ AlbClient = (*elbv2.Client)(nil)
var _ Ec2Client = (*ec2.Client)(nil)
17 changes: 8 additions & 9 deletions cage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package cage

import (
"context"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go/service/ecs/ecsiface"
"github.com/aws/aws-sdk-go/service/elbv2/elbv2iface"

"github.com/loilo-inc/canarycage/awsiface"
)

type Cage interface {
Expand All @@ -15,16 +14,16 @@ type Cage interface {

type cage struct {
env *Envars
ecs ecsiface.ECSAPI
alb elbv2iface.ELBV2API
ec2 ec2iface.EC2API
ecs awsiface.EcsClient
alb awsiface.AlbClient
ec2 awsiface.Ec2Client
}

type Input struct {
Env *Envars
ECS ecsiface.ECSAPI
ALB elbv2iface.ELBV2API
EC2 ec2iface.EC2API
ECS awsiface.EcsClient
ALB awsiface.AlbClient
EC2 awsiface.Ec2Client
}

func NewCage(input *Input) Cage {
Expand Down
29 changes: 16 additions & 13 deletions cli/cage/commands/flags.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
package commands

import (
"context"

"github.com/apex/log"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/loilo-inc/canarycage"
"github.com/urfave/cli/v2"
)

func RegionFlag(dest *string) *cli.StringFlag {
return &cli.StringFlag{
Name: "region",
EnvVars: []string{cage.RegionKey},
EnvVars: []string{cage.RegionKey},
Usage: "aws region for ecs. if not specified, try to load from aws sessions automatically",
Destination: dest,
}
}
func ClusterFlag(dest *string) *cli.StringFlag {
return &cli.StringFlag{
Name: "cluster",
EnvVars: []string{cage.ClusterKey},
EnvVars: []string{cage.ClusterKey},
Usage: "ecs cluster name. if not specified, load from service.json",
Destination: dest,
}
}
func ServiceFlag(dest *string) *cli.StringFlag {
return &cli.StringFlag{
Name: "service",
EnvVars: []string{cage.ServiceKey},
EnvVars: []string{cage.ServiceKey},
Usage: "service name. if not specified, load from service.json",
Destination: dest,
}
}
func TaskDefinitionArnFlag(dest *string) *cli.StringFlag {
return &cli.StringFlag{
Name: "taskDefinitionArn",
EnvVars: []string{cage.TaskDefinitionArnKey},
EnvVars: []string{cage.TaskDefinitionArnKey},
Usage: "full arn for next task definition. if not specified, use task-definition.json for registration",
Destination: dest,
}
Expand All @@ -43,7 +45,7 @@ func TaskDefinitionArnFlag(dest *string) *cli.StringFlag {
func CanaryTaskIdleDurationFlag(dest *int) *cli.IntFlag {
return &cli.IntFlag{
Name: "canaryTaskIdleDuration",
EnvVars: []string{cage.CanaryTaskIdleDuration},
EnvVars: []string{cage.CanaryTaskIdleDuration},
Usage: "Idle duration seconds for ensuring canary task that has no attached load balancer",
Destination: dest,
Value: 10,
Expand All @@ -54,20 +56,21 @@ func (c *cageCommands) aggregateEnvars(
ctx *cli.Context,
envars *cage.Envars,
) {
var _region string
ses, err := session.NewSession()
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
log.Fatalf(err.Error())
}

if envars.Region != "" {
_region = envars.Region
log.Infof("🗺 region was set: %s", _region)
} else if *ses.Config.Region != "" {
_region = *ses.Config.Region
log.Infof("🗺 region was loaded from sessions: %s", _region)
log.Infof("🗺 region was set: %s", envars.Region)
}

if cfg.Region != "" {
log.Infof("🗺 region was loaded from default config: %s", cfg.Region)
} else {
log.Fatalf("🙄 region must specified by --region flag or aws session")
}

if ctx.NArg() > 0 {
dir := ctx.Args().Get(0)
td, svc, err := cage.LoadDefinitionsFromFiles(dir)
Expand Down
28 changes: 14 additions & 14 deletions cli/cage/commands/rollout.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package commands

import (
"context"

"github.com/apex/log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ecs"
elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
"github.com/loilo-inc/canarycage"
"github.com/urfave/cli/v2"
)
Expand All @@ -26,26 +28,24 @@ func (c *cageCommands) RollOut() *cli.Command {
CanaryTaskIdleDurationFlag(&envars.CanaryTaskIdleDuration),
&cli.StringFlag{
Name: "canaryInstanceArn",
EnvVars: []string{cage.CanaryInstanceArnKey},
EnvVars: []string{cage.CanaryInstanceArnKey},
Usage: "EC2 instance ARN for placing canary task. required only when LaunchType is EC2",
Destination: &envars.CanaryInstanceArn,
},
},
Action: func(ctx *cli.Context) error {
c.aggregateEnvars(ctx, &envars)
var ses *session.Session
if o, err := session.NewSession(&aws.Config{
Region: &envars.Region,
}); err != nil {
var cfg aws.Config
if o, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(envars.Region)); err != nil {
return err
} else {
ses = o
cfg = o
}
cagecli := cage.NewCage(&cage.Input{
Env: &envars,
ECS: ecs.New(ses),
EC2: ec2.New(ses),
ALB: elbv2.New(ses),
ECS: ecs.NewFromConfig(cfg),
EC2: ec2.NewFromConfig(cfg),
ALB: elbv2.NewFromConfig(cfg),
})
result, err := cagecli.RollOut(c.ctx)
if err != nil {
Expand Down
32 changes: 16 additions & 16 deletions cli/cage/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package commands
import (
"context"
"fmt"

"github.com/apex/log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ecs"
ecstypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
cage "github.com/loilo-inc/canarycage"
"github.com/urfave/cli/v2"
)
Expand All @@ -26,13 +28,11 @@ func (c *cageCommands) Run() *cli.Command {
},
Action: func(ctx *cli.Context) error {
c.aggregateEnvars(ctx, &envars)
var ses *session.Session
if o, err := session.NewSession(&aws.Config{
Region: &envars.Region,
}); err != nil {
var cfg aws.Config
if o, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(envars.Region)); err != nil {
return err
} else {
ses = o
cfg = o
}
rest := ctx.Args().Tail()
if len(rest) < 1 {
Expand All @@ -47,15 +47,15 @@ func (c *cageCommands) Run() *cli.Command {
commands := rest[1:]
cagecli := cage.NewCage(&cage.Input{
Env: &envars,
ECS: ecs.New(ses),
ALB: elbv2.New(ses),
EC2: ec2.New(ses),
ECS: ecs.NewFromConfig(cfg),
EC2: ec2.NewFromConfig(cfg),
ALB: elbv2.NewFromConfig(cfg),
})
_, err := cagecli.Run(context.Background(), &cage.RunInput{
Container: &container,
Overrides: &ecs.TaskOverride{
ContainerOverrides: []*ecs.ContainerOverride{
{Command: aws.StringSlice(commands),
Overrides: &ecstypes.TaskOverride{
ContainerOverrides: []ecstypes.ContainerOverride{
{Command: commands,
Name: &container},
},
},
Expand Down
25 changes: 12 additions & 13 deletions cli/cage/commands/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package commands

import (
"context"

"github.com/apex/log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ecs"
elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
"github.com/loilo-inc/canarycage"
"github.com/urfave/cli/v2"
)
Expand All @@ -28,19 +29,17 @@ func (c *cageCommands) Up() *cli.Command {
},
Action: func(ctx *cli.Context) error {
c.aggregateEnvars(ctx, &envars)
var ses *session.Session
if o, err := session.NewSession(&aws.Config{
Region: &envars.Region,
}); err != nil {
var cfg aws.Config
if o, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(envars.Region)); err != nil {
return err
} else {
ses = o
cfg = o
}
cagecli := cage.NewCage(&cage.Input{
Env: &envars,
ECS: ecs.New(ses),
ALB: elbv2.New(ses),
EC2: ec2.New(ses),
ECS: ecs.NewFromConfig(cfg),
EC2: ec2.NewFromConfig(cfg),
ALB: elbv2.NewFromConfig(cfg),
})
_, err := cagecli.Up(context.Background())
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package cage
import (
"encoding/json"
"fmt"
"github.com/apex/log"
"github.com/aws/aws-sdk-go/service/ecs"
"os"
"path/filepath"

"github.com/apex/log"
"github.com/aws/aws-sdk-go-v2/service/ecs"
)

type Envars struct {
Expand Down
5 changes: 3 additions & 2 deletions env_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cage

import (
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/stretchr/testify/assert"
"testing"

"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/stretchr/testify/assert"
)

func TestEnsureEnvars(t *testing.T) {
Expand Down
17 changes: 16 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,29 @@ go 1.22

require (
github.com/apex/log v1.9.0
github.com/aws/aws-sdk-go v1.44.239
github.com/aws/aws-sdk-go-v2 v1.26.0
github.com/aws/aws-sdk-go-v2/config v1.27.9
github.com/aws/aws-sdk-go-v2/service/ec2 v1.154.0
github.com/aws/aws-sdk-go-v2/service/ecs v1.41.5
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.30.4
github.com/golang/mock v1.6.0
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.8.2
github.com/urfave/cli/v2 v2.27.1
)

require (
github.com/aws/aws-sdk-go-v2/credentials v1.17.9 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.4 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.4 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.6 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.20.3 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.3 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.28.5 // indirect
github.com/aws/smithy-go v1.20.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
Expand Down
Loading

0 comments on commit d153e0d

Please sign in to comment.