@@ -15,7 +15,8 @@ import (
15
15
"k8s.io/apimachinery/pkg/watch"
16
16
ctrl "sigs.k8s.io/controller-runtime"
17
17
"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"
19
20
)
20
21
21
22
// PodReconciler reconciles a Pod object
@@ -32,41 +33,58 @@ type PodReconciler struct {
32
33
// For more details, check Reconcile and its Result here:
33
34
// - https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/reconcile
34
35
func (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
+ })
44
51
}
45
- r .Monitor .eventpool .Add (queue , watch.Event {
46
- Type : watch .Deleted ,
47
- Object : & pod ,
48
- })
52
+ return
49
53
}
50
- return reconcile.Result {}, nil
51
- }
52
- r .Pods [req .NamespacedName ] = pod
54
+ r .Pods [req .NamespacedName ] = pod
53
55
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
+ }
58
60
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
+ }()
63
66
64
67
return ctrl.Result {}, nil
65
68
}
66
69
67
70
// SetupWithManager sets up the controller with the Manager.
68
71
func (r * PodReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
69
72
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
+ }).
70
88
For (& corev1.Pod {}).
71
89
Complete (r )
72
90
}
0 commit comments