@@ -147,25 +147,11 @@ func (kr *k8sobjectsreceiver) startWatch(ctx context.Context, config *K8sObjects
147
147
kr .stopperChanList = append (kr .stopperChanList , stopperChan )
148
148
kr .mu .Unlock ()
149
149
150
- resourceVersion := config .ResourceVersion
151
- var err error
152
- if resourceVersion == "" {
153
- // Proper use of the Kubernetes API Watch capability when no resourceVersion is supplied is to do a list first
154
- // to get the initial state and a useable resourceVersion.
155
- // See https://kubernetes.io/docs/reference/using-api/api-concepts/#efficient-detection-of-changes for details.
156
- resourceVersion , err = kr .doInitialList (ctx , config , resource )
157
- if err != nil {
158
- kr .setting .Logger .Error ("could not perform initial list for watch" , zap .String ("resource" , config .gvr .String ()), zap .Error (err ))
159
- return
160
- }
161
- // If we still don't have a resourceVersion we can try 1 as a last ditch effort.
162
- // This also helps our unit tests since the fake client can't handle returning resource versions
163
- // as part of a list of objects.
164
- if resourceVersion == "" {
165
- resourceVersion = defaultResourceVersion
166
- }
150
+ resourceVersion , err := getResourceVersion (ctx , config , resource )
151
+ if err != nil {
152
+ kr .setting .Logger .Error ("could not retrieve an initial resourceVersion" , zap .String ("resource" , config .gvr .String ()), zap .Error (err ))
153
+ return
167
154
}
168
-
169
155
watchFunc := func (options metav1.ListOptions ) (apiWatch.Interface , error ) {
170
156
options .FieldSelector = config .FieldSelector
171
157
options .LabelSelector = config .LabelSelector
@@ -202,28 +188,33 @@ func (kr *k8sobjectsreceiver) startWatch(ctx context.Context, config *K8sObjects
202
188
203
189
}
204
190
205
- func (kr * k8sobjectsreceiver ) doInitialList (ctx context.Context , config * K8sObjectsConfig , resource dynamic.ResourceInterface ) (string , error ) {
206
- objects , err := resource .List (ctx , metav1.ListOptions {
207
- FieldSelector : config .FieldSelector ,
208
- LabelSelector : config .LabelSelector ,
209
- })
210
- if err != nil {
211
- return "" , err
212
- }
191
+ func getResourceVersion (ctx context.Context , config * K8sObjectsConfig , resource dynamic.ResourceInterface ) (string , error ) {
192
+ resourceVersion := config .ResourceVersion
193
+ if resourceVersion == "" || resourceVersion == "0" {
194
+ // Proper use of the Kubernetes API Watch capability when no resourceVersion is supplied is to do a list first
195
+ // to get the initial state and a useable resourceVersion.
196
+ // See https://kubernetes.io/docs/reference/using-api/api-concepts/#efficient-detection-of-changes for details.
197
+ objects , err := resource .List (ctx , metav1.ListOptions {
198
+ FieldSelector : config .FieldSelector ,
199
+ LabelSelector : config .LabelSelector ,
200
+ })
201
+ if err != nil {
202
+ return "" , fmt .Errorf ("could not perform initial list for watch on %v, %w" , config .gvr .String (), err )
203
+ }
204
+ if objects == nil {
205
+ return "" , fmt .Errorf ("nil objects returned, this is an error in the k8sobjectsreceiver" )
206
+ }
213
207
214
- if objects == nil {
215
- return "" , fmt .Errorf ("nil objects returned, this is an error in the k8sobjectsreceiver" )
216
- }
208
+ resourceVersion = objects .GetResourceVersion ()
217
209
218
- if len (objects .Items ) > 0 {
219
- logs := pullObjectsToLogData (objects , time .Now (), config )
220
- obsCtx := kr .obsrecv .StartLogsOp (ctx )
221
- err = kr .consumer .ConsumeLogs (obsCtx , logs )
222
- kr .obsrecv .EndLogsOp (obsCtx , metadata .Type , logs .LogRecordCount (), err )
210
+ // If we still don't have a resourceVersion we can try 1 as a last ditch effort.
211
+ // This also helps our unit tests since the fake client can't handle returning resource versions
212
+ // as part of a list of objects.
213
+ if resourceVersion == "" || resourceVersion == "0" {
214
+ resourceVersion = defaultResourceVersion
215
+ }
223
216
}
224
-
225
- return objects .GetResourceVersion (), nil
226
-
217
+ return resourceVersion , nil
227
218
}
228
219
229
220
// Start ticking immediately.
0 commit comments