Add tools update routine and cleanup logging

This change adds an update routine in the cache worker, for github tools
downloads.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2025-05-07 23:01:22 +00:00
parent ffbb3b8d41
commit 52007f4ffa
10 changed files with 320 additions and 86 deletions

10
cache/cache_test.go vendored
View file

@ -55,7 +55,7 @@ func (c *CacheTestSuite) TestSetCacheWorks() {
c.Require().Len(githubToolsCache.entities, 0)
SetGithubToolsCache(c.entity, tools)
c.Require().Len(githubToolsCache.entities, 1)
cachedTools, ok := GetGithubToolsCache(c.entity)
cachedTools, ok := GetGithubToolsCache(c.entity.ID)
c.Require().True(ok)
c.Require().Len(cachedTools, 1)
c.Require().Equal(tools[0].GetDownloadURL(), cachedTools[0].GetDownloadURL())
@ -72,11 +72,11 @@ func (c *CacheTestSuite) TestTimedOutToolsCache() {
c.Require().Len(githubToolsCache.entities, 0)
SetGithubToolsCache(c.entity, tools)
c.Require().Len(githubToolsCache.entities, 1)
entity := githubToolsCache.entities[c.entity.String()]
entity := githubToolsCache.entities[c.entity.ID]
entity.updatedAt = entity.updatedAt.Add(-2 * time.Hour)
githubToolsCache.entities[c.entity.String()] = entity
githubToolsCache.entities[c.entity.ID] = entity
cachedTools, ok := GetGithubToolsCache(c.entity)
cachedTools, ok := GetGithubToolsCache(c.entity.ID)
c.Require().False(ok)
c.Require().Nil(cachedTools)
}
@ -84,7 +84,7 @@ func (c *CacheTestSuite) TestTimedOutToolsCache() {
func (c *CacheTestSuite) TestGetInexistentCache() {
c.Require().NotNil(githubToolsCache)
c.Require().Len(githubToolsCache.entities, 0)
cachedTools, ok := GetGithubToolsCache(c.entity)
cachedTools, ok := GetGithubToolsCache(c.entity.ID)
c.Require().False(ok)
c.Require().Nil(cachedTools)
}

View file

@ -26,6 +26,7 @@ func (g *GithubCredentials) SetCredentials(credentials params.GithubCredentials)
defer g.mux.Unlock()
g.cache[credentials.ID] = credentials
UpdateCredentialsInAffectedEntities(credentials)
}
func (g *GithubCredentials) GetCredentials(id uint) (params.GithubCredentials, bool) {

60
cache/entity_cache.go vendored
View file

@ -1,7 +1,6 @@
package cache
import (
"log/slog"
"sync"
"github.com/cloudbase/garm/params"
@ -28,15 +27,24 @@ type EntityCache struct {
entities map[string]EntityItem
}
func (e *EntityCache) UpdateCredentialsInAffectedEntities(creds params.GithubCredentials) {
e.mux.Lock()
defer e.mux.Unlock()
for entityID, cache := range e.entities {
if cache.Entity.Credentials.ID == creds.ID {
cache.Entity.Credentials = creds
e.entities[entityID] = cache
}
}
}
func (e *EntityCache) GetEntity(entityID string) (params.GithubEntity, bool) {
e.mux.Lock()
defer e.mux.Unlock()
if cache, ok := e.entities[entityID]; ok {
// Updating specific credential details will not update entity cache which
// uses those credentials.
// Entity credentials in the cache are only updated if you swap the creds
// on the entity. We get the updated credentials from the credentials cache.
// Get the credentials from the credentials cache.
creds, ok := GetGithubCredentials(cache.Entity.Credentials.ID)
if ok {
cache.Entity.Credentials = creds
@ -173,7 +181,6 @@ func (e *EntityCache) FindPoolsMatchingAllTags(entityID string, tags []string) [
if cache, ok := e.entities[entityID]; ok {
var pools []params.Pool
slog.Debug("Finding pools matching all tags", "entityID", entityID, "tags", tags, "pools", cache.Pools)
for _, pool := range cache.Pools {
if pool.HasRequiredLabels(tags) {
pools = append(pools, pool)
@ -212,6 +219,35 @@ func (e *EntityCache) GetEntityScaleSets(entityID string) []params.ScaleSet {
return nil
}
func (e *EntityCache) GetEntitiesUsingGredentials(credsID uint) []params.GithubEntity {
e.mux.Lock()
defer e.mux.Unlock()
var entities []params.GithubEntity
for _, cache := range e.entities {
if cache.Entity.Credentials.ID == credsID {
entities = append(entities, cache.Entity)
}
}
return entities
}
func (e *EntityCache) GetAllEntities() []params.GithubEntity {
e.mux.Lock()
defer e.mux.Unlock()
var entities []params.GithubEntity
for _, cache := range e.entities {
// Get the credentials from the credentials cache.
creds, ok := GetGithubCredentials(cache.Entity.Credentials.ID)
if ok {
cache.Entity.Credentials = creds
}
entities = append(entities, cache.Entity)
}
return entities
}
func GetEntity(entityID string) (params.GithubEntity, bool) {
return entityCache.GetEntity(entityID)
}
@ -267,3 +303,15 @@ func GetEntityPools(entityID string) []params.Pool {
func GetEntityScaleSets(entityID string) []params.ScaleSet {
return entityCache.GetEntityScaleSets(entityID)
}
func UpdateCredentialsInAffectedEntities(creds params.GithubCredentials) {
entityCache.UpdateCredentialsInAffectedEntities(creds)
}
func GetEntitiesUsingGredentials(credsID uint) []params.GithubEntity {
return entityCache.GetEntitiesUsingGredentials(credsID)
}
func GetAllEntities() []params.GithubEntity {
return entityCache.GetAllEntities()
}

12
cache/tools_cache.go vendored
View file

@ -29,14 +29,14 @@ type GithubToolsCache struct {
entities map[string]GithubEntityTools
}
func (g *GithubToolsCache) Get(entity params.GithubEntity) ([]commonParams.RunnerApplicationDownload, bool) {
func (g *GithubToolsCache) Get(entityID string) ([]commonParams.RunnerApplicationDownload, bool) {
g.mux.Lock()
defer g.mux.Unlock()
if cache, ok := g.entities[entity.String()]; ok {
if cache, ok := g.entities[entityID]; ok {
if time.Since(cache.updatedAt) > 1*time.Hour {
// Stale cache, remove it.
delete(g.entities, entity.String())
delete(g.entities, entityID)
return nil, false
}
return cache.tools, true
@ -48,7 +48,7 @@ func (g *GithubToolsCache) Set(entity params.GithubEntity, tools []commonParams.
g.mux.Lock()
defer g.mux.Unlock()
g.entities[entity.String()] = GithubEntityTools{
g.entities[entity.ID] = GithubEntityTools{
updatedAt: time.Now(),
entity: entity,
tools: tools,
@ -59,6 +59,6 @@ func SetGithubToolsCache(entity params.GithubEntity, tools []commonParams.Runner
githubToolsCache.Set(entity, tools)
}
func GetGithubToolsCache(entity params.GithubEntity) ([]commonParams.RunnerApplicationDownload, bool) {
return githubToolsCache.Get(entity)
func GetGithubToolsCache(entityID string) ([]commonParams.RunnerApplicationDownload, bool) {
return githubToolsCache.Get(entityID)
}