github.com/cilium/cilium@v1.16.2/operator/pkg/ciliumendpointslice/ceptocesmap.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package ciliumendpointslice 5 6 import ( 7 capi_v2a1 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1" 8 "github.com/cilium/cilium/pkg/k8s/resource" 9 "github.com/cilium/cilium/pkg/lock" 10 ) 11 12 type CEPName resource.Key 13 type CESName resource.Key 14 15 // CESToCEPMapping is used to map Cilium Endpoints to CiliumEndpointSlices and 16 // retrieving all the Cilium Endpoints mapped to the given CiliumEndpointSlice. 17 // This map is protected by lock for consistent and concurrent access. 18 type CESToCEPMapping struct { 19 mutex lock.RWMutex 20 // cepNameToCESName is used to map CiliumEndpoint name to CiliumEndpointSlice name. 21 cepNameToCESName map[CEPName]CESName 22 // cesNameToCEPNameSet is used to map CiliumEndpointSlice name to all CiliumEndpoints names it contains. 23 cesNameToCEPNameSet map[CESName]map[CEPName]struct{} 24 cesData map[CESName]CESData 25 } 26 27 // CESData contains all CES data except endpoints. 28 // CES is reconicled to have endpoints equal to CEPs mapped to it 29 // and other fields set from the CESData. 30 type CESData struct { 31 ns string 32 } 33 34 // Creates and intializes the new CESToCEPMapping 35 func newCESToCEPMapping() *CESToCEPMapping { 36 return &CESToCEPMapping{ 37 cepNameToCESName: make(map[CEPName]CESName), 38 cesNameToCEPNameSet: make(map[CESName]map[CEPName]struct{}), 39 cesData: make(map[CESName]CESData), 40 } 41 } 42 43 // Insert the CEP in cache, map CEP name to CES name 44 func (c *CESToCEPMapping) insertCEP(cepName CEPName, cesName CESName) { 45 c.mutex.Lock() 46 defer c.mutex.Unlock() 47 c.cepNameToCESName[cepName] = cesName 48 c.cesNameToCEPNameSet[cesName][cepName] = struct{}{} 49 } 50 51 // Remove the CEP entry from map 52 func (c *CESToCEPMapping) deleteCEP(cepName CEPName) { 53 c.mutex.Lock() 54 defer c.mutex.Unlock() 55 delete(c.cesNameToCEPNameSet[c.cepNameToCESName[cepName]], cepName) 56 delete(c.cepNameToCESName, cepName) 57 } 58 59 // Return CES to which the given CEP is assigned 60 func (c *CESToCEPMapping) getCESName(cepName CEPName) (CESName, bool) { 61 c.mutex.RLock() 62 defer c.mutex.RUnlock() 63 name, ok := c.cepNameToCESName[cepName] 64 return name, ok 65 } 66 67 func (c *CESToCEPMapping) hasCEP(cepName CEPName) bool { 68 c.mutex.RLock() 69 defer c.mutex.RUnlock() 70 _, ok := c.cepNameToCESName[cepName] 71 return ok 72 } 73 74 // Return total number of CEPs stored in cache 75 func (c *CESToCEPMapping) countCEPs() int { 76 c.mutex.RLock() 77 defer c.mutex.RUnlock() 78 return len(c.cepNameToCESName) 79 } 80 81 // Return total number of CEPs mapped to the given CES 82 func (c *CESToCEPMapping) countCEPsInCES(ces CESName) int { 83 c.mutex.RLock() 84 defer c.mutex.RUnlock() 85 return len(c.cesNameToCEPNameSet[ces]) 86 } 87 88 // Return CEP Names mapped to the given CES 89 func (c *CESToCEPMapping) getCEPsInCES(ces CESName) []CEPName { 90 c.mutex.RLock() 91 defer c.mutex.RUnlock() 92 ceps := make([]CEPName, 0, len(c.cesNameToCEPNameSet[ces])) 93 for cep := range c.cesNameToCEPNameSet[ces] { 94 ceps = append(ceps, cep) 95 } 96 return ceps 97 } 98 99 // Initializes mapping structure for CES 100 func (c *CESToCEPMapping) insertCES(cesName CESName, ns string) { 101 c.mutex.Lock() 102 defer c.mutex.Unlock() 103 c.cesNameToCEPNameSet[cesName] = make(map[CEPName]struct{}) 104 c.cesData[cesName] = CESData{ 105 ns: ns, 106 } 107 } 108 109 // Remove mapping structure for CES 110 func (c *CESToCEPMapping) deleteCES(cesName CESName) { 111 c.mutex.Lock() 112 defer c.mutex.Unlock() 113 delete(c.cesNameToCEPNameSet, cesName) 114 delete(c.cesData, cesName) 115 } 116 117 func (c *CESToCEPMapping) hasCESName(cesName CESName) bool { 118 c.mutex.RLock() 119 defer c.mutex.RUnlock() 120 _, ok := c.cesNameToCEPNameSet[cesName] 121 return ok 122 } 123 124 // Return the total number of CESs. 125 func (c *CESToCEPMapping) getCESCount() int { 126 c.mutex.RLock() 127 defer c.mutex.RUnlock() 128 return len(c.cesNameToCEPNameSet) 129 } 130 131 // Return names of all CESs. 132 func (c *CESToCEPMapping) getAllCESs() []CESName { 133 c.mutex.RLock() 134 defer c.mutex.RUnlock() 135 cess := make([]CESName, 0, len(c.cesNameToCEPNameSet)) 136 for ces := range c.cesNameToCEPNameSet { 137 cess = append(cess, ces) 138 } 139 return cess 140 } 141 142 // Return the CES data 143 func (c *CESToCEPMapping) getCESData(name CESName) CESData { 144 c.mutex.RLock() 145 defer c.mutex.RUnlock() 146 data := c.cesData[name] 147 return data 148 } 149 150 func (ces CESName) key() resource.Key { 151 return resource.Key(ces) 152 } 153 154 func (cep CEPName) key() resource.Key { 155 return resource.Key(cep) 156 } 157 158 func (ces CESName) string() string { 159 return ces.key().String() 160 } 161 162 func (cep CEPName) string() string { 163 return cep.key().String() 164 } 165 166 func NewCESName(name string) CESName { 167 return CESName(resource.Key{Name: name}) 168 } 169 170 func NewCEPName(name, ns string) CEPName { 171 return CEPName(resource.Key{Name: name, Namespace: ns}) 172 } 173 174 func GetCEPNameFromCCEP(cep *capi_v2a1.CoreCiliumEndpoint, namespace string) CEPName { 175 return NewCEPName(cep.Name, namespace) 176 }