/
cache.go
95 lines (80 loc) · 3.12 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package configuration
import (
"context"
"sync"
errs "github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)
var configCache = &cache{}
var cacheLog = logf.Log.WithName("cache_toolchainconfig")
type cache struct {
sync.RWMutex
configObj runtime.Object
secrets map[string]map[string]string // map of secret key-value pairs indexed by secret name
}
func (c *cache) set(config runtime.Object, secrets map[string]map[string]string) {
c.Lock()
defer c.Unlock()
c.configObj = config.DeepCopyObject()
c.secrets = CopyOf(secrets)
}
func (c *cache) get() (runtime.Object, map[string]map[string]string) {
c.RLock()
defer c.RUnlock()
if c.configObj == nil {
return nil, CopyOf(c.secrets)
}
return c.configObj.DeepCopyObject(), CopyOf(c.secrets)
}
func UpdateConfig(config runtime.Object, secrets map[string]map[string]string) {
configCache.set(config, secrets)
}
// loadLatest retrieves the latest configuration object and secrets using the provided client and updates the cache.
// If the resource is not found, then returns nil for the configuration and secret.
// If any failure happens while getting the configuration object or secrets, then returns an error.
func LoadLatest(cl client.Client, configObj client.Object) (runtime.Object, map[string]map[string]string, error) {
namespace, err := GetWatchNamespace()
if err != nil {
return nil, nil, errs.Wrap(err, "failed to get watch namespace")
}
if err := cl.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: "config"}, configObj); err != nil {
if apierrors.IsNotFound(err) {
cacheLog.Info("ToolchainConfig resource with the name 'config' wasn't found, default configuration will be used", "namespace", namespace)
return nil, nil, nil
}
return nil, nil, err
}
allSecrets, err := LoadSecrets(cl, namespace)
if err != nil {
return nil, nil, err
}
configCache.set(configObj, allSecrets)
configCopy, secretsCopy := configCache.get()
return configCopy, secretsCopy, nil
}
// getConfig returns a cached configuration object
// If no config is stored in the cache, then it retrieves it from the cluster using the provided LoadConfiguration func
// and stores in the cache.
// If the resource is not found, then returns nil for the configuration and secret.
// If any failure happens while getting the configuration object or secrets, then returns an error.
func GetConfig(cl client.Client, configObj client.Object) (runtime.Object, map[string]map[string]string, error) {
config, secrets := configCache.get()
if config == nil {
return LoadLatest(cl, configObj)
}
return config, secrets, nil
}
// getCachedConfig returns the cached toolchainconfig or a toolchainconfig with default values
func GetCachedConfig() (runtime.Object, map[string]map[string]string) {
return configCache.get()
}
// Reset resets the cache.
// Should be used only in tests, but since it has to be used in other packages,
// then the function has to be exported and placed here.
func ResetCache() {
configCache = &cache{}
}