diff --git a/cmd/reduceclustercapacity.go b/cmd/reduceclustercapacity.go index 97b1e7b..e7be4aa 100644 --- a/cmd/reduceclustercapacity.go +++ b/cmd/reduceclustercapacity.go @@ -63,8 +63,7 @@ func reduceClusterCapacity(cmd *cobra.Command, args []string) error { return newRuntimeError("failed to initialize a session: %w", err) } - var drainer capacity.Drainer - drainer, err = capacity.NewDrainer(cluster, ecsconst.MaxListableContainerInstances, ecs.New(sess)) + drainer, err := capacity.NewDrainer(cluster, ecsconst.MaxListableContainerInstances, ecs.New(sess)) if err != nil { return newRuntimeError("failed to initialize a Drainer: %w", err) } @@ -96,7 +95,7 @@ func reduceClusterCapacity(cmd *cobra.Command, args []string) error { return newRuntimeError("failed to create an event rule for interruption warnings: %w", err) } - if err = sfr.ReduceCapacity(amount, drainer, capacity.NewSQSQueuePoller(queueURL, sqsSvc)); err != nil { + if err := sfr.ReduceCapacity(amount, drainer, capacity.NewSQSQueuePoller(queueURL, sqsSvc)); err != nil { return newRuntimeError("failed to reduce the cluster capacity: %w", err) } diff --git a/cmd/replaceautoscalinggroupinstances.go b/cmd/replaceautoscalinggroupinstances.go index d13c701..aeb02cf 100644 --- a/cmd/replaceautoscalinggroupinstances.go +++ b/cmd/replaceautoscalinggroupinstances.go @@ -43,14 +43,12 @@ func replaceAutoScalingGroupInstances(cmd *cobra.Command, args []string) error { return newRuntimeError("failed to initialize a session: %w", err) } - var asg *capacity.AutoScalingGroup - asg, err = capacity.NewAutoScalingGroup(name, autoscaling.New(sess), ec2.New(sess)) + asg, err := capacity.NewAutoScalingGroup(name, autoscaling.New(sess), ec2.New(sess)) if err != nil { return newRuntimeError("failed to initialize a AutoScalingGroup: %w", err) } - var drainer capacity.Drainer - drainer, err = capacity.NewDrainer(cluster, batchSize, ecs.New(sess)) + drainer, err := capacity.NewDrainer(cluster, batchSize, ecs.New(sess)) if err != nil { return newRuntimeError("failed to initialize a Drainer: %w", err) } diff --git a/cmd/terminatespotfleetinstances.go b/cmd/terminatespotfleetinstances.go index c700154..da8d674 100644 --- a/cmd/terminatespotfleetinstances.go +++ b/cmd/terminatespotfleetinstances.go @@ -41,14 +41,12 @@ func terminateSpotFleetInstances(cmd *cobra.Command, args []string) error { return newRuntimeError("failed to initialize a session: %w", err) } - var sfr *capacity.SpotFleetRequest - sfr, err = capacity.NewSpotFleetRequest(id, ec2.New(sess)) + sfr, err := capacity.NewSpotFleetRequest(id, ec2.New(sess)) if err != nil { return newRuntimeError("failed to initialize a SpotFleetRequest: %w", err) } - var drainer capacity.Drainer - drainer, err = capacity.NewDrainer(cluster, batchSize, ecs.New(sess)) + drainer, err := capacity.NewDrainer(cluster, batchSize, ecs.New(sess)) if err != nil { return newRuntimeError("failed to initialize a Drainer: %w", err) } diff --git a/internal/capacity/autoscalinggroup.go b/internal/capacity/autoscalinggroup.go index 3aeea0c..be3b250 100644 --- a/internal/capacity/autoscalinggroup.go +++ b/internal/capacity/autoscalinggroup.go @@ -32,8 +32,7 @@ type AutoScalingGroup struct { func NewAutoScalingGroup(name string, asSvc autoscalingiface.AutoScalingAPI, ec2Svc ec2iface.EC2API) (*AutoScalingGroup, error) { asg := AutoScalingGroup{asSvc: asSvc, ec2Svc: ec2Svc, name: name} - err := asg.reload() - if err != nil { + if err := asg.reload(); err != nil { return nil, err } return &asg, nil @@ -87,24 +86,25 @@ func (asg *AutoScalingGroup) reload() error { return xerrors.Errorf("the auto scaling group \"%s\" doesn't exist", asg.name) } - group := resp.AutoScalingGroups[0] - originalDesiredCapacity := *group.DesiredCapacity - originalMaxSize := *group.MaxSize - var stateSavedAt time.Time - for _, t := range group.Tags { + asg.Group = resp.AutoScalingGroups[0] + asg.OriginalDesiredCapacity = asg.DesiredCapacity + asg.OriginalMaxSize = asg.MaxSize + for _, t := range asg.Tags { switch *t.Key { case "ecsmec:OriginalDesiredCapacity": - originalDesiredCapacity, err = strconv.ParseInt(*t.Value, 10, 64) + originalDesiredCapacity, err := strconv.ParseInt(*t.Value, 10, 64) if err != nil { return xerrors.Errorf("ecsmec:OriginalDesiredCapacity is invalid (%s): %w", *t.Value, err) } + asg.OriginalDesiredCapacity = &originalDesiredCapacity case "ecsmec:OriginalMaxSize": - originalMaxSize, err = strconv.ParseInt(*t.Value, 10, 64) + originalMaxSize, err := strconv.ParseInt(*t.Value, 10, 64) if err != nil { return xerrors.Errorf("ecsmec:OriginalMaxSize is invalid (%s): %w", *t.Value, err) } + asg.OriginalMaxSize = &originalMaxSize case "ecsmec:StateSavedAt": - stateSavedAt, err = time.Parse(time.RFC3339, *t.Value) + stateSavedAt, err := time.Parse(time.RFC3339, *t.Value) if err != nil { return xerrors.Errorf("ecsmec:StateSavedAt is invalid (%s): %w", *t.Value, err) } @@ -112,10 +112,6 @@ func (asg *AutoScalingGroup) reload() error { } } - asg.Group = group - asg.OriginalDesiredCapacity = &originalDesiredCapacity - asg.OriginalMaxSize = &originalMaxSize - return nil } diff --git a/internal/capacity/autoscalinggroup_test.go b/internal/capacity/autoscalinggroup_test.go index e57bb2d..276b11a 100644 --- a/internal/capacity/autoscalinggroup_test.go +++ b/internal/capacity/autoscalinggroup_test.go @@ -439,8 +439,7 @@ func TestAutoScalingGroup_ReplaceInstances(t *testing.T) { t.Fatal(err) } - err = group.ReplaceInstances(drainerMock) - if err != nil { + if err := group.ReplaceInstances(drainerMock); err != nil { t.Errorf("err = %#v; want nil", err) } }) @@ -517,8 +516,7 @@ func TestAutoScalingGroup_ReplaceInstances(t *testing.T) { t.Fatal(err) } - err = group.ReplaceInstances(drainerMock) - if err != nil { + if err := group.ReplaceInstances(drainerMock); err != nil { t.Errorf("err = %#v; want nil", err) } }) @@ -584,8 +582,7 @@ func TestAutoScalingGroup_ReplaceInstances(t *testing.T) { t.Fatal(err) } - err = group.ReplaceInstances(drainerMock) - if err != nil { + if err := group.ReplaceInstances(drainerMock); err != nil { t.Errorf("err = %#v; want nil", err) } }) @@ -660,8 +657,7 @@ func TestAutoScalingGroup_ReplaceInstances(t *testing.T) { t.Fatal(err) } - err = group.ReplaceInstances(drainerMock) - if err != nil { + if err := group.ReplaceInstances(drainerMock); err != nil { t.Errorf("err = %#v; want nil", err) } }) @@ -745,8 +741,7 @@ func TestAutoScalingGroup_ReduceCapacity(t *testing.T) { t.Fatal(err) } - err = group.ReduceCapacity(int64(len(instancesToTerminate)), drainerMock) - if err != nil { + if err := group.ReduceCapacity(int64(len(instancesToTerminate)), drainerMock); err != nil { t.Errorf("err = %#v; want nil", err) } diff --git a/internal/capacity/drainer.go b/internal/capacity/drainer.go index c6fb3df..5ba11d6 100644 --- a/internal/capacity/drainer.go +++ b/internal/capacity/drainer.go @@ -155,7 +155,7 @@ func (d *drainer) drainContainerInstances(arns []*string, wait bool) error { // Stop tasks manually because tasks that don't belong to a service won't stop // even after their cluster instance's status becomes "DRAINING" log.Printf("Stop the task \"%s\"\n", *t.TaskArn) - _, err = d.ecsSvc.StopTask(&ecs.StopTaskInput{ + _, err := d.ecsSvc.StopTask(&ecs.StopTaskInput{ Cluster: t.ClusterArn, Reason: aws.String("Task stopped by ecsmec"), Task: t.TaskArn, diff --git a/internal/capacity/drainer_test.go b/internal/capacity/drainer_test.go index f146fba..bc2cb37 100644 --- a/internal/capacity/drainer_test.go +++ b/internal/capacity/drainer_test.go @@ -94,8 +94,7 @@ func TestDrainer_Drain(t *testing.T) { t.Fatal(err) } - err = drainer.Drain(instanceIDs) - if err != nil { + if err := drainer.Drain(instanceIDs); err != nil { t.Errorf("err = %#v; want nil", err) } }) @@ -125,8 +124,7 @@ func TestDrainer_Drain(t *testing.T) { t.Fatal(err) } - err = drainer.Drain(instanceIDs) - if err == nil { + if err := drainer.Drain(instanceIDs); err == nil { t.Errorf("err = nil; want non-nil") } }) @@ -225,8 +223,7 @@ func TestDrainer_ProcessInterruptions(t *testing.T) { t.Fatal(err) } - var entries []*sqs.DeleteMessageBatchRequestEntry - entries, err = drainer.ProcessInterruptions(messages) + entries, err := drainer.ProcessInterruptions(messages) if err != nil { t.Errorf("err = %#v; want nil", err) } @@ -264,8 +261,7 @@ func TestDrainer_ProcessInterruptions(t *testing.T) { t.Fatal(err) } - var entries []*sqs.DeleteMessageBatchRequestEntry - entries, err = drainer.ProcessInterruptions(messages) + entries, err := drainer.ProcessInterruptions(messages) if err != nil { t.Errorf("err = nil; want non-nil") } diff --git a/internal/capacity/poller.go b/internal/capacity/poller.go index 3a75948..a850436 100644 --- a/internal/capacity/poller.go +++ b/internal/capacity/poller.go @@ -53,8 +53,7 @@ func (p *SQSQueuePoller) PollOnce(callback func([]*sqs.Message) ([]*sqs.DeleteMe return xerrors.Errorf("failed to receive messages: %w", err) } - var entries []*sqs.DeleteMessageBatchRequestEntry - entries, err = callback(resp.Messages) + entries, err := callback(resp.Messages) if err != nil { return err } diff --git a/internal/capacity/spotfleetrequest_test.go b/internal/capacity/spotfleetrequest_test.go index 8384a47..0a1ae2d 100644 --- a/internal/capacity/spotfleetrequest_test.go +++ b/internal/capacity/spotfleetrequest_test.go @@ -76,8 +76,7 @@ func TestSpotFleetRequest_TerminateAllInstances(t *testing.T) { t.Fatal(err) } - err = sfr.TerminateAllInstances(drainerMock) - if err != nil { + if err := sfr.TerminateAllInstances(drainerMock); err != nil { t.Errorf("err = %#v; want nil", err) } }) @@ -110,8 +109,7 @@ func TestSpotFleetRequest_TerminateAllInstances(t *testing.T) { t.Fatal(err) } - err = sfr.TerminateAllInstances(drainerMock) - if err == nil { + if err := sfr.TerminateAllInstances(drainerMock); err == nil { t.Errorf("err = nil; want non-nil") } }) @@ -236,8 +234,7 @@ func TestSpotFleetRequest_ReduceCapacity(t *testing.T) { t.Fatal(err) } - err = sfr.ReduceCapacity(tt.amount, drainerMock, pollerMock) - if err != nil { + if err := sfr.ReduceCapacity(tt.amount, drainerMock, pollerMock); err != nil { t.Errorf("err = %#v; want nil", err) } }) @@ -268,8 +265,7 @@ func TestSpotFleetRequest_ReduceCapacity(t *testing.T) { t.Fatal(err) } - err = sfr.ReduceCapacity(1, drainerMock, pollerMock) - if err != nil { + if err := sfr.ReduceCapacity(1, drainerMock, pollerMock); err != nil { t.Errorf("err = %#v; want nil", err) } }) @@ -362,8 +358,7 @@ func TestSpotFleetRequest_ReduceCapacity(t *testing.T) { t.Fatal(err) } - err = sfr.ReduceCapacity(4, drainerMock, pollerMock) - if err == nil { + if err := sfr.ReduceCapacity(4, drainerMock, pollerMock); err == nil { t.Errorf("err = nil; want non-nil") } }) diff --git a/internal/service/service.go b/internal/service/service.go index dd3e62e..e9c05b2 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -85,12 +85,11 @@ func (s *Service) copy(cluster string, serviceName string, overrides Definition) } func (s *Service) createAndWaitUntilStable(config *ecs.CreateServiceInput) error { - _, err := s.ecsSvc.CreateService(config) - if err != nil { + if _, err := s.ecsSvc.CreateService(config); err != nil { return xerrors.Errorf("failed to create the service \"%s\": %w", *config.ServiceName, err) } - err = s.ecsSvc.WaitUntilServicesStable(&ecs.DescribeServicesInput{ + err := s.ecsSvc.WaitUntilServicesStable(&ecs.DescribeServicesInput{ Cluster: config.Cluster, Services: []*string{config.ServiceName}, }) @@ -141,7 +140,7 @@ func (s *Service) stopAndWaitUntilStopped(cluster string, serviceName string) er } for arns := range sliceutil.ChunkSlice(taskArns, ecsconst.MaxDescribableTasks) { - err = s.ecsSvc.WaitUntilTasksStopped(&ecs.DescribeTasksInput{ + err := s.ecsSvc.WaitUntilTasksStopped(&ecs.DescribeTasksInput{ Cluster: aws.String(cluster), Tasks: arns, })