@@ -48,38 +48,40 @@ var Flags AgentCmdFlags
4848const schemaVersion string = "v2.0.0"
4949
5050// Run starts the agent process
51- func Run (cmd * cobra.Command , args []string ) {
51+ func Run (cmd * cobra.Command , args []string ) error {
5252 logs .Log .Printf ("Preflight agent version: %s (%s)" , version .PreflightVersion , version .Commit )
5353 ctx , cancel := context .WithCancel (context .Background ())
5454 defer cancel ()
5555
5656 file , err := os .Open (Flags .ConfigFilePath )
5757 if err != nil {
58- logs . Log . Fatalf ("Failed to load config file for agent from: %s" , Flags .ConfigFilePath )
58+ return fmt . Errorf ("Failed to load config file for agent from: %s" , Flags .ConfigFilePath )
5959 }
6060 defer file .Close ()
6161
6262 b , err := io .ReadAll (file )
6363 if err != nil {
64- logs . Log . Fatalf ("Failed to read config file: %s" , err )
64+ return fmt . Errorf ("Failed to read config file: %s" , err )
6565 }
6666
6767 cfg , err := ParseConfig (b )
6868 if err != nil {
69- logs . Log . Fatalf ("Failed to parse config file: %s" , err )
69+ return fmt . Errorf ("Failed to parse config file: %s" , err )
7070 }
7171
7272 config , preflightClient , err := ValidateAndCombineConfig (logs .Log , cfg , Flags )
7373 if err != nil {
74- logs . Log . Fatalf ("While evaluating configuration: %v" , err )
74+ return fmt . Errorf ("While evaluating configuration: %v" , err )
7575 }
7676
7777 group , gctx := errgroup .WithContext (ctx )
7878 defer func () {
79- // TODO: replace Fatalf log calls with Errorf and return the error
8079 cancel ()
81- if err := group .Wait (); err != nil {
82- logs .Log .Fatalf ("failed to wait for controller-runtime component to stop: %v" , err )
80+ if groupErr := group .Wait (); groupErr != nil {
81+ err = multierror .Append (
82+ err ,
83+ fmt .Errorf ("failed to wait for controller-runtime component to stop: %v" , groupErr ),
84+ )
8385 }
8486 }()
8587
@@ -139,7 +141,7 @@ func Run(cmd *cobra.Command, args []string) {
139141 // the agent pod's events.
140142 eventf , err := newEventf (config .InstallNS )
141143 if err != nil {
142- logs . Log . Fatalf ("failed to create event recorder: %v" , err )
144+ return fmt . Errorf ("failed to create event recorder: %v" , err )
143145 }
144146
145147 dataGatherers := map [string ]datagatherer.DataGatherer {}
@@ -149,12 +151,12 @@ func Run(cmd *cobra.Command, args []string) {
149151 kind := dgConfig .Kind
150152 if dgConfig .DataPath != "" {
151153 kind = "local"
152- logs . Log . Fatalf ("running data gatherer %s of type %s as Local, data-path override present: %s" , dgConfig .Name , dgConfig .Kind , dgConfig .DataPath )
154+ return fmt . Errorf ("running data gatherer %s of type %s as Local, data-path override present: %s" , dgConfig .Name , dgConfig .Kind , dgConfig .DataPath )
153155 }
154156
155157 newDg , err := dgConfig .Config .NewDataGatherer (gctx )
156158 if err != nil {
157- logs . Log . Fatalf ("failed to instantiate %q data gatherer %q: %v" , kind , dgConfig .Name , err )
159+ return fmt . Errorf ("failed to instantiate %q data gatherer %q: %v" , kind , dgConfig .Name , err )
158160 }
159161
160162 logs .Log .Printf ("starting %q datagatherer" , dgConfig .Name )
@@ -205,18 +207,21 @@ func Run(cmd *cobra.Command, args []string) {
205207 // TODO(wallrj): Pass a context to gatherAndOutputData, so that we don't
206208 // have to wait for it to finish before exiting the process.
207209 for {
208- gatherAndOutputData (eventf , config , preflightClient , dataGatherers )
210+ if err := gatherAndOutputData (eventf , config , preflightClient , dataGatherers ); err != nil {
211+ return err
212+ }
209213
210214 if config .OneShot {
211215 break
212216 }
213217
214218 select {
215219 case <- gctx .Done ():
216- return
220+ return nil
217221 case <- time .After (config .Period ):
218222 }
219223 }
224+ return nil
220225}
221226
222227// Creates an event recorder for the agent's Pod object. Expects the env var
@@ -226,7 +231,7 @@ func Run(cmd *cobra.Command, args []string) {
226231func newEventf (installNS string ) (Eventf , error ) {
227232 restcfg , err := kubeconfig .LoadRESTConfig ("" )
228233 if err != nil {
229- logs . Log . Fatalf ("failed to load kubeconfig: %v" , err )
234+ return nil , fmt . Errorf ("failed to load kubeconfig: %v" , err )
230235 }
231236 scheme := runtime .NewScheme ()
232237 _ = corev1 .AddToScheme (scheme )
@@ -256,31 +261,35 @@ func newEventf(installNS string) (Eventf, error) {
256261// Like Printf but for sending events to the agent's Pod object.
257262type Eventf func (eventType , reason , msg string , args ... interface {})
258263
259- func gatherAndOutputData (eventf Eventf , config CombinedConfig , preflightClient client.Client , dataGatherers map [string ]datagatherer.DataGatherer ) {
264+ func gatherAndOutputData (eventf Eventf , config CombinedConfig , preflightClient client.Client , dataGatherers map [string ]datagatherer.DataGatherer ) error {
260265 var readings []* api.DataReading
261266
262267 if config .InputPath != "" {
263268 logs .Log .Printf ("Reading data from local file: %s" , config .InputPath )
264269 data , err := os .ReadFile (config .InputPath )
265270 if err != nil {
266- logs . Log . Fatalf ("failed to read local data file: %s" , err )
271+ return fmt . Errorf ("failed to read local data file: %s" , err )
267272 }
268273 err = json .Unmarshal (data , & readings )
269274 if err != nil {
270- logs . Log . Fatalf ("failed to unmarshal local data file: %s" , err )
275+ return fmt . Errorf ("failed to unmarshal local data file: %s" , err )
271276 }
272277 } else {
273- readings = gatherData (config , dataGatherers )
278+ var err error
279+ readings , err = gatherData (config , dataGatherers )
280+ if err != nil {
281+ return err
282+ }
274283 }
275284
276285 if config .OutputPath != "" {
277286 data , err := json .MarshalIndent (readings , "" , " " )
278287 if err != nil {
279- logs . Log . Fatal ("failed to marshal JSON" )
288+ return fmt . Errorf ("failed to marshal JSON: %s" , err )
280289 }
281290 err = os .WriteFile (config .OutputPath , data , 0644 )
282291 if err != nil {
283- logs . Log . Fatalf ("failed to output to local file: %s" , err )
292+ return fmt . Errorf ("failed to output to local file: %s" , err )
284293 }
285294 logs .Log .Printf ("Data saved to local file: %s" , config .OutputPath )
286295 } else {
@@ -296,12 +305,13 @@ func gatherAndOutputData(eventf Eventf, config CombinedConfig, preflightClient c
296305 logs .Log .Printf ("retrying in %v after error: %s" , t , err )
297306 })
298307 if err != nil {
299- logs . Log . Fatalf ("Exiting due to fatal error uploading: %v" , err )
308+ return fmt . Errorf ("Exiting due to fatal error uploading: %v" , err )
300309 }
301310 }
311+ return nil
302312}
303313
304- func gatherData (config CombinedConfig , dataGatherers map [string ]datagatherer.DataGatherer ) []* api.DataReading {
314+ func gatherData (config CombinedConfig , dataGatherers map [string ]datagatherer.DataGatherer ) ( []* api.DataReading , error ) {
305315 var readings []* api.DataReading
306316
307317 var dgError * multierror.Error
@@ -340,10 +350,10 @@ func gatherData(config CombinedConfig, dataGatherers map[string]datagatherer.Dat
340350 }
341351
342352 if config .StrictMode && dgError .ErrorOrNil () != nil {
343- logs . Log . Fatalf ("halting datagathering in strict mode due to error: %s" , dgError .ErrorOrNil ())
353+ return nil , fmt . Errorf ("halting datagathering in strict mode due to error: %s" , dgError .ErrorOrNil ())
344354 }
345355
346- return readings
356+ return readings , nil
347357}
348358
349359func postData (config CombinedConfig , preflightClient client.Client , readings []* api.DataReading ) error {
@@ -368,7 +378,7 @@ func postData(config CombinedConfig, preflightClient client.Client, readings []*
368378 if config .OrganizationID == "" {
369379 data , err := json .Marshal (readings )
370380 if err != nil {
371- logs . Log . Fatalf ("Cannot marshal readings: %+v" , err )
381+ return fmt . Errorf ("Cannot marshal readings: %+v" , err )
372382 }
373383
374384 // log and collect metrics about the upload size
0 commit comments