garm/cache/template_cache.go

80 lines
1.5 KiB
Go
Raw Normal View History

Add runner install template management (#525) * Add template api endpoints Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com> * Added template bypass Pools and scale sets will automatically migrate to the new template system for runner install scripts. If a pool or a scale set cannot be migrate, it is left alone. It is expected that users set a runner install template manually for scenarios we don't yet have a template for (windows on gitea for example). Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com> * Integrate templates with pool create/update Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com> * Add webapp integration with templates Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com> * Add unit tests Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com> * Populate all relevant context fields Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com> * Update dependencies Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com> * Fix lint Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com> * Validate uint Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com> * Add CLI template management Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com> * Some editor improvements and bugfixes Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com> * Fix scale set return values post create Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com> * Fix template websocket events filter Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com> --------- Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2025-09-23 13:46:27 +03:00
package cache
import (
"sync"
commonParams "github.com/cloudbase/garm-provider-common/params"
"github.com/cloudbase/garm/params"
)
var templateCache *TemplateCache
func init() {
tplCache := &TemplateCache{
cache: make(map[uint]params.Template),
}
templateCache = tplCache
}
type TemplateCache struct {
mux sync.Mutex
cache map[uint]params.Template
}
func (t *TemplateCache) SetTemplateCache(tpl params.Template) {
t.mux.Lock()
defer t.mux.Unlock()
t.cache[tpl.ID] = tpl
}
func (t *TemplateCache) GetTemplate(id uint) (params.Template, bool) {
t.mux.Lock()
defer t.mux.Unlock()
tpl, ok := t.cache[id]
if !ok {
return params.Template{}, false
}
return tpl, true
}
func (t *TemplateCache) ListTemplates(osType *commonParams.OSType, forgeType *params.EndpointType) []params.Template {
ret := []params.Template{}
for _, val := range t.cache {
if osType != nil && val.OSType != *osType {
continue
}
if forgeType != nil && val.ForgeType != *forgeType {
continue
}
ret = append(ret, val)
}
return ret
}
func (t *TemplateCache) DeleteTemplate(id uint) {
t.mux.Lock()
defer t.mux.Unlock()
delete(t.cache, id)
}
func SetTemplateCache(tpl params.Template) {
templateCache.SetTemplateCache(tpl)
}
func GetTemplate(id uint) (params.Template, bool) {
return templateCache.GetTemplate(id)
}
func ListTemplates(osType *commonParams.OSType, forgeType *params.EndpointType) []params.Template {
return templateCache.ListTemplates(osType, forgeType)
}
func DeleteTemplate(id uint) {
templateCache.DeleteTemplate(id)
}