@@ -102,6 +102,7 @@ type conditionalInformer struct {
102
102
permissionVerbs []string
103
103
isApplied bool
104
104
isResourceInError bool
105
+ transformers transformers.Transformers
105
106
}
106
107
107
108
func (i * conditionalInformer ) Name () string {
@@ -120,6 +121,7 @@ func CollectSingleSnapshot(ctx context.Context,
120
121
metricsClient versioned.Interface ,
121
122
cfg * config.Controller ,
122
123
v version.Interface ,
124
+ castwareNamespace string ,
123
125
) (* castai.Delta , error ) {
124
126
tweakListOptions := func (options * metav1.ListOptions ) {
125
127
if cfg .ForcePagination && options .ResourceVersion == "0" {
@@ -131,7 +133,7 @@ func CollectSingleSnapshot(ctx context.Context,
131
133
f := informers .NewSharedInformerFactoryWithOptions (clientset , 0 , informers .WithTweakListOptions (tweakListOptions ))
132
134
df := dynamicinformer .NewFilteredDynamicSharedInformerFactory (dynamicClient , 0 , metav1 .NamespaceAll , tweakListOptions )
133
135
134
- defaultInformers := getDefaultInformers (f )
136
+ defaultInformers := getDefaultInformers (f , castwareNamespace , cfg . FilterEmptyReplicaSets )
135
137
conditionalInformers := getConditionalInformers (clientset , cfg , f , df , metricsClient , log )
136
138
additionalTransformers := createAdditionalTransformers (cfg )
137
139
@@ -146,7 +148,8 @@ func CollectSingleSnapshot(ctx context.Context,
146
148
147
149
handledInformers := map [string ]* custominformers.HandledInformer {}
148
150
for typ , i := range defaultInformers {
149
- handledInformers [typ .String ()] = custominformers .NewHandledInformer (log , queue , i .informer , typ , i .filters , additionalTransformers ... )
151
+ transformers := append (i .transformers , additionalTransformers ... )
152
+ handledInformers [typ .String ()] = custominformers .NewHandledInformer (log , queue , i .informer , typ , i .filters , transformers ... )
150
153
}
151
154
for typ , i := range handledConditionalInformers {
152
155
handledInformers [typ ] = i
@@ -208,6 +211,7 @@ func New(
208
211
agentVersion * config.AgentVersion ,
209
212
healthzProvider * HealthzProvider ,
210
213
selfSubjectAccessReview authorizationtypev1.SelfSubjectAccessReviewInterface ,
214
+ castwareNamespace string ,
211
215
) * Controller {
212
216
healthzProvider .Initializing ()
213
217
@@ -225,7 +229,7 @@ func New(
225
229
df := dynamicinformer .NewFilteredDynamicSharedInformerFactory (dynamicClient , defaultResync , metav1 .NamespaceAll , tweakListOptions )
226
230
discovery := clientset .Discovery ()
227
231
228
- defaultInformers := getDefaultInformers (f )
232
+ defaultInformers := getDefaultInformers (f , castwareNamespace , cfg . FilterEmptyReplicaSets )
229
233
conditionalInformers := getConditionalInformers (clientset , cfg , f , df , metricsClient , log )
230
234
additionalTransformers := createAdditionalTransformers (cfg )
231
235
@@ -236,7 +240,8 @@ func New(
236
240
if ! informerEnabled (cfg , name ) {
237
241
continue
238
242
}
239
- handledInformers [name ] = custominformers .NewHandledInformer (log , queue , i .informer , typ , i .filters , additionalTransformers ... )
243
+ transformers := append (i .transformers , additionalTransformers ... )
244
+ handledInformers [name ] = custominformers .NewHandledInformer (log , queue , i .informer , typ , i .filters , transformers ... )
240
245
}
241
246
242
247
eventType := reflect .TypeOf (& corev1.Event {})
@@ -448,8 +453,9 @@ func startConditionalInformers(ctx context.Context,
448
453
log .Infof ("Starting conditional informer for %v" , informer .Name ())
449
454
conditionalInformers [i ].isApplied = true
450
455
456
+ transformers := append (informer .transformers , additionalTransformers ... )
451
457
name := fmt .Sprintf ("%s::%s" , informer .groupVersion .String (), informer .kind )
452
- handledInformer := custominformers .NewHandledInformer (log , queue , informer .informerFactory (), informer .apiType , nil , additionalTransformers ... )
458
+ handledInformer := custominformers .NewHandledInformer (log , queue , informer .informerFactory (), informer .apiType , nil , transformers ... )
453
459
handledInformers [name ] = handledInformer
454
460
455
461
go handledInformer .Run (ctx .Done ())
@@ -985,11 +991,12 @@ func getConditionalInformers(clientset kubernetes.Interface, cfg *config.Control
985
991
}
986
992
987
993
type defaultInformer struct {
988
- informer cache.SharedInformer
989
- filters filters.Filters
994
+ informer cache.SharedInformer
995
+ filters filters.Filters
996
+ transformers transformers.Transformers
990
997
}
991
998
992
- func getDefaultInformers (f informers.SharedInformerFactory ) map [reflect.Type ]defaultInformer {
999
+ func getDefaultInformers (f informers.SharedInformerFactory , castwareNamespace string , filterEmptyReplicaSets bool ) map [reflect.Type ]defaultInformer {
993
1000
return map [reflect.Type ]defaultInformer {
994
1001
reflect .TypeOf (& corev1.Node {}): {informer : f .Core ().V1 ().Nodes ().Informer ()},
995
1002
reflect .TypeOf (& corev1.Pod {}): {informer : f .Core ().V1 ().Pods ().Informer ()},
@@ -998,11 +1005,28 @@ func getDefaultInformers(f informers.SharedInformerFactory) map[reflect.Type]def
998
1005
reflect .TypeOf (& corev1.ReplicationController {}): {informer : f .Core ().V1 ().ReplicationControllers ().Informer ()},
999
1006
reflect .TypeOf (& corev1.Namespace {}): {informer : f .Core ().V1 ().Namespaces ().Informer ()},
1000
1007
reflect .TypeOf (& appsv1.Deployment {}): {informer : f .Apps ().V1 ().Deployments ().Informer ()},
1001
- reflect .TypeOf (& appsv1.ReplicaSet {}): {informer : f .Apps ().V1 ().ReplicaSets ().Informer ()},
1002
- reflect .TypeOf (& appsv1.DaemonSet {}): {informer : f .Apps ().V1 ().DaemonSets ().Informer ()},
1003
- reflect .TypeOf (& appsv1.StatefulSet {}): {informer : f .Apps ().V1 ().StatefulSets ().Informer ()},
1004
- reflect .TypeOf (& storagev1.StorageClass {}): {informer : f .Storage ().V1 ().StorageClasses ().Informer ()},
1005
- reflect .TypeOf (& batchv1.Job {}): {informer : f .Batch ().V1 ().Jobs ().Informer ()},
1008
+ reflect .TypeOf (& appsv1.ReplicaSet {}): {
1009
+ informer : f .Apps ().V1 ().ReplicaSets ().Informer (),
1010
+ transformers : transformers.Transformers {
1011
+ func (e castai.EventType , obj interface {}) (castai.EventType , interface {}) {
1012
+ replicaSet , ok := obj .(* appsv1.ReplicaSet )
1013
+ if ! ok || ! filterEmptyReplicaSets {
1014
+ return e , obj
1015
+ }
1016
+
1017
+ if e == castai .EventDelete || replicaSet .Namespace == castwareNamespace ||
1018
+ (replicaSet .Spec .Replicas != nil && * replicaSet .Spec .Replicas > 0 && replicaSet .Status .Replicas > 0 ) || replicaSet .OwnerReferences == nil {
1019
+ return e , obj
1020
+ }
1021
+
1022
+ return castai .EventDelete , obj
1023
+ },
1024
+ },
1025
+ },
1026
+ reflect .TypeOf (& appsv1.DaemonSet {}): {informer : f .Apps ().V1 ().DaemonSets ().Informer ()},
1027
+ reflect .TypeOf (& appsv1.StatefulSet {}): {informer : f .Apps ().V1 ().StatefulSets ().Informer ()},
1028
+ reflect .TypeOf (& storagev1.StorageClass {}): {informer : f .Storage ().V1 ().StorageClasses ().Informer ()},
1029
+ reflect .TypeOf (& batchv1.Job {}): {informer : f .Batch ().V1 ().Jobs ().Informer ()},
1006
1030
reflect .TypeOf (& corev1.Service {}): {
1007
1031
informer : f .Core ().V1 ().Services ().Informer (),
1008
1032
filters : filters.Filters {
0 commit comments