fix: Refactor ec2 init for config-api (#9576)

This commit is contained in:
Sebastian Spaink 2021-08-09 11:13:20 -05:00 committed by GitHub
parent e6abb46d87
commit dbc4e269be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 19 deletions

View File

@ -124,6 +124,20 @@ func (r *AwsEc2Processor) Init() error {
return errors.New("no tags specified in configuration")
}
for _, tag := range r.ImdsTags {
if len(tag) == 0 || !isImdsTagAllowed(tag) {
return fmt.Errorf("not allowed metadata tag specified in configuration: %s", tag)
}
r.imdsTags[tag] = struct{}{}
}
if len(r.imdsTags) == 0 && len(r.EC2Tags) == 0 {
return errors.New("no allowed metadata tags specified in configuration")
}
return nil
}
func (r *AwsEc2Processor) Start(acc telegraf.Accumulator) error {
ctx := context.Background()
cfg, err := awsconfig.LoadDefaultConfig(ctx)
if err != nil {
@ -161,21 +175,6 @@ func (r *AwsEc2Processor) Init() error {
}
}
for _, tag := range r.ImdsTags {
if len(tag) > 0 && isImdsTagAllowed(tag) {
r.imdsTags[tag] = struct{}{}
} else {
return fmt.Errorf("not allowed metadata tag specified in configuration: %s", tag)
}
}
if len(r.imdsTags) == 0 && len(r.EC2Tags) == 0 {
return errors.New("no allowed metadata tags specified in configuration")
}
return nil
}
func (r *AwsEc2Processor) Start(acc telegraf.Accumulator) error {
if r.Ordered {
r.parallel = parallel.NewOrdered(acc, r.asyncAdd, DefaultMaxOrderedQueueSize, r.MaxParallelCalls)
} else {

View File

@ -13,8 +13,7 @@ func TestBasicStartup(t *testing.T) {
p.Log = &testutil.Logger{}
p.ImdsTags = []string{"accountId", "instanceId"}
acc := &testutil.Accumulator{}
require.NoError(t, p.Start(acc))
require.NoError(t, p.Stop())
require.NoError(t, p.Init())
require.Len(t, acc.GetTelegrafMetrics(), 0)
require.Len(t, acc.Errors, 0)
@ -26,8 +25,7 @@ func TestBasicStartupWithEC2Tags(t *testing.T) {
p.ImdsTags = []string{"accountId", "instanceId"}
p.EC2Tags = []string{"Name"}
acc := &testutil.Accumulator{}
require.NoError(t, p.Start(acc))
require.NoError(t, p.Stop())
require.NoError(t, p.Init())
require.Len(t, acc.GetTelegrafMetrics(), 0)
require.Len(t, acc.Errors, 0)