github.com/cilium/cilium@v1.16.2/pkg/ipam/metrics/mock/mock.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package mock 5 6 import ( 7 "fmt" 8 9 "github.com/cilium/cilium/pkg/lock" 10 "github.com/cilium/cilium/pkg/trigger" 11 ) 12 13 type mockMetrics struct { 14 mutex lock.RWMutex 15 allocationAttempts map[string]histogram 16 releaseAttempts map[string]histogram 17 ipAllocations map[string]int64 18 ipReleases map[string]int64 19 interfaceAllocations map[string]int64 20 allocatedIPs map[string]int 21 availableInterfaces int 22 interfaceCandidates int 23 emptyInterfaceSlots int 24 availableIPsPerSubnet map[string]int 25 nodes map[string]int 26 resyncCount int64 27 nodeIPAvailable map[string]int 28 nodeIPUsed map[string]int 29 nodeIPNeeded map[string]int 30 } 31 32 type histogram struct { 33 count int64 34 sum float64 35 } 36 37 // NewMockMetrics returns a new metrics implementation with a mocked backend 38 func NewMockMetrics() *mockMetrics { 39 return &mockMetrics{ 40 allocationAttempts: map[string]histogram{}, 41 releaseAttempts: map[string]histogram{}, 42 interfaceAllocations: map[string]int64{}, 43 ipAllocations: map[string]int64{}, 44 ipReleases: map[string]int64{}, 45 allocatedIPs: map[string]int{}, 46 nodes: map[string]int{}, 47 availableIPsPerSubnet: map[string]int{}, 48 nodeIPAvailable: map[string]int{}, 49 nodeIPUsed: map[string]int{}, 50 nodeIPNeeded: map[string]int{}, 51 } 52 } 53 54 func (m *mockMetrics) GetAllocationAttempts(typ, status, subnetID string) int64 { 55 m.mutex.RLock() 56 defer m.mutex.RUnlock() 57 return m.allocationAttempts[fmt.Sprintf("type=%s, status=%s, subnetId=%s", typ, status, subnetID)].count 58 } 59 60 func (m *mockMetrics) AllocationAttempt(typ, status, subnetID string, observer float64) { 61 m.mutex.Lock() 62 defer m.mutex.Unlock() 63 key := fmt.Sprintf("type=%s, status=%s, subnetId=%s", typ, status, subnetID) 64 value := m.allocationAttempts[key] 65 value.count++ 66 value.sum += observer 67 m.allocationAttempts[key] = value 68 } 69 70 func (m *mockMetrics) ReleaseAttempt(typ, status, subnetID string, observer float64) { 71 m.mutex.Lock() 72 defer m.mutex.Unlock() 73 key := fmt.Sprintf("type=%s, status=%s, subnetId=%s", typ, status, subnetID) 74 value := m.releaseAttempts[key] 75 value.count++ 76 value.sum += observer 77 m.releaseAttempts[key] = value 78 } 79 80 func (m *mockMetrics) IncInterfaceAllocation(subnetID string) { 81 m.mutex.Lock() 82 m.interfaceAllocations[fmt.Sprintf("subnetId=%s", subnetID)]++ 83 m.mutex.Unlock() 84 } 85 86 func (m *mockMetrics) IPAllocations(subnetID string) int64 { 87 m.mutex.RLock() 88 defer m.mutex.RUnlock() 89 return m.ipAllocations["subnetId="+subnetID] 90 } 91 92 func (m *mockMetrics) AddIPAllocation(subnetID string, allocated int64) { 93 m.mutex.Lock() 94 m.ipAllocations["subnetId="+subnetID] += allocated 95 m.mutex.Unlock() 96 } 97 98 func (m *mockMetrics) AddIPRelease(subnetID string, released int64) { 99 m.mutex.Lock() 100 m.ipReleases["subnetId="+subnetID] += released 101 m.mutex.Unlock() 102 } 103 104 func (m *mockMetrics) AllocatedIPs(typ string) int { 105 m.mutex.RLock() 106 defer m.mutex.RUnlock() 107 return m.allocatedIPs[typ] 108 } 109 110 func (m *mockMetrics) SetAllocatedIPs(typ string, allocated int) { 111 m.mutex.Lock() 112 m.allocatedIPs[typ] = allocated 113 m.mutex.Unlock() 114 } 115 116 func (m *mockMetrics) AvailableInterfaces() int { 117 m.mutex.RLock() 118 defer m.mutex.RUnlock() 119 return m.availableInterfaces 120 } 121 122 func (m *mockMetrics) SetAvailableInterfaces(available int) { 123 m.mutex.Lock() 124 m.availableInterfaces = available 125 m.mutex.Unlock() 126 } 127 128 func (m *mockMetrics) InterfaceCandidates() int { 129 m.mutex.RLock() 130 defer m.mutex.RUnlock() 131 return m.interfaceCandidates 132 } 133 134 func (m *mockMetrics) SetInterfaceCandidates(interfaceCandidates int) { 135 m.mutex.Lock() 136 m.interfaceCandidates = interfaceCandidates 137 m.mutex.Unlock() 138 } 139 140 func (m *mockMetrics) EmptyInterfaceSlots() int { 141 m.mutex.RLock() 142 defer m.mutex.RUnlock() 143 return m.emptyInterfaceSlots 144 } 145 146 func (m *mockMetrics) SetEmptyInterfaceSlots(emptyInterfaceSlots int) { 147 m.mutex.Lock() 148 m.emptyInterfaceSlots = emptyInterfaceSlots 149 m.mutex.Unlock() 150 } 151 152 func (m *mockMetrics) Nodes(category string) int { 153 m.mutex.RLock() 154 defer m.mutex.RUnlock() 155 return m.nodes[category] 156 } 157 158 func (m *mockMetrics) SetNodes(category string, nodes int) { 159 m.mutex.Lock() 160 m.nodes[category] = nodes 161 m.mutex.Unlock() 162 } 163 164 func (m *mockMetrics) SetAvailableIPsPerSubnet(subnetID, availabilityZone string, available int) { 165 m.mutex.Lock() 166 m.availableIPsPerSubnet[fmt.Sprintf("subnetId=%s, availabilityZone=%s", subnetID, availabilityZone)] = available 167 m.mutex.Unlock() 168 } 169 170 func (m *mockMetrics) ResyncCount() int64 { 171 m.mutex.Lock() 172 defer m.mutex.Unlock() 173 return m.resyncCount 174 } 175 176 func (m *mockMetrics) IncResyncCount() { 177 m.mutex.Lock() 178 m.resyncCount++ 179 m.mutex.Unlock() 180 } 181 182 func (m *mockMetrics) SetIPAvailable(s string, n int) { 183 m.mutex.Lock() 184 m.nodeIPAvailable[s] = n 185 m.mutex.Unlock() 186 } 187 188 func (m *mockMetrics) SetIPUsed(s string, n int) { 189 m.mutex.Lock() 190 m.nodeIPUsed[s] = n 191 m.mutex.Unlock() 192 } 193 194 func (m *mockMetrics) SetIPNeeded(s string, n int) { 195 m.mutex.Lock() 196 m.nodeIPNeeded[s] = n 197 m.mutex.Unlock() 198 } 199 200 func (m *mockMetrics) GetPerNodeMetrics(n string) (*int, *int, *int) { 201 m.mutex.Lock() 202 defer m.mutex.Unlock() 203 var avail, used, needed *int 204 if c, ok := m.nodeIPAvailable[n]; ok { 205 avail = &c 206 } 207 if c, ok := m.nodeIPUsed[n]; ok { 208 used = &c 209 } 210 if c, ok := m.nodeIPNeeded[n]; ok { 211 needed = &c 212 } 213 return avail, used, needed 214 } 215 216 func (m *mockMetrics) DeleteNode(n string) { 217 m.mutex.Lock() 218 defer m.mutex.Unlock() 219 delete(m.nodeIPAvailable, n) 220 delete(m.nodeIPUsed, n) 221 delete(m.nodeIPNeeded, n) 222 } 223 224 func (m *mockMetrics) PoolMaintainerTrigger() trigger.MetricsObserver { 225 return nil 226 } 227 228 func (m *mockMetrics) K8sSyncTrigger() trigger.MetricsObserver { 229 return nil 230 } 231 232 func (m *mockMetrics) ResyncTrigger() trigger.MetricsObserver { 233 return nil 234 }