From 2944cdd680adc124ffd89bb4803a2431c9dc0856 Mon Sep 17 00:00:00 2001 From: Yash Mehrotra Date: Fri, 8 Mar 2024 13:50:32 +0530 Subject: [PATCH] feat: add resource selector matchable --- models/config.go | 49 ++++++++++++++++++++++++++++++++++ types/resource_selector.go | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/models/config.go b/models/config.go index 66ece0e5..b8ede7eb 100644 --- a/models/config.go +++ b/models/config.go @@ -140,6 +140,55 @@ func (c ConfigItem) GetSelectorID() string { return selectorID } +func (c ConfigItem) GetID() string { + return c.ID.String() +} + +func (c ConfigItem) GetName() string { + if c.Name == nil { + return "" + } + return *c.Name +} + +func (c ConfigItem) GetNamespace() string { + if c.Namespace == nil { + return "" + } + return *c.Namespace +} + +func (c ConfigItem) GetType() string { + if c.Type == nil { + return "" + } + return *c.Type +} + +func (c ConfigItem) GetStatus() string { + if c.Status == nil { + return "" + } + return *c.Status +} + +func (c ConfigItem) GetLabels() map[string]string { + m := make(map[string]string) + if c.Tags == nil { + return m + } + for k, v := range *c.Tags { + m[k] = v + } + return m +} + +func (c ConfigItem) GetFields() map[string]string { + return map[string]string{ + "config_class": c.ConfigClass, + } +} + // ConfigScraper represents the config_scrapers database table type ConfigScraper struct { ID uuid.UUID `json:"id"` diff --git a/types/resource_selector.go b/types/resource_selector.go index 8bbce49f..11ad89aa 100644 --- a/types/resource_selector.go +++ b/types/resource_selector.go @@ -82,6 +82,50 @@ func (c ResourceSelector) Hash() string { return hash.Sha256Hex(strings.Join(items, "|")) } +func (rs ResourceSelector) Matches(s ResourceSelectable) bool { + if rs.ID != "" && rs.ID != s.GetID() { + return false + } + if rs.Name != "" && rs.Name != s.GetName() { + return false + } + if rs.Namespace != "" && rs.Namespace != s.GetNamespace() { + return false + } + if len(rs.Types) > 0 && !rs.Types.Contains(s.GetType()) { + return false + } + if len(rs.Statuses) > 0 && !rs.Types.Contains(s.GetStatus()) { + return false + } + + if len(rs.LabelSelector) > 0 { + for k, v := range collections.SelectorToMap(rs.LabelSelector) { + if sVal, exists := s.GetLabels()[k]; exists { + if v != "" && v != sVal { + return false + } + } else { + return false + } + } + } + + if len(rs.FieldSelector) > 0 { + for k, v := range collections.SelectorToMap(rs.FieldSelector) { + if sVal, exists := s.GetFields()[k]; exists { + if v != "" && v != sVal { + return false + } + } else { + return false + } + } + } + + return true +} + type ResourceSelectors []ResourceSelector func (rs *ResourceSelectors) Scan(val any) error { @@ -113,3 +157,13 @@ func (ResourceSelectors) GormDBDataType(db *gorm.DB, field *schema.Field) string func (rs ResourceSelectors) GormValue(ctx context.Context, db *gorm.DB) clause.Expr { return GormValue(rs) } + +type ResourceSelectable interface { + GetID() string + GetName() string + GetNamespace() string + GetType() string + GetStatus() string + GetLabels() map[string]string + GetFields() map[string]string +}