From ff402b4ac2f400071bd163c41b75a8d2b99e2389 Mon Sep 17 00:00:00 2001 From: talsabagport Date: Tue, 6 Jun 2023 18:28:28 +0300 Subject: [PATCH] Map only identifier and blueprint in get entities for delete stale entities --- pkg/k8s/controller.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pkg/k8s/controller.go b/pkg/k8s/controller.go index 317a88f..ea9d7a5 100644 --- a/pkg/k8s/controller.go +++ b/pkg/k8s/controller.go @@ -177,7 +177,7 @@ func (c *Controller) syncHandler(item EventItem) error { } func (c *Controller) objectHandler(obj interface{}, item EventItem) error { - portEntities, err := c.getObjectEntities(obj) + portEntities, err := c.getObjectEntities(obj, c.resource.Port.Entity.Mappings) if err != nil { utilruntime.HandleError(fmt.Errorf("error getting entities for object key '%s': %v", item.Key, err)) return nil @@ -198,7 +198,7 @@ func (c *Controller) objectHandler(obj interface{}, item EventItem) error { return nil } -func (c *Controller) getObjectEntities(obj interface{}) ([]port.Entity, error) { +func (c *Controller) getObjectEntities(obj interface{}, mappings []port.EntityMapping) ([]port.Entity, error) { unstructuredObj, ok := obj.(*unstructured.Unstructured) if !ok { return nil, fmt.Errorf("error casting to unstructured") @@ -220,8 +220,8 @@ func (c *Controller) getObjectEntities(obj interface{}) ([]port.Entity, error) { return nil, nil } - entities := make([]port.Entity, 0, len(c.resource.Port.Entity.Mappings)) - for _, entityMapping := range c.resource.Port.Entity.Mappings { + entities := make([]port.Entity, 0, len(mappings)) + for _, entityMapping := range mappings { var portEntity *port.Entity portEntity, err = mapping.NewEntity(structuredObj, entityMapping) if err != nil { @@ -258,8 +258,15 @@ func (c *Controller) GetEntitiesSet() (map[string]interface{}, error) { if err != nil { return nil, fmt.Errorf("error listing K8s objects of resource '%s': %v", c.resource.Kind, err) } + mappings := make([]port.EntityMapping, 0, len(c.resource.Port.Entity.Mappings)) + for _, m := range c.resource.Port.Entity.Mappings { + mappings = append(mappings, port.EntityMapping{ + Identifier: m.Identifier, + Blueprint: m.Blueprint, + }) + } for _, obj := range objects { - entities, err := c.getObjectEntities(obj) + entities, err := c.getObjectEntities(obj, mappings) if err != nil { return nil, fmt.Errorf("error getting entities of object: %v", err) }