@@ -6,15 +6,18 @@ package kube // import "github.com/open-telemetry/opentelemetry-collector-contri
66import (
77 "context"
88
9+ "github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
910 apps_v1 "k8s.io/api/apps/v1"
1011 batch_v1 "k8s.io/api/batch/v1"
1112 api_v1 "k8s.io/api/core/v1"
1213 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314 "k8s.io/apimachinery/pkg/fields"
1415 "k8s.io/apimachinery/pkg/labels"
1516 "k8s.io/apimachinery/pkg/runtime"
17+ "k8s.io/apimachinery/pkg/runtime/schema"
1618 "k8s.io/apimachinery/pkg/watch"
1719 "k8s.io/client-go/kubernetes"
20+ "k8s.io/client-go/metadata"
1821 "k8s.io/client-go/tools/cache"
1922)
2023
@@ -88,11 +91,11 @@ func newKubeSystemSharedInformer(
8891) cache.SharedInformer {
8992 informer := cache .NewSharedInformer (
9093 & cache.ListWatch {
91- ListFunc : func (opts metav1.ListOptions ) (runtime.Object , error ) {
94+ ListWithContextFunc : func (ctx context. Context , opts metav1.ListOptions ) (runtime.Object , error ) {
9295 opts .FieldSelector = fields .OneTermEqualSelector ("metadata.name" , kubeSystemNamespace ).String ()
9396 return client .CoreV1 ().Namespaces ().List (context .Background (), opts )
9497 },
95- WatchFunc : func (opts metav1.ListOptions ) (watch.Interface , error ) {
98+ WatchFuncWithContext : func (ctx context. Context , opts metav1.ListOptions ) (watch.Interface , error ) {
9699 opts .FieldSelector = fields .OneTermEqualSelector ("metadata.name" , kubeSystemNamespace ).String ()
97100 return client .CoreV1 ().Namespaces ().Watch (context .Background (), opts )
98101 },
@@ -129,21 +132,6 @@ func namespaceInformerWatchFunc(client kubernetes.Interface) cache.WatchFuncWith
129132 }
130133}
131134
132- func newReplicaSetSharedInformer (
133- client kubernetes.Interface ,
134- namespace string ,
135- ) cache.SharedInformer {
136- informer := cache .NewSharedInformer (
137- & cache.ListWatch {
138- ListWithContextFunc : replicasetListFuncWithSelectors (client , namespace ),
139- WatchFuncWithContext : replicasetWatchFuncWithSelectors (client , namespace ),
140- },
141- & apps_v1.ReplicaSet {},
142- watchSyncPeriod ,
143- )
144- return informer
145- }
146-
147135func replicasetListFuncWithSelectors (client kubernetes.Interface , namespace string ) cache.ListWithContextFunc {
148136 return func (ctx context.Context , opts metav1.ListOptions ) (runtime.Object , error ) {
149137 return client .AppsV1 ().ReplicaSets (namespace ).List (ctx , opts )
@@ -210,6 +198,77 @@ func statefulsetWatchFuncWithSelectors(client kubernetes.Interface, namespace st
210198 }
211199}
212200
201+ // NewReplicaSetMetaInformerProvider returns a provider with the SAME signature
202+ // as your existing helpers, but emits metav1.PartialObjectMetadata.
203+ // It uses ListWithContextFunc and WatchFuncWithContext.
204+ func NewReplicaSetMetaInformerProvider (apiCfg k8sconfig.APIConfig ) func (client kubernetes.Interface , namespace string ) cache.SharedInformer {
205+ return func (client kubernetes.Interface , namespace string ) cache.SharedInformer {
206+ // Build metadata client from APIConfig (mirrors your MakeDynamicClient flow)
207+ if err := apiCfg .Validate (); err != nil {
208+ return cache .NewSharedIndexInformer (
209+ & cache.ListWatch {
210+ ListWithContextFunc : func (ctx context.Context , _ metav1.ListOptions ) (runtime.Object , error ) { return nil , err },
211+ WatchFuncWithContext : func (ctx context.Context , _ metav1.ListOptions ) (watch.Interface , error ) { return nil , err },
212+ },
213+ & metav1.PartialObjectMetadata {},
214+ watchSyncPeriod ,
215+ cache.Indexers {},
216+ )
217+ }
218+
219+ restCfg , err := k8sconfig .CreateRestConfig (apiCfg )
220+ if err != nil {
221+ return cache .NewSharedIndexInformer (
222+ & cache.ListWatch {
223+ ListWithContextFunc : func (ctx context.Context , _ metav1.ListOptions ) (runtime.Object , error ) { return nil , err },
224+ WatchFuncWithContext : func (ctx context.Context , _ metav1.ListOptions ) (watch.Interface , error ) { return nil , err },
225+ },
226+ & metav1.PartialObjectMetadata {},
227+ watchSyncPeriod ,
228+ cache.Indexers {},
229+ )
230+ }
231+
232+ mc , err := metadata .NewForConfig (restCfg )
233+ if err != nil {
234+ return cache .NewSharedIndexInformer (
235+ & cache.ListWatch {
236+ ListWithContextFunc : func (ctx context.Context , _ metav1.ListOptions ) (runtime.Object , error ) { return nil , err },
237+ WatchFuncWithContext : func (ctx context.Context , _ metav1.ListOptions ) (watch.Interface , error ) { return nil , err },
238+ },
239+ & metav1.PartialObjectMetadata {},
240+ watchSyncPeriod ,
241+ cache.Indexers {},
242+ )
243+ }
244+
245+ gvr := schema.GroupVersionResource {Group : "apps" , Version : "v1" , Resource : "replicasets" }
246+
247+ lw := & cache.ListWatch {
248+ ListWithContextFunc : func (ctx context.Context , opts metav1.ListOptions ) (runtime.Object , error ) {
249+ // Populate selectors here if you mirror your existing helpers
250+ if namespace == "" {
251+ return mc .Resource (gvr ).List (ctx , opts )
252+ }
253+ return mc .Resource (gvr ).Namespace (namespace ).List (ctx , opts )
254+ },
255+ WatchFuncWithContext : func (ctx context.Context , opts metav1.ListOptions ) (watch.Interface , error ) {
256+ if namespace == "" {
257+ return mc .Resource (gvr ).Watch (ctx , opts )
258+ }
259+ return mc .Resource (gvr ).Namespace (namespace ).Watch (ctx , opts )
260+ },
261+ }
262+
263+ return cache .NewSharedIndexInformer (
264+ lw ,
265+ & metav1.PartialObjectMetadata {},
266+ watchSyncPeriod ,
267+ cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc },
268+ )
269+ }
270+ }
271+
213272func newDaemonSetSharedInformer (
214273 client kubernetes.Interface ,
215274 namespace string ,
0 commit comments