From 28adb688a8b4628eb9a67921e419587af1696aba Mon Sep 17 00:00:00 2001 From: RRashmit <132996156+RRashmit@users.noreply.github.com> Date: Fri, 20 Dec 2024 14:05:49 +0000 Subject: [PATCH] Updates NAP receivers when NAP directives are deleted (#940) --- internal/collector/otel_collector_plugin.go | 45 ++++++++++++++++++- .../collector/otel_collector_plugin_test.go | 16 +++++++ internal/collector/otelcol.tmpl | 4 +- internal/config/types.go | 7 ++- 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/internal/collector/otel_collector_plugin.go b/internal/collector/otel_collector_plugin.go index 85daaa94d..07f425236 100644 --- a/internal/collector/otel_collector_plugin.go +++ b/internal/collector/otel_collector_plugin.go @@ -539,7 +539,50 @@ func (oc *Collector) updateTcplogReceivers(nginxConfigContext *model.NginxConfig } } - return newTcplogReceiverAdded + tcplogReceiverDeleted := oc.areNapReceiversDeleted(nginxConfigContext) + + return newTcplogReceiverAdded || tcplogReceiverDeleted +} + +func (oc *Collector) areNapReceiversDeleted(nginxConfigContext *model.NginxConfigContext) bool { + listenAddressesToBeDeleted := oc.getConfigDeletedNapReceivers(nginxConfigContext) + if len(listenAddressesToBeDeleted) != 0 { + oc.deleteNapReceivers(listenAddressesToBeDeleted) + return true + } + + return false +} + +func (oc *Collector) deleteNapReceivers(listenAddressesToBeDeleted map[string]bool) { + filteredReceivers := (oc.config.Collector.Receivers.TcplogReceivers)[:0] + for _, receiver := range oc.config.Collector.Receivers.TcplogReceivers { + if !listenAddressesToBeDeleted[receiver.ListenAddress] { + filteredReceivers = append(filteredReceivers, receiver) + } + } + oc.config.Collector.Receivers.TcplogReceivers = filteredReceivers +} + +func (oc *Collector) getConfigDeletedNapReceivers(nginxConfigContext *model.NginxConfigContext) map[string]bool { + elements := make(map[string]bool) + + for _, tcplogReceiver := range oc.config.Collector.Receivers.TcplogReceivers { + elements[tcplogReceiver.ListenAddress] = true + } + + if nginxConfigContext.NAPSysLogServers != nil { + addressesToDelete := make(map[string]bool) + for _, napAddress := range nginxConfigContext.NAPSysLogServers { + if !elements[napAddress] { + addressesToDelete[napAddress] = true + } + } + + return addressesToDelete + } + + return elements } func (oc *Collector) doesTcplogReceiverAlreadyExist(listenAddress string) bool { diff --git a/internal/collector/otel_collector_plugin_test.go b/internal/collector/otel_collector_plugin_test.go index b948e7e06..48db96981 100644 --- a/internal/collector/otel_collector_plugin_test.go +++ b/internal/collector/otel_collector_plugin_test.go @@ -755,6 +755,22 @@ func TestCollector_updateTcplogReceivers(t *testing.T) { assert.Equal(t, "localhost:151", conf.Collector.Receivers.TcplogReceivers[0].ListenAddress) assert.Len(t, conf.Collector.Receivers.TcplogReceivers[0].Operators, 4) }) + + t.Run("Test 3: TcplogReceiver deleted", func(tt *testing.T) { + tcplogReceiverDeleted := collector.updateTcplogReceivers(&model.NginxConfigContext{}) + assert.True(t, tcplogReceiverDeleted) + assert.Empty(t, conf.Collector.Receivers.TcplogReceivers) + }) + + t.Run("Test 4: New tcplogReceiver added and deleted another", func(tt *testing.T) { + tcplogReceiverDeleted := collector.updateTcplogReceivers(&model.NginxConfigContext{NAPSysLogServers: []string{ + "localhost:152", + }}) + assert.True(t, tcplogReceiverDeleted) + assert.Len(t, conf.Collector.Receivers.TcplogReceivers, 1) + assert.Equal(t, "localhost:152", conf.Collector.Receivers.TcplogReceivers[0].ListenAddress) + assert.Len(t, conf.Collector.Receivers.TcplogReceivers[0].Operators, 4) + }) } func createFakeCollector() *typesfakes.FakeCollectorInterface { diff --git a/internal/collector/otelcol.tmpl b/internal/collector/otelcol.tmpl index d92d2b18e..35c7f88ab 100644 --- a/internal/collector/otelcol.tmpl +++ b/internal/collector/otelcol.tmpl @@ -214,6 +214,7 @@ service: {{- end}} {{- end}} pipelines: + {{- if or (ne .Receivers.HostMetrics nil) (gt (len .Receivers.OtlpReceivers) 0) (gt (len .Receivers.NginxReceivers) 0) (gt (len .Receivers.NginxPlusReceivers) 0) }} metrics: receivers: {{- if ne .Receivers.HostMetrics nil }} @@ -252,7 +253,8 @@ service: {{- if ne .Exporters.Debug nil }} - debug {{- end }} - {{- if ne .Receivers.TcplogReceivers nil }} + {{- end }} + {{- if gt (len .Receivers.TcplogReceivers) 0 }} logs: receivers: {{- range $index, $tcplogReceiver := .Receivers.TcplogReceivers }} diff --git a/internal/config/types.go b/internal/config/types.go index 377ccd88d..39fe78925 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -366,9 +366,14 @@ func (c *Config) AreReceiversConfigured() bool { } return c.Collector.Receivers.NginxPlusReceivers != nil || + len(c.Collector.Receivers.NginxPlusReceivers) > 0 || c.Collector.Receivers.OtlpReceivers != nil || + len(c.Collector.Receivers.OtlpReceivers) > 0 || c.Collector.Receivers.NginxReceivers != nil || - c.Collector.Receivers.HostMetrics != nil + len(c.Collector.Receivers.NginxReceivers) > 0 || + c.Collector.Receivers.HostMetrics != nil || + c.Collector.Receivers.TcplogReceivers != nil || + len(c.Collector.Receivers.TcplogReceivers) > 0 } func isAllowedDir(dir string, allowedDirs []string) bool {