github.com/Axway/agent-sdk@v1.1.101/pkg/agent/handler/watchresource.go (about) 1 package handler 2 3 import ( 4 "context" 5 "fmt" 6 7 agentcache "github.com/Axway/agent-sdk/pkg/agent/cache" 8 v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" 9 "github.com/Axway/agent-sdk/pkg/config" 10 "github.com/Axway/agent-sdk/pkg/watchmanager/proto" 11 ) 12 13 type watchResourceHandler struct { 14 agentCacheManager agentcache.Manager 15 watchGroupKindMap map[string]bool 16 } 17 18 type watchTopicFeatures interface { 19 GetWatchResourceFilters() []config.ResourceFilter 20 } 21 22 func getWatchResourceKey(group, kind string) string { 23 return fmt.Sprintf("%s:%s", group, kind) 24 } 25 26 // NewWatchResourceHandler creates a Handler for custom watch resources to store resource in agent cache 27 func NewWatchResourceHandler(agentCacheManager agentcache.Manager, feature watchTopicFeatures) Handler { 28 watchGroupKindMap := make(map[string]bool) 29 30 filters := feature.GetWatchResourceFilters() 31 for _, filter := range filters { 32 key := getWatchResourceKey(filter.Group, filter.Kind) 33 watchGroupKindMap[key] = filter.IsCachedResource 34 } 35 36 return &watchResourceHandler{ 37 agentCacheManager: agentCacheManager, 38 watchGroupKindMap: watchGroupKindMap, 39 } 40 } 41 42 func (h *watchResourceHandler) Handle(ctx context.Context, _ *proto.EventMeta, resource *v1.ResourceInstance) error { 43 action := GetActionFromContext(ctx) 44 key := getWatchResourceKey(resource.Group, resource.Kind) 45 ok := h.watchGroupKindMap[key] 46 if !ok { 47 return nil 48 } 49 50 if action != proto.Event_DELETED { 51 h.agentCacheManager.AddWatchResource(resource) 52 return nil 53 } 54 55 return h.agentCacheManager.DeleteWatchResource(resource.Group, resource.Kind, resource.Metadata.ID) 56 }