Skip to content

Commit

Permalink
Merge pull request #70 from port-labs/PORT-8514_add_support_for_searc…
Browse files Browse the repository at this point in the history
…h_relation

PORT-8514 search relation support
  • Loading branch information
razsamuel authored Jun 19, 2024
2 parents 8b8bb64 + 6c575d9 commit 5f61510
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 13 deletions.
36 changes: 36 additions & 0 deletions pkg/jq/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jq
import (
"fmt"
"os"
"reflect"
"strings"
"sync"

Expand Down Expand Up @@ -121,3 +122,38 @@ func ParseMapInterface(jqQueries map[string]string, obj interface{}) (map[string

return mapInterface, nil
}

func ParseRelations(jqQueries map[string]interface{}, obj interface{}) (map[string]interface{}, error) {
mapInterface := make(map[string]interface{}, len(jqQueries))

for key, jqQuery := range jqQueries {

if reflect.TypeOf(jqQuery).Kind() == reflect.String {
queryRes, _ := ParseMapInterface(map[string]string{key: jqQuery.(string)}, obj)
mapInterface = goutils.MergeMaps(mapInterface, queryRes)
} else if reflect.TypeOf(jqQuery).Kind() == reflect.Map {
for mapKey, mapValue := range jqQuery.(map[string]interface{}) {
queryRes, _ := ParseRelations(map[string]interface{}{mapKey: mapValue}, obj)
for queryKey, queryVal := range queryRes {
if mapInterface[key] == nil {
mapInterface[key] = make(map[string]interface{})
}
mapInterface[key].(map[string]interface{})[queryKey] = queryVal
}
}
} else if reflect.TypeOf(jqQuery).Kind() == reflect.Slice {
jqArrayValue := reflect.ValueOf(jqQuery)
relations := make([]interface{}, jqArrayValue.Len())
for i := 0; i < jqArrayValue.Len(); i++ {
relation, err := ParseRelations(map[string]interface{}{key: jqArrayValue.Index(i).Interface()}, obj)
if err != nil {
return nil, err
}
relations[i] = relation[key]
}
mapInterface[key] = relations
}
}

return mapInterface, nil
}
90 changes: 85 additions & 5 deletions pkg/k8s/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package k8s
import (
"context"
"fmt"
"github.com/port-labs/port-k8s-exporter/pkg/jq"
"github.com/stretchr/testify/assert"
"reflect"
"strings"
Expand Down Expand Up @@ -215,7 +216,7 @@ func TestCreateDeployment(t *testing.T) {
"obj": ".spec.selector",
"arr": ".spec.template.spec.containers",
},
Relations: map[string]string{
Relations: map[string]interface{}{
"k8s-relation": "\"e_AgPMYvq1tAs8TuqM\"",
},
},
Expand All @@ -226,6 +227,85 @@ func TestCreateDeployment(t *testing.T) {
f.runControllerSyncHandler(item, false)
}

func TestJqSearchRelation(t *testing.T) {

mapping := []port.EntityMapping{
{
Identifier: ".metadata.name",
Blueprint: "\"k8s-export-test-bp\"",
Icon: "\"Microservice\"",
Team: "\"Test\"",
Properties: map[string]string{},
Relations: map[string]interface{}{
"k8s-relation": map[string]interface{}{
"combinator": "\"or\"",
"rules": []interface{}{
map[string]interface{}{
"property": "\"$identifier\"",
"operator": "\"=\"",
"value": "\"e_AgPMYvq1tAs8TuqM\"",
},
},
},
},
},
}
res, _ := jq.ParseRelations(mapping[0].Relations, nil)
assert.Equal(t, res, map[string]interface{}{
"k8s-relation": map[string]interface{}{
"combinator": "or",
"rules": []interface{}{
map[string]interface{}{
"property": "$identifier",
"operator": "=",
"value": "e_AgPMYvq1tAs8TuqM",
},
},
},
})

}

func TestCreateDeploymentWithSearchRelation(t *testing.T) {
d := newDeployment()
objects := []runtime.Object{newUnstructured(d)}
item := EventItem{Key: getKey(d, t), ActionType: CreateAction}
resource := newResource("", []port.EntityMapping{
{
Identifier: ".metadata.name",
Blueprint: "\"k8s-export-test-bp\"",
Icon: "\"Microservice\"",
Team: "\"Test\"",
Properties: map[string]string{
"text": "\"pod\"",
"num": "1",
"bool": "true",
"obj": ".spec.selector",
"arr": ".spec.template.spec.containers",
},
Relations: map[string]interface{}{
"k8s-relation": map[string]interface{}{
"combinator": "\"or\"",
"rules": []interface{}{
map[string]interface{}{
"property": "\"$identifier\"",
"operator": "\"=\"",
"value": "\"e_AgPMYvq1tAs8TuqM\"",
},
map[string]interface{}{
"property": "\"$identifier\"",
"operator": "\"=\"",
"value": ".metadata.name",
},
},
},
},
},
})
f := newFixture(t, "", "", "", resource, objects)
f.runControllerSyncHandler(item, false)
}

func TestUpdateDeployment(t *testing.T) {
d := newDeployment()
objects := []runtime.Object{newUnstructured(d)}
Expand All @@ -242,7 +322,7 @@ func TestUpdateDeployment(t *testing.T) {
"obj": ".spec.selector",
"arr": ".spec.template.spec.containers",
},
Relations: map[string]string{
Relations: map[string]interface{}{
"k8s-relation": "\"e_AgPMYvq1tAs8TuqM\"",
},
},
Expand Down Expand Up @@ -379,7 +459,7 @@ func TestUpdateHandlerWithIndividualPropertyChanges(t *testing.T) {
"generateName": ".metadata.generateName",
"creationTimestamp": ".metadata.creationTimestamp",
},
Relations: map[string]string{
Relations: map[string]interface{}{
"k8s-relation": "\"e_AgPMYvq1tAs8TuqM\"",
},
},
Expand All @@ -392,7 +472,7 @@ func TestUpdateHandlerWithIndividualPropertyChanges(t *testing.T) {
Icon: "\"Microservice\"",
Team: "\"Test\"",
Properties: map[string]string{},
Relations: map[string]string{},
Relations: map[string]interface{}{},
},
{
Identifier: ".metadata.name",
Expand All @@ -405,7 +485,7 @@ func TestUpdateHandlerWithIndividualPropertyChanges(t *testing.T) {
"generateName": ".metadata.generateName",
"creationTimestamp": ".metadata.creationTimestamp",
},
Relations: map[string]string{
Relations: map[string]interface{}{
"k8s-relation": "\"e_AgPMYvq1tAs8TuqM\"",
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/port/mapping/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewEntity(obj interface{}, mapping port.EntityMapping) (*port.Entity, error
if err != nil {
return &port.Entity{}, err
}
entity.Relations, err = jq.ParseMapInterface(mapping.Relations, obj)
entity.Relations, err = jq.ParseRelations(mapping.Relations, obj)
if err != nil {
return &port.Entity{}, err
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/port/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ type ResponseBody struct {
}

type EntityMapping struct {
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"`
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]interface{} `json:"relations,omitempty" yaml:"relations,omitempty"`
}

type EntityMappings struct {
Expand Down

0 comments on commit 5f61510

Please sign in to comment.