@@ -68,6 +68,34 @@ type PluginConfig struct {
68
68
Config map [string ]any `yaml:",inline"` // to keep the plugin-specific config
69
69
}
70
70
71
+ // UnmarshalYAML implements yaml.Unmarshaler.
72
+ func (pc * PluginConfig ) UnmarshalYAML (unmarshal func (any ) error ) error {
73
+ type raw PluginConfig
74
+ aux := raw {}
75
+
76
+ if err := unmarshal (& aux ); err != nil {
77
+ return err
78
+ }
79
+
80
+ if aux .Type == "" {
81
+ return errors .New ("missing required field 'type'" )
82
+ }
83
+
84
+ if aux .MaxRetry == 0 {
85
+ aux .MaxRetry = 1
86
+ }
87
+
88
+ if aux .TimeOut == 0 {
89
+ aux .TimeOut = time .Second * 5
90
+ }
91
+
92
+ * pc = PluginConfig (aux )
93
+ return nil
94
+ }
95
+
96
+ type PluginConfigList []PluginConfig
97
+
98
+
71
99
func (pb * PluginBroker ) Init (ctx context.Context , pluginCfg * csconfig.PluginCfg , profileConfigs []* csconfig.ProfileCfg , configPaths * csconfig.ConfigurationPaths ) error {
72
100
pb .PluginChannel = make (chan models.ProfileAlert )
73
101
pb .notificationConfigsByPluginType = make (map [string ][][]byte )
@@ -195,14 +223,17 @@ func (pb *PluginBroker) loadConfig(path string) error {
195
223
continue
196
224
}
197
225
198
- pluginConfigs , err := ParsePluginConfigFile (configFilePath )
226
+ fin , err := os . Open (configFilePath )
199
227
if err != nil {
200
- return err
228
+ return fmt . Errorf ( "while opening %s: %w" , configFilePath , err )
201
229
}
202
230
203
- for _ , pluginConfig := range pluginConfigs {
204
- SetRequiredFields (& pluginConfig )
231
+ pluginConfigs , err := NewPluginConfigList (fin )
232
+ if err != nil {
233
+ return fmt .Errorf ("error in %s: %w" , configFilePath , err )
234
+ }
205
235
236
+ for _ , pluginConfig := range pluginConfigs {
206
237
if _ , ok := pb .pluginConfigByName [pluginConfig .Name ]; ok {
207
238
log .Warningf ("notification '%s' is defined multiple times" , pluginConfig .Name )
208
239
}
@@ -392,15 +423,10 @@ func (pb *PluginBroker) pushNotificationsToPlugin(ctx context.Context, pluginNam
392
423
return err
393
424
}
394
425
395
- func ParsePluginConfigFile (path string ) ([]PluginConfig , error ) {
396
- parsedConfigs := make ([]PluginConfig , 0 )
397
-
398
- yamlFile , err := os .Open (path )
399
- if err != nil {
400
- return nil , fmt .Errorf ("while opening %s: %w" , path , err )
401
- }
426
+ func NewPluginConfigList (fin io.Reader ) (PluginConfigList , error ) {
427
+ parsedConfigs := make (PluginConfigList , 0 )
402
428
403
- dec := yaml .NewDecoder (yamlFile )
429
+ dec := yaml .NewDecoder (fin )
404
430
dec .SetStrict (true )
405
431
406
432
idx := - 1
@@ -410,40 +436,26 @@ func ParsePluginConfigFile(path string) ([]PluginConfig, error) {
410
436
411
437
idx += 1
412
438
413
- err = dec .Decode (& pc )
439
+ err : = dec .Decode (& pc )
414
440
if err != nil {
415
441
if errors .Is (err , io .EOF ) {
416
442
break
417
443
}
418
444
419
- return nil , fmt .Errorf ("while decoding %s got error %s " , path , err )
445
+ return nil , fmt .Errorf ("document %d: %w " , idx , err )
420
446
}
421
447
422
448
// if the yaml document is empty, skip
423
449
if reflect .DeepEqual (pc , PluginConfig {}) {
424
450
continue
425
451
}
426
452
427
- if pc .Type == "" {
428
- return nil , fmt .Errorf ("field 'type' missing in %s (position %d)" , path , idx )
429
- }
430
-
431
453
parsedConfigs = append (parsedConfigs , pc )
432
454
}
433
455
434
456
return parsedConfigs , nil
435
457
}
436
458
437
- func SetRequiredFields (pluginCfg * PluginConfig ) {
438
- if pluginCfg .MaxRetry == 0 {
439
- pluginCfg .MaxRetry ++
440
- }
441
-
442
- if pluginCfg .TimeOut == time .Second * 0 {
443
- pluginCfg .TimeOut = time .Second * 5
444
- }
445
- }
446
-
447
459
func getUUID () (string , error ) {
448
460
uuidv4 , err := uuid .NewRandom ()
449
461
if err != nil {
0 commit comments