Skip to content

🧹 resource watcher improvements#1369

Merged
chris-rock merged 11 commits intomainfrom
chris-rock/resource-watcher
Feb 12, 2026
Merged

🧹 resource watcher improvements#1369
chris-rock merged 11 commits intomainfrom
chris-rock/resource-watcher

Conversation

@chris-rock
Copy link
Copy Markdown
Member

This makes sure we properly identifies scanned resources as live K8s cluster resources. It scans only the
specific resources that changed using cnspec's k8s-resources filter.

  • Added K8sResourceIdentifier to track type, namespace, and name
  • Updated debouncer to collect full resource identifiers
  • Scanner generates inventory with k8s-resources option for targeted scanning
  • More efficient: scans only changed resources, not all of a type

Fixes #1366

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Feb 5, 2026

Test Results

  5 files  ± 0   42 suites  ±0   37m 21s ⏱️ + 2m 11s
348 tests +21  348 ✅ +21  0 💤 ±0  0 ❌ ±0 
370 runs  +24  368 ✅ +24  2 💤 ±0  0 ❌ ±0 

Results for commit 294d5ee. ± Comparison against base commit a00fa19.

This pull request removes 2 and adds 23 tests. Note that renamed tests count towards both.
go.mondoo.com/mondoo-operator/controllers/resource_watcher ‑ TestCombineManifests
go.mondoo.com/mondoo-operator/controllers/resource_watcher ‑ TestCombineManifests_Empty
go.mondoo.com/mondoo-operator/controllers/resource_watcher ‑ TestDebouncer_MultipleNamespaces
go.mondoo.com/mondoo-operator/controllers/resource_watcher ‑ TestDeployment_EmptyClusterUIDAndIntegrationMRN
go.mondoo.com/mondoo-operator/controllers/resource_watcher ‑ TestDeployment_WithClusterUIDAndIntegrationMRN
go.mondoo.com/mondoo-operator/controllers/resource_watcher ‑ TestK8sResourceIdentifier_String
go.mondoo.com/mondoo-operator/controllers/resource_watcher ‑ TestToSingular
go.mondoo.com/mondoo-operator/controllers/resource_watcher ‑ TestToSingular/Deployments
go.mondoo.com/mondoo-operator/controllers/resource_watcher ‑ TestToSingular/INGRESSES
go.mondoo.com/mondoo-operator/controllers/resource_watcher ‑ TestToSingular/configmaps
go.mondoo.com/mondoo-operator/controllers/resource_watcher ‑ TestToSingular/cronjobs
go.mondoo.com/mondoo-operator/controllers/resource_watcher ‑ TestToSingular/daemonsets
…

♻️ This comment has been updated with latest results.

@chris-rock chris-rock force-pushed the chris-rock/resource-watcher branch 3 times, most recently from ba19c8e to 0352cb0 Compare February 9, 2026 12:09
chris-rock and others added 9 commits February 11, 2026 16:07
This makes sure we properly identifies scanned resources as live K8s cluster resources.
Instead of scanning all resources of changed types, now scans only the
specific resources that changed using cnspec's k8s-resources filter.

- Added K8sResourceIdentifier to track type, namespace, and name
- Updated debouncer to collect full resource identifiers
- Scanner generates inventory with k8s-resources option for targeted scanning
- More efficient: scans only changed resources, not all of a type

Fixes #1366
Use explicit mapping between plural and singular resource type names
instead of naive string manipulation. This fixes scanning for ingresses
(ingresses → ingress, not ingresse) and other irregular plurals.

- Add resourceTypePluralization map and ToSingular() function
- Store plural form in K8sResourceIdentifier.Type
- Convert to singular only in String() for cnspec k8s-resources filter
- Add tests for pluralization logic

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…consistency

Avoid emitting empty namespace/namespaces-exclude in inventory options,
return unknown resource types as-is instead of naively stripping trailing 's',
and fix plural key format in debouncer test.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When ClusterUID is not provided, ManagedBy was set to "mondoo-operator-"
(with trailing hyphen). Now defaults to "mondoo-operator" and only appends
the hyphen and UID when present. Also extracts IIFEs into plain variables
for readability.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Make ToSingular a pure function by removing the watcherLogger call.
Also apply Go modernize lints: use slices.Contains for namespace
filtering and replace interface{} with any.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
These shared types were defined in scanner.go but used across
debouncer.go, watcher.go, and their tests. Moving them to a
dedicated types.go improves discoverability.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Previously, failures to fetch the cluster UID or integration MRN
would abort the entire deployment sync. These are optional metadata
for asset labeling and should not block the resource watcher from
being deployed, e.g. in RBAC-restricted environments.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Remove unused `scheme` field from ResourceWatcher and `gvk` from
  resourceEventHandler (leftover from YAML serialization approach)
- Move lastScanTime update to after scan completion so the rate limit
  interval is measured between scan completions, not scan starts
- Sort discovery targets for deterministic inventory YAML output

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@chris-rock chris-rock force-pushed the chris-rock/resource-watcher branch from 0352cb0 to b1ac12c Compare February 11, 2026 15:09
chris-rock and others added 2 commits February 11, 2026 16:10
…notations

The rebase conflict resolution missed updating this test call to include
the new integrationMRN and clusterUID parameters.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add TestReconcile_ResourceWatcher to verify the resource watcher detects
K8s resource changes and scans them via cnspec. The test enables the
resource watcher with short debounce/scan intervals, waits for the
deployment to become ready, creates a test deployment to trigger a scan,
and polls until assets appear upstream and are scored.

Also fix the resource watcher deployment to use MondooOperatorImage
instead of CnspecImage, since the deployment runs /mondoo-operator
(not cnspec).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Member

@imilchev imilchev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@chris-rock chris-rock merged commit 7a106fb into main Feb 12, 2026
24 checks passed
@chris-rock chris-rock deleted the chris-rock/resource-watcher branch February 12, 2026 08:35
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix Event based scanning

2 participants