-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
reserved.go
64 lines (56 loc) · 1.99 KB
/
reserved.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package identity
import (
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/lock"
)
var (
// cacheMU protects the following map.
cacheMU lock.RWMutex
// ReservedIdentityCache that maps all reserved identities from their
// numeric identity to their corresponding identity.
reservedIdentityCache = map[NumericIdentity]*Identity{}
)
// AddReservedIdentity adds the reserved numeric identity with the respective
// label into the map of reserved identity cache, and returns the resulting Identity.
// This identity must not be mutated!
func AddReservedIdentity(ni NumericIdentity, lbl string) *Identity {
identity := NewIdentity(ni, labels.Labels{lbl: labels.NewLabel(lbl, "", labels.LabelSourceReserved)})
cacheMU.Lock()
reservedIdentityCache[ni] = identity
cacheMU.Unlock()
return identity
}
// AddReservedIdentityWithLabels is the same as AddReservedIdentity but accepts
// multiple labels. Returns the resulting Identity.
// This identity must not be mutated!
func AddReservedIdentityWithLabels(ni NumericIdentity, lbls labels.Labels) *Identity {
identity := NewIdentity(ni, lbls)
cacheMU.Lock()
reservedIdentityCache[ni] = identity
cacheMU.Unlock()
return identity
}
// LookupReservedIdentity looks up a reserved identity by its NumericIdentity
// and returns it if found. Returns nil if not found.
// This identity must not be mutated!
func LookupReservedIdentity(ni NumericIdentity) *Identity {
cacheMU.RLock()
defer cacheMU.RUnlock()
return reservedIdentityCache[ni]
}
func init() {
iterateReservedIdentityLabels(func(ni NumericIdentity, lbls labels.Labels) {
AddReservedIdentityWithLabels(ni, lbls)
})
}
// IterateReservedIdentities iterates over all reserved identities and
// executes the given function for each identity.
func IterateReservedIdentities(f func(_ NumericIdentity, _ *Identity)) {
cacheMU.RLock()
defer cacheMU.RUnlock()
for ni, identity := range reservedIdentityCache {
f(ni, identity)
}
}