@@ -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 )
@@ -213,10 +215,11 @@ func Run(cmd *cobra.Command, args []string) {
213215
214216 select {
215217 case <- gctx .Done ():
216- return
218+ return nil
217219 case <- time .After (config .Period ):
218220 }
219221 }
222+ return nil
220223}
221224
222225// Creates an event recorder for the agent's Pod object. Expects the env var
@@ -226,7 +229,7 @@ func Run(cmd *cobra.Command, args []string) {
226229func newEventf (installNS string ) (Eventf , error ) {
227230 restcfg , err := kubeconfig .LoadRESTConfig ("" )
228231 if err != nil {
229- logs . Log . Fatalf ("failed to load kubeconfig: %v" , err )
232+ return nil , fmt . Errorf ("failed to load kubeconfig: %v" , err )
230233 }
231234 scheme := runtime .NewScheme ()
232235 _ = corev1 .AddToScheme (scheme )
@@ -256,31 +259,35 @@ func newEventf(installNS string) (Eventf, error) {
256259// Like Printf but for sending events to the agent's Pod object.
257260type Eventf func (eventType , reason , msg string , args ... interface {})
258261
259- func gatherAndOutputData (eventf Eventf , config CombinedConfig , preflightClient client.Client , dataGatherers map [string ]datagatherer.DataGatherer ) {
262+ func gatherAndOutputData (eventf Eventf , config CombinedConfig , preflightClient client.Client , dataGatherers map [string ]datagatherer.DataGatherer ) error {
260263 var readings []* api.DataReading
261264
262265 if config .InputPath != "" {
263266 logs .Log .Printf ("Reading data from local file: %s" , config .InputPath )
264267 data , err := os .ReadFile (config .InputPath )
265268 if err != nil {
266- logs . Log . Fatalf ("failed to read local data file: %s" , err )
269+ return fmt . Errorf ("failed to read local data file: %s" , err )
267270 }
268271 err = json .Unmarshal (data , & readings )
269272 if err != nil {
270- logs . Log . Fatalf ("failed to unmarshal local data file: %s" , err )
273+ return fmt . Errorf ("failed to unmarshal local data file: %s" , err )
271274 }
272275 } else {
273- readings = gatherData (config , dataGatherers )
276+ var err error
277+ readings , err = gatherData (config , dataGatherers )
278+ if err != nil {
279+ return err
280+ }
274281 }
275282
276283 if config .OutputPath != "" {
277284 data , err := json .MarshalIndent (readings , "" , " " )
278285 if err != nil {
279- logs . Log . Fatal ("failed to marshal JSON" )
286+ return fmt . Errorf ("failed to marshal JSON: %s" , err )
280287 }
281288 err = os .WriteFile (config .OutputPath , data , 0644 )
282289 if err != nil {
283- logs . Log . Fatalf ("failed to output to local file: %s" , err )
290+ return fmt . Errorf ("failed to output to local file: %s" , err )
284291 }
285292 logs .Log .Printf ("Data saved to local file: %s" , config .OutputPath )
286293 } else {
@@ -296,12 +303,13 @@ func gatherAndOutputData(eventf Eventf, config CombinedConfig, preflightClient c
296303 logs .Log .Printf ("retrying in %v after error: %s" , t , err )
297304 })
298305 if err != nil {
299- logs . Log . Fatalf ("Exiting due to fatal error uploading: %v" , err )
306+ return fmt . Errorf ("Exiting due to fatal error uploading: %v" , err )
300307 }
301308 }
309+ return nil
302310}
303311
304- func gatherData (config CombinedConfig , dataGatherers map [string ]datagatherer.DataGatherer ) []* api.DataReading {
312+ func gatherData (config CombinedConfig , dataGatherers map [string ]datagatherer.DataGatherer ) ( []* api.DataReading , error ) {
305313 var readings []* api.DataReading
306314
307315 var dgError * multierror.Error
@@ -340,10 +348,10 @@ func gatherData(config CombinedConfig, dataGatherers map[string]datagatherer.Dat
340348 }
341349
342350 if config .StrictMode && dgError .ErrorOrNil () != nil {
343- logs . Log . Fatalf ("halting datagathering in strict mode due to error: %s" , dgError .ErrorOrNil ())
351+ return nil , fmt . Errorf ("halting datagathering in strict mode due to error: %s" , dgError .ErrorOrNil ())
344352 }
345353
346- return readings
354+ return readings , nil
347355}
348356
349357func postData (config CombinedConfig , preflightClient client.Client , readings []* api.DataReading ) error {
@@ -368,7 +376,7 @@ func postData(config CombinedConfig, preflightClient client.Client, readings []*
368376 if config .OrganizationID == "" {
369377 data , err := json .Marshal (readings )
370378 if err != nil {
371- logs . Log . Fatalf ("Cannot marshal readings: %+v" , err )
379+ return fmt . Errorf ("Cannot marshal readings: %+v" , err )
372380 }
373381
374382 // log and collect metrics about the upload size
0 commit comments