Skip to content

Commit

Permalink
[chore] Move newNodeSharedInformer to a common/shared pkg (#32591)
Browse files Browse the repository at this point in the history
**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
This PR implements some basic refactoring required for
#32295.
Doing this in a standalone PR was requested at
#32295 (comment).

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:** <Issue number if applicable>

**Testing:** <Describe what testing was performed and which tests were
added.>

**Documentation:** <Describe the documentation added.>

Signed-off-by: ChrsMark <chrismarkou92@gmail.com>
Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com>
  • Loading branch information
ChrsMark and TylerHelmuth authored Apr 29, 2024
1 parent dd361de commit 3f64154
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 24 deletions.
1 change: 1 addition & 0 deletions connector/datadogconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions exporter/datadogexporter/integrationtest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions internal/k8sconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion internal/k8sconfig/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/kubelet/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/metadataproviders/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion processor/k8sattributesprocessor/internal/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 0 additions & 22 deletions processor/k8sattributesprocessor/internal/kube/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 3f64154

Please sign in to comment.