Merge pull request #18581 from matt-gp/aws-sd-set-directory

Fix(discovery/aws): Added SetDirectory method to AWS SD
This commit is contained in:
Joe Adams 2026-04-26 12:09:03 -04:00 committed by GitHub
commit c540c34ad9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 141 additions and 0 deletions

View File

@ -342,6 +342,36 @@ func (c *SDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery.Di
}
}
// SetDirectory joins any relative file paths with dir.
func (c *SDConfig) SetDirectory(dir string) {
switch c.Role {
case RoleEC2:
if c.EC2SDConfig != nil {
c.EC2SDConfig.SetDirectory(dir)
}
case RoleECS:
if c.ECSSDConfig != nil {
c.ECSSDConfig.SetDirectory(dir)
}
case RoleElasticache:
if c.ElasticacheSDConfig != nil {
c.ElasticacheSDConfig.SetDirectory(dir)
}
case RoleLightsail:
if c.LightsailSDConfig != nil {
c.LightsailSDConfig.SetDirectory(dir)
}
case RoleMSK:
if c.MSKSDConfig != nil {
c.MSKSDConfig.SetDirectory(dir)
}
case RoleRDS:
if c.RDSSDConfig != nil {
c.RDSSDConfig.SetDirectory(dir)
}
}
}
// loadRegion finds the region in order: AWS config/env vars ->IMDS.
func loadRegion(ctx context.Context, specifiedRegion string) (string, error) {
if specifiedRegion != "" {

View File

@ -487,3 +487,89 @@ region = ` + randomRegion + `
require.Contains(t, err.Error(), "failed to get region from IMDS")
})
}
func TestSDConfigSetDirectory(t *testing.T) {
tmpDir := t.TempDir()
tests := []struct {
name string
yaml string
role Role
}{
{
name: "EC2",
yaml: `
role: ec2
region: us-east-1
`,
role: RoleEC2,
},
{
name: "ECS",
yaml: `
role: ecs
region: us-west-2
clusters: [test-cluster]
`,
role: RoleECS,
},
{
name: "Elasticache",
yaml: `
role: elasticache
region: eu-west-1
`,
role: RoleElasticache,
},
{
name: "Lightsail",
yaml: `
role: lightsail
region: ap-south-1
`,
role: RoleLightsail,
},
{
name: "MSK",
yaml: `
role: msk
region: us-east-2
`,
role: RoleMSK,
},
{
name: "RDS",
yaml: `
role: rds
region: us-west-1
`,
role: RoleRDS,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var cfg SDConfig
err := yaml.Unmarshal([]byte(tt.yaml), &cfg)
require.NoError(t, err)
require.Equal(t, tt.role, cfg.Role)
// Call SetDirectory - should not panic
require.NotPanics(t, func() {
cfg.SetDirectory(tmpDir)
})
})
}
t.Run("SetDirectoryWithNilConfigs", func(t *testing.T) {
// Test that SetDirectory doesn't panic when called on an SDConfig
// where the role-specific config might be nil (this was the original bug)
cfg := SDConfig{
Role: RoleEC2,
// EC2SDConfig is nil - this would have caused a panic before the fix
}
// This should not panic
require.NotPanics(t, func() {
cfg.SetDirectory(tmpDir)
})
})
}

View File

@ -129,6 +129,11 @@ func (c *ECSSDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery
return NewECSDiscovery(c, opts)
}
// SetDirectory joins any relative file paths with dir.
func (c *ECSSDConfig) SetDirectory(dir string) {
c.HTTPClientConfig.SetDirectory(dir)
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for the ECS Config.
func (c *ECSSDConfig) UnmarshalYAML(unmarshal func(any) error) error {
*c = DefaultECSSDConfig

View File

@ -217,6 +217,11 @@ func (c *ElasticacheSDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (d
return NewElasticacheDiscovery(c, opts)
}
// SetDirectory joins any relative file paths with dir.
func (c *ElasticacheSDConfig) SetDirectory(dir string) {
c.HTTPClientConfig.SetDirectory(dir)
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for the Elasticache Config.
func (c *ElasticacheSDConfig) UnmarshalYAML(unmarshal func(any) error) error {
*c = DefaultElasticacheSDConfig

View File

@ -97,6 +97,11 @@ func (c *LightsailSDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (dis
return NewLightsailDiscovery(c, opts)
}
// SetDirectory joins any relative file paths with dir.
func (c *LightsailSDConfig) SetDirectory(dir string) {
c.HTTPClientConfig.SetDirectory(dir)
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for the Lightsail Config.
func (c *LightsailSDConfig) UnmarshalYAML(unmarshal func(any) error) error {
*c = DefaultLightsailSDConfig

View File

@ -129,6 +129,11 @@ func (c *MSKSDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery
return NewMSKDiscovery(c, opts)
}
// SetDirectory joins any relative file paths with dir.
func (c *MSKSDConfig) SetDirectory(dir string) {
c.HTTPClientConfig.SetDirectory(dir)
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for the MSK Config.
func (c *MSKSDConfig) UnmarshalYAML(unmarshal func(any) error) error {
*c = DefaultMSKSDConfig

View File

@ -248,6 +248,11 @@ func (c *RDSSDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery
return NewRDSDiscovery(c, opts)
}
// SetDirectory joins any relative file paths with dir.
func (c *RDSSDConfig) SetDirectory(dir string) {
c.HTTPClientConfig.SetDirectory(dir)
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for the RDS Config.
func (c *RDSSDConfig) UnmarshalYAML(unmarshal func(any) error) error {
*c = DefaultRDSSDConfig