@@ -15,7 +15,8 @@ import (
1515 "k8s.io/apimachinery/pkg/watch"
1616 ctrl "sigs.k8s.io/controller-runtime"
1717 "sigs.k8s.io/controller-runtime/pkg/client"
18- "sigs.k8s.io/controller-runtime/pkg/reconcile"
18+ "sigs.k8s.io/controller-runtime/pkg/event"
19+ "sigs.k8s.io/controller-runtime/pkg/predicate"
1920)
2021
2122// PodReconciler reconciles a Pod object
@@ -32,41 +33,58 @@ type PodReconciler struct {
3233// For more details, check Reconcile and its Result here:
3334// - https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/reconcile 3435func (r * PodReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
35- var pod corev1.Pod
36- err := r .Client .Get (context .Background (), req .NamespacedName , & pod )
37- if errors .IsNotFound (err ) {
38- // pod is gone - that's ok
39- if pod , ok := r .Pods [req .NamespacedName ]; ok {
40- delete (r .Pods , req .NamespacedName )
41- queue := pod .Annotations [workspaceIDAnnotation ]
42- if queue == "" {
43- return ctrl.Result {}, nil
36+ go func () {
37+ var pod corev1.Pod
38+ err := r .Client .Get (context .Background (), req .NamespacedName , & pod )
39+ if errors .IsNotFound (err ) {
40+ // pod is gone - that's ok
41+ if pod , ok := r .Pods [req .NamespacedName ]; ok {
42+ delete (r .Pods , req .NamespacedName )
43+ queue := pod .Annotations [workspaceIDAnnotation ]
44+ if queue == "" {
45+ return
46+ }
47+ r .Monitor .eventpool .Add (queue , watch.Event {
48+ Type : watch .Deleted ,
49+ Object : & pod ,
50+ })
4451 }
45- r .Monitor .eventpool .Add (queue , watch.Event {
46- Type : watch .Deleted ,
47- Object : & pod ,
48- })
52+ return
4953 }
50- return reconcile.Result {}, nil
51- }
52- r .Pods [req .NamespacedName ] = pod
54+ r .Pods [req .NamespacedName ] = pod
5355
54- queue := pod .Annotations [workspaceIDAnnotation ]
55- if queue == "" {
56- return ctrl. Result {}, nil
57- }
56+ queue := pod .Annotations [workspaceIDAnnotation ]
57+ if queue == "" {
58+ return
59+ }
5860
59- r .Monitor .eventpool .Add (queue , watch.Event {
60- Type : watch .Modified ,
61- Object : & pod ,
62- })
61+ r .Monitor .eventpool .Add (queue , watch.Event {
62+ Type : watch .Modified ,
63+ Object : & pod ,
64+ })
65+ }()
6366
6467 return ctrl.Result {}, nil
6568}
6669
6770// SetupWithManager sets up the controller with the Manager.
6871func (r * PodReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
6972 return ctrl .NewControllerManagedBy (mgr ).
73+ WithEventFilter (
74+ predicate.Funcs {
75+ UpdateFunc : func (e event.UpdateEvent ) bool {
76+ _ , ok := e .ObjectNew .GetAnnotations ()[workspaceIDAnnotation ]
77+ return ok
78+ },
79+ CreateFunc : func (e event.CreateEvent ) bool {
80+ _ , ok := e .Object .GetAnnotations ()[workspaceIDAnnotation ]
81+ return ok
82+ },
83+ DeleteFunc : func (e event.DeleteEvent ) bool {
84+ _ , ok := e .Object .GetAnnotations ()[workspaceIDAnnotation ]
85+ return ok
86+ },
87+ }).
7088 For (& corev1.Pod {}).
7189 Complete (r )
7290}
0 commit comments