2025-05-20 09:40:15 +00:00
|
|
|
// Copyright 2025 Cloudbase Solutions SRL
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
|
// a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
|
// under the License.
|
2025-05-05 23:34:53 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"github.com/cloudbase/garm/params"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var instanceCache *InstanceCache
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
cache := &InstanceCache{
|
|
|
|
|
cache: make(map[string]params.Instance),
|
|
|
|
|
}
|
|
|
|
|
instanceCache = cache
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type InstanceCache struct {
|
|
|
|
|
mux sync.Mutex
|
|
|
|
|
|
|
|
|
|
cache map[string]params.Instance
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *InstanceCache) SetInstance(instance params.Instance) {
|
|
|
|
|
i.mux.Lock()
|
|
|
|
|
defer i.mux.Unlock()
|
|
|
|
|
|
2025-05-06 17:50:12 +00:00
|
|
|
i.cache[instance.Name] = instance
|
2025-05-05 23:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-06 17:50:12 +00:00
|
|
|
func (i *InstanceCache) GetInstance(name string) (params.Instance, bool) {
|
2025-05-05 23:34:53 +00:00
|
|
|
i.mux.Lock()
|
|
|
|
|
defer i.mux.Unlock()
|
|
|
|
|
|
2025-05-06 17:50:12 +00:00
|
|
|
if instance, ok := i.cache[name]; ok {
|
2025-05-05 23:34:53 +00:00
|
|
|
return instance, true
|
|
|
|
|
}
|
|
|
|
|
return params.Instance{}, false
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-06 17:50:12 +00:00
|
|
|
func (i *InstanceCache) DeleteInstance(name string) {
|
2025-05-05 23:34:53 +00:00
|
|
|
i.mux.Lock()
|
|
|
|
|
defer i.mux.Unlock()
|
|
|
|
|
|
2025-05-06 17:50:12 +00:00
|
|
|
delete(i.cache, name)
|
2025-05-05 23:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *InstanceCache) GetAllInstances() []params.Instance {
|
|
|
|
|
i.mux.Lock()
|
|
|
|
|
defer i.mux.Unlock()
|
|
|
|
|
|
|
|
|
|
instances := make([]params.Instance, 0, len(i.cache))
|
|
|
|
|
for _, instance := range i.cache {
|
|
|
|
|
instances = append(instances, instance)
|
|
|
|
|
}
|
2025-05-08 21:39:55 +00:00
|
|
|
sortByCreationDate(instances)
|
2025-05-05 23:34:53 +00:00
|
|
|
return instances
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *InstanceCache) GetInstancesForPool(poolID string) []params.Instance {
|
|
|
|
|
i.mux.Lock()
|
|
|
|
|
defer i.mux.Unlock()
|
|
|
|
|
|
|
|
|
|
var filteredInstances []params.Instance
|
|
|
|
|
for _, instance := range i.cache {
|
|
|
|
|
if instance.PoolID == poolID {
|
|
|
|
|
filteredInstances = append(filteredInstances, instance)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-08 21:39:55 +00:00
|
|
|
sortByCreationDate(filteredInstances)
|
2025-05-05 23:34:53 +00:00
|
|
|
return filteredInstances
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *InstanceCache) GetInstancesForScaleSet(scaleSetID uint) []params.Instance {
|
|
|
|
|
i.mux.Lock()
|
|
|
|
|
defer i.mux.Unlock()
|
|
|
|
|
|
|
|
|
|
var filteredInstances []params.Instance
|
|
|
|
|
for _, instance := range i.cache {
|
|
|
|
|
if instance.ScaleSetID == scaleSetID {
|
|
|
|
|
filteredInstances = append(filteredInstances, instance)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-08 21:39:55 +00:00
|
|
|
sortByCreationDate(filteredInstances)
|
2025-05-05 23:34:53 +00:00
|
|
|
return filteredInstances
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-24 22:36:44 +00:00
|
|
|
func (i *InstanceCache) GetEntityInstances(entityID string) []params.Instance {
|
|
|
|
|
pools := GetEntityPools(entityID)
|
|
|
|
|
poolsAsMap := map[string]bool{}
|
|
|
|
|
for _, pool := range pools {
|
|
|
|
|
poolsAsMap[pool.ID] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret := []params.Instance{}
|
|
|
|
|
for _, val := range i.GetAllInstances() {
|
|
|
|
|
if _, ok := poolsAsMap[val.PoolID]; ok {
|
|
|
|
|
ret = append(ret, val)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-05 23:34:53 +00:00
|
|
|
func SetInstanceCache(instance params.Instance) {
|
|
|
|
|
instanceCache.SetInstance(instance)
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-06 17:50:12 +00:00
|
|
|
func GetInstanceCache(name string) (params.Instance, bool) {
|
|
|
|
|
return instanceCache.GetInstance(name)
|
2025-05-05 23:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-06 17:50:12 +00:00
|
|
|
func DeleteInstanceCache(name string) {
|
|
|
|
|
instanceCache.DeleteInstance(name)
|
2025-05-05 23:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetAllInstancesCache() []params.Instance {
|
|
|
|
|
return instanceCache.GetAllInstances()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetInstancesForPool(poolID string) []params.Instance {
|
|
|
|
|
return instanceCache.GetInstancesForPool(poolID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetInstancesForScaleSet(scaleSetID uint) []params.Instance {
|
|
|
|
|
return instanceCache.GetInstancesForScaleSet(scaleSetID)
|
|
|
|
|
}
|
2025-08-24 22:36:44 +00:00
|
|
|
|
|
|
|
|
func GetEntityInstances(entityID string) []params.Instance {
|
|
|
|
|
return instanceCache.GetEntityInstances(entityID)
|
|
|
|
|
}
|