Skip to content

Commit

Permalink
fix: loadBalancerArnをパラメタから削除。tgから引けるので
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp committed Aug 28, 2018
1 parent 09fecb5 commit d925a1c
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 37 deletions.
11 changes: 2 additions & 9 deletions cli/cage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func main() {
envars := &cage.Envars{
Region: aws.String(""),
Cluster: aws.String(""),
LoadBalancerArn: aws.String(""),
NextServiceName: aws.String(""),
NextServiceDefinitionBase64: aws.String(""),
CurrentServiceName: aws.String(""),
Expand All @@ -47,7 +46,7 @@ func main() {
Usage: "generate config file skeleton json",
},
cli.BoolFlag{
Name: "dry-run",
Name: "dryRun",
Usage: "describe roll out plan without affecting any resources",
},
cli.StringFlag{
Expand All @@ -63,12 +62,6 @@ func main() {
Usage: "ecs cluster name",
Destination: envars.Cluster,
},
cli.StringFlag{
Name: "loadBalancerArn",
EnvVar: cage.LoadBalancerArnKey,
Usage: "full arn of service load balancer",
Destination: envars.LoadBalancerArn,
},
cli.StringFlag{
Name: "nextServiceName",
EnvVar: cage.NextServiceNameKey,
Expand Down Expand Up @@ -143,7 +136,7 @@ func main() {
if err != nil {
log.Fatalf(err.Error())
}
if ctx.Bool("dry-run") {
if ctx.Bool("dryRun") {
DryRun(envars)
} else {
if err := Action(envars); err != nil {
Expand Down
4 changes: 0 additions & 4 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ type Envars struct {
_ struct{} `type:"struct"`
Region *string `json:"region" type:"string"`
Cluster *string `json:"cluster" type:"string" required:"true"`
LoadBalancerArn *string `json:"loadBalancerArn" type:"string" required:"true"`
NextServiceName *string `json:"nextServiceName" type:"string" required:"true"`
CurrentServiceName *string `json:"currentServiceName" type:"string" required:"true"`
NextServiceDefinitionBase64 *string `json:"nextServiceDefinitionBase64" type:"string"`
Expand All @@ -24,7 +23,6 @@ type Envars struct {
const ClusterKey = "CAGE_ECS_CLUSTER"
const NextServiceNameKey = "CAGE_NEXT_SERVICE_NAME"
const CurrentServiceNameKey = "CAGE_CURRENT_SERVICE_NAME"
const LoadBalancerArnKey = "CAGE_LB_ARN"

// either required
const NextTaskDefinitionBase64Key = "CAGE_NEXT_TASK_DEFINITION_BASE64"
Expand Down Expand Up @@ -53,8 +51,6 @@ func EnsureEnvars(
// required
if isEmpty(dest.Cluster) {
return NewErrorf("--cluster [%s] is required", ClusterKey)
} else if isEmpty(dest.LoadBalancerArn) {
return NewErrorf("--loadBalancerArn [%s] is required", LoadBalancerArnKey)
} else if isEmpty(dest.CurrentServiceName) {
return NewErrorf("--currentServiceName [%s] is required", CurrentServiceNameKey)
} else if isEmpty(dest.NextServiceName) {
Expand Down
7 changes: 0 additions & 7 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func TestEnsureEnvars(t *testing.T) {
CurrentServiceName: aws.String("service-current"),
NextTaskDefinitionBase64: aws.String("hoge"),
NextServiceDefinitionBase64: aws.String("next"),
LoadBalancerArn: aws.String("lb"),
AvailabilityThreshold: aws.Float64(0.9),
ResponseTimeThreshold: aws.Float64(0.5),
RollOutPeriod: aws.Int64(60),
Expand All @@ -33,7 +32,6 @@ func TestEnsureEnvars4(t *testing.T) {
CurrentServiceName: aws.String("service"),
NextTaskDefinitionBase64: aws.String("current"),
NextServiceName: aws.String("next"),
LoadBalancerArn: aws.String("lb"),
}
if err := EnsureEnvars(e); err != nil {
t.Fatalf(err.Error())
Expand All @@ -48,15 +46,13 @@ func TestEnsureEnvars2(t *testing.T) {
CurrentServiceNameKey,
NextTaskDefinitionBase64Key,
ClusterKey,
LoadBalancerArnKey,
}
for i, v := range arr {
m := make(map[string]*string)
m[NextServiceNameKey] = dummy
m[CurrentServiceNameKey] = dummy
m[NextTaskDefinitionBase64Key] = dummy
m[ClusterKey] = dummy
m[LoadBalancerArnKey] = dummy
for j, u := range arr {
if i == j {
m[u] = nil
Expand All @@ -67,7 +63,6 @@ func TestEnsureEnvars2(t *testing.T) {
NextServiceName: m[NextServiceNameKey],
NextTaskDefinitionBase64: m[NextTaskDefinitionBase64Key],
Cluster: m[ClusterKey],
LoadBalancerArn: m[LoadBalancerArnKey],
}
err := EnsureEnvars(e)
if err == nil {
Expand All @@ -83,7 +78,6 @@ func dummyEnvs() *Envars {
NextServiceName: dummy,
NextTaskDefinitionBase64: dummy,
Cluster: dummy,
LoadBalancerArn: dummy,
}
}
func TestEnsureEnvars3(t *testing.T) {
Expand Down Expand Up @@ -123,7 +117,6 @@ func TestUnmarshalEnvars(t *testing.T) {
}
assert.Equal(t, "us-east-2", *dest.Region)
assert.Equal(t, "cluster", *dest.Cluster)
assert.Equal(t, "arn://lb", *dest.LoadBalancerArn)
assert.Equal(t, "service-next", *dest.NextServiceName)
assert.Equal(t, "service-current", *dest.CurrentServiceName)
assert.Equal(t, "next-task", *dest.NextTaskDefinitionBase64)
Expand Down
3 changes: 2 additions & 1 deletion metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (envars *Envars) GetServiceMetricStatistics(

func (envars *Envars) AccumulatePeriodicServiceHealth(
cw cloudwatchiface.CloudWatchAPI,
loadBalancerArn *string,
targetGroupArn *string,
startTime time.Time,
endTime time.Time,
Expand All @@ -90,7 +91,7 @@ func (envars *Envars) AccumulatePeriodicServiceHealth(
tgId string
err error
)
if lbId, err = ExtractAlbId(*envars.LoadBalancerArn); err != nil {
if lbId, err = ExtractAlbId(*loadBalancerArn); err != nil {
return nil, err
}
if tgId, err = ExtractTargetGroupId(*targetGroupArn); err != nil {
Expand Down
7 changes: 3 additions & 4 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func TestEnvars_AccumulatePeriodicServiceHealth2(t *testing.T) {
defer func() { newTimer = time.NewTimer }()
newTimer = fakeTimer
envars := &Envars{
LoadBalancerArn: aws.String("arn://hoge/app/aaa/bbb"),
}
ctrl := gomock.NewController(t)
cw := mock_cloudwatch.NewMockCloudWatchAPI(ctrl)
Expand Down Expand Up @@ -78,15 +77,15 @@ func TestEnvars_AccumulatePeriodicServiceHealth2(t *testing.T) {
}).AnyTimes()
// cwがdata pointsを返さなくても指定範囲内でリトライする
o, err := envars.AccumulatePeriodicServiceHealth(
cw, aws.String("hoge/targetgroup/aa/bb"), time.Now(), time.Now(),
cw, aws.String("hoge/app/aa/bb"), aws.String("hoge/targetgroup/aa/bb"), time.Now(), time.Now(),
)
assert.Nil(t, err)
assert.Equal(t, 1.0, o.availability)
assert.Equal(t, 0.5, o.responseTime)
}

func TestEnvars_AccumulatePeriodicServiceHealth(t *testing.T) {
envars := &Envars{LoadBalancerArn: aws.String("ff/app/aa/bb")}
envars := &Envars{}
ctrl := gomock.NewController(t)
cw := mock_cloudwatch.NewMockCloudWatchAPI(ctrl)
cw.EXPECT().GetMetricStatistics(gomock.Any()).Return(&cloudwatch.GetMetricStatisticsOutput{
Expand All @@ -101,7 +100,7 @@ func TestEnvars_AccumulatePeriodicServiceHealth(t *testing.T) {
return fakeTimer(d)
}
_, err := envars.AccumulatePeriodicServiceHealth(
cw, aws.String("hoge/targetgroup/aa/bb"), time.Now(), time.Now(),
cw, aws.String("hoge/app/aa/bb"), aws.String("hoge/targetgroup/aa/bb"), time.Now(), time.Now(),
)
assert.NotNil(t, err)
assert.Equal(t, 20, callCnt)
Expand Down
7 changes: 4 additions & 3 deletions rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (envars *Envars) StartGradualRollOut(

func (envars *Envars) CanaryTest(
cw cloudwatchiface.CloudWatchAPI,
loadBalancerArn *string,
targetGroupArn *string,
totalRollOutCnt int64,
) error {
Expand All @@ -92,7 +93,7 @@ func (envars *Envars) CanaryTest(
"start accumulating periodic service health of '%s' during %s ~ %s",
*envars.NextServiceName, startTime.String(), endTime.String(),
)
health, err := envars.AccumulatePeriodicServiceHealth(cw, targetGroupArn, startTime, endTime)
health, err := envars.AccumulatePeriodicServiceHealth(cw, loadBalancerArn, targetGroupArn, startTime, endTime)
if err != nil {
return err
}
Expand Down Expand Up @@ -156,7 +157,7 @@ func (envars *Envars) RollOut(
return err
}
// Phase2: service-nextのperiodic healthを計測
if err := envars.CanaryTest(ctx.Cw, lb.TargetGroupArn, totalRollOutCnt); err != nil {
if err := envars.CanaryTest(ctx.Cw, tg.LoadBalancerArns[0], tg.TargetGroupArn, totalRollOutCnt); err != nil {
return err
}
// Phase3: service-currentからタスクを指定数消す
Expand Down Expand Up @@ -193,7 +194,7 @@ func (envars *Envars) RollOut(
if oldTaskCount == 0 && newTaskCount >= originalDesiredCount {
// ロールアウトが終わったら最終検証を行う
log.Infof("estimated roll out completed. Do final canary test...")
if err := envars.CanaryTest(ctx.Cw, lb.TargetGroupArn, totalRollOutCnt); err != nil {
if err := envars.CanaryTest(ctx.Cw, tg.LoadBalancerArns[0], tg.TargetGroupArn, totalRollOutCnt); err != nil {
log.Errorf("final canary test has failed due to: %s", err)
return err
}
Expand Down
1 change: 0 additions & 1 deletion rollout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func DefaultEnvars() *Envars {
return &Envars{
Region: aws.String("us-west-2"),
RollOutPeriod: aws.Int64(0),
LoadBalancerArn: aws.String("hoge/app/1111/hoge"),
Cluster: aws.String("cage-test"),
CurrentServiceName: aws.String(kCurrentServiceName),
NextServiceName: aws.String(kNextServiceName),
Expand Down
22 changes: 14 additions & 8 deletions test-integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/apex/log"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/elbv2/elbv2iface"
"github.com/aws/aws-sdk-go/service/elbv2"
"net/http"
"time"
Expand Down Expand Up @@ -135,9 +134,16 @@ func setupEnvars() *cage.Envars {
return envars
}

func GetAlbDNS(alb elbv2iface.ELBV2API, arn *string) (*string, error) {
out, err := alb.DescribeLoadBalancers(&elbv2.DescribeLoadBalancersInput{
LoadBalancerArns: []*string{arn},
func GetAlbDNS(ctx *cage.Context, envars *cage.Envars) (*string, error) {
o, _ := ctx.Ecs.DescribeServices(&ecs.DescribeServicesInput{
Cluster: envars.Cluster,
Services: []*string{envars.CurrentServiceName},
})
o2, _ := ctx.Alb.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{
TargetGroupArns: []*string{o.Services[0].LoadBalancers[0].TargetGroupArn},
})
out, err := ctx.Alb.DescribeLoadBalancers(&elbv2.DescribeLoadBalancersInput{
LoadBalancerArns: []*string{o2.TargetGroups[0].LoadBalancerArns[0]},
})
if err != nil {
log.Errorf("failed to get alb info due to: %s", err)
Expand All @@ -149,11 +155,11 @@ func GetAlbDNS(alb elbv2iface.ELBV2API, arn *string) (*string, error) {

func PollLoadBalancer(
envars *cage.Envars,
alb elbv2iface.ELBV2API,
ctx *cage.Context,
interval time.Duration,
stop chan bool,
) error {
dns, err := GetAlbDNS(alb, envars.LoadBalancerArn)
dns, err := GetAlbDNS(ctx, envars)
if err != nil {
log.Errorf(err.Error())
return err
Expand Down Expand Up @@ -208,7 +214,7 @@ func testInternal(t *testing.T, envars *cage.Envars) (*cage.RollOutResult, error
return err
})
eg.Go(func() error {
return PollLoadBalancer(envars, ctx.Alb, time.Duration(10)*time.Second, stop)
return PollLoadBalancer(envars, ctx, time.Duration(10)*time.Second, stop)
})
return result, eg.Wait()
}
Expand Down Expand Up @@ -274,4 +280,4 @@ func TestHealthyToUnHealthy(t *testing.T) {
// 新規サービスのタスクがALBヘルスチェック通らない場合ロールバックされること
err := testAbnormal(t, kUnhealthyTDArn, "healthy2unhealthy")
assert.Nil(t, err)
}
}
2 changes: 2 additions & 0 deletions test/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,10 @@ func (ctx *MockContext) DescribeTargetGroups(input *elbv2.DescribeTargetGroupsIn
TargetGroups: []*elbv2.TargetGroup{
{
TargetGroupName: aws.String("tgname"),
TargetGroupArn: input.TargetGroupArns[0],
HealthyThresholdCount: aws.Int64(1),
HealthCheckIntervalSeconds: aws.Int64(0),
LoadBalancerArns: []*string { aws.String("arn://hoge/app/aa/bb") },
},
},
}, nil
Expand Down

0 comments on commit d925a1c

Please sign in to comment.