@@ -11,7 +11,6 @@ import (
11
11
ecsTypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
12
12
"github.com/gruntwork-io/go-commons/collections"
13
13
"github.com/gruntwork-io/go-commons/errors"
14
- "github.com/gruntwork-io/go-commons/logging"
15
14
"github.com/gruntwork-io/go-commons/retry"
16
15
)
17
16
@@ -29,8 +28,9 @@ func GetContainerInstanceArns(opts *Options, clusterName string) ([]string, erro
29
28
return nil , err
30
29
}
31
30
32
- logger := logging .GetProjectLogger ()
33
- logger .Infof ("Looking up Container Instance ARNs for ECS cluster %s" , clusterName )
31
+ if opts .Logger != nil {
32
+ opts .Logger .Debugf ("Looking up Container Instance ARNs for ECS cluster %s" , clusterName )
33
+ }
34
34
35
35
input := & ecs.ListContainerInstancesInput {Cluster : aws .String (clusterName )}
36
36
arns := []string {}
@@ -60,15 +60,16 @@ func StartDrainingContainerInstances(opts *Options, clusterName string, containe
60
60
return err
61
61
}
62
62
63
- logger := logging .GetProjectLogger ()
64
63
batchSize := 10
65
64
numBatches := int (math .Ceil (float64 (len (containerInstanceArns ) / batchSize )))
66
65
67
66
errList := NewMultipleDrainContainerInstanceErrors ()
68
67
for batchIdx , batchedArnList := range collections .BatchListIntoGroupsOf (containerInstanceArns , batchSize ) {
69
68
batchedArns := aws .StringSlice (batchedArnList )
70
69
71
- logger .Infof ("Putting batch %d/%d of container instances in cluster %s into DRAINING state" , batchIdx , numBatches , clusterName )
70
+ if opts .Logger != nil {
71
+ opts .Logger .Debugf ("Putting batch %d/%d of container instances in cluster %s into DRAINING state" , batchIdx , numBatches , clusterName )
72
+ }
72
73
input := & ecs.UpdateContainerInstancesStateInput {
73
74
Cluster : aws .String (clusterName ),
74
75
ContainerInstances : aws .ToStringSlice (batchedArns ),
@@ -77,18 +78,25 @@ func StartDrainingContainerInstances(opts *Options, clusterName string, containe
77
78
_ , err := client .UpdateContainerInstancesState (opts .Context , input )
78
79
if err != nil {
79
80
errList .AddError (err )
80
- logger .Errorf ("Encountered error starting to drain container instances in batch %d: %s" , batchIdx , err )
81
- logger .Errorf ("Container Instance ARNs: %s" , strings .Join (batchedArnList , "," ))
81
+ if opts .Logger != nil {
82
+ opts .Logger .Errorf ("Encountered error starting to drain container instances in batch %d: %s" , batchIdx , err )
83
+ opts .Logger .Errorf ("Container Instance ARNs: %s" , strings .Join (batchedArnList , "," ))
84
+ }
82
85
continue
83
86
}
84
87
85
- logger .Infof ("Started draining %d container instances from batch %d" , len (batchedArnList ), batchIdx )
88
+ if opts .Logger != nil {
89
+ opts .Logger .Debugf ("Started draining %d container instances from batch %d" , len (batchedArnList ), batchIdx )
90
+ }
86
91
}
87
92
88
93
if ! errList .IsEmpty () {
89
94
return errors .WithStackTrace (errList )
90
95
}
91
- logger .Infof ("Successfully started draining all %d container instances" , len (containerInstanceArns ))
96
+
97
+ if opts .Logger != nil {
98
+ opts .Logger .Debugf ("Successfully started draining all %d container instances" , len (containerInstanceArns ))
99
+ }
92
100
return nil
93
101
}
94
102
@@ -100,22 +108,25 @@ func WaitForContainerInstancesToDrain(opts *Options, clusterName string, contain
100
108
return err
101
109
}
102
110
103
- logger := logging .GetProjectLogger ()
104
- logger .Infof ("Checking if all ECS Tasks have been drained from the ECS Container Instances in Cluster %s." , clusterName )
111
+ if opts .Logger != nil {
112
+ opts .Logger .Debugf ("Checking if all ECS Tasks have been drained from the ECS Container Instances in Cluster %s." , clusterName )
113
+ }
105
114
106
115
batchSize := 100
107
116
numBatches := int (math .Ceil (float64 (len (containerInstanceArns ) / batchSize )))
108
117
109
118
err = retry .DoWithRetry (
110
- logger .Logger ,
119
+ opts .Logger ,
111
120
"Wait for Container Instances to be Drained" ,
112
121
maxRetries , sleepBetweenRetries ,
113
122
func () error {
114
123
responses := []* ecs.DescribeContainerInstancesOutput {}
115
124
for batchIdx , batchedArnList := range collections .BatchListIntoGroupsOf (containerInstanceArns , batchSize ) {
116
125
batchedArns := aws .StringSlice (batchedArnList )
117
126
118
- logger .Infof ("Fetching description of batch %d/%d of ECS Instances in Cluster %s." , batchIdx , numBatches , clusterName )
127
+ if opts .Logger != nil {
128
+ opts .Logger .Debugf ("Fetching description of batch %d/%d of ECS Instances in Cluster %s." , batchIdx , numBatches , clusterName )
129
+ }
119
130
input := & ecs.DescribeContainerInstancesInput {
120
131
Cluster : aws .String (clusterName ),
121
132
ContainerInstances : aws .ToStringSlice (batchedArns ),
@@ -134,7 +145,9 @@ func WaitForContainerInstancesToDrain(opts *Options, clusterName string, contain
134
145
135
146
// Yay, all done.
136
147
if drained , _ := allInstancesFullyDrained (responses ); drained == true {
137
- logger .Infof ("All container instances have been drained in Cluster %s!" , clusterName )
148
+ if opts .Logger != nil {
149
+ opts .Logger .Debugf ("All container instances have been drained in Cluster %s!" , clusterName )
150
+ }
138
151
return nil
139
152
}
140
153
@@ -181,19 +194,24 @@ func allInstancesFullyDrained(responses []*ecs.DescribeContainerInstancesOutput)
181
194
}
182
195
183
196
func instanceFullyDrained (instance ecsTypes.ContainerInstance ) bool {
184
- logger := logging .GetProjectLogger ()
185
197
instanceArn := instance .ContainerInstanceArn
186
198
187
199
if * instance .Status == "ACTIVE" {
188
- logger .Infof ("The ECS Container Instance %s is still in ACTIVE status" , * instanceArn )
200
+ if opts .Logger != nil {
201
+ opts .Logger .Debugf ("The ECS Container Instance %s is still in ACTIVE status" , * instanceArn )
202
+ }
189
203
return false
190
204
}
191
205
if instance .PendingTasksCount > 0 {
192
- logger .Infof ("The ECS Container Instance %s still has pending tasks" , * instanceArn )
206
+ if opts .Logger != nil {
207
+ opts .Logger .Debugf ("The ECS Container Instance %s still has pending tasks" , * instanceArn )
208
+ }
193
209
return false
194
210
}
195
211
if instance .RunningTasksCount > 0 {
196
- logger .Infof ("The ECS Container Instance %s still has running tasks" , * instanceArn )
212
+ if opts .Logger != nil {
213
+ opts .Logger .Debugf ("The ECS Container Instance %s still has running tasks" , * instanceArn )
214
+ }
197
215
return false
198
216
}
199
217
0 commit comments