crowdsec/pkg/acquisition/modules/docker/utils.go
Laurence Jones 9088f31b7d
enhance: container discovery via labels (#2959)
* wip: attempt to autodiscover via labels

* wip: remove labels dep on docker acquistion

* wip: remove labels dep on docker acquistion

* wip: add debug

* wip: try fix parser maps

* wip: remove redundant pointer

* wip: add debug

* wip: cant type assert

* wip: reinstate debug

* wip: reinstate debug

* wip: reinstate debug

* wip: oops

* wip: add a debug

* wip: fix labels

* wip: remove redundant paramter

* wip: rename config option to be more self declarative

* wip: update log wording

* wip: the if check was not correct

* wip: me lost

* fix: add checks to typecast and log useful information

* add tests for parseLabels

* return nil instead of pointer to empty struct

* simplify EvalContainer return value

---------

Co-authored-by: Sebastien Blot <sebastien@crowdsec.net>
2024-05-24 14:27:25 +01:00

38 lines
746 B
Go

package dockeracquisition
import (
"strings"
)
func parseLabels(labels map[string]string) map[string]interface{} {
result := make(map[string]interface{})
for key, value := range labels {
parseKeyToMap(result, key, value)
}
return result
}
func parseKeyToMap(m map[string]interface{}, key string, value string) {
if !strings.HasPrefix(key, "crowdsec") {
return
}
parts := strings.Split(key, ".")
if len(parts) < 2 || parts[0] != "crowdsec" {
return
}
for i := 0; i < len(parts); i++ {
if parts[i] == "" {
return
}
}
for i := 1; i < len(parts)-1; i++ {
if _, ok := m[parts[i]]; !ok {
m[parts[i]] = make(map[string]interface{})
}
m = m[parts[i]].(map[string]interface{})
}
m[parts[len(parts)-1]] = value
}