Skip to content

Commit e4bb1af

Browse files
odubajDTChrsMark
andauthored
[chore][processor/k8sattributes] migrate from using deprecated k8s API (open-telemetry#44080)
<!-- Issue number (e.g. open-telemetry#1234) or full URL to issue, if applicable. --> #### Link to tracking issue Part of open-telemetry#43891 --------- Signed-off-by: odubajDT <[email protected]> Co-authored-by: Christos Markou <[email protected]>
1 parent 1e1b554 commit e4bb1af

File tree

2 files changed

+60
-74
lines changed

2 files changed

+60
-74
lines changed

processor/k8sattributesprocessor/internal/kube/informer.go

Lines changed: 56 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,28 @@ func newSharedInformer(
5757
) cache.SharedInformer {
5858
informer := cache.NewSharedInformer(
5959
&cache.ListWatch{
60-
ListFunc: informerListFuncWithSelectors(client, namespace, ls, fs),
61-
WatchFunc: informerWatchFuncWithSelectors(client, namespace, ls, fs),
60+
ListWithContextFunc: informerListFuncWithSelectors(client, namespace, ls, fs),
61+
WatchFuncWithContext: informerWatchFuncWithSelectors(client, namespace, ls, fs),
6262
},
6363
&api_v1.Pod{},
6464
watchSyncPeriod,
6565
)
6666
return informer
6767
}
6868

69-
//nolint:staticcheck // SA1019 TODO: resolve as part of https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/43891
70-
func informerListFuncWithSelectors(client kubernetes.Interface, namespace string, ls labels.Selector, fs fields.Selector) cache.ListFunc {
71-
return func(opts metav1.ListOptions) (runtime.Object, error) {
69+
func informerListFuncWithSelectors(client kubernetes.Interface, namespace string, ls labels.Selector, fs fields.Selector) cache.ListWithContextFunc {
70+
return func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
7271
opts.LabelSelector = ls.String()
7372
opts.FieldSelector = fs.String()
74-
return client.CoreV1().Pods(namespace).List(context.Background(), opts)
73+
return client.CoreV1().Pods(namespace).List(ctx, opts)
7574
}
7675
}
7776

78-
//nolint:staticcheck // SA1019 TODO: resolve as part of https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/43891
79-
func informerWatchFuncWithSelectors(client kubernetes.Interface, namespace string, ls labels.Selector, fs fields.Selector) cache.WatchFunc {
80-
return func(opts metav1.ListOptions) (watch.Interface, error) {
77+
func informerWatchFuncWithSelectors(client kubernetes.Interface, namespace string, ls labels.Selector, fs fields.Selector) cache.WatchFuncWithContext {
78+
return func(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
8179
opts.LabelSelector = ls.String()
8280
opts.FieldSelector = fs.String()
83-
return client.CoreV1().Pods(namespace).Watch(context.Background(), opts)
81+
return client.CoreV1().Pods(namespace).Watch(ctx, opts)
8482
}
8583
}
8684

@@ -110,26 +108,24 @@ func newNamespaceSharedInformer(
110108
) cache.SharedInformer {
111109
informer := cache.NewSharedInformer(
112110
&cache.ListWatch{
113-
ListFunc: namespaceInformerListFunc(client),
114-
WatchFunc: namespaceInformerWatchFunc(client),
111+
ListWithContextFunc: namespaceInformerListFunc(client),
112+
WatchFuncWithContext: namespaceInformerWatchFunc(client),
115113
},
116114
&api_v1.Namespace{},
117115
watchSyncPeriod,
118116
)
119117
return informer
120118
}
121119

122-
//nolint:staticcheck // SA1019 TODO: resolve as part of https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/43891
123-
func namespaceInformerListFunc(client kubernetes.Interface) cache.ListFunc {
124-
return func(opts metav1.ListOptions) (runtime.Object, error) {
125-
return client.CoreV1().Namespaces().List(context.Background(), opts)
120+
func namespaceInformerListFunc(client kubernetes.Interface) cache.ListWithContextFunc {
121+
return func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
122+
return client.CoreV1().Namespaces().List(ctx, opts)
126123
}
127124
}
128125

129-
//nolint:staticcheck // SA1019 TODO: resolve as part of https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/43891
130-
func namespaceInformerWatchFunc(client kubernetes.Interface) cache.WatchFunc {
131-
return func(opts metav1.ListOptions) (watch.Interface, error) {
132-
return client.CoreV1().Namespaces().Watch(context.Background(), opts)
126+
func namespaceInformerWatchFunc(client kubernetes.Interface) cache.WatchFuncWithContext {
127+
return func(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
128+
return client.CoreV1().Namespaces().Watch(ctx, opts)
133129
}
134130
}
135131

@@ -139,26 +135,24 @@ func newReplicaSetSharedInformer(
139135
) cache.SharedInformer {
140136
informer := cache.NewSharedInformer(
141137
&cache.ListWatch{
142-
ListFunc: replicasetListFuncWithSelectors(client, namespace),
143-
WatchFunc: replicasetWatchFuncWithSelectors(client, namespace),
138+
ListWithContextFunc: replicasetListFuncWithSelectors(client, namespace),
139+
WatchFuncWithContext: replicasetWatchFuncWithSelectors(client, namespace),
144140
},
145141
&apps_v1.ReplicaSet{},
146142
watchSyncPeriod,
147143
)
148144
return informer
149145
}
150146

151-
//nolint:staticcheck // SA1019 TODO: resolve as part of https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/43891
152-
func replicasetListFuncWithSelectors(client kubernetes.Interface, namespace string) cache.ListFunc {
153-
return func(opts metav1.ListOptions) (runtime.Object, error) {
154-
return client.AppsV1().ReplicaSets(namespace).List(context.Background(), opts)
147+
func replicasetListFuncWithSelectors(client kubernetes.Interface, namespace string) cache.ListWithContextFunc {
148+
return func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
149+
return client.AppsV1().ReplicaSets(namespace).List(ctx, opts)
155150
}
156151
}
157152

158-
//nolint:staticcheck // SA1019 TODO: resolve as part of https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/43891
159-
func replicasetWatchFuncWithSelectors(client kubernetes.Interface, namespace string) cache.WatchFunc {
160-
return func(opts metav1.ListOptions) (watch.Interface, error) {
161-
return client.AppsV1().ReplicaSets(namespace).Watch(context.Background(), opts)
153+
func replicasetWatchFuncWithSelectors(client kubernetes.Interface, namespace string) cache.WatchFuncWithContext {
154+
return func(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
155+
return client.AppsV1().ReplicaSets(namespace).Watch(ctx, opts)
162156
}
163157
}
164158

@@ -168,26 +162,24 @@ func newDeploymentSharedInformer(
168162
) cache.SharedInformer {
169163
informer := cache.NewSharedInformer(
170164
&cache.ListWatch{
171-
ListFunc: deploymentListFuncWithSelectors(client, namespace),
172-
WatchFunc: deploymentWatchFuncWithSelectors(client, namespace),
165+
ListWithContextFunc: deploymentListFuncWithSelectors(client, namespace),
166+
WatchFuncWithContext: deploymentWatchFuncWithSelectors(client, namespace),
173167
},
174168
&apps_v1.Deployment{},
175169
watchSyncPeriod,
176170
)
177171
return informer
178172
}
179173

180-
//nolint:staticcheck // SA1019 TODO: resolve as part of https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/43891
181-
func deploymentListFuncWithSelectors(client kubernetes.Interface, namespace string) cache.ListFunc {
182-
return func(opts metav1.ListOptions) (runtime.Object, error) {
183-
return client.AppsV1().Deployments(namespace).List(context.Background(), opts)
174+
func deploymentListFuncWithSelectors(client kubernetes.Interface, namespace string) cache.ListWithContextFunc {
175+
return func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
176+
return client.AppsV1().Deployments(namespace).List(ctx, opts)
184177
}
185178
}
186179

187-
//nolint:staticcheck // SA1019 TODO: resolve as part of https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/43891
188-
func deploymentWatchFuncWithSelectors(client kubernetes.Interface, namespace string) cache.WatchFunc {
189-
return func(opts metav1.ListOptions) (watch.Interface, error) {
190-
return client.AppsV1().Deployments(namespace).Watch(context.Background(), opts)
180+
func deploymentWatchFuncWithSelectors(client kubernetes.Interface, namespace string) cache.WatchFuncWithContext {
181+
return func(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
182+
return client.AppsV1().Deployments(namespace).Watch(ctx, opts)
191183
}
192184
}
193185

@@ -197,26 +189,24 @@ func newStatefulSetSharedInformer(
197189
) cache.SharedInformer {
198190
informer := cache.NewSharedInformer(
199191
&cache.ListWatch{
200-
ListFunc: statefulsetListFuncWithSelectors(client, namespace),
201-
WatchFunc: statefulsetWatchFuncWithSelectors(client, namespace),
192+
ListWithContextFunc: statefulsetListFuncWithSelectors(client, namespace),
193+
WatchFuncWithContext: statefulsetWatchFuncWithSelectors(client, namespace),
202194
},
203195
&apps_v1.StatefulSet{},
204196
watchSyncPeriod,
205197
)
206198
return informer
207199
}
208200

209-
//nolint:staticcheck // SA1019 TODO: resolve as part of https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/43891
210-
func statefulsetListFuncWithSelectors(client kubernetes.Interface, namespace string) cache.ListFunc {
211-
return func(opts metav1.ListOptions) (runtime.Object, error) {
212-
return client.AppsV1().StatefulSets(namespace).List(context.Background(), opts)
201+
func statefulsetListFuncWithSelectors(client kubernetes.Interface, namespace string) cache.ListWithContextFunc {
202+
return func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
203+
return client.AppsV1().StatefulSets(namespace).List(ctx, opts)
213204
}
214205
}
215206

216-
//nolint:staticcheck // SA1019 TODO: resolve as part of https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/43891
217-
func statefulsetWatchFuncWithSelectors(client kubernetes.Interface, namespace string) cache.WatchFunc {
218-
return func(opts metav1.ListOptions) (watch.Interface, error) {
219-
return client.AppsV1().StatefulSets(namespace).Watch(context.Background(), opts)
207+
func statefulsetWatchFuncWithSelectors(client kubernetes.Interface, namespace string) cache.WatchFuncWithContext {
208+
return func(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
209+
return client.AppsV1().StatefulSets(namespace).Watch(ctx, opts)
220210
}
221211
}
222212

@@ -226,8 +216,8 @@ func newDaemonSetSharedInformer(
226216
) cache.SharedInformer {
227217
informer := cache.NewSharedInformer(
228218
&cache.ListWatch{
229-
ListFunc: daemonsetListFuncWithSelectors(client, namespace),
230-
WatchFunc: daemonsetWatchFuncWithSelectors(client, namespace),
219+
ListWithContextFunc: daemonsetListFuncWithSelectors(client, namespace),
220+
WatchFuncWithContext: daemonsetWatchFuncWithSelectors(client, namespace),
231221
},
232222
&apps_v1.DaemonSet{},
233223
watchSyncPeriod,
@@ -241,39 +231,35 @@ func newJobSharedInformer(
241231
) cache.SharedInformer {
242232
informer := cache.NewSharedInformer(
243233
&cache.ListWatch{
244-
ListFunc: jobListFuncWithSelectors(client, namespace),
245-
WatchFunc: jobWatchFuncWithSelectors(client, namespace),
234+
ListWithContextFunc: jobListFuncWithSelectors(client, namespace),
235+
WatchFuncWithContext: jobWatchFuncWithSelectors(client, namespace),
246236
},
247237
&batch_v1.Job{},
248238
watchSyncPeriod,
249239
)
250240
return informer
251241
}
252242

253-
//nolint:staticcheck // SA1019 TODO: resolve as part of https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/43891
254-
func jobListFuncWithSelectors(client kubernetes.Interface, namespace string) cache.ListFunc {
255-
return func(opts metav1.ListOptions) (runtime.Object, error) {
256-
return client.BatchV1().Jobs(namespace).List(context.Background(), opts)
243+
func jobListFuncWithSelectors(client kubernetes.Interface, namespace string) cache.ListWithContextFunc {
244+
return func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
245+
return client.BatchV1().Jobs(namespace).List(ctx, opts)
257246
}
258247
}
259248

260-
//nolint:staticcheck // SA1019 TODO: resolve as part of https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/43891
261-
func jobWatchFuncWithSelectors(client kubernetes.Interface, namespace string) cache.WatchFunc {
262-
return func(opts metav1.ListOptions) (watch.Interface, error) {
263-
return client.BatchV1().Jobs(namespace).Watch(context.Background(), opts)
249+
func jobWatchFuncWithSelectors(client kubernetes.Interface, namespace string) cache.WatchFuncWithContext {
250+
return func(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
251+
return client.BatchV1().Jobs(namespace).Watch(ctx, opts)
264252
}
265253
}
266254

267-
//nolint:staticcheck // SA1019 TODO: resolve as part of https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/43891
268-
func daemonsetListFuncWithSelectors(client kubernetes.Interface, namespace string) cache.ListFunc {
269-
return func(opts metav1.ListOptions) (runtime.Object, error) {
270-
return client.AppsV1().DaemonSets(namespace).List(context.Background(), opts)
255+
func daemonsetListFuncWithSelectors(client kubernetes.Interface, namespace string) cache.ListWithContextFunc {
256+
return func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
257+
return client.AppsV1().DaemonSets(namespace).List(ctx, opts)
271258
}
272259
}
273260

274-
//nolint:staticcheck // SA1019 TODO: resolve as part of https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/43891
275-
func daemonsetWatchFuncWithSelectors(client kubernetes.Interface, namespace string) cache.WatchFunc {
276-
return func(opts metav1.ListOptions) (watch.Interface, error) {
277-
return client.AppsV1().DaemonSets(namespace).Watch(context.Background(), opts)
261+
func daemonsetWatchFuncWithSelectors(client kubernetes.Interface, namespace string) cache.WatchFuncWithContext {
262+
return func(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
263+
return client.AppsV1().DaemonSets(namespace).Watch(ctx, opts)
278264
}
279265
}

processor/k8sattributesprocessor/internal/kube/informer_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func Test_informerListFuncWithSelectors(t *testing.T) {
9090
assert.NoError(t, err)
9191
listFunc := informerListFuncWithSelectors(c, "test-ns", ls, fs)
9292
opts := metav1.ListOptions{}
93-
obj, err := listFunc(opts)
93+
obj, err := listFunc(t.Context(), opts)
9494
assert.NoError(t, err)
9595
assert.NotNil(t, obj)
9696
}
@@ -100,7 +100,7 @@ func Test_namespaceInformerListFunc(t *testing.T) {
100100
assert.NoError(t, err)
101101
listFunc := namespaceInformerListFunc(c)
102102
opts := metav1.ListOptions{}
103-
obj, err := listFunc(opts)
103+
obj, err := listFunc(t.Context(), opts)
104104
assert.NoError(t, err)
105105
assert.NotNil(t, obj)
106106
}
@@ -127,7 +127,7 @@ func Test_informerWatchFuncWithSelectors(t *testing.T) {
127127
assert.NoError(t, err)
128128
watchFunc := informerWatchFuncWithSelectors(c, "test-ns", ls, fs)
129129
opts := metav1.ListOptions{}
130-
obj, err := watchFunc(opts)
130+
obj, err := watchFunc(t.Context(), opts)
131131
assert.NoError(t, err)
132132
assert.NotNil(t, obj)
133133
}
@@ -137,7 +137,7 @@ func Test_namespaceInformerWatchFunc(t *testing.T) {
137137
assert.NoError(t, err)
138138
watchFunc := namespaceInformerWatchFunc(c)
139139
opts := metav1.ListOptions{}
140-
obj, err := watchFunc(opts)
140+
obj, err := watchFunc(t.Context(), opts)
141141
assert.NoError(t, err)
142142
assert.NotNil(t, obj)
143143
}

0 commit comments

Comments
 (0)