Skip to content

Commit

Permalink
add_cloud_metadata: env var override for providers
Browse files Browse the repository at this point in the history
Add support for configuring the add_cloud_metadata providers
with an environment variable, $BEATS_ADD_CLOUD_METADATA_PROVIDERS.

This may be useful when deploying Elastic Agent standalone in a
cloud provider managed Kubernetes cluster, where the cloud provider
is known at deployment time.
  • Loading branch information
axw committed Mar 28, 2024
1 parent f502623 commit fa753f5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ cloud or on-premise).
The second optional setting is `providers`. The `providers` settings accepts a
list of cloud provider names to be used. If `providers` is not configured, then
all providers that do not access a remote endpoint are enabled by default.
The list of providers may alternatively be configured with the environment
variable `BEATS_ADD_CLOUD_METADATA_PROVIDERS`, by setting it to a comma-separated
list of provider names.

List of names the `providers` setting supports:

Expand Down
17 changes: 17 additions & 0 deletions libbeat/processors/add_cloud_metadata/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"fmt"
"net"
"net/http"
"os"
"strings"
"time"

conf "github.com/elastic/elastic-agent-libs/config"
Expand Down Expand Up @@ -73,6 +75,21 @@ func selectProviders(configList providerList, providers map[string]provider) map
}

func providersFilter(configList providerList, allProviders map[string]provider) func(string) bool {
if v, ok := os.LookupEnv("BEATS_ADD_CLOUD_METADATA_PROVIDERS"); ok {
// We allow users to override the config and defaults with
// this environment variable as a workaround in case the
// configured/default providers misbehave.
configList = nil
for _, name := range strings.Split(v, ",") {
configList = append(configList, strings.TrimSpace(name))
}
if len(configList) == 0 {
// User explicitly disabled all providers.
return func(string) bool {
return false
}
}
}
if len(configList) == 0 {
return func(name string) bool {
ff, ok := allProviders[name]
Expand Down
26 changes: 26 additions & 0 deletions libbeat/processors/add_cloud_metadata/providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package add_cloud_metadata

import (
"os"
"sort"
"testing"

Expand All @@ -26,6 +27,10 @@ import (
conf "github.com/elastic/elastic-agent-libs/config"
)

func init() {
os.Unsetenv("BEATS_ADD_CLOUD_METADATA_PROVIDERS")
}

func TestProvidersFilter(t *testing.T) {
var all []string

Check failure on line 35 in libbeat/processors/add_cloud_metadata/providers_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

Consider pre-allocating `all` (prealloc)

Check failure on line 35 in libbeat/processors/add_cloud_metadata/providers_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

Consider pre-allocating `all` (prealloc)
var allLocal []string
Expand All @@ -38,13 +43,24 @@ func TestProvidersFilter(t *testing.T) {

cases := map[string]struct {
config map[string]interface{}
env string
fail bool
expected []string
}{
"all with local access only if not configured": {
config: map[string]interface{}{},
expected: allLocal,
},
"BEATS_ADD_CLOUD_METADATA_PROVIDERS overrides default": {
config: map[string]interface{}{},
env: "alibaba, digitalocean",
expected: []string{"alibaba", "digitalocean"},
},
"none if BEATS_ADD_CLOUD_METADATA_PROVIDERS is explicitly set to an empty list": {
config: map[string]interface{}{},
env: " ",
expected: nil,
},
"fail to load if unknown name is used": {
config: map[string]interface{}{
"providers": []string{"unknown"},
Expand All @@ -56,6 +72,13 @@ func TestProvidersFilter(t *testing.T) {
"providers": []string{"aws", "gcp", "digitalocean"},
},
},
"BEATS_ADD_CLOUD_METADATA_PROVIDERS overrides selected": {
config: map[string]interface{}{
"providers": []string{"aws", "gcp", "digitalocean"},
},
env: "alibaba, digitalocean",
expected: []string{"alibaba", "digitalocean"},
},
}

copyStrings := func(in []string) (out []string) {
Expand All @@ -68,6 +91,9 @@ func TestProvidersFilter(t *testing.T) {
for name, test := range cases {
t.Run(name, func(t *testing.T) {
rawConfig := conf.MustNewConfigFrom(test.config)
if test.env != "" {
t.Setenv("BEATS_ADD_CLOUD_METADATA_PROVIDERS", test.env)
}

config := defaultConfig()
err := rawConfig.Unpack(&config)
Expand Down

0 comments on commit fa753f5

Please sign in to comment.