@@ -303,6 +303,7 @@ var _ = Describe("NodeModulesConfigReconciler_Reconcile", func() {
303303 wh .EXPECT ().ProcessModuleSpec (contextWithValueMatch , nmc , & spec1 , nil , & node ),
304304 wh .EXPECT ().ProcessUnconfiguredModuleStatus (contextWithValueMatch , nmc , & status2 , & node ),
305305 wh .EXPECT ().GarbageCollectInUseLabels (ctx , nmc ),
306+ wh .EXPECT ().GarbageCollectWorkerPods (ctx , nmc ),
306307 wh .EXPECT ().UpdateNodeLabels (ctx , nmc , & node ).Return (loaded , unloaded , err ),
307308 wh .EXPECT ().RecordEvents (& node , loaded , unloaded ),
308309 )
@@ -330,6 +331,7 @@ var _ = Describe("NodeModulesConfigReconciler_Reconcile", func() {
330331 fmt .Errorf ("error processing Module %s: %v" , namespace + "/" + mod0Name , errorMeassge ),
331332 fmt .Errorf ("error processing orphan status for Module %s: %v" , namespace + "/" + mod2Name , errorMeassge ),
332333 fmt .Errorf ("failed to GC in-use labels for NMC %s: %v" , types.NamespacedName {Name : nmcName }, errorMeassge ),
334+ fmt .Errorf ("failed to GC orphan worker pods for NMC %s: %v" , types.NamespacedName {Name : nmcName }, errorMeassge ),
333335 fmt .Errorf ("could not update node's labels for NMC %s: %v" , types.NamespacedName {Name : nmcName }, errorMeassge ),
334336 }
335337
@@ -381,6 +383,7 @@ var _ = Describe("NodeModulesConfigReconciler_Reconcile", func() {
381383 wh .EXPECT ().ProcessModuleSpec (contextWithValueMatch , nmc , & spec0 , & status0 , & node ).Return (errors .New (errorMeassge )),
382384 wh .EXPECT ().ProcessUnconfiguredModuleStatus (contextWithValueMatch , nmc , & status2 , & node ).Return (errors .New (errorMeassge )),
383385 wh .EXPECT ().GarbageCollectInUseLabels (ctx , nmc ).Return (errors .New (errorMeassge )),
386+ wh .EXPECT ().GarbageCollectWorkerPods (ctx , nmc ).Return (errors .New (errorMeassge )),
384387 wh .EXPECT ().UpdateNodeLabels (ctx , nmc , & node ).Return (nil , nil , errors .New (errorMeassge )),
385388 )
386389
@@ -389,6 +392,91 @@ var _ = Describe("NodeModulesConfigReconciler_Reconcile", func() {
389392 })
390393})
391394
395+ var _ = Describe ("nmcReconcilerHelperImpl_GarbageCollectWorkerPods" , func () {
396+ var (
397+ ctx = context .TODO ()
398+ client * testclient.MockClient
399+ pm * pod.MockWorkerPodManager
400+ nrh nmcReconcilerHelper
401+ )
402+
403+ BeforeEach (func () {
404+ ctrl := gomock .NewController (GinkgoT ())
405+ client = testclient .NewMockClient (ctrl )
406+ pm = pod .NewMockWorkerPodManager (ctrl )
407+ nrh = newNMCReconcilerHelper (client , pm , nil , nil )
408+ })
409+
410+ It ("should delete orphaned worker pod" , func () {
411+ nmcSpec := kmmv1beta1.NodeModulesConfigSpec {
412+ Modules : []kmmv1beta1.NodeModuleSpec {
413+ {
414+ ModuleItem : kmmv1beta1.ModuleItem {Name : nameSecond },
415+ },
416+ },
417+ }
418+ nmcObj := & kmmv1beta1.NodeModulesConfig {
419+ ObjectMeta : metav1.ObjectMeta {Name : nmcName },
420+ Spec : nmcSpec ,
421+ }
422+ pod := v1.Pod {
423+ ObjectMeta : metav1.ObjectMeta {
424+ Name : fmt .Sprintf ("kmm-worker-%s-%s" , nmcObj .Name , nameFirst ),
425+ Namespace : "d" ,
426+ Labels : map [string ]string {
427+ constants .ModuleNameLabel : nameFirst ,
428+ },
429+ Finalizers : []string {pod .NodeModulesConfigFinalizer },
430+ },
431+ }
432+
433+ gomock .InOrder (
434+ pm .EXPECT ().ListWorkerPodsOnNode (ctx , nmcName ).Return ([]v1.Pod {pod }, nil ),
435+ client .EXPECT ().Patch (ctx , gomock .Any (), gomock .Any ()).Return (nil ),
436+ pm .EXPECT ().DeletePod (ctx , gomock .Any ()).Return (nil ),
437+ )
438+
439+ Expect (
440+ nrh .GarbageCollectWorkerPods (ctx , nmcObj ),
441+ ).NotTo (
442+ HaveOccurred (),
443+ )
444+ })
445+
446+ It ("should do nothing because there are not orphaned worker pods" , func () {
447+ nmcSpec := kmmv1beta1.NodeModulesConfigSpec {
448+ Modules : []kmmv1beta1.NodeModuleSpec {
449+ {
450+ ModuleItem : kmmv1beta1.ModuleItem {Name : nameFirst },
451+ },
452+ },
453+ }
454+ nmcObj := & kmmv1beta1.NodeModulesConfig {
455+ ObjectMeta : metav1.ObjectMeta {Name : nmcName },
456+ Spec : nmcSpec ,
457+ }
458+ pod := v1.Pod {
459+ ObjectMeta : metav1.ObjectMeta {
460+ Name : fmt .Sprintf ("kmm-worker-%s-%s" , nmcObj .Name , nameFirst ),
461+ Namespace : "d" ,
462+ Labels : map [string ]string {
463+ constants .ModuleNameLabel : nameFirst ,
464+ },
465+ },
466+ }
467+
468+ gomock .InOrder (
469+ pm .EXPECT ().ListWorkerPodsOnNode (ctx , nmcName ).Return ([]v1.Pod {pod }, nil ),
470+ )
471+
472+ Expect (
473+ nrh .GarbageCollectWorkerPods (ctx , nmcObj ),
474+ ).NotTo (
475+ HaveOccurred (),
476+ )
477+ })
478+ })
479+
392480var _ = Describe ("nmcReconcilerHelperImpl_GarbageCollectInUseLabels" , func () {
393481 var (
394482 ctx = context .TODO ()
0 commit comments