Skip to content

Commit 48d67af

Browse files
authored
[autodiscovery] Update Config File Reader to Support Discovery Probe (#52087)
### What does this PR do? Allow the config file reader to process configs with only a Discovery attribute defined. This is for a new feature that will not be released yet. Note, we also don't want to report the integration.Config to the backend because it still needs to be fully discovered at runtime in Autodiscovery using the discovery probes that will be implemented. Currently the raw file provided config looks like: ``` ad_identifiers: - krakend discovery: {} ``` but with the Discover feature it will later be converted into something that is actually scheduled like: ``` ad_identifiers: - krakend discovery: {} instances: - openmetrics_url: localhost:1312/telemetry ``` ### Describe how you validated your changes Unit Tests and CI Co-authored-by: gabe.dossantos <gabe.dossantos@datadoghq.com>
1 parent 478d59a commit 48d67af

6 files changed

Lines changed: 41 additions & 7 deletions

File tree

comp/core/autodiscovery/configresolver/configresolver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func Resolve(tpl integration.Config, svc listeners.Service) (integration.Config,
4848
MetricsExcluded: svc.HasFilter(filter.MetricsFilter),
4949
LogsExcluded: svc.HasFilter(filter.LogsFilter),
5050
ImageName: svc.GetImageName(),
51+
Discovery: tpl.Discovery,
5152
}
5253
copy(resolvedConfig.InitConfig, tpl.InitConfig)
5354
copy(resolvedConfig.Instances, tpl.Instances)

comp/core/autodiscovery/configresolver/configresolver_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,26 @@ func TestResolve(t *testing.T) {
907907
ServiceID: "a5901276aed1",
908908
},
909909
},
910+
{
911+
testName: "discovery marker is preserved through resolution",
912+
svc: &dummyService{
913+
ID: "a5901276aed1",
914+
ADIdentifiers: []string{"redis"},
915+
Hosts: map[string]string{"bridge": "127.0.0.1"},
916+
},
917+
tpl: integration.Config{
918+
Name: "redis",
919+
ADIdentifiers: []string{"redis"},
920+
Discovery: &integration.DiscoveryConfig{},
921+
},
922+
out: integration.Config{
923+
Name: "redis",
924+
ADIdentifiers: []string{"redis"},
925+
Instances: []integration.Data{},
926+
Discovery: &integration.DiscoveryConfig{},
927+
ServiceID: "a5901276aed1",
928+
},
929+
},
910930
}
911931

912932
for i, tc := range testCases {

comp/core/autodiscovery/providers/config_reader.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type ConfigFormatWrapper struct {
5151
ConfigFormat string
5252
Filename string
5353
Hash string
54+
IsDiscovery bool // Marks this config as a discovery template (instances will be populated at runtime later)
5455
}
5556

5657
type configPkg struct {
@@ -445,9 +446,9 @@ func GetIntegrationConfigFromFile(name, fpath string) (integration.Config, Confi
445446
return conf, ConfigFormatWrapper{}, err
446447
}
447448

448-
// If no valid instances were found & this is neither a metrics file, nor a logs file
449-
// this is not a valid configuration file
450-
if cf.MetricConfig == nil && cf.LogsConfig == nil && len(cf.Instances) < 1 {
449+
// If no valid instances were found & this is neither a metrics file, a logs file,
450+
// nor a discovery-only template, this is not a valid configuration file
451+
if cf.MetricConfig == nil && cf.LogsConfig == nil && cf.Discovery == nil && len(cf.Instances) < 1 {
451452
return conf, ConfigFormatWrapper{}, errors.New("Configuration file contains no valid instances")
452453
}
453454

@@ -523,7 +524,7 @@ func GetIntegrationConfigFromFile(name, fpath string) (integration.Config, Confi
523524
conf.Source = "file:" + fpath
524525
hash := sha256.Sum256([]byte(scrubbedConfigFormat))
525526

526-
return conf, ConfigFormatWrapper{ConfigFormat: scrubbedConfigFormat, Filename: fpath, Hash: hex.EncodeToString(hash[:])}, err
527+
return conf, ConfigFormatWrapper{ConfigFormat: scrubbedConfigFormat, Filename: fpath, Hash: hex.EncodeToString(hash[:]), IsDiscovery: cf.Discovery != nil}, err
527528
}
528529

529530
func containsString(slice []string, str string) bool {

comp/core/autodiscovery/providers/config_reader_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ func TestGetIntegrationConfig(t *testing.T) {
129129
config, _, err = GetIntegrationConfigFromFile("foo", "tests/testcheck.yaml")
130130
require.Nil(t, err)
131131
assert.Nil(t, config.Discovery)
132+
133+
// discovery-only: a file with only `discovery: {}` is valid and has no instances.
134+
config, _, err = GetIntegrationConfigFromFile("foo", "tests/discovery_only.yaml")
135+
require.Nil(t, err)
136+
require.NotNil(t, config.Discovery, "discovery-only file should produce a non-nil Discovery field")
137+
assert.Empty(t, config.Instances)
138+
assert.Nil(t, config.MetricConfig)
139+
assert.Nil(t, config.LogsConfig)
132140
}
133141

134142
func TestReadConfigFiles(t *testing.T) {
@@ -137,7 +145,7 @@ func TestReadConfigFiles(t *testing.T) {
137145

138146
configs, errors, err := ReadConfigFiles(GetAll)
139147
require.Nil(t, err)
140-
require.Equal(t, 22, len(configs))
148+
require.Equal(t, 23, len(configs))
141149
require.Equal(t, 4, len(errors))
142150

143151
for _, c := range configs {
@@ -148,7 +156,7 @@ func TestReadConfigFiles(t *testing.T) {
148156

149157
configs, _, err = ReadConfigFiles(WithoutAdvancedAD)
150158
require.Nil(t, err)
151-
require.Equal(t, 20, len(configs))
159+
require.Equal(t, 21, len(configs))
152160

153161
expectedConfig1 := integration.Config{
154162
Name: "advanced_ad",

comp/core/autodiscovery/providers/file_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func TestCollect(t *testing.T) {
8282
assert.Equal(t, 0, len(get("ignored")))
8383

8484
// total number of configurations found
85-
assert.Equal(t, 19, len(configs))
85+
assert.Equal(t, 20, len(configs))
8686

8787
// incorrect configs get saved in the Errors map (invalid.yaml & notaconfig.yaml & ad_deprecated.yaml & null_instances.yml)
8888
assert.Equal(t, 4, len(provider.Errors))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ad_identifiers:
2+
- discovery_only_id
3+
4+
discovery: {}

0 commit comments

Comments
 (0)