@@ -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 {
@@ -200,7 +202,11 @@ func Run(cmd *cobra.Command, args []string) {
200202 break
201203 }
202204
203- time .Sleep (config .Period )
205+ select {
206+ case <- gctx .Done ():
207+ return
208+ case <- time .After (config .Period ):
209+ }
204210 }
205211}
206212
0 commit comments