Skip to content

Commit

Permalink
adding function for duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
gizas committed Mar 7, 2024
1 parent 9306516 commit 98d9808
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions utils/hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,18 @@ func IsDisabled(hints mapstr.M, key string) bool {
func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedHints []string) (mapstr.M, []string) {
hints := mapstr.M{}
var incorrecthints []string
found := false
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
}
}
found = checkSupportedHints(parts[1], allSupportedHints)

// Insert only if there is no entry already. container level annotations take
// higher priority.
Expand All @@ -244,12 +240,7 @@ func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedH
// 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 {
if parts[1] == checksupported {
found = true
break
}
}
found = checkSupportedHints(parts[1], allSupportedHints)
if len(parts) == 2 {
// key will be the hint type
hintKey := fmt.Sprintf("%s.%s", key, parts[1])
Expand Down Expand Up @@ -309,3 +300,15 @@ func GetHintsAsList(hints mapstr.M, key string) []mapstr.M {
}
return configs
}

// checkSupportedHints gets a specific hint annotation and compares it with the supported list of hints
func checkSupportedHints(actualannotation string, allSupportedHints []string) bool {
found := false
for _, checksupported := range allSupportedHints {
if actualannotation == checksupported {
found = true
break
}
}
return found
}

0 comments on commit 98d9808

Please sign in to comment.