From 3f641540da38313ef4a982dc9fef49d2952ff88a Mon Sep 17 00:00:00 2001 From: Chris Mark Date: Mon, 29 Apr 2024 11:54:36 +0300 Subject: [PATCH] [chore] Move newNodeSharedInformer to a common/shared pkg (#32591) **Description:** This PR implements some basic refactoring required for https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/32295. Doing this in a standalone PR was requested at https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/32295#issuecomment-2064312509. The change moves a library method called `newNodeSharedInformer` from the `k8sattributes` processor package to a generic one since it can be re-used by multiple various components. Note: I'm not sure if a changelog is required for this change, so I left it out for now. **Link to tracking Issue:** **Testing:** **Documentation:** Signed-off-by: ChrsMark Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com> --- connector/datadogconnector/go.mod | 1 + .../datadogexporter/integrationtest/go.mod | 1 + internal/k8sconfig/config.go | 30 +++++++++++++++++++ internal/k8sconfig/go.mod | 3 +- internal/kubelet/go.mod | 1 + internal/metadataproviders/go.mod | 1 + .../internal/kube/client.go | 2 +- .../internal/kube/informer.go | 22 -------------- 8 files changed, 37 insertions(+), 24 deletions(-) diff --git a/connector/datadogconnector/go.mod b/connector/datadogconnector/go.mod index a34c445b8f0d..78ba4d114951 100644 --- a/connector/datadogconnector/go.mod +++ b/connector/datadogconnector/go.mod @@ -83,6 +83,7 @@ require ( github.com/golang/protobuf v1.5.4 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/gnostic-models v0.6.8 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7 // indirect github.com/google/uuid v1.6.0 // indirect diff --git a/exporter/datadogexporter/integrationtest/go.mod b/exporter/datadogexporter/integrationtest/go.mod index eeacde29609c..eff41b07d85c 100644 --- a/exporter/datadogexporter/integrationtest/go.mod +++ b/exporter/datadogexporter/integrationtest/go.mod @@ -82,6 +82,7 @@ require ( github.com/golang/protobuf v1.5.4 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/gnostic-models v0.6.8 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect diff --git a/internal/k8sconfig/config.go b/internal/k8sconfig/config.go index b1ef343d97b2..578a6fe079a8 100644 --- a/internal/k8sconfig/config.go +++ b/internal/k8sconfig/config.go @@ -4,16 +4,24 @@ package k8sconfig // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig" import ( + "context" "fmt" "net" "net/http" "os" + "time" quotaclientset "github.com/openshift/client-go/quota/clientset/versioned" + api_v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/fields" + "k8s.io/apimachinery/pkg/runtime" k8sruntime "k8s.io/apimachinery/pkg/util/runtime" + "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/dynamic" k8s "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" + "k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/clientcmd" ) @@ -179,3 +187,25 @@ func MakeOpenShiftQuotaClient(apiConf APIConfig) (quotaclientset.Interface, erro return client, nil } + +func NewNodeSharedInformer(client k8s.Interface, nodeName string, watchSyncPeriod time.Duration) cache.SharedInformer { + informer := cache.NewSharedInformer( + &cache.ListWatch{ + ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { + if nodeName != "" { + opts.FieldSelector = fields.OneTermEqualSelector("metadata.name", nodeName).String() + } + return client.CoreV1().Nodes().List(context.Background(), opts) + }, + WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) { + if nodeName != "" { + opts.FieldSelector = fields.OneTermEqualSelector("metadata.name", nodeName).String() + } + return client.CoreV1().Nodes().Watch(context.Background(), opts) + }, + }, + &api_v1.Node{}, + watchSyncPeriod, + ) + return informer +} diff --git a/internal/k8sconfig/go.mod b/internal/k8sconfig/go.mod index b43e665d446b..8b1c41a414d4 100644 --- a/internal/k8sconfig/go.mod +++ b/internal/k8sconfig/go.mod @@ -4,6 +4,7 @@ go 1.21.0 require ( github.com/openshift/client-go v0.0.0-20210521082421-73d9475a9142 + k8s.io/api v0.29.3 k8s.io/apimachinery v0.29.3 k8s.io/client-go v0.29.3 ) @@ -18,6 +19,7 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/gnostic-models v0.6.8 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/google/uuid v1.4.0 // indirect github.com/imdario/mergo v0.3.11 // indirect @@ -40,7 +42,6 @@ require ( gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - k8s.io/api v0.29.3 // indirect k8s.io/klog/v2 v2.110.1 // indirect k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect diff --git a/internal/kubelet/go.mod b/internal/kubelet/go.mod index aeeb4f385da9..ef0287149c8a 100644 --- a/internal/kubelet/go.mod +++ b/internal/kubelet/go.mod @@ -23,6 +23,7 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/gnostic-models v0.6.8 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/google/uuid v1.4.0 // indirect github.com/imdario/mergo v0.3.11 // indirect diff --git a/internal/metadataproviders/go.mod b/internal/metadataproviders/go.mod index 67adbfc00f4a..e7ff0fcacf02 100644 --- a/internal/metadataproviders/go.mod +++ b/internal/metadataproviders/go.mod @@ -38,6 +38,7 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/gnostic-models v0.6.8 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect diff --git a/processor/k8sattributesprocessor/internal/kube/client.go b/processor/k8sattributesprocessor/internal/kube/client.go index 91d0f4f648b7..466755d25f70 100644 --- a/processor/k8sattributesprocessor/internal/kube/client.go +++ b/processor/k8sattributesprocessor/internal/kube/client.go @@ -172,7 +172,7 @@ func New(logger *zap.Logger, apiCfg k8sconfig.APIConfig, rules ExtractionRules, } if c.extractNodeLabelsAnnotations() || c.extractNodeUID() { - c.nodeInformer = newNodeSharedInformer(c.kc, c.Filters.Node) + c.nodeInformer = k8sconfig.NewNodeSharedInformer(c.kc, c.Filters.Node, 5*time.Minute) } return c, err diff --git a/processor/k8sattributesprocessor/internal/kube/informer.go b/processor/k8sattributesprocessor/internal/kube/informer.go index 73b355f491b3..ddb10c24f060 100644 --- a/processor/k8sattributesprocessor/internal/kube/informer.go +++ b/processor/k8sattributesprocessor/internal/kube/informer.go @@ -40,28 +40,6 @@ type InformerProviderNode func( client kubernetes.Interface, ) cache.SharedInformer -func newNodeSharedInformer(client kubernetes.Interface, nodeName string) cache.SharedInformer { - informer := cache.NewSharedInformer( - &cache.ListWatch{ - ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { - if nodeName != "" { - opts.FieldSelector = fields.OneTermEqualSelector("metadata.name", nodeName).String() - } - return client.CoreV1().Nodes().List(context.Background(), opts) - }, - WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) { - if nodeName != "" { - opts.FieldSelector = fields.OneTermEqualSelector("metadata.name", nodeName).String() - } - return client.CoreV1().Nodes().Watch(context.Background(), opts) - }, - }, - &api_v1.Node{}, - watchSyncPeriod, - ) - return informer -} - // InformerProviderReplicaSet defines a function type that returns a new SharedInformer. It is used to // allow passing custom shared informers to the watch client. type InformerProviderReplicaSet func(