diff --git a/.chloggen/k8sobjects-filter-delete.yaml b/.chloggen/k8sobjects-filter-delete.yaml new file mode 100755 index 000000000000..e245115d7c60 --- /dev/null +++ b/.chloggen/k8sobjects-filter-delete.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: k8sobjectreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Adds option to excluded `DELETED` updates when in watch mode. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [26042] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/receiver/k8sobjectsreceiver/README.md b/receiver/k8sobjectsreceiver/README.md index 9c1079d4403d..8084997f62f5 100644 --- a/receiver/k8sobjectsreceiver/README.md +++ b/receiver/k8sobjectsreceiver/README.md @@ -50,6 +50,7 @@ the K8s API server. This can be one of `none` (for no auth), `serviceAccount` - `label_selector`: select objects by label(s) - `field_selector`: select objects by field(s) - `interval`: the interval at which object is pulled, default 60 minutes. Only useful for `pull` mode. +- `exclude_deleted`: whether the object's deleted updates should be dropped. Only usable in `watch` mode. - `resource_version` allows watch resources starting from a specific version (default = `1`). Only available for `watch` mode. If not specified, the receiver will do an initial list to get the resourceVersion before starting the watch. See [Efficient Detection of Change](https://kubernetes.io/docs/reference/using-api/api-concepts/#efficient-detection-of-changes) for details on why this is necessary. - `namespaces`: An array of `namespaces` to collect events from. (default = `all`) - `group`: API group name. It is an optional config. When given resource object is present in multiple groups, diff --git a/receiver/k8sobjectsreceiver/config.go b/receiver/k8sobjectsreceiver/config.go index bb492c45d39c..1f66bce452f8 100644 --- a/receiver/k8sobjectsreceiver/config.go +++ b/receiver/k8sobjectsreceiver/config.go @@ -40,6 +40,7 @@ type K8sObjectsConfig struct { FieldSelector string `mapstructure:"field_selector"` Interval time.Duration `mapstructure:"interval"` ResourceVersion string `mapstructure:"resource_version"` + ExcludeDeleted bool `mapstructure:"exclude_deleted"` gvr *schema.GroupVersionResource } @@ -86,6 +87,10 @@ func (c *Config) Validate() error { if object.Mode == PullMode && object.Interval == 0 { object.Interval = defaultPullInterval } + + if object.Mode == PullMode && object.ExcludeDeleted { + return fmt.Errorf("ExcludeDeleted can only be enabled for watch mode") + } object.gvr = gvr } diff --git a/receiver/k8sobjectsreceiver/config_test.go b/receiver/k8sobjectsreceiver/config_test.go index 293f73245cb0..4bbc98143dea 100644 --- a/receiver/k8sobjectsreceiver/config_test.go +++ b/receiver/k8sobjectsreceiver/config_test.go @@ -50,6 +50,7 @@ func TestLoadConfig(t *testing.T) { Namespaces: []string{"default"}, Group: "events.k8s.io", ResourceVersion: "", + ExcludeDeleted: true, gvr: &schema.GroupVersionResource{ Group: "events.k8s.io", Version: "v1", @@ -130,6 +131,9 @@ func TestLoadConfig(t *testing.T) { { id: component.NewIDWithName(metadata.Type, "invalid_resource"), }, + { + id: component.NewIDWithName(metadata.Type, "exclude_deleted_with_pull"), + }, } for _, tt := range tests { diff --git a/receiver/k8sobjectsreceiver/receiver.go b/receiver/k8sobjectsreceiver/receiver.go index 2c226d8e8626..7bd607d91bc7 100644 --- a/receiver/k8sobjectsreceiver/receiver.go +++ b/receiver/k8sobjectsreceiver/receiver.go @@ -172,6 +172,11 @@ func (kr *k8sobjectsreceiver) startWatch(ctx context.Context, config *K8sObjects kr.setting.Logger.Warn("Watch channel closed unexpectedly", zap.String("resource", config.gvr.String())) return } + + if data.Type == apiWatch.Deleted && config.ExcludeDeleted { + continue + } + logs, err := watchObjectsToLogData(&data, time.Now(), config) if err != nil { kr.setting.Logger.Error("error converting objects to log data", zap.Error(err)) diff --git a/receiver/k8sobjectsreceiver/testdata/config.yaml b/receiver/k8sobjectsreceiver/testdata/config.yaml index 765d06269eba..2d70e910eff6 100644 --- a/receiver/k8sobjectsreceiver/testdata/config.yaml +++ b/receiver/k8sobjectsreceiver/testdata/config.yaml @@ -8,6 +8,7 @@ k8sobjects: mode: watch group: events.k8s.io namespaces: [default] + exclude_deleted: true k8sobjects/pull_with_resource: objects: - name: pods @@ -30,3 +31,8 @@ k8sobjects/invalid_resource: objects: - name: fake_resource mode: watch +k8sobjects/exclude_deleted_with_pull: + objects: + - name: events + mode: pull + exclude_deleted: true