diff --git a/pkg/k8s/controller.go b/pkg/k8s/controller.go index 048334f..4da895b 100644 --- a/pkg/k8s/controller.go +++ b/pkg/k8s/controller.go @@ -244,42 +244,40 @@ func (c *Controller) getObjectEntities(obj interface{}, selector port.Selector, } entities := make([]port.Entity, 0, len(mappings)) - if itemsToParse != "" { + objectsToMap := make([]interface{}, 0) + + if (itemsToParse == "") { + objectsToMap = append(objectsToMap, structuredObj) + } else { items, parseItemsError := jq.ParseArray(itemsToParse, structuredObj) if parseItemsError != nil { return nil, parseItemsError } - editedObject, ok := structuredObj.(map[string]interface{}) + + mappedObject, ok := structuredObj.(map[string]interface{}) if !ok { return nil, fmt.Errorf("error parsing object '%#v'", structuredObj) } for _, item := range items { - editedObject["item"] = item - selectorResult, err := isPassSelector(editedObject, selector) - - if err != nil { - return nil, err - } - - if selectorResult { - currentEntities, err := mapEntities(editedObject, mappings) - if err != nil { - return nil, err - } - - entities = append(entities, currentEntities...) + copiedObject := make(map[string]interface{}) + for key, value := range mappedObject { + copiedObject[key] = value } + copiedObject["item"] = item + objectsToMap = append(objectsToMap, copiedObject) } - } else { - selectorResult, err := isPassSelector(structuredObj, selector) + } + + for _, objectToMap := range objectsToMap { + selectorResult, err := isPassSelector(objectToMap, selector) if err != nil { return nil, err } if selectorResult { - currentEntities, err := mapEntities(structuredObj, mappings) + currentEntities, err := mapEntities(objectToMap, mappings) if err != nil { return nil, err } diff --git a/pkg/port/models.go b/pkg/port/models.go index 4d97b4e..722f7d9 100644 --- a/pkg/port/models.go +++ b/pkg/port/models.go @@ -179,22 +179,22 @@ type ResponseBody struct { } type EntityMapping struct { - Identifier string `json:"identifier"` - Title string `json:"title"` - Blueprint string `json:"blueprint"` - Icon string `json:"icon,omitempty"` - Team string `json:"team,omitempty"` - Properties map[string]string `json:"properties,omitempty"` - Relations map[string]string `json:"relations,omitempty"` + Identifier string `json:"identifier" yaml:"identifier"` + Title string `json:"title" yaml:"title"` + Blueprint string `json:"blueprint" yaml:"blueprint"` + Icon string `json:"icon,omitempty" yaml:"icon,omitempty"` + Team string `json:"team,omitempty" yaml:"team,omitempty"` + Properties map[string]string `json:"properties,omitempty" yaml:"properties,omitempty"` + Relations map[string]string `json:"relations,omitempty" yaml:"relations,omitempty"` } type EntityMappings struct { - Mappings []EntityMapping `json:"mappings"` + Mappings []EntityMapping `json:"mappings" yaml:"mappings"` } type Port struct { - Entity EntityMappings `json:"entity"` - ItemsToParse string `json:"itemsToParse"` + Entity EntityMappings `json:"entity" yaml:"entity"` + ItemsToParse string `json:"itemsToParse" yaml:"itemsToParse"` } type Selector struct { @@ -202,9 +202,9 @@ type Selector struct { } type Resource struct { - Kind string `json:"kind"` - Selector Selector `json:"selector,omitempty"` - Port Port `json:"port"` + Kind string `json:"kind" yaml:"kind"` + Selector Selector `json:"selector,omitempty" yaml:"selector,omitempty"` + Port Port `json:"port" yaml:"port"` } type EventListenerSettings struct {