@@ -22,6 +22,7 @@ package config
22
22
23
23
import (
24
24
"encoding/json"
25
+ "errors"
25
26
"fmt"
26
27
"io/ioutil"
27
28
"net"
@@ -61,7 +62,6 @@ type OrchestratorConfig struct {
61
62
// BasicConfig holds the basic parameter configurations for the application.
62
63
type BasicConfig struct {
63
64
Logging log.Config `yaml:"logging"`
64
- Verbose bool `yaml:"verbose"`
65
65
Arachne ArachneConfiguration `yaml:"arachne"`
66
66
}
67
67
@@ -70,7 +70,7 @@ type Extended struct {
70
70
Metrics metrics.Opt
71
71
}
72
72
73
- // RemoteStore holds all Remotes
73
+ // RemoteStore holds all Remotes.
74
74
type RemoteStore map [string ]Remote
75
75
76
76
// Remote holds the info for every target to be echoed.
@@ -191,48 +191,50 @@ func Get(cf string, ec *Extended, logger zap.Logger) (*AppConfig, error) {
191
191
192
192
data , err := ioutil .ReadFile (cf )
193
193
if err != nil {
194
- logger .Error ("error reading the configuration file" , zap .String ("file" , cf ), zap .Error (err ))
195
194
return nil , err
196
195
}
197
196
198
197
b , err := unmarshalBasicConfig (data , cf , logger )
198
+ if err != nil {
199
+ return nil , err
200
+ }
201
+
199
202
mc , err := ec .Metrics .UnmarshalConfig (data , cf , logger )
200
203
if err != nil {
201
204
return nil , err
202
205
}
203
206
204
207
cfg := AppConfig {
205
208
Logging : b .Logging ,
206
- Verbose : b .Verbose ,
207
209
PIDPath : b .Arachne .PIDPath ,
208
210
Orchestrator : b .Arachne .Orchestrator ,
209
211
StandaloneTargetConfig : b .Arachne .StandaloneTargetConfig ,
210
212
Metrics : mc ,
211
213
}
212
214
215
+ if ! cfg .Orchestrator .Enabled && cfg .StandaloneTargetConfig == "" {
216
+ return nil , errors .New ("the standalone-mode target configuration file has not been specified" )
217
+ }
218
+
213
219
return & cfg , nil
214
220
}
215
221
216
- // unmarshalBasicConfig fetches the configuration file from local path
222
+ // unmarshalBasicConfig fetches the configuration file from local path.
217
223
func unmarshalBasicConfig (data []byte , fname string , logger zap.Logger ) (* BasicConfig , error ) {
218
224
219
225
cfg := new (BasicConfig )
220
226
if err := yaml .Unmarshal (data , cfg ); err != nil {
221
- logger .Error ("error unmarshaling the configuration file" ,
222
- zap .String ("file" , fname ),
223
- zap .Error (err ))
224
- return nil , err
227
+ return nil , fmt .Errorf ("error unmarshaling the configuration file %s: %v" , fname , err )
225
228
}
226
229
// Validate on the merged config at the end
227
230
if err := validator .Validate (cfg ); err != nil {
228
- logger .Error ("invalid info in configuration file" , zap .String ("file" , fname ), zap .Error (err ))
229
- return nil , err
231
+ return nil , fmt .Errorf ("invalid info in configuration file %s: %v" , fname , err )
230
232
}
231
233
232
234
return cfg , nil
233
235
}
234
236
235
- // FetchRemoteList fetches the configuration file from local path or, remotely, from Arachne Orchestrator
237
+ // FetchRemoteList fetches the configuration file from local path or, remotely, from Arachne Orchestrator.
236
238
func FetchRemoteList (
237
239
gl * Global ,
238
240
maxNumRemoteTargets int ,
@@ -254,7 +256,7 @@ func FetchRemoteList(
254
256
zap .String ("path" , gl .App .StandaloneTargetConfig ))
255
257
256
258
if err := localFileReadable (gl .App .StandaloneTargetConfig ); err != nil {
257
- logger .Fatal ("unable to retrieve local configuration file" ,
259
+ logger .Fatal ("unable to retrieve local target configuration file" ,
258
260
zap .String ("file" , gl .App .StandaloneTargetConfig ),
259
261
zap .Error (err ))
260
262
}
@@ -294,7 +296,7 @@ func FetchRemoteList(
294
296
return err
295
297
}
296
298
297
- // createHTTPClient returns an HTTP client to connect to remote server
299
+ // createHTTPClient returns an HTTP client to connect to remote server.
298
300
func createHTTPClient (timeout time.Duration , disableKeepAlives bool ) * http.Client {
299
301
client := & http.Client {
300
302
Transport : & http.Transport {
@@ -309,7 +311,7 @@ func createHTTPClient(timeout time.Duration, disableKeepAlives bool) *http.Clien
309
311
return client
310
312
}
311
313
312
- // GetHostname returns the hostname
314
+ // GetHostname returns the hostname.
313
315
func GetHostname (logger zap.Logger ) (string , error ) {
314
316
host , err := os .Hostname ()
315
317
if err != nil {
@@ -319,7 +321,7 @@ func GetHostname(logger zap.Logger) (string, error) {
319
321
return host , nil
320
322
}
321
323
322
- // refreshRemoteList checks with Arachne Orchestrator if new a config file should be fetched
324
+ // refreshRemoteList checks with Arachne Orchestrator if new a config file should be fetched.
323
325
func refreshRemoteList (
324
326
gl * Global ,
325
327
remotes RemoteStore ,
@@ -335,8 +337,6 @@ func refreshRemoteList(
335
337
retryTime := gl .RemoteConfig .PollOrchestratorInterval .Failure
336
338
337
339
for {
338
- var err error
339
-
340
340
hostname , _ := GetHostname (logger )
341
341
RESTReq := fmt .Sprintf ("http://%s/%s/%s?hostname=%s" ,
342
342
gl .App .Orchestrator .AddrPort ,
@@ -395,7 +395,7 @@ func fetchRemoteListFromOrchestrator(
395
395
// Build the request
396
396
req , err := http .NewRequest ("GET" , url , nil )
397
397
if err != nil {
398
- logger .Debug ("NewRequest" , zap .Error (err ))
398
+ logger .Warn ("NewRequest" , zap .Error (err ))
399
399
return 0 , nil , err
400
400
}
401
401
resp , err := client .Do (req )
@@ -411,19 +411,14 @@ func fetchRemoteListFromOrchestrator(
411
411
switch resp .StatusCode {
412
412
case http .StatusOK :
413
413
logger .Debug ("HTTP response status code from Orchestrator" )
414
-
415
- if bResp , err = ioutil .ReadAll (resp .Body ); err != nil {
416
- break
417
- }
418
-
414
+ bResp , err = ioutil .ReadAll (resp .Body )
419
415
case http .StatusNotFound :
420
- logger . Error ("HTTP response from Orchestrator: 'Idle mode'" )
416
+ err = fmt . Errorf ("HTTP response from Orchestrator: 'Idle mode'" )
421
417
case http .StatusBadRequest :
422
- logger . Error ("HTTP response from Orchestrator: 'Please specify hostname or DC!'" )
418
+ err = fmt . Errorf ("HTTP response from Orchestrator: 'Please specify hostname or DC!'" )
423
419
case http .StatusInternalServerError :
424
420
logger .Warn ("HTTP response from Orchestrator: Error opening requested configuration file" )
425
421
default :
426
- logger .Error ("unhandled HTTP response from Orchestrator" )
427
422
err = fmt .Errorf ("unhandled HTTP response from Orchestrator" )
428
423
}
429
424
@@ -448,8 +443,7 @@ func readRemoteList(
448
443
c := new (RemoteFileConfig )
449
444
450
445
if err := json .Unmarshal (raw , c ); err != nil {
451
- logger .Error ("configuration file parse error" , zap .Error (err ))
452
- return err
446
+ return fmt .Errorf ("configuration file parse error (%v)" , err )
453
447
}
454
448
455
449
// Populate global variables
@@ -475,15 +469,15 @@ func readRemoteList(
475
469
srcIP , err = network .GetSourceAddr ("ip6" , strings .ToLower (c .Local .SrcAddress ),
476
470
glRC .HostName , glRC .InterfaceName , logger )
477
471
if err != nil {
478
- return fmt .Errorf ("could not retrieve an IPv4 or IPv6 source address" )
472
+ return fmt .Errorf ("could not retrieve an IPv4 or IPv6 source address (%v)" , err )
479
473
}
480
474
}
481
475
glRC .SrcAddress = * srcIP
482
476
logger .Debug ("Arachne agent's source IP address" , zap .Object ("address" , glRC .SrcAddress ))
483
477
484
478
glRC .TargetTCPPort = c .Local .TargetTCPPort
485
479
if glRC .Timeout , err = time .ParseDuration (c .Local .Timeout ); err != nil {
486
- return fmt .Errorf ("failed to parse the timeout" )
480
+ return fmt .Errorf ("failed to parse the timeout (%v)" , err )
487
481
}
488
482
glRC .SrcTCPPortRange [0 ] = c .Local .BaseSrcTCPPort
489
483
if c .Local .NumSrcTCPPorts > maxNumSrcTCPPorts {
@@ -498,18 +492,18 @@ func readRemoteList(
498
492
"source ports [%d-%d]" , glRC .SrcTCPPortRange [0 ], glRC .SrcTCPPortRange [1 ])
499
493
}
500
494
if glRC .BatchInterval , err = time .ParseDuration (c .Local .BatchInterval ); err != nil {
501
- return fmt .Errorf ("failed to parse the batch interval" )
495
+ return fmt .Errorf ("failed to parse the batch interval (%v)" , err )
502
496
}
503
497
if glRC .BatchInterval < minBatchInterval {
504
498
return fmt .Errorf ("the batch cycle interval cannot be shorter than %v" , minBatchInterval )
505
499
}
506
500
if glRC .PollOrchestratorInterval .Success , err =
507
501
time .ParseDuration (c .Local .PollOrchestratorIntervalSuccess ); err != nil {
508
- return fmt .Errorf ("failed to parse the Orchestrator poll interval for success" )
502
+ return fmt .Errorf ("failed to parse the Orchestrator poll interval for success (%v)" , err )
509
503
}
510
504
if glRC .PollOrchestratorInterval .Failure , err =
511
505
time .ParseDuration (c .Local .PollOrchestratorIntervalFailure ); err != nil {
512
- return fmt .Errorf ("failed to parse the Orchestrator poll interval for failure" )
506
+ return fmt .Errorf ("failed to parse the Orchestrator poll interval for failure (%v)" , err )
513
507
}
514
508
515
509
glRC .QoSEnabled = isTrue (c .Local .QoSEnabled )
@@ -537,7 +531,7 @@ func readRemoteList(
537
531
return nil
538
532
}
539
533
540
- // Validate and create map of ipv4 and ipv6 addresses with string as their key
534
+ // Validate and create map of ipv4 and ipv6 addresses with string as their key.
541
535
func walkTargets (grc * RemoteConfig , ts []target , ext bool , remotes RemoteStore , logger zap.Logger ) {
542
536
543
537
for _ , t := range ts {
@@ -563,7 +557,8 @@ func walkTargets(grc *RemoteConfig, ts []target, ext bool, remotes RemoteStore,
563
557
zap .String ("target" , currIP .String ()))
564
558
continue
565
559
}
566
- remotes [currIP .String ()] = Remote {currIP , network .Family (& currIP ), t .HostName , ext }
560
+ remotes [currIP .String ()] = Remote {currIP , network .Family (& currIP ),
561
+ t .HostName , ext }
567
562
}
568
563
}
569
564
0 commit comments