@@ -50,7 +50,7 @@ var Flags AgentCmdFlags
50
50
const schemaVersion string = "v2.0.0"
51
51
52
52
// Run starts the agent process
53
- func Run (cmd * cobra.Command , args []string ) {
53
+ func Run (cmd * cobra.Command , args []string ) error {
54
54
logs .Log .Printf ("Preflight agent version: %s (%s)" , version .PreflightVersion , version .Commit )
55
55
ctx , cancel := context .WithCancel (
56
56
klog .NewContext (
@@ -62,31 +62,33 @@ func Run(cmd *cobra.Command, args []string) {
62
62
63
63
file , err := os .Open (Flags .ConfigFilePath )
64
64
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 )
66
66
}
67
67
defer file .Close ()
68
68
69
69
b , err := io .ReadAll (file )
70
70
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 )
72
72
}
73
73
74
74
cfg , err := ParseConfig (b )
75
75
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 )
77
77
}
78
78
79
79
config , preflightClient , err := ValidateAndCombineConfig (logs .Log , cfg , Flags )
80
80
if err != nil {
81
- logs . Log . Fatalf ("While evaluating configuration: %v" , err )
81
+ return fmt . Errorf ("While evaluating configuration: %v" , err )
82
82
}
83
83
84
84
group , gctx := errgroup .WithContext (ctx )
85
85
defer func () {
86
- // TODO: replace Fatalf log calls with Errorf and return the error
87
86
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
+ )
90
92
}
91
93
}()
92
94
@@ -159,7 +161,7 @@ func Run(cmd *cobra.Command, args []string) {
159
161
// the agent pod's events.
160
162
eventf , err := newEventf (config .InstallNS )
161
163
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 )
163
165
}
164
166
165
167
dataGatherers := map [string ]datagatherer.DataGatherer {}
@@ -169,12 +171,12 @@ func Run(cmd *cobra.Command, args []string) {
169
171
kind := dgConfig .Kind
170
172
if dgConfig .DataPath != "" {
171
173
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 )
173
175
}
174
176
175
177
newDg , err := dgConfig .Config .NewDataGatherer (gctx )
176
178
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 )
178
180
}
179
181
180
182
logs .Log .Printf ("starting %q datagatherer" , dgConfig .Name )
@@ -225,18 +227,21 @@ func Run(cmd *cobra.Command, args []string) {
225
227
// TODO(wallrj): Pass a context to gatherAndOutputData, so that we don't
226
228
// have to wait for it to finish before exiting the process.
227
229
for {
228
- gatherAndOutputData (eventf , config , preflightClient , dataGatherers )
230
+ if err := gatherAndOutputData (eventf , config , preflightClient , dataGatherers ); err != nil {
231
+ return err
232
+ }
229
233
230
234
if config .OneShot {
231
235
break
232
236
}
233
237
234
238
select {
235
239
case <- gctx .Done ():
236
- return
240
+ return nil
237
241
case <- time .After (config .Period ):
238
242
}
239
243
}
244
+ return nil
240
245
}
241
246
242
247
// 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) {
246
251
func newEventf (installNS string ) (Eventf , error ) {
247
252
restcfg , err := kubeconfig .LoadRESTConfig ("" )
248
253
if err != nil {
249
- logs . Log . Fatalf ("failed to load kubeconfig: %v" , err )
254
+ return nil , fmt . Errorf ("failed to load kubeconfig: %v" , err )
250
255
}
251
256
scheme := runtime .NewScheme ()
252
257
_ = corev1 .AddToScheme (scheme )
@@ -276,31 +281,35 @@ func newEventf(installNS string) (Eventf, error) {
276
281
// Like Printf but for sending events to the agent's Pod object.
277
282
type Eventf func (eventType , reason , msg string , args ... interface {})
278
283
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 {
280
285
var readings []* api.DataReading
281
286
282
287
if config .InputPath != "" {
283
288
logs .Log .Printf ("Reading data from local file: %s" , config .InputPath )
284
289
data , err := os .ReadFile (config .InputPath )
285
290
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 )
287
292
}
288
293
err = json .Unmarshal (data , & readings )
289
294
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 )
291
296
}
292
297
} else {
293
- readings = gatherData (config , dataGatherers )
298
+ var err error
299
+ readings , err = gatherData (config , dataGatherers )
300
+ if err != nil {
301
+ return err
302
+ }
294
303
}
295
304
296
305
if config .OutputPath != "" {
297
306
data , err := json .MarshalIndent (readings , "" , " " )
298
307
if err != nil {
299
- logs . Log . Fatal ("failed to marshal JSON" )
308
+ return fmt . Errorf ("failed to marshal JSON: %s" , err )
300
309
}
301
310
err = os .WriteFile (config .OutputPath , data , 0644 )
302
311
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 )
304
313
}
305
314
logs .Log .Printf ("Data saved to local file: %s" , config .OutputPath )
306
315
} else {
@@ -316,12 +325,13 @@ func gatherAndOutputData(eventf Eventf, config CombinedConfig, preflightClient c
316
325
logs .Log .Printf ("retrying in %v after error: %s" , t , err )
317
326
})
318
327
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 )
320
329
}
321
330
}
331
+ return nil
322
332
}
323
333
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 ) {
325
335
var readings []* api.DataReading
326
336
327
337
var dgError * multierror.Error
@@ -360,10 +370,10 @@ func gatherData(config CombinedConfig, dataGatherers map[string]datagatherer.Dat
360
370
}
361
371
362
372
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 ())
364
374
}
365
375
366
- return readings
376
+ return readings , nil
367
377
}
368
378
369
379
func postData (config CombinedConfig , preflightClient client.Client , readings []* api.DataReading ) error {
@@ -388,7 +398,7 @@ func postData(config CombinedConfig, preflightClient client.Client, readings []*
388
398
if config .OrganizationID == "" {
389
399
data , err := json .Marshal (readings )
390
400
if err != nil {
391
- logs . Log . Fatalf ("Cannot marshal readings: %+v" , err )
401
+ return fmt . Errorf ("Cannot marshal readings: %+v" , err )
392
402
}
393
403
394
404
// log and collect metrics about the upload size
0 commit comments