Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditions on podupdater function of kubernetes autodiscovery #37431

Merged
merged 26 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6dedace
first update for nodePodUpdater
gizas Dec 7, 2023
314885f
first update for nodePodUpdater with key function
gizas Dec 8, 2023
a44a7b8
first update for namespacePodUpdater
gizas Dec 8, 2023
39de52d
updating with delta
gizas Dec 12, 2023
7511a0d
fixing referneces only to one wather as an argument
gizas Dec 13, 2023
6d55935
removing comments
gizas Dec 13, 2023
3f2132e
removing test files
gizas Dec 13, 2023
4c4f458
updating changelog
gizas Dec 13, 2023
3763fe4
Merge branch 'main' of github.com:elastic/beats into conditionsonPodu…
gizas Dec 13, 2023
107bc05
Merge branch 'main' of github.com:elastic/beats into conditionsonPodu…
gizas Jan 9, 2024
b2cf925
updates for tests because we changed autodiscover watcher interface
gizas Jan 9, 2024
675a9fe
replacing oldobject with cachedobject
gizas Jan 10, 2024
da0b201
keeping only watcher struct
gizas Jan 11, 2024
b641d4f
Merge branch 'main' of github.com:elastic/beats into conditionsonPodu…
gizas Jan 12, 2024
99c949f
udpating changelog
gizas Jan 12, 2024
f3900af
Update CHANGELOG.next.asciidoc
gizas Jan 12, 2024
e65f21d
Update CHANGELOG.next.asciidoc
gizas Jan 12, 2024
109f152
updating elastic-agent-autodiscovery library to v0.6.7
gizas Jan 15, 2024
f7aec12
updating NOTICE.txt
gizas Jan 15, 2024
fc49bc1
Merge branch 'main' of github.com:elastic/beats into conditionsonPodu…
gizas Jan 15, 2024
9ef1313
updating kubernetes_test.go interface functions
gizas Jan 15, 2024
2383df8
updating pod_test.go by removing unused functions
gizas Jan 15, 2024
d39b3b6
Merge branch 'main' into conditionsonPodupdater
gizas Jan 15, 2024
51295fc
updating receiver name in tests
gizas Jan 15, 2024
c22d63a
fixing lint events
gizas Jan 15, 2024
082d99e
Merge branch 'main' into conditionsonPodupdater
gizas Jan 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Setting environmental variable ELASTIC_NETINFO:false in Elastic Agent pod will d
- Upgrade to Go 1.21.6. {pull}37615[37615]
- The Elasticsearch output can now configure performance presets with the `preset` configuration field. {pull}37259[37259]
- Upgrade to elastic-agent-libs v0.7.3 and golang.org/x/crypto v0.17.0. {pull}37544[37544]
- Make more selective the Pod autodiscovery upon node and namespace update events. {issue}37338[37338] {pull}37431[37431]

*Auditbeat*

Expand Down
4 changes: 2 additions & 2 deletions libbeat/autodiscover/providers/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ func NewPodEventer(uuid uuid.UUID, cfg *conf.C, client k8s.Interface, publish fu
watcher.AddEventHandler(p)

if nodeWatcher != nil && (config.Hints.Enabled() || metaConf.Node.Enabled()) {
updater := kubernetes.NewNodePodUpdater(p.unlockedUpdate, watcher.Store(), &p.crossUpdate)
updater := kubernetes.NewNodePodUpdater(p.unlockedUpdate, watcher.Store(), p.nodeWatcher, &p.crossUpdate)
nodeWatcher.AddEventHandler(updater)
}

if namespaceWatcher != nil && (config.Hints.Enabled() || metaConf.Namespace.Enabled()) {
updater := kubernetes.NewNamespacePodUpdater(p.unlockedUpdate, watcher.Store(), &p.crossUpdate)
updater := kubernetes.NewNamespacePodUpdater(p.unlockedUpdate, watcher.Store(), p.namespaceWatcher, &p.crossUpdate)
namespaceWatcher.AddEventHandler(updater)
}

Expand Down
102 changes: 90 additions & 12 deletions libbeat/autodiscover/providers/kubernetes/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ import (
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
k8sfake "k8s.io/client-go/kubernetes/fake"

interfaces "k8s.io/client-go/kubernetes"
caches "k8s.io/client-go/tools/cache"

"github.com/elastic/beats/v7/libbeat/autodiscover/template"
"github.com/elastic/elastic-agent-autodiscover/bus"
"github.com/elastic/elastic-agent-autodiscover/kubernetes"
Expand Down Expand Up @@ -1988,6 +1992,11 @@ func TestNamespacePodUpdater(t *testing.T) {
}
}

namespace := &kubernetes.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
}}

cases := map[string]struct {
pods []interface{}
expected []interface{}
Expand All @@ -2014,14 +2023,19 @@ func TestNamespacePodUpdater(t *testing.T) {
t.Run(title, func(t *testing.T) {
handler := &mockUpdaterHandler{}
store := &mockUpdaterStore{objects: c.pods}
updater := kubernetes.NewNamespacePodUpdater(handler.OnUpdate, store, &sync.Mutex{})

namespace := &kubernetes.Namespace{
//We simulate an update on the namespace with the addition of one label
namespace1 := &kubernetes.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
}
updater.OnUpdate(namespace)
Labels: map[string]string{
"beta.kubernetes.io/arch": "arm64",
},
}}

watcher := &mockUpdaterWatcher{cachedObject: namespace}
updater := kubernetes.NewNamespacePodUpdater(handler.OnUpdate, store, watcher, &sync.Mutex{})

updater.OnUpdate(namespace1)

assert.EqualValues(t, c.expected, handler.objects)
})
Expand All @@ -2040,8 +2054,15 @@ func TestNodePodUpdater(t *testing.T) {
}
}

node := &kubernetes.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
}

cases := map[string]struct {
pods []interface{}
pods []interface{}

expected []interface{}
}{
"no pods": {},
Expand All @@ -2066,14 +2087,21 @@ func TestNodePodUpdater(t *testing.T) {
t.Run(title, func(t *testing.T) {
handler := &mockUpdaterHandler{}
store := &mockUpdaterStore{objects: c.pods}
updater := kubernetes.NewNodePodUpdater(handler.OnUpdate, store, &sync.Mutex{})

node := &kubernetes.Node{
//We simulate an update on the node with the addition of one label
node1 := &kubernetes.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
}
updater.OnUpdate(node)
Annotations: map[string]string{
"beta.kubernetes.io/arch": "arm64",
},
}}

watcher := &mockUpdaterWatcher{cachedObject: node}
updater := kubernetes.NewNodePodUpdater(handler.OnUpdate, store, watcher, &sync.Mutex{})

//This is when the update happens.
updater.OnUpdate(node1)

assert.EqualValues(t, c.expected, handler.objects)
})
Expand All @@ -2092,10 +2120,60 @@ type mockUpdaterStore struct {
objects []interface{}
}

var store caches.Store
var client interfaces.Interface
var err error

type mockUpdaterWatcher struct {
cachedObject runtime.Object
}

func (s *mockUpdaterWatcher) CachedObject() runtime.Object {
return s.cachedObject
}

func (s *mockUpdaterWatcher) Client() interfaces.Interface {
return client
}

func (s *mockUpdaterWatcher) Start() error {
return err
}

func (s *mockUpdaterWatcher) Stop() {
}

func (s *mockUpdaterWatcher) Store() caches.Store {
return store
}

func (s *mockUpdaterWatcher) AddEventHandler(kubernetes.ResourceEventHandler) {
}

func (s *mockUpdaterStore) List() []interface{} {
return s.objects
}

type mockNamespaceStore struct {
objects interface{}
exist bool
err error
}

func (s *mockNamespaceStore) GetByKey(string) (interface{}, bool, error) {
return s.objects, s.exist, s.err
}

type mockNodeStore struct {
objects interface{}
exist bool
err error
}

func (s *mockNodeStore) GetByKey(string) (interface{}, bool, error) {
return s.objects, s.exist, s.err
}

func NewMockPodEventerManager(pod *pod) EventManager {
em := &eventerManager{}
em.eventer = pod
Expand Down
Loading