@@ -33,12 +33,11 @@ func (cm *CollectorManager) StartFinalizerWatcher() {
33
33
cm .podFinalizerStateMutex = & sync.Mutex {}
34
34
// Initialize map
35
35
cm .podFinalizerState = make (map [string ]* PodProfileFinalizerState )
36
-
37
36
// Initialize watcher
38
- podFinalizerWatcher : = watcher .NewWatcher (cm .dynamicClient , false )
37
+ cm . podFinalizerWatcher = watcher .NewWatcher (cm .dynamicClient , false )
39
38
40
39
// Start watcher
41
- err := podFinalizerWatcher .Start (watcher.WatchNotifyFunctions {
40
+ err := cm . podFinalizerWatcher .Start (watcher.WatchNotifyFunctions {
42
41
AddFunc : func (obj * unstructured.Unstructured ) {
43
42
cm .handlePodAddEvent (obj )
44
43
},
@@ -56,35 +55,9 @@ func (cm *CollectorManager) StartFinalizerWatcher() {
56
55
57
56
if err != nil {
58
57
log .Printf ("Error starting watcher: %v" , err )
58
+ cm .podFinalizerWatcher = nil
59
59
return
60
60
}
61
-
62
- // Initialize factory and informer
63
- // factory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(cm.dynamicClient, 0, metav1.NamespaceAll, func(lo *metav1.ListOptions) {
64
- // lo.FieldSelector = "spec.nodeName=" + cm.config.NodeName
65
- // })
66
- // // Informer for Pods
67
- // informer := factory.ForResource(schema.GroupVersionResource{
68
- // Group: "",
69
- // Version: "v1",
70
- // Resource: "pods",
71
- // }).Informer()
72
-
73
- // // Add event handlers to informer
74
- // informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
75
- // AddFunc: func(obj interface{}) {
76
- // cm.handlePodAddEvent(obj)
77
- // },
78
- // UpdateFunc: func(oldObj, newObj interface{}) {
79
- // cm.handlePodUpdateEvent(oldObj, newObj)
80
- // },
81
- // DeleteFunc: func(obj interface{}) {
82
- // cm.handlePodDeleteEvent(obj)
83
- // },
84
- // })
85
-
86
- // // Run the informer
87
- // go informer.Run(cm.podFinalizerControl)
88
61
}
89
62
90
63
func generateTableKey (obj metav1.Object ) string {
@@ -93,8 +66,6 @@ func generateTableKey(obj metav1.Object) string {
93
66
94
67
func (cm * CollectorManager ) handlePodAddEvent (obj interface {}) {
95
68
// Add pod to finalizer map
96
-
97
- // Convert object to Pod
98
69
pod , err := ConvertInterfaceToPod (obj )
99
70
if err != nil {
100
71
log .Printf ("the interface is not a Pod %v" , err )
@@ -127,7 +98,6 @@ func (cm *CollectorManager) handlePodAddEvent(obj interface{}) {
127
98
cm .startFinalizationTimer (pod )
128
99
}
129
100
}
130
-
131
101
}
132
102
133
103
func (cm * CollectorManager ) handlePodUpdateEvent (obj interface {}) {
@@ -182,76 +152,10 @@ func (cm *CollectorManager) handlePodUpdateEvent(obj interface{}) {
182
152
}
183
153
}
184
154
185
- // func (cm *CollectorManager) handlePodUpdateEvent(oldObj, newObj interface{}) {
186
- // // Need to access the status of the old and new pod to check if the pod became ready
187
-
188
- // // Convert interface to Pod object
189
- // oldPod, err := ConvertInterfaceToPod(oldObj)
190
- // if err != nil {
191
- // log.Printf("the interface is not a Pod %v", err)
192
- // return
193
- // }
194
- // newPod, err := ConvertInterfaceToPod(newObj)
195
- // if err != nil {
196
- // log.Printf("the interface is not a Pod %v", err)
197
- // return
198
- // }
199
-
200
- // // Check if recoding
201
- // cm.podFinalizerStateMutex.Lock()
202
- // finalizerState, ok := cm.podFinalizerState[generateTableKey(oldPod)]
203
- // if !ok || !finalizerState.Recording {
204
- // // Discard
205
- // cm.podFinalizerStateMutex.Unlock()
206
- // return
207
- // }
208
- // cm.podFinalizerStateMutex.Unlock()
209
-
210
- // // Check old pod status
211
- // oldPodReady := false
212
- // for _, condition := range oldPod.Status.Conditions {
213
- // if condition.Type == v1.PodReady && condition.Status == v1.ConditionTrue {
214
- // oldPodReady = true
215
- // }
216
- // }
217
-
218
- // newPodReady := false
219
- // for _, condition := range newPod.Status.Conditions {
220
- // if condition.Type == v1.PodReady && condition.Status == v1.ConditionTrue {
221
- // newPodReady = true
222
- // }
223
- // }
224
-
225
- // if newPodReady && !oldPodReady {
226
- // // Pod became ready, add finalizer
227
- // // Get mutex
228
- // cm.podFinalizerStateMutex.Lock()
229
- // defer cm.podFinalizerStateMutex.Unlock()
230
-
231
- // // Check if pod is in map
232
- // podState, ok := cm.podFinalizerState[generateTableKey(newPod)]
233
- // if !ok {
234
- // log.Printf("Pod %s in namespace %s not in finalizer map", newPod.GetName(), newPod.GetNamespace())
235
- // return
236
- // }
237
-
238
- // // Check if timer is running
239
- // if podState.FinalizationTimer != nil {
240
- // // Timer is running, no need to add finalizer
241
- // return
242
- // }
243
-
244
- // // Timer is not running, add finalizer
245
- // podState.FinalizationTimer = cm.startFinalizationTimer(newPod)
246
- // } else if !newPodReady && oldPodReady {
247
- // cm.stopTimer(&newPod.ObjectMeta)
248
- // }
249
- // }
250
-
251
155
// Timer function
252
156
func (cm * CollectorManager ) startFinalizationTimer (pod * v1.Pod ) * time.Timer {
253
- jitter := time . Duration (rand .Intn (int (cm .config .FinalizeJitter ))) * time . Second
254
- finalizationTimer := time .NewTimer (time .Duration (cm .config .FinalizeTime + uint64 ( jitter ) ) * time .Second )
157
+ jitter := uint64 (rand .Intn (int (cm .config .FinalizeJitter )))
158
+ finalizationTimer := time .NewTimer (time .Duration (cm .config .FinalizeTime + jitter ) * time .Second )
255
159
256
160
// This goroutine waits for the timer to finish.
257
161
go func () {
@@ -379,7 +283,11 @@ func (cm *CollectorManager) MarkPodNotRecording(pod, namespace string) {
379
283
}
380
284
381
285
func (cm * CollectorManager ) StopFinalizerWatcher () {
382
- close (cm .podFinalizerControl )
286
+ if cm .podFinalizerWatcher != nil {
287
+ cm .podFinalizerWatcher .Stop ()
288
+ } else {
289
+ log .Printf ("Pod finalizer watcher not started" )
290
+ }
383
291
}
384
292
385
293
func ConvertInterfaceToPod (obj interface {}) (* v1.Pod , error ) {
0 commit comments