github.phpd.cn/cilium/cilium@v1.6.12/pkg/aws/eni/metrics/mock/mock.go (about) 1 // Copyright 2019 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 mock 16 17 import ( 18 "fmt" 19 "time" 20 21 "github.com/cilium/cilium/pkg/lock" 22 "github.com/cilium/cilium/pkg/trigger" 23 ) 24 25 type mockMetrics struct { 26 mutex lock.RWMutex 27 allocationAttempts map[string]int64 28 ipAllocations map[string]int64 29 ipReleases map[string]int64 30 allocatedIPs map[string]int 31 availableENIs int 32 availableIPsPerSubnet map[string]int 33 nodes map[string]int 34 ec2ApiCall map[string]float64 35 ec2RateLimit map[string]time.Duration 36 resyncCount int64 37 } 38 39 // NewMockMetrics returns a new metrics implementation with a mocked backend 40 func NewMockMetrics() *mockMetrics { 41 return &mockMetrics{ 42 allocationAttempts: 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 ec2ApiCall: map[string]float64{}, 49 ec2RateLimit: map[string]time.Duration{}, 50 } 51 } 52 53 func (m *mockMetrics) ENIAllocationAttempts(status, subnetID string) int64 { 54 m.mutex.RLock() 55 defer m.mutex.RUnlock() 56 return m.allocationAttempts[fmt.Sprintf("status=%s, subnetId=%s", status, subnetID)] 57 } 58 59 func (m *mockMetrics) IncENIAllocationAttempt(status, subnetID string) { 60 m.mutex.Lock() 61 m.allocationAttempts[fmt.Sprintf("status=%s, subnetId=%s", status, subnetID)]++ 62 m.mutex.Unlock() 63 } 64 65 func (m *mockMetrics) IPAllocations(subnetID string) int64 { 66 m.mutex.RLock() 67 defer m.mutex.RUnlock() 68 return m.ipAllocations["subnetId="+subnetID] 69 } 70 71 func (m *mockMetrics) AddIPAllocation(subnetID string, allocated int64) { 72 m.mutex.Lock() 73 m.ipAllocations["subnetId="+subnetID] += allocated 74 m.mutex.Unlock() 75 } 76 77 func (m *mockMetrics) AddIPRelease(subnetID string, released int64) { 78 m.mutex.Lock() 79 m.ipReleases["subnetId="+subnetID] += released 80 m.mutex.Unlock() 81 } 82 83 func (m *mockMetrics) AllocatedIPs(typ string) int { 84 m.mutex.RLock() 85 defer m.mutex.RUnlock() 86 return m.allocatedIPs[typ] 87 } 88 89 func (m *mockMetrics) SetAllocatedIPs(typ string, allocated int) { 90 m.mutex.Lock() 91 m.allocatedIPs[typ] = allocated 92 m.mutex.Unlock() 93 } 94 95 func (m *mockMetrics) AvailableENIs() int { 96 m.mutex.RLock() 97 defer m.mutex.RUnlock() 98 return m.availableENIs 99 } 100 101 func (m *mockMetrics) SetAvailableENIs(available int) { 102 m.mutex.Lock() 103 m.availableENIs = available 104 m.mutex.Unlock() 105 } 106 107 func (m *mockMetrics) Nodes(category string) int { 108 m.mutex.RLock() 109 defer m.mutex.RUnlock() 110 return m.nodes[category] 111 } 112 113 func (m *mockMetrics) SetNodes(category string, nodes int) { 114 m.mutex.Lock() 115 m.nodes[category] = nodes 116 m.mutex.Unlock() 117 } 118 119 func (m *mockMetrics) EC2APICall(operation, status string) float64 { 120 m.mutex.RLock() 121 defer m.mutex.RUnlock() 122 return m.ec2ApiCall[fmt.Sprintf("operation=%s, status=%s", operation, status)] 123 } 124 125 func (m *mockMetrics) ObserveEC2APICall(operation, status string, duration float64) { 126 m.mutex.Lock() 127 m.ec2ApiCall[fmt.Sprintf("operation=%s, status=%s", operation, status)] += duration 128 m.mutex.Unlock() 129 } 130 131 func (m *mockMetrics) EC2RateLimit(operation string) time.Duration { 132 m.mutex.RLock() 133 defer m.mutex.RUnlock() 134 return m.ec2RateLimit[operation] 135 } 136 137 func (m *mockMetrics) ObserveEC2RateLimit(operation string, delay time.Duration) { 138 m.mutex.Lock() 139 m.ec2RateLimit[operation] += delay 140 m.mutex.Unlock() 141 } 142 143 func (m *mockMetrics) SetAvailableIPsPerSubnet(subnetID, availabilityZone string, available int) { 144 m.mutex.Lock() 145 m.availableIPsPerSubnet[fmt.Sprintf("subnetId=%s, availabilityZone=%s", subnetID, availabilityZone)] = available 146 m.mutex.Unlock() 147 } 148 149 func (m *mockMetrics) ResyncCount() int64 { 150 m.mutex.Lock() 151 defer m.mutex.Unlock() 152 return m.resyncCount 153 } 154 155 func (m *mockMetrics) IncResyncCount() { 156 m.mutex.Lock() 157 m.resyncCount++ 158 m.mutex.Unlock() 159 } 160 161 func (m *mockMetrics) PoolMaintainerTrigger() trigger.MetricsObserver { 162 return nil 163 } 164 165 func (m *mockMetrics) K8sSyncTrigger() trigger.MetricsObserver { 166 return nil 167 } 168 169 func (m *mockMetrics) ResyncTrigger() trigger.MetricsObserver { 170 return nil 171 }