Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce checks for not supported annotations for Hints based autodiscover of Elastic Agent #81

Merged
merged 13 commits into from
Mar 13, 2024
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).


[0.6.7]: https://github.com/elastic/elastic-agent-autodiscover/compare/v0.6.2...v0.6.7

## [0.6.9]

### Changed

- Update GenerateHints function to check supported list of hints


[0.6.9]: https://github.com/elastic/elastic-agent-autodiscover/compare/v0.6.8...v0.6.9
30 changes: 28 additions & 2 deletions utils/hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,26 @@ func IsDisabled(hints mapstr.M, key string) bool {
}

// GenerateHints parses annotations based on a prefix and sets up hints that can be picked up by individual Beats.
func GenerateHints(annotations mapstr.M, container, prefix string) mapstr.M {
func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedHints []string) (mapstr.M, error) {
hints := mapstr.M{}
returnerror := error(nil)
if rawEntries, err := annotations.GetValue(prefix); err == nil {
if entries, ok := rawEntries.(mapstr.M); ok {
for key, rawValue := range entries {
found := false
// If there are top level hints like co.elastic.logs/ then just add the values after the /
// Only consider namespaced annotations
parts := strings.Split(key, "/")
if len(parts) == 2 {
hintKey := fmt.Sprintf("%s.%s", parts[0], parts[1])
//We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that start with co.elastic.hints
for _, checksupported := range allSupportedHints {
if parts[1] == checksupported {
found = true
break
}
}

// Insert only if there is no entry already. container level annotations take
// higher priority.
if _, err := hints.GetValue(hintKey); err != nil {
Expand All @@ -233,6 +243,13 @@ func GenerateHints(annotations mapstr.M, container, prefix string) mapstr.M {
if strings.HasPrefix(hintKey, container) {
// Split the key to get part[1] to be the hint
parts := strings.Split(hintKey, "/")
//We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that start with co.elastic.hints
for _, checksupported := range allSupportedHints {
gizas marked this conversation as resolved.
Show resolved Hide resolved
if parts[1] == checksupported {
found = true
break
}
}
if len(parts) == 2 {
// key will be the hint type
hintKey := fmt.Sprintf("%s.%s", key, parts[1])
Expand All @@ -244,11 +261,20 @@ func GenerateHints(annotations mapstr.M, container, prefix string) mapstr.M {
}
}
}

if !found {
returnerror = fmt.Errorf("provided hint :%v is not in the supported list", parts[1])
} //End of check

}
}
}

return hints
if returnerror == nil {
gizas marked this conversation as resolved.
Show resolved Hide resolved
return hints, nil
}

return hints, returnerror
}

// GetHintsAsList gets a set of hints and tries to convert them into a list of hints
Expand Down
18 changes: 17 additions & 1 deletion utils/hints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ func TestGetProcessors(t *testing.T) {
}

func TestGenerateHints(t *testing.T) {
const (
integration = "package"
datastreams = "data_streams"
host = "host"
period = "period"
timeout = "timeout"
metricspath = "metrics_path"
username = "username"
password = "password"
stream = "stream" // this is the container stream: stdout/stderr
processors = "processors"
)

var allSupportedHints = []string{"enabled", integration, datastreams, host, period, timeout, metricspath, username, password, stream, processors}

tests := []struct {
annotations map[string]string
result mapstr.M
Expand Down Expand Up @@ -219,7 +234,8 @@ func TestGenerateHints(t *testing.T) {
continue
}
}
assert.Equal(t, test.result, GenerateHints(annMap, "foobar", "co.elastic"))
generateHints, _ := GenerateHints(annMap, "foobar", "co.elastic", allSupportedHints)
constanca-m marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, test.result, generateHints)
}
}
func TestGetHintsAsList(t *testing.T) {
Expand Down
Loading