github.com/zhyoulun/cilium@v1.6.12/pkg/testutils/identitynotifier.go (about) 1 // Copyright 2020 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package testutils 16 17 import ( 18 "github.com/cilium/cilium/pkg/identity" 19 "github.com/cilium/cilium/pkg/lock" 20 "github.com/cilium/cilium/pkg/policy/api" 21 ) 22 23 type DummyIdentityNotifier struct { 24 mutex lock.Mutex 25 selectors map[api.FQDNSelector][]identity.NumericIdentity 26 } 27 28 func NewDummyIdentityNotifier() *DummyIdentityNotifier { 29 return &DummyIdentityNotifier{ 30 selectors: make(map[api.FQDNSelector][]identity.NumericIdentity), 31 } 32 } 33 34 // Lock must be held during any calls to RegisterForIdentityUpdatesLocked or 35 // UnregisterForIdentityUpdatesLocked. 36 func (d *DummyIdentityNotifier) Lock() { 37 d.mutex.Lock() 38 } 39 40 // Unlock must be called after calls to RegisterForIdentityUpdatesLocked or 41 // UnregisterForIdentityUpdatesLocked are done. 42 func (d *DummyIdentityNotifier) Unlock() { 43 d.mutex.Unlock() 44 } 45 46 // RegisterForIdentityUpdatesLocked starts managing this selector. 47 func (d *DummyIdentityNotifier) RegisterForIdentityUpdatesLocked(selector api.FQDNSelector) (identities []identity.NumericIdentity) { 48 ids, ok := d.selectors[selector] 49 if !ok { 50 d.selectors[selector] = []identity.NumericIdentity{} 51 } 52 return ids 53 } 54 55 // UnregisterForIdentityUpdatesLocked stops managing this selector. 56 func (d *DummyIdentityNotifier) UnregisterForIdentityUpdatesLocked(selector api.FQDNSelector) { 57 delete(d.selectors, selector) 58 } 59 60 func (d *DummyIdentityNotifier) InjectIdentitiesForSelector(fqdnSel api.FQDNSelector, ids []identity.NumericIdentity) { 61 d.selectors[fqdnSel] = ids 62 } 63 64 // IsRegistered returns whether this selector is being managed. 65 func (d *DummyIdentityNotifier) IsRegistered(selector api.FQDNSelector) bool { 66 _, ok := d.selectors[selector] 67 return ok 68 }