Skip to content

Commit

Permalink
first commit with checks for hints vocabulary
Browse files Browse the repository at this point in the history
  • Loading branch information
gizas committed Mar 6, 2024
1 parent 018039d commit 9bd3cb6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
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
21 changes: 19 additions & 2 deletions utils/hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,9 @@ 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 {
Expand All @@ -212,6 +213,18 @@ func GenerateHints(annotations mapstr.M, container, prefix string) mapstr.M {
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
found := false
for _, checksupported := range allSupportedHints {
if parts[1] == checksupported {
found = true
break
}
}
if !found {
returnerror = fmt.Errorf("provided hint :%v is not in the supported list", parts[1])
} //End of check

// Insert only if there is no entry already. container level annotations take
// higher priority.
if _, err := hints.GetValue(hintKey); err != nil {
Expand Down Expand Up @@ -248,7 +261,11 @@ func GenerateHints(annotations mapstr.M, container, prefix string) mapstr.M {
}
}

return hints
if returnerror == nil {
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)
assert.Equal(t, test.result, generateHints)
}
}
func TestGetHintsAsList(t *testing.T) {
Expand Down

0 comments on commit 9bd3cb6

Please sign in to comment.