@@ -50,7 +50,7 @@ var Flags AgentCmdFlags
5050const schemaVersion string = "v2.0.0"
5151
5252// Run starts the agent process
53- func Run (cmd * cobra.Command , args []string ) {
53+ func Run (cmd * cobra.Command , args []string ) error {
5454 logs .Log .Printf ("Preflight agent version: %s (%s)" , version .PreflightVersion , version .Commit )
5555 ctx , cancel := context .WithCancel (
5656 klog .NewContext (
@@ -62,31 +62,33 @@ func Run(cmd *cobra.Command, args []string) {
6262
6363 file , err := os .Open (Flags .ConfigFilePath )
6464 if err != nil {
65- logs . Log . Fatalf ("Failed to load config file for agent from: %s" , Flags .ConfigFilePath )
65+ return fmt . Errorf ("Failed to load config file for agent from: %s" , Flags .ConfigFilePath )
6666 }
6767 defer file .Close ()
6868
6969 b , err := io .ReadAll (file )
7070 if err != nil {
71- logs . Log . Fatalf ("Failed to read config file: %s" , err )
71+ return fmt . Errorf ("Failed to read config file: %s" , err )
7272 }
7373
7474 cfg , err := ParseConfig (b )
7575 if err != nil {
76- logs . Log . Fatalf ("Failed to parse config file: %s" , err )
76+ return fmt . Errorf ("Failed to parse config file: %s" , err )
7777 }
7878
7979 config , preflightClient , err := ValidateAndCombineConfig (logs .Log , cfg , Flags )
8080 if err != nil {
81- logs . Log . Fatalf ("While evaluating configuration: %v" , err )
81+ return fmt . Errorf ("While evaluating configuration: %v" , err )
8282 }
8383
8484 group , gctx := errgroup .WithContext (ctx )
8585 defer func () {
86- // TODO: replace Fatalf log calls with Errorf and return the error
8786 cancel ()
88- if err := group .Wait (); err != nil {
89- logs .Log .Fatalf ("failed to wait for controller-runtime component to stop: %v" , err )
87+ if groupErr := group .Wait (); groupErr != nil {
88+ err = multierror .Append (
89+ err ,
90+ fmt .Errorf ("failed to wait for controller-runtime component to stop: %v" , groupErr ),
91+ )
9092 }
9193 }()
9294
@@ -159,7 +161,7 @@ func Run(cmd *cobra.Command, args []string) {
159161 // the agent pod's events.
160162 eventf , err := newEventf (config .InstallNS )
161163 if err != nil {
162- logs . Log . Fatalf ("failed to create event recorder: %v" , err )
164+ return fmt . Errorf ("failed to create event recorder: %v" , err )
163165 }
164166
165167 dataGatherers := map [string ]datagatherer.DataGatherer {}
@@ -169,12 +171,12 @@ func Run(cmd *cobra.Command, args []string) {
169171 kind := dgConfig .Kind
170172 if dgConfig .DataPath != "" {
171173 kind = "local"
172- logs . Log . Fatalf ("running data gatherer %s of type %s as Local, data-path override present: %s" , dgConfig .Name , dgConfig .Kind , dgConfig .DataPath )
174+ return fmt . Errorf ("running data gatherer %s of type %s as Local, data-path override present: %s" , dgConfig .Name , dgConfig .Kind , dgConfig .DataPath )
173175 }
174176
175177 newDg , err := dgConfig .Config .NewDataGatherer (gctx )
176178 if err != nil {
177- logs . Log . Fatalf ("failed to instantiate %q data gatherer %q: %v" , kind , dgConfig .Name , err )
179+ return fmt . Errorf ("failed to instantiate %q data gatherer %q: %v" , kind , dgConfig .Name , err )
178180 }
179181
180182 logs .Log .Printf ("starting %q datagatherer" , dgConfig .Name )
@@ -225,18 +227,21 @@ func Run(cmd *cobra.Command, args []string) {
225227 // TODO(wallrj): Pass a context to gatherAndOutputData, so that we don't
226228 // have to wait for it to finish before exiting the process.
227229 for {
228- gatherAndOutputData (eventf , config , preflightClient , dataGatherers )
230+ if err := gatherAndOutputData (eventf , config , preflightClient , dataGatherers ); err != nil {
231+ return err
232+ }
229233
230234 if config .OneShot {
231235 break
232236 }
233237
234238 select {
235239 case <- gctx .Done ():
236- return
240+ return nil
237241 case <- time .After (config .Period ):
238242 }
239243 }
244+ return nil
240245}
241246
242247// Creates an event recorder for the agent's Pod object. Expects the env var
@@ -246,7 +251,7 @@ func Run(cmd *cobra.Command, args []string) {
246251func newEventf (installNS string ) (Eventf , error ) {
247252 restcfg , err := kubeconfig .LoadRESTConfig ("" )
248253 if err != nil {
249- logs . Log . Fatalf ("failed to load kubeconfig: %v" , err )
254+ return nil , fmt . Errorf ("failed to load kubeconfig: %v" , err )
250255 }
251256 scheme := runtime .NewScheme ()
252257 _ = corev1 .AddToScheme (scheme )
@@ -276,31 +281,35 @@ func newEventf(installNS string) (Eventf, error) {
276281// Like Printf but for sending events to the agent's Pod object.
277282type Eventf func (eventType , reason , msg string , args ... interface {})
278283
279- func gatherAndOutputData (eventf Eventf , config CombinedConfig , preflightClient client.Client , dataGatherers map [string ]datagatherer.DataGatherer ) {
284+ func gatherAndOutputData (eventf Eventf , config CombinedConfig , preflightClient client.Client , dataGatherers map [string ]datagatherer.DataGatherer ) error {
280285 var readings []* api.DataReading
281286
282287 if config .InputPath != "" {
283288 logs .Log .Printf ("Reading data from local file: %s" , config .InputPath )
284289 data , err := os .ReadFile (config .InputPath )
285290 if err != nil {
286- logs . Log . Fatalf ("failed to read local data file: %s" , err )
291+ return fmt . Errorf ("failed to read local data file: %s" , err )
287292 }
288293 err = json .Unmarshal (data , & readings )
289294 if err != nil {
290- logs . Log . Fatalf ("failed to unmarshal local data file: %s" , err )
295+ return fmt . Errorf ("failed to unmarshal local data file: %s" , err )
291296 }
292297 } else {
293- readings = gatherData (config , dataGatherers )
298+ var err error
299+ readings , err = gatherData (config , dataGatherers )
300+ if err != nil {
301+ return err
302+ }
294303 }
295304
296305 if config .OutputPath != "" {
297306 data , err := json .MarshalIndent (readings , "" , " " )
298307 if err != nil {
299- logs . Log . Fatal ("failed to marshal JSON" )
308+ return fmt . Errorf ("failed to marshal JSON: %s" , err )
300309 }
301310 err = os .WriteFile (config .OutputPath , data , 0644 )
302311 if err != nil {
303- logs . Log . Fatalf ("failed to output to local file: %s" , err )
312+ return fmt . Errorf ("failed to output to local file: %s" , err )
304313 }
305314 logs .Log .Printf ("Data saved to local file: %s" , config .OutputPath )
306315 } else {
@@ -316,12 +325,13 @@ func gatherAndOutputData(eventf Eventf, config CombinedConfig, preflightClient c
316325 logs .Log .Printf ("retrying in %v after error: %s" , t , err )
317326 })
318327 if err != nil {
319- logs . Log . Fatalf ("Exiting due to fatal error uploading: %v" , err )
328+ return fmt . Errorf ("Exiting due to fatal error uploading: %v" , err )
320329 }
321330 }
331+ return nil
322332}
323333
324- func gatherData (config CombinedConfig , dataGatherers map [string ]datagatherer.DataGatherer ) []* api.DataReading {
334+ func gatherData (config CombinedConfig , dataGatherers map [string ]datagatherer.DataGatherer ) ( []* api.DataReading , error ) {
325335 var readings []* api.DataReading
326336
327337 var dgError * multierror.Error
@@ -360,10 +370,10 @@ func gatherData(config CombinedConfig, dataGatherers map[string]datagatherer.Dat
360370 }
361371
362372 if config .StrictMode && dgError .ErrorOrNil () != nil {
363- logs . Log . Fatalf ("halting datagathering in strict mode due to error: %s" , dgError .ErrorOrNil ())
373+ return nil , fmt . Errorf ("halting datagathering in strict mode due to error: %s" , dgError .ErrorOrNil ())
364374 }
365375
366- return readings
376+ return readings , nil
367377}
368378
369379func postData (config CombinedConfig , preflightClient client.Client , readings []* api.DataReading ) error {
@@ -388,7 +398,7 @@ func postData(config CombinedConfig, preflightClient client.Client, readings []*
388398 if config .OrganizationID == "" {
389399 data , err := json .Marshal (readings )
390400 if err != nil {
391- logs . Log . Fatalf ("Cannot marshal readings: %+v" , err )
401+ return fmt . Errorf ("Cannot marshal readings: %+v" , err )
392402 }
393403
394404 // log and collect metrics about the upload size
0 commit comments