@@ -25,6 +25,10 @@ import (
2525 "sigs.k8s.io/controller-runtime/pkg/event"
2626)
2727
28+ const (
29+ farosGroupVersion = "faros.pusher.com/v1alpha1"
30+ )
31+
2832// OwnerInNamespacePredicate filters events to check the owner of the event
2933// object is in the controller's namespace
3034type OwnerInNamespacePredicate struct {
@@ -66,7 +70,7 @@ func (p OwnerInNamespacePredicate) ownerInNamespace(ownerRefs []metav1.OwnerRefe
6670 return false
6771 }
6872 for _ , ref := range ownerRefs {
69- if ref .Kind == "GitTrack" && ref .APIVersion == "faros.pusher.com/v1alpha1" {
73+ if ref .Kind == "GitTrack" && ref .APIVersion == farosGroupVersion {
7074 for _ , gt := range gtList .Items {
7175 if ref .UID == gt .UID {
7276 return true
@@ -83,3 +87,78 @@ func NewOwnerInNamespacePredicate(client client.Client) OwnerInNamespacePredicat
8387 client : client ,
8488 }
8589}
90+
91+ // OwnersOwnerInNamespacePredicate filters events to check the owners owner of
92+ // the event object is in the controller's namespace
93+ type OwnersOwnerInNamespacePredicate struct {
94+ client client.Client
95+ ownerInNamespacePredicate OwnerInNamespacePredicate
96+ }
97+
98+ // Create returns true if the event object owners owner is in the same namespace
99+ func (p OwnersOwnerInNamespacePredicate ) Create (e event.CreateEvent ) bool {
100+ return p .ownersOwnerInNamespace (e .Meta .GetOwnerReferences ())
101+ }
102+
103+ // Update returns true if the event object owners owner is in the same namespace
104+ func (p OwnersOwnerInNamespacePredicate ) Update (e event.UpdateEvent ) bool {
105+ return p .ownersOwnerInNamespace (e .MetaNew .GetOwnerReferences ())
106+ }
107+
108+ // Delete returns true if the event object owners owner is in the same namespace
109+ func (p OwnersOwnerInNamespacePredicate ) Delete (e event.DeleteEvent ) bool {
110+ return p .ownersOwnerInNamespace (e .Meta .GetOwnerReferences ())
111+ }
112+
113+ // Generic returns true if the event object owners owner is in the same namespace
114+ func (p OwnersOwnerInNamespacePredicate ) Generic (e event.GenericEvent ) bool {
115+ return p .ownersOwnerInNamespace (e .Meta .GetOwnerReferences ())
116+ }
117+
118+ // ownersOwnerInNamespace returns true if the the GitTrackObject's GitTrack
119+ // owner of the event object is in the namespace managed by the controller
120+ //
121+ // This works on the premise that listing objects from the client will only
122+ // return those in its cache.
123+ // When it is restricted to a namespace this should only be the GitTracks
124+ // in the namespace the controller is managing.
125+ func (p OwnersOwnerInNamespacePredicate ) ownersOwnerInNamespace (ownerRefs []metav1.OwnerReference ) bool {
126+ cgtoList := & farosv1alpha1.ClusterGitTrackObjectList {}
127+ err := p .client .List (context .TODO (), & client.ListOptions {}, cgtoList )
128+ if err != nil {
129+ // We can't list CGTOs so fail closed and ignore the requests
130+ return false
131+ }
132+ gtoList := & farosv1alpha1.GitTrackObjectList {}
133+ err = p .client .List (context .TODO (), & client.ListOptions {}, gtoList )
134+ if err != nil {
135+ // We can't list GTOs so fail closed and ignore the requests
136+ return false
137+ }
138+
139+ for _ , ref := range ownerRefs {
140+ if ref .Kind == "GitTrackObject" && ref .APIVersion == farosGroupVersion {
141+ for _ , gto := range gtoList .Items {
142+ if ref .UID == gto .UID {
143+ return p .ownerInNamespacePredicate .ownerInNamespace (gto .GetOwnerReferences ())
144+ }
145+ }
146+ }
147+ if ref .Kind == "ClusterGitTrackObject" && ref .APIVersion == farosGroupVersion {
148+ for _ , cgto := range cgtoList .Items {
149+ if ref .UID == cgto .UID {
150+ return p .ownerInNamespacePredicate .ownerInNamespace (cgto .GetOwnerReferences ())
151+ }
152+ }
153+ }
154+ }
155+ return false
156+ }
157+
158+ // NewOwnersOwnerInNamespacePredicate constructs a new OwnersOwnerInNamespacePredicate
159+ func NewOwnersOwnerInNamespacePredicate (client client.Client ) OwnersOwnerInNamespacePredicate {
160+ return OwnersOwnerInNamespacePredicate {
161+ client : client ,
162+ ownerInNamespacePredicate : NewOwnerInNamespacePredicate (client ),
163+ }
164+ }
0 commit comments