@@ -171,15 +171,14 @@ func Run(ctx context.Context, config *Config) error {
171
171
}
172
172
}
173
173
174
+ eg , nctx := errgroup .WithContext (ctx )
174
175
var numRequests atomic.Int64
175
- errCh := make (chan error )
176
- defer close (errCh )
177
176
for _ , n := range namespaces {
178
- selector , err := chooseSelector (ctx , client , n , resource .kind , resource .name , config .LabelSelector )
177
+ selector , err := chooseSelector (nctx , client , n , resource .kind , resource .name , config .LabelSelector )
179
178
if err != nil {
180
179
return err
181
180
}
182
- a , err := WatchTargets (ctx ,
181
+ a , err := WatchTargets (nctx ,
183
182
client .CoreV1 ().Pods (n ),
184
183
selector ,
185
184
config .FieldSelector ,
@@ -189,39 +188,31 @@ func Run(ctx context.Context, config *Config) error {
189
188
return errors .Wrap (err , "failed to set up watch" )
190
189
}
191
190
192
- go func () {
191
+ eg . Go ( func () error {
193
192
for {
194
193
select {
195
194
case target , ok := <- a :
196
195
if ! ok {
197
- errCh <- fmt .Errorf ("lost watch connection" )
198
- return
196
+ return fmt .Errorf ("lost watch connection" )
199
197
}
200
198
numRequests .Add (1 )
201
199
if numRequests .Load () > int64 (config .MaxLogRequests ) {
202
- errCh <- fmt .Errorf (
200
+ return fmt .Errorf (
203
201
"stern reached the maximum number of log requests (%d)," +
204
202
" use --max-log-requests to increase the limit" ,
205
203
config .MaxLogRequests )
206
- return
207
204
}
208
205
go func () {
209
- tailTarget (ctx , target )
206
+ tailTarget (nctx , target )
210
207
numRequests .Add (- 1 )
211
208
}()
212
- case <- ctx .Done ():
213
- return
209
+ case <- nctx .Done ():
210
+ return nil
214
211
}
215
212
}
216
- }()
217
- }
218
-
219
- select {
220
- case e := <- errCh :
221
- return e
222
- case <- ctx .Done ():
223
- return nil
213
+ })
224
214
}
215
+ return eg .Wait ()
225
216
}
226
217
227
218
func chooseSelector (ctx context.Context , client clientset.Interface , namespace , kind , name string , selector labels.Selector ) (labels.Selector , error ) {
0 commit comments