Skip to content

Commit 449c80b

Browse files
committed
Use errgroup for all go routines
Instead of using log.Fatal in some of them Signed-off-by: Richard Wall <[email protected]>
1 parent 3da4d47 commit 449c80b

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

pkg/agent/run.go

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,16 @@ func Run(cmd *cobra.Command, args []string) {
7474
logs.Log.Fatalf("While evaluating configuration: %v", err)
7575
}
7676

77-
go func() {
77+
group, gctx := errgroup.WithContext(ctx)
78+
defer func() {
79+
// TODO: replace Fatalf log calls with Errorf and return the error
80+
cancel()
81+
if err := group.Wait(); err != nil {
82+
logs.Log.Fatalf("failed to wait for controller-runtime component to stop: %v", err)
83+
}
84+
}()
85+
86+
group.Go(func() error {
7887
server := http.NewServeMux()
7988

8089
if Flags.Profiling {
@@ -105,21 +114,23 @@ func Run(cmd *cobra.Command, args []string) {
105114

106115
err := http.ListenAndServe(":8081", server)
107116
if err != nil && !errors.Is(err, http.ErrServerClosed) {
108-
logs.Log.Fatalf("failed to run the health check server: %s", err)
117+
return fmt.Errorf("failed to run the health check server: %s", err)
109118
}
110-
}()
119+
return nil
120+
})
111121

112122
_, isVenConn := preflightClient.(*client.VenConnClient)
113123
if isVenConn {
114-
go func() {
115-
err := preflightClient.(manager.Runnable).Start(ctx)
124+
group.Go(func() error {
125+
err := preflightClient.(manager.Runnable).Start(gctx)
116126
if err != nil {
117-
logs.Log.Fatalf("failed to start a controller-runtime component: %v", err)
127+
return fmt.Errorf("failed to start a controller-runtime component: %v", err)
118128
}
119129

120130
// The agent must stop if the controller-runtime component stops.
121131
cancel()
122-
}()
132+
return nil
133+
})
123134
}
124135

125136
// To help users notice issues with the agent, we show the error messages in
@@ -130,15 +141,6 @@ func Run(cmd *cobra.Command, args []string) {
130141
}
131142

132143
dataGatherers := map[string]datagatherer.DataGatherer{}
133-
group, gctx := errgroup.WithContext(ctx)
134-
135-
defer func() {
136-
// TODO: replace Fatalf log calls with Errorf and return the error
137-
cancel()
138-
if err := group.Wait(); err != nil {
139-
logs.Log.Fatalf("failed to wait for controller-runtime component to stop: %v", err)
140-
}
141-
}()
142144

143145
// load datagatherer config and boot each one
144146
for _, dgConfig := range config.DataGatherers {
@@ -193,15 +195,24 @@ func Run(cmd *cobra.Command, args []string) {
193195
// begin the datagathering loop, periodically sending data to the
194196
// configured output using data in datagatherer caches or refreshing from
195197
// APIs each cycle depending on datagatherer implementation
196-
for {
197-
gatherAndOutputData(eventf, config, preflightClient, dataGatherers)
198+
group.Go(func() error {
199+
for {
200+
gatherAndOutputData(eventf, config, preflightClient, dataGatherers)
201+
202+
if config.OneShot {
203+
break
204+
}
198205

199-
if config.OneShot {
200-
break
206+
select {
207+
case <-gctx.Done():
208+
return nil
209+
case <-time.After(config.Period):
210+
}
201211
}
212+
return nil
213+
})
202214

203-
time.Sleep(config.Period)
204-
}
215+
<-gctx.Done()
205216
}
206217

207218
// Creates an event recorder for the agent's Pod object. Expects the env var

0 commit comments

Comments
 (0)