Skip to content

Commit

Permalink
feat: add resource selector matchable
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored and moshloop committed Mar 8, 2024
1 parent a121315 commit 2944cdd
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
49 changes: 49 additions & 0 deletions models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
54 changes: 54 additions & 0 deletions types/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 2944cdd

Please sign in to comment.