fix: Refactor ec2 init for config-api (#9576)
This commit is contained in:
parent
e6abb46d87
commit
dbc4e269be
|
|
@ -124,6 +124,20 @@ func (r *AwsEc2Processor) Init() error {
|
||||||
return errors.New("no tags specified in configuration")
|
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()
|
ctx := context.Background()
|
||||||
cfg, err := awsconfig.LoadDefaultConfig(ctx)
|
cfg, err := awsconfig.LoadDefaultConfig(ctx)
|
||||||
if err != nil {
|
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 {
|
if r.Ordered {
|
||||||
r.parallel = parallel.NewOrdered(acc, r.asyncAdd, DefaultMaxOrderedQueueSize, r.MaxParallelCalls)
|
r.parallel = parallel.NewOrdered(acc, r.asyncAdd, DefaultMaxOrderedQueueSize, r.MaxParallelCalls)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,7 @@ func TestBasicStartup(t *testing.T) {
|
||||||
p.Log = &testutil.Logger{}
|
p.Log = &testutil.Logger{}
|
||||||
p.ImdsTags = []string{"accountId", "instanceId"}
|
p.ImdsTags = []string{"accountId", "instanceId"}
|
||||||
acc := &testutil.Accumulator{}
|
acc := &testutil.Accumulator{}
|
||||||
require.NoError(t, p.Start(acc))
|
require.NoError(t, p.Init())
|
||||||
require.NoError(t, p.Stop())
|
|
||||||
|
|
||||||
require.Len(t, acc.GetTelegrafMetrics(), 0)
|
require.Len(t, acc.GetTelegrafMetrics(), 0)
|
||||||
require.Len(t, acc.Errors, 0)
|
require.Len(t, acc.Errors, 0)
|
||||||
|
|
@ -26,8 +25,7 @@ func TestBasicStartupWithEC2Tags(t *testing.T) {
|
||||||
p.ImdsTags = []string{"accountId", "instanceId"}
|
p.ImdsTags = []string{"accountId", "instanceId"}
|
||||||
p.EC2Tags = []string{"Name"}
|
p.EC2Tags = []string{"Name"}
|
||||||
acc := &testutil.Accumulator{}
|
acc := &testutil.Accumulator{}
|
||||||
require.NoError(t, p.Start(acc))
|
require.NoError(t, p.Init())
|
||||||
require.NoError(t, p.Stop())
|
|
||||||
|
|
||||||
require.Len(t, acc.GetTelegrafMetrics(), 0)
|
require.Len(t, acc.GetTelegrafMetrics(), 0)
|
||||||
require.Len(t, acc.Errors, 0)
|
require.Len(t, acc.Errors, 0)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue