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 all 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 NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12257,11 +12257,11 @@ SOFTWARE.

--------------------------------------------------------------------------------
Dependency : github.com/elastic/elastic-agent-autodiscover
Version: v0.6.6
Version: v0.6.7
Licence type (autodetected): Apache-2.0
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/elastic/[email protected].6/LICENSE:
Contents of probable licence file $GOMODCACHE/github.com/elastic/[email protected].7/LICENSE:

Apache License
Version 2.0, January 2004
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ require (
github.com/aws/smithy-go v1.13.5
github.com/awslabs/kinesis-aggregation/go/v2 v2.0.0-20220623125934-28468a6701b5
github.com/elastic/bayeux v1.0.5
github.com/elastic/elastic-agent-autodiscover v0.6.6
github.com/elastic/elastic-agent-autodiscover v0.6.7
github.com/elastic/elastic-agent-libs v0.7.3
github.com/elastic/elastic-agent-shipper-client v0.5.1-0.20230228231646-f04347b666f3
github.com/elastic/elastic-agent-system-metrics v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,8 @@ github.com/elastic/bayeux v1.0.5 h1:UceFq01ipmT3S8DzFK+uVAkbCdiPR0Bqei8qIGmUeY0=
github.com/elastic/bayeux v1.0.5/go.mod h1:CSI4iP7qeo5MMlkznGvYKftp8M7qqP/3nzmVZoXHY68=
github.com/elastic/dhcp v0.0.0-20200227161230-57ec251c7eb3 h1:lnDkqiRFKm0rxdljqrj3lotWinO9+jFmeDXIC4gvIQs=
github.com/elastic/dhcp v0.0.0-20200227161230-57ec251c7eb3/go.mod h1:aPqzac6AYkipvp4hufTyMj5PDIphF3+At8zr7r51xjY=
github.com/elastic/elastic-agent-autodiscover v0.6.6 h1:P1y0dDpbhJc7Uw/xe85irPEad4Vljygc+y4iSxtqW7A=
github.com/elastic/elastic-agent-autodiscover v0.6.6/go.mod h1:chulyCAyZb/njMHgzkhC/yWnt8v/Y6eCRUhmFVnsA5o=
github.com/elastic/elastic-agent-autodiscover v0.6.7 h1:+KVjltN0rPsBrU8b156gV4lOTBgG/vt0efFCFARrf3g=
github.com/elastic/elastic-agent-autodiscover v0.6.7/go.mod h1:hFeFqneS2r4jD0/QzGkrNk0YVdN0JGh7lCWdsH7zcI4=
github.com/elastic/elastic-agent-client/v7 v7.6.0 h1:FEn6FjzynW4TIQo5G096Tr7xYK/P5LY9cSS6wRbXZTc=
github.com/elastic/elastic-agent-client/v7 v7.6.0/go.mod h1:GlUKrbVd/O1CRAZonpBeN3J0RlVqP6VGcrBjFWca+aM=
github.com/elastic/elastic-agent-libs v0.7.3 h1:tc6JDXYR+2XFMHJVv+7+M0OwAbZPxm3caLJEd943dlE=
Expand Down
6 changes: 3 additions & 3 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 Expand Up @@ -407,7 +407,7 @@ func (p *pod) containerPodEvents(flag string, pod *kubernetes.Pod, c *kubernetes
ports = []kubernetes.ContainerPort{{ContainerPort: 0}}
}

var events []bus.Event
events := []bus.Event{}
portsMap := mapstr.M{}

ShouldPut(meta, "container", cmeta, p.logger)
Expand Down
82 changes: 70 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,6 +2120,36 @@ 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
}
Expand Down
5 changes: 5 additions & 0 deletions metricbeat/module/kubernetes/util/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"

"github.com/elastic/elastic-agent-autodiscover/kubernetes"
Expand Down Expand Up @@ -180,3 +181,7 @@ func (m *mockWatcher) Store() cache.Store {
func (m *mockWatcher) Client() k8s.Interface {
return nil
}

func (m *mockWatcher) CachedObject() runtime.Object {
return nil
}
Loading