Skip to content

Commit

Permalink
Conditions on podupdater function of kubernetes autodiscovery (elasti…
Browse files Browse the repository at this point in the history
…c#37431)


* first update for nodePodUpdater with key function

* first update for namespacePodUpdater

* updating elastic-agent-autodiscovery library to v0.6.7

* updating NOTICE.txt

* updating kubernetes_test.go interface functions

* updating pod_test.go by removing unused functions

* updating receiver name in tests

* fixing lint events

---------

Co-authored-by: Michal Pristas <[email protected]>
  • Loading branch information
2 people authored and Scholar-Li committed Feb 5, 2024
1 parent f43bb61 commit 729c208
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 20 deletions.
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
}

0 comments on commit 729c208

Please sign in to comment.