-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
authmap_cache.go
158 lines (130 loc) · 3.69 KB
/
authmap_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package auth
import (
"errors"
"fmt"
"github.com/cilium/ebpf"
"github.com/sirupsen/logrus"
"golang.org/x/exp/maps"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/maps/authmap"
"github.com/cilium/cilium/pkg/metrics"
"github.com/cilium/cilium/pkg/time"
)
type authMapCache struct {
logger logrus.FieldLogger
authmap authMap
cacheEntries map[authKey]authInfoCache
cacheEntriesMutex lock.RWMutex
pressureGauge *metrics.GaugeWithThreshold
}
func newAuthMapCache(logger logrus.FieldLogger, authMap authMap) *authMapCache {
var pressureGauge *metrics.GaugeWithThreshold
if metrics.BPFMapPressure {
pressureGauge = metrics.NewBPFMapPressureGauge(authmap.MapName, 0)
}
return &authMapCache{
logger: logger,
authmap: authMap,
cacheEntries: map[authKey]authInfoCache{},
pressureGauge: pressureGauge,
}
}
func (r *authMapCache) All() (map[authKey]authInfo, error) {
r.cacheEntriesMutex.RLock()
defer r.cacheEntriesMutex.RUnlock()
result := make(map[authKey]authInfo)
for k, v := range r.cacheEntries {
result[k] = v.authInfo
}
return maps.Clone(result), nil
}
func (r *authMapCache) Get(key authKey) (authInfo, error) {
info, err := r.GetCacheInfo(key)
return info.authInfo, err
}
func (r *authMapCache) GetCacheInfo(key authKey) (authInfoCache, error) {
r.cacheEntriesMutex.RLock()
defer r.cacheEntriesMutex.RUnlock()
info, ok := r.cacheEntries[key]
if !ok {
return authInfoCache{}, fmt.Errorf("failed to get auth info for key: %s", key)
}
return info, nil
}
func (r *authMapCache) Update(key authKey, info authInfo) error {
r.cacheEntriesMutex.Lock()
defer r.cacheEntriesMutex.Unlock()
if err := r.authmap.Update(key, info); err != nil {
return err
}
r.cacheEntries[key] = authInfoCache{
authInfo: info,
storedAt: time.Now(),
}
r.updatePressureMetric()
return nil
}
func (r *authMapCache) Delete(key authKey) error {
r.cacheEntriesMutex.Lock()
defer r.cacheEntriesMutex.Unlock()
if err := r.authmap.Delete(key); err != nil {
if !errors.Is(err, ebpf.ErrKeyNotExist) {
return fmt.Errorf("failed to delete auth entry from map: %w", err)
}
r.logger.
WithField("key", key).
Warning("Failed to delete already deleted auth entry")
}
delete(r.cacheEntries, key)
r.updatePressureMetric()
return nil
}
func (r *authMapCache) MaxEntries() uint32 {
return r.authmap.MaxEntries()
}
func (r *authMapCache) DeleteIf(predicate func(key authKey, info authInfo) bool) error {
r.cacheEntriesMutex.Lock()
defer r.cacheEntriesMutex.Unlock()
for k, v := range r.cacheEntries {
if predicate(k, v.authInfo) {
// delete every entry individually to keep the cache in sync in case of an error
if err := r.authmap.Delete(k); err != nil {
if !errors.Is(err, ebpf.ErrKeyNotExist) {
return fmt.Errorf("failed to delete auth entry from map: %w", err)
}
r.logger.
WithField("key", k).
Warning("Failed to delete already deleted auth entry")
}
delete(r.cacheEntries, k)
}
}
r.updatePressureMetric()
return nil
}
func (r *authMapCache) restoreCache() error {
r.logger.Debug("Starting cache restore")
all, err := r.authmap.All()
if err != nil {
return fmt.Errorf("failed to load all auth map entries: %w", err)
}
for k, v := range all {
r.cacheEntries[k] = authInfoCache{
authInfo: v,
storedAt: time.Now(),
}
}
r.updatePressureMetric()
r.logger.
WithField("cached_entries", len(r.cacheEntries)).
Debug("Restored entries")
return nil
}
func (r *authMapCache) updatePressureMetric() {
if r.pressureGauge == nil {
return
}
r.pressureGauge.Set(float64(len(r.cacheEntries)) / float64(r.authmap.MaxEntries()))
}