Skip to content

Commit

Permalink
feat: allow namespace/name for scope in resource selector
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Oct 11, 2024
1 parent caf1e17 commit 9431a3d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
2 changes: 2 additions & 0 deletions query/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var (
getterCache = cache.New(time.Second*90, time.Minute*5)

immutableCache = cache.New(cache.NoExpiration, time.Hour*12)

scopeCache = cache.New(time.Hour, time.Hour*2)
)

func FlushGettersCache() {
Expand Down
49 changes: 46 additions & 3 deletions query/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,17 @@ func SetResourceSelectorClause(ctx context.Context, resourceSelector types.Resou
}

if resourceSelector.Scope != "" {
scope, err := getScopeID(ctx, resourceSelector.Scope, table)
if err != nil {
return nil, fmt.Errorf("error fetching scope: %w", err)
}
switch table {
case "checks":
query = query.Where("canary_id = ?", resourceSelector.Scope)
query = query.Where("canary_id = ?", scope)
case "config_items":
query = query.Where("scraper_id = ?", resourceSelector.Scope)
query = query.Where("scraper_id = ?", scope)
case "components":
query = query.Where("topology_id = ?", resourceSelector.Scope)
query = query.Where("topology_id = ?", scope)
default:
return nil, api.Errorf(api.EINVALID, "scope is not supported for %s", table)
}
Expand Down Expand Up @@ -480,3 +484,42 @@ func propertySelectorRequirementToSQLClause(q *gorm.DB, r labels.Requirement) *g

return q
}

// getScopeID takes either uuid or namespace/name and table to return the appropriate scope_id
func getScopeID(ctx context.Context, scope string, table string) (id string, err error) {
// If scope is a valid uuid, return as is
if _, err = uuid.Parse(scope); err == nil {
return scope, nil
}

key := table + scope
if cacheVal, exists := scopeCache.Get(key); exists {
return cacheVal.(string), nil
}

parts := strings.Split(scope, "/")
if len(parts) != 2 {
return "", fmt.Errorf("scope should be either uuid or namespace/name format")
}
namespace, name := parts[0], parts[1]

var scopeTable string
switch table {
case "checks":
scopeTable = "canaries"
case "config_items":
scopeTable = "config_scrapers"
case "components":
scopeTable = "topologies"
default:
return "", api.Errorf(api.EINVALID, "scope is not supported for %s", table)
}

err = ctx.DB().Table(scopeTable).Select("id").Where("name = ? AND namespace = ?", name, namespace).Find(&id).Error
if err != nil {
return "", err
}

scopeCache.Set(key, id, cache.NoExpiration)
return id, nil
}
9 changes: 5 additions & 4 deletions types/resource_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ type ResourceSelector struct {
// Additionally, the special "self" value can be used to select resources without an agent.
Agent string `yaml:"agent,omitempty" json:"agent,omitempty"`

// Scope is the id parent of the resource to select.
// Example: For config items, the scope is the scraper id
// - for checks, it's canaries and
// - for components, it's topology.
// Scope is the reference for parent of the resource to select.
// For config items, the scope is the scraper id
// For checks, it's canaries and
// For components, it's topology.
// It can either be a uuid or namespace/name
Scope string `yaml:"scope,omitempty" json:"scope,omitempty"`

// Cache directives
Expand Down

0 comments on commit 9431a3d

Please sign in to comment.