-
Notifications
You must be signed in to change notification settings - Fork 89
/
cnr.go
336 lines (289 loc) · 10.2 KB
/
cnr.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
Copyright 2022 The Katalyst Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package monitor
import (
"context"
"fmt"
"sync"
"time"
corev1 "k8s.io/api/core/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
coreinformers "k8s.io/client-go/informers/core/v1"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/cri-api/pkg/errors"
"k8s.io/klog/v2"
apis "github.com/kubewharf/katalyst-api/pkg/apis/node/v1alpha1"
informers "github.com/kubewharf/katalyst-api/pkg/client/informers/externalversions/node/v1alpha1"
listers "github.com/kubewharf/katalyst-api/pkg/client/listers/node/v1alpha1"
"github.com/kubewharf/katalyst-core/pkg/client"
"github.com/kubewharf/katalyst-core/pkg/config/controller"
"github.com/kubewharf/katalyst-core/pkg/config/generic"
"github.com/kubewharf/katalyst-core/pkg/metrics"
"github.com/kubewharf/katalyst-core/pkg/util/native"
)
const (
cnrMonitorControllerName = "cnr-monitor"
cnrMonitorWorkerCount = 1
)
const (
// maxToleranceLantency is the max tolerance lantency for cnr report lantency
maxToleranceLantency = 5 * time.Minute
)
type CNRMonitorController struct {
ctx context.Context
client *client.GenericClientSet
cnrListerSynced cache.InformerSynced
cnrLister listers.CustomNodeResourceLister
nodeListerSynced cache.InformerSynced
nodeLister corelisters.NodeLister
podListerSynced cache.InformerSynced
podLister corelisters.PodLister
// queue for cnr
cnrSyncQueue workqueue.RateLimitingInterface
// metricsEmitter for emit metrics
metricsEmitter metrics.MetricEmitter
// podTimeMap for record pod scheduled time
podTimeMap sync.Map
}
// NewCNRMonitorController create a new CNRMonitorController
func NewCNRMonitorController(
ctx context.Context,
genericConf *generic.GenericConfiguration,
_ *controller.GenericControllerConfiguration,
_ *controller.CNRMonitorConfig,
client *client.GenericClientSet,
nodeInformer coreinformers.NodeInformer,
podInformer coreinformers.PodInformer,
cnrInformer informers.CustomNodeResourceInformer,
metricsEmitter metrics.MetricEmitter,
) (*CNRMonitorController, error) {
cnrMonitorController := &CNRMonitorController{
ctx: ctx,
client: client,
cnrSyncQueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), cnrMonitorControllerName),
podTimeMap: sync.Map{},
}
// init cnr informer
cnrInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: cnrMonitorController.addCNREventHandler,
UpdateFunc: cnrMonitorController.updateCNREventHandler,
})
// init cnr lister
cnrMonitorController.cnrLister = cnrInformer.Lister()
// init cnr synced
cnrMonitorController.cnrListerSynced = cnrInformer.Informer().HasSynced
// init node lister
cnrMonitorController.nodeLister = nodeInformer.Lister()
// init node synced
cnrMonitorController.nodeListerSynced = nodeInformer.Informer().HasSynced
// init pod informer
podInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: cnrMonitorController.updatePodEventHandler,
})
// init pod lister
cnrMonitorController.podLister = podInformer.Lister()
// init pod synced
cnrMonitorController.podListerSynced = podInformer.Informer().HasSynced
if metricsEmitter == nil {
// if metricsEmitter is nil, use dummy metrics
cnrMonitorController.metricsEmitter = metrics.DummyMetrics{}
} else {
// if metricsEmitter is not nil, use metricsEmitter with tags
cnrMonitorController.metricsEmitter = metricsEmitter.WithTags(cnrMonitorControllerName)
}
return cnrMonitorController, nil
}
func (ctrl *CNRMonitorController) Run() {
defer utilruntime.HandleCrash()
defer ctrl.cnrSyncQueue.ShutDown()
defer klog.Infof("Shutting down %s controller", cnrMonitorControllerName)
// wait for cnr cache sync
if !cache.WaitForCacheSync(ctrl.ctx.Done(), ctrl.cnrListerSynced, ctrl.nodeListerSynced, ctrl.podListerSynced) {
utilruntime.HandleError(fmt.Errorf("unable to sync caches for %s controller", cnrMonitorControllerName))
return
}
klog.Infof("Caches are synced for %s controller", cnrMonitorControllerName)
klog.Infof("start %d workers for %s controller", cnrMonitorWorkerCount, cnrMonitorControllerName)
for i := 0; i < cnrMonitorWorkerCount; i++ {
go wait.Until(ctrl.cnrMonitorWorker, time.Second, ctrl.ctx.Done())
}
// gc podTimeMap
klog.Infof("start gc podTimeMap...")
go wait.Until(ctrl.gcPodTimeMap, maxToleranceLantency, ctrl.ctx.Done())
<-ctrl.ctx.Done()
}
func (ctrl *CNRMonitorController) cnrMonitorWorker() {
for ctrl.processNextCNR() {
}
}
// processNextCNR dequeues items, processes them, and marks them done.
// It enforces that the sync is never invoked concurrently with the same key.
func (ctrl *CNRMonitorController) processNextCNR() bool {
key, quit := ctrl.cnrSyncQueue.Get()
if quit {
return false
}
defer ctrl.cnrSyncQueue.Done(key)
err := ctrl.syncCNR(key.(string))
if err == nil {
ctrl.cnrSyncQueue.Forget(key)
return true
}
// if err is not nil, requeue the key
utilruntime.HandleError(fmt.Errorf("sync %s failed with %v", key, err))
ctrl.cnrSyncQueue.AddRateLimited(key)
return true
}
func (ctrl *CNRMonitorController) syncCNR(key string) error {
_, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
cnr, err := ctrl.cnrLister.Get(name)
if errors.IsNotFound(err) {
// cnr is deleted, so we can skip
klog.Info("CNR has been deleted %v", key)
return nil
}
if err != nil {
return err
}
// hasAnomaly is used to record whether cnr has anomaly
hasAnomaly := false
// check numa exclusive anomaly
klog.Infof("Check Numa Exclusive Anomaly...")
if ctrl.checkNumaExclusiveAnomaly(cnr) {
hasAnomaly = true
klog.Infof("Emit Numa Exclusive Anomaly metric...")
err = ctrl.emitCNRAnomalyMetric(cnr, reasonNumaExclusiveAnomaly)
if err != nil {
return err
}
}
// check numa allocatable sum anomaly
klog.Infof("Check Numa Allocatable Sum Anomaly...")
if ctrl.checkNumaAllocatableSumAnomaly(cnr) {
hasAnomaly = true
klog.Infof("Emit Numa Allocatable Sum Anomaly metric...")
err = ctrl.emitCNRAnomalyMetric(cnr, reasonNumaAllocatableSumAnomaly)
if err != nil {
return err
}
}
// check pod allocation sum anomaly
klog.Infof("Check Pod Allocation Sum Anomaly...")
if ctrl.checkPodAllocationSumAnomaly(cnr) {
hasAnomaly = true
klog.Infof("Emit Pod Allocation Sum Anomaly metric...")
err = ctrl.emitCNRAnomalyMetric(cnr, reasonPodAllocationSumAnomaly)
if err != nil {
return err
}
}
// if hasAnomaly is true, re-enqueue cnr use AddAfter func with 30s duration
if hasAnomaly {
klog.Infof("CNR %s has anomaly, re-enqueue it after 30s", cnr.Name)
ctrl.cnrSyncQueue.AddAfter(key, 30*time.Second)
}
return nil
}
// enqueueCNR enqueues the given CNR in the work queue.
func (ctrl *CNRMonitorController) enqueueCNR(cnr *apis.CustomNodeResource) {
if cnr == nil {
klog.Warning("trying to enqueue a nil cnr")
return
}
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(cnr)
if err != nil {
utilruntime.HandleError(fmt.Errorf("Cound't get key for CNR %+v: %v", cnr, err))
return
}
ctrl.cnrSyncQueue.Add(key)
}
func (ctrl *CNRMonitorController) addCNREventHandler(obj interface{}) {
klog.Infof("CNR create event found...")
cnr, ok := obj.(*apis.CustomNodeResource)
if !ok {
klog.Errorf("cannot convert obj to *apis.CNR")
return
}
klog.V(4).Infof("notice addition of cnr %s", cnr.Name)
ctrl.enqueueCNR(cnr)
}
func (ctrl *CNRMonitorController) updateCNREventHandler(_, newObj interface{}) {
klog.Infof("CNR update event found...")
cnr, ok := newObj.(*apis.CustomNodeResource)
if !ok {
klog.Errorf("cannot convert newObj to *apis.CNR")
return
}
klog.V(4).Infof("notice update of cnr %s", cnr.Name)
// check and emit cnr pod report lantency metric
klog.Infof("Check and Emit CNR Report Lantency metric...")
err := ctrl.checkAndEmitCNRReportLantencyMetric(cnr)
if err != nil {
klog.Errorf("check and emit cnr report lantency metric failed: %v", err)
}
ctrl.enqueueCNR(cnr)
}
func (ctrl *CNRMonitorController) updatePodEventHandler(oldObj, newObj interface{}) {
klog.Infof("Pod update event found...")
oldPod, ok := oldObj.(*corev1.Pod)
if !ok {
klog.Errorf("cannot convert oldObj to Pod")
return
}
newPod, ok := newObj.(*corev1.Pod)
if !ok {
klog.Errorf("cannot convert newObj to Pod")
return
}
if oldPod.Spec.NodeName == "" && newPod.Spec.NodeName != "" {
klog.Infof("notice pod: %v scheduled to node: %v", newPod.Name, newPod.Spec.NodeName)
// record pod scheduled time
ctrl.podTimeMap.Store(native.GenerateUniqObjectUIDKey(newPod), time.Now())
}
}
// gcPodTimeMap gc podTimeMap which over maxToleranceLantency not used
func (ctrl *CNRMonitorController) gcPodTimeMap() {
klog.Infof("gc podTimeMap...")
ctrl.podTimeMap.Range(func(key, value interface{}) bool {
notUsedTime := time.Now().Sub(value.(time.Time))
if notUsedTime > maxToleranceLantency {
klog.Infof("gc podTimeMap: %v, which not used over %v minutes", key, notUsedTime.Minutes())
ctrl.podTimeMap.Delete(key)
namespace, podName, _, err := native.ParseUniqObjectUIDKey(key.(string))
if err != nil {
klog.Errorf("failed to parse uniq object uid key %s", key)
return true
}
pod, err := ctrl.podLister.Pods(namespace).Get(podName)
if err != nil {
klog.Errorf("failed to get pod %s/%s", namespace, podName)
return true
}
if !native.PodIsTerminated(pod) {
klog.Infof("Emit Timeout CNR Report Lantency metric...")
// emit timeout cnr report lantency metric
ctrl.emitCNRReportLantencyMetric(pod.Spec.NodeName, key.(string), maxToleranceLantency.Milliseconds(), "true")
if err != nil {
klog.Errorf("emit cnr report lantency metric failed: %v", err)
}
}
}
return true // return true to continue iterating
})
}