Skip to content

🐛 handle asset explorer discovery targets properly#7089

Merged
imilchev merged 2 commits intomainfrom
ivan/asset-explorer-discovery-targets
Apr 2, 2026
Merged

🐛 handle asset explorer discovery targets properly#7089
imilchev merged 2 commits intomainfrom
ivan/asset-explorer-discovery-targets

Conversation

@imilchev
Copy link
Copy Markdown
Member

@imilchev imilchev commented Apr 2, 2026

Summary

Fixes discovery target filtering for staged discovery. Previously, --discover pods would still scan intermediate hierarchy levels (namespaces) because all discovered assets with platform IDs were treated as scannable.

Problem

With staged discovery (AssetExplorer), the K8s provider emits namespace assets in Stage 1 so that AssetExplorer can connect to them and trigger Stage 2 (workload discovery). But the scanner treats every asset with a platform ID as scannable — so namespaces were added to the progress bar and scanned even when the user only asked for pods.

Solution

Strip platform IDs from intermediate assets that don't match discovery targets. The existing "no platform IDs → skip" logic in AssetExplorer and the scanner already handles these correctly:

  • Assets without platform IDs are still connected (triggering the next discovery stage)
  • Their children are discovered normally
  • They are never added to the progress bar or sent for scanning
  • They are closed after their children are processed

This requires zero changes to AssetExplorer or the scanner — it's purely a provider-side convention.

Changes

  • providers/k8s/resources/discovery.go — In discoverClusterStage, check if namespaces are a discovery target. If not, set ns.PlatformIds = nil before emitting. The namespace is still emitted with its connection config (triggering Stage 2), but skipped by the scanner.
  • docs/adr/002-staged-discovery.md — New "Traversal-Only Assets" section documenting the pattern with code examples and a generalization table for K8s/GCP/AWS.
  • .claude/skills/staged-discovery/SKILL.md — New step for stripping platform IDs, updated verification and checklist.

Example

# Before: --discover pods scans namespaces too
cnspec scan k8s --discover pods
→ Scans: [cluster, ns-default, ns-prod, pod-a, pod-b, pod-c]

# After: namespaces are traversal-only (no platform IDs)
cnspec scan k8s --discover pods
→ Scans: [pod-a, pod-b, pod-c]
→ Traversed but not scanned: [ns-default, ns-prod]

Test plan

  • cnspec scan k8s --discover pods — only pods appear in the progress bar, namespaces are traversed but not scanned
  • cnspec scan k8s --discover namespaces — namespaces are scanned (platform IDs preserved)
  • cnspec scan k8s --discover auto — everything is scanned (backward compatible)
  • cnspec scan k8s — default discovery unchanged
  • go build ./providers/k8s/... compiles

🤖 Generated with Claude Code

Copy link
Copy Markdown

@mondoo-code-review mondoo-code-review bot left a comment

Choose a reason for hiding this comment

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

Discovery target filtering for k8s namespaces works correctly; documentation and skill guide updated accordingly.

Additional findings (file/line not in diff):

  • 🟡 providers/k8s/resources/discovery.go:192 — Pre-existing issue (not introduced by this PR): if invConfig.Discover == nil and conn.Asset() == nil, the early return is skipped and the function will panic at line 217 (invConfig.Discover.Targets). discoverNamespaceStage (line 273) handles this correctly by returning unconditionally when Discover is nil. Consider aligning the guard here, e.g.:
if invConfig.Discover == nil || len(invConfig.Discover.Targets) == 0 {
    if conn.Asset() != nil {
        in.Spec.Assets = append(in.Spec.Assets, conn.Asset())
    }
    return in, nil
}

@github-actions

This comment has been minimized.

Signed-off-by: Ivan Milchev <ivan@mondoo.com>
@imilchev imilchev force-pushed the ivan/asset-explorer-discovery-targets branch from f4aab07 to a9e2d77 Compare April 2, 2026 08:19
@imilchev imilchev changed the title 🧹 handle asset explorer discovery targets properly 🐛 handle asset explorer discovery targets properly Apr 2, 2026
Signed-off-by: Ivan Milchev <ivan@mondoo.com>
@imilchev imilchev force-pushed the ivan/asset-explorer-discovery-targets branch from a9e2d77 to 9730353 Compare April 2, 2026 08:23
Copy link
Copy Markdown

@mondoo-code-review mondoo-code-review bot left a comment

Choose a reason for hiding this comment

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

Discovery target filtering now correctly prevents intermediate K8s namespace assets from appearing in scan results when not explicitly targeted.

@@ -1,4 +1,4 @@
// Copyright (c) Mondoo, Inc.
// Copyright Mondoo, Inc. 2024, 2026
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 warning — This is a generated file (Code generated by resources. DO NOT EDIT.) with an unrelated copyright header change. It shouldn't be part of this PR — was it accidentally included? If the generator changed the copyright format, that change should be in a separate commit/PR.

@imilchev imilchev merged commit f8a5fe1 into main Apr 2, 2026
21 checks passed
@imilchev imilchev deleted the ivan/asset-explorer-discovery-targets branch April 2, 2026 08:30
@github-actions github-actions bot locked and limited conversation to collaborators Apr 2, 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.

1 participant