forked from nocalhost/nocalhost
/
cacheresource.go
303 lines (282 loc) · 8.7 KB
/
cacheresource.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
* This source code is licensed under the Apache License Version 2.0.
*/
package cluster
import (
"context"
"encoding/json"
"fmt"
"github.com/lsutils/nocalhost/internal/nocalhost-api/cache"
"github.com/lsutils/nocalhost/internal/nocalhost-api/model"
"github.com/lsutils/nocalhost/pkg/nocalhost-api/pkg/clientgo"
"github.com/lsutils/nocalhost/pkg/nocalhost-api/pkg/log"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/flowcontrol"
"k8s.io/client-go/util/retry"
"math"
"math/rand"
"strconv"
"sync"
"time"
)
// resourceCache for cache resources(like cpu, memory, storage, pod number...), init cache with expire 15 seconds
var resourceCache = cache.NewCache(time.Second * 120)
var defaultValue = []model.Resource{
{ResourceName: v1.ResourcePods, Capacity: 0, Used: 0, Percentage: 0},
{ResourceName: v1.ResourceCPU, Capacity: 0, Used: 0, Percentage: 0},
{ResourceName: v1.ResourceMemory, Capacity: 0, Used: 0, Percentage: 0},
{ResourceName: v1.ResourceStorage, Capacity: 0, Used: 0, Percentage: 0},
}
var lock = sync.Mutex{}
var cacheRunnable = sync.Map{}
func Add(kubeconfig string) {
lock.Lock()
defer lock.Unlock()
load, ok := cacheRunnable.Load(kubeconfig)
if ok && load != nil {
return
}
if value, loaded := cacheRunnable.LoadAndDelete(kubeconfig); loaded && value != nil {
value.(context.CancelFunc)()
}
ctx, cancelFunc := context.WithCancel(context.TODO())
cacheRunnable.Store(kubeconfig, cancelFunc)
go func() {
after := time.NewTicker(time.Second * 30)
c := make(chan struct{}, 1)
c <- struct{}{}
for {
select {
case <-ctx.Done():
after.Stop()
close(c)
return
case <-after.C:
c <- struct{}{}
case <-c:
go reload(kubeconfig)
}
}
}()
}
func Remove(kubeconfig string) {
lock.Lock()
defer lock.Unlock()
if cancelFunc, found := cacheRunnable.LoadAndDelete(kubeconfig); found && cancelFunc != nil {
cancelFunc.(context.CancelFunc)()
}
}
// remove unneeded kubeconfig goroutines
func Merge(availableKubeConfigs map[string]string) {
lock.Lock()
defer lock.Unlock()
needsToBeDelete := make([]string, 0)
cacheRunnable.Range(func(key, value interface{}) bool {
if kubeconfig, found := availableKubeConfigs[key.(string)]; !found || len(kubeconfig) == 0 {
needsToBeDelete = append(needsToBeDelete, key.(string))
}
return true
})
for _, kubeconfig := range needsToBeDelete {
if value, loaded := cacheRunnable.LoadAndDelete(kubeconfig); loaded && value != nil {
value.(context.CancelFunc)()
}
}
}
func GetFromCache(kubeconfig string) []model.Resource {
get, found := resourceCache.Get(kubeconfig)
if found && get != nil {
return get.([]model.Resource)
} else {
return defaultValue
}
}
func reload(kubeconfig string) {
var resources []model.Resource
err := retry.OnError(wait.Backoff{
Steps: 5,
Duration: time.Duration(rand.Intn(4000)) * time.Millisecond,
Factor: 1.0,
Jitter: 0.1,
}, func(err error) bool { return err != nil }, func() error {
temp, err := fetchData(kubeconfig)
if err != nil {
return err
} else {
resources = temp
return nil
}
})
if err != nil || len(resources) == 0 {
return
}
// if resources is default value
if isSame(resources, defaultValue) {
// if resourceCache found not default value, then not need to replace it
if v, found := resourceCache.Get(kubeconfig); found && !isSame(v.([]model.Resource), defaultValue) {
return
}
}
resourceCache.Set(kubeconfig, resources)
}
// fetchData info by using metrics-api
func fetchData(kubeconfig string) ([]model.Resource, error) {
goclient, err := clientgo.NewGoClient([]byte(kubeconfig))
if err != nil {
log.Warn(fmt.Sprintf("init goclinet error, error: %v", err))
return defaultValue, err
}
restClient, err := goclient.GetRestClient()
if err != nil {
log.Warn(fmt.Sprintf("init restclient error, error: %v", err))
return defaultValue, err
}
list, err := goclient.GetClusterNode()
if err != nil {
log.Warn(fmt.Sprintf("get clusterNode error, error: %v", err))
return defaultValue, err
}
summaries := make([]model.Summary, len(list.Items), len(list.Items))
// nodes which get data error, those nodes needs to be ignored
errorNodes := sync.Map{}
wg := sync.WaitGroup{}
wg.Add(len(list.Items))
for i, node := range list.Items {
node := node
i := i
go func() {
// using metrics-api to get nodes stats summary
defer wg.Done()
var bytes []byte
err = retry.OnError(wait.Backoff{
Steps: 3,
Duration: time.Duration(rand.Intn(4000)) * time.Millisecond,
Factor: 1.0,
Jitter: 0.1,
}, func(err error) bool {
return err != nil
}, func() error {
temp, errs := restClient.Get().
Timeout(time.Second * 10).
Throttle(flowcontrol.NewTokenBucketRateLimiter(1000, 1000)).
RequestURI(fmt.Sprintf("/api/v1/nodes/%s/proxy/stats/summary", node.Name)).
DoRaw(context.Background())
if errs != nil {
return errs
} else {
bytes = temp
return nil
}
})
if err != nil || len(bytes) == 0 {
log.Warnf("get stats summary error, err: %v, ignore", err)
errorNodes.Store(node.Name, node.Name)
}
var s model.Summary
if err = json.Unmarshal(bytes, &s); err != nil {
errorNodes.Store(node.Name, node.Name)
}
summaries[i] = s
}()
}
wg.Wait()
summaries = summaries[0:]
var cpuTotal, memoryTotal, storageTotal, podTotal int64
cpuTotalMap := make(map[string]int64)
memoryTotalMap := make(map[string]int64)
storageTotalMap := make(map[string]int64)
podTotalMap := make(map[string]int64)
for _, node := range list.Items {
cpu := node.Status.Allocatable.Cpu().MilliValue()
// convert bytes to mega bytes (B --> MB)
memory := node.Status.Capacity.Memory().Value() / 1024 / 1024
storage := node.Status.Capacity.StorageEphemeral().Value() / 1024 / 1024
pod := node.Status.Capacity.Pods().Value()
cpuTotalMap[node.Name] = cpu
memoryTotalMap[node.Name] = memory
storageTotalMap[node.Name] = storage
podTotalMap[node.Name] = pod
cpuTotal += cpu
memoryTotal += memory
storageTotal += storage
podTotal += pod
}
cpuAvgs := make([]float64, 0, 0)
memoryAvgs := make([]float64, 0, 0)
storageAvgs := make([]float64, 0, 0)
podAvgs := make([]float64, 0, 0)
for _, summary := range summaries {
cpu := int64(summary.Node.CPU.UsageNanoCores) / int64(1000*1000)
memory := int64(summary.Node.Memory.WorkingSetBytes) / 1024 / 1024
storage := int64(summary.Node.Fs.UsedBytes) / 1024 / 1024
pod := int64(len(summary.Pods))
if _, found := errorNodes.Load(summary.Node.NodeName); !found {
cpuAvgs = append(cpuAvgs, DivInt64(cpu, cpuTotalMap[summary.Node.NodeName]))
memoryAvgs = append(memoryAvgs, DivInt64(memory, memoryTotalMap[summary.Node.NodeName]))
storageAvgs = append(storageAvgs, DivInt64(storage, storageTotalMap[summary.Node.NodeName]))
podAvgs = append(podAvgs, DivInt64(pod, podTotalMap[summary.Node.NodeName]))
}
}
// if all data is 0, then needs to retry
if cpuTotal+memoryTotal+storageTotal+podTotal == 0 {
return defaultValue, errors.New("all info is zero")
}
resources := make([]model.Resource, 0, 4)
resources = append(resources, model.Resource{
ResourceName: v1.ResourcePods,
Capacity: float64(podTotal),
Used: math.Floor(Avg(podAvgs) * float64(podTotal)),
Percentage: Avg(podAvgs),
}, model.Resource{
ResourceName: v1.ResourceCPU,
Capacity: DivFloat64(float64(cpuTotal), 1000),
Used: DivFloat64(Avg(cpuAvgs)*DivFloat64(float64(cpuTotal), 1000), 1),
Percentage: Avg(cpuAvgs),
}, model.Resource{
ResourceName: v1.ResourceMemory,
Capacity: DivFloat64(float64(memoryTotal), 1024),
Used: DivFloat64(Avg(memoryAvgs)*DivFloat64(float64(memoryTotal), 1024), 1),
Percentage: Avg(memoryAvgs),
}, model.Resource{
ResourceName: v1.ResourceStorage,
Capacity: DivFloat64(float64(storageTotal), 1024),
Used: DivFloat64(Avg(storageAvgs)*DivFloat64(float64(storageTotal), 1024), 1),
Percentage: Avg(storageAvgs),
})
return resources, nil
}
func Avg(numbers []float64) float64 {
numbers = numbers[0:]
total := float64(0)
for _, number := range numbers {
total += number
}
return DivFloat64(total, float64(len(numbers)))
}
func DivFloat64(a float64, b float64) float64 {
if b == 0 {
b = 1
}
if float, err := strconv.ParseFloat(fmt.Sprintf("%.2f", a/b), 64); err == nil {
return float
}
return 0
}
func DivInt64(a int64, b int64) float64 {
return DivFloat64(float64(a), float64(b))
}
func isSame(a, b []model.Resource) bool {
maps := map[v1.ResourceName]model.Resource{}
for _, m := range a {
maps[m.ResourceName] = m
}
for _, m := range b {
if v, found := maps[m.ResourceName]; !found || !v.Equals(m) {
return false
}
}
return true
}