Skip to content

[feat]: return list of discovered targets #1392

@ademariag

Description

@ademariag

Description of the feature

Feature Request: kapitan list --targets

Summary

Add a kapitan list --targets command to quickly enumerate all discovered targets, with support for multiple output formats (plain, JSON, YAML) to enable tooling integration and shell autocompletion.

Motivation

Currently, discovering available targets requires either:

  • Manually inspecting the inventory/targets/ directory structure
  • Running a full kapitan compile and observing output
  • Parsing inventory files directly with external tools

This creates friction for:

  1. Shell autocompletion — Tab-completing kapitan compile -t <TAB> requires knowing valid targets
  2. CI/CD pipelines — Dynamic target discovery for matrix builds or selective compilation
  3. IDE/editor integration — Providing target suggestions in development environments
  4. Scripting — Wrapping Kapitan in higher-level automation without hardcoding target lists

Proposed Solution

Command Interface

# Plain list (default) — one target per line
kapitan list --targets

# JSON output
kapitan list --targets --output-format=json
kapitan list --targets -o json

# YAML output  
kapitan list --targets --output-format=yaml
kapitan list --targets -o yaml

Output Examples

Plain (default):

production/cluster-eu-west-1
production/cluster-us-east-1
staging/cluster-staging
dev/local

JSON:

{
  "targets": [
    "production/cluster-eu-west-1",
    "production/cluster-us-east-1",
    "staging/cluster-staging",
    "dev/local"
  ]
}

YAML:

targets:
  - production/cluster-eu-west-1
  - production/cluster-us-east-1
  - staging/cluster-staging
  - dev/local

Performance Considerations

Speed is critical for this feature, particularly for autocompletion where latency directly impacts UX. Target times should be < 100ms for typical inventories.

Recommended Implementation Approach

  1. Minimal inventory loading — Only discover target names from filesystem paths; avoid full inventory compilation/reclass resolution

  2. Skip class resolution — Don't process classes: inheritance chains; just enumerate files in inventory/targets/

  3. Lazy imports — Defer heavy module imports (Jinja2, reclass, etc.) that aren't needed for simple enumeration

  4. Consider caching — Optional: cache target list with filesystem mtime checking for very large inventories

  5. Parallel directory walking — For inventories with deep nesting, os.scandir() or similar efficient directory traversal

Additional Considerations

Future Extensibility

The kapitan list subcommand could later support:

  • `kapitan list --targets production/* — glob filtering

Flags

Flag Short Description
--output-format -o Output format: plain (default), json, yaml
--inventory-path -i Override inventory path (existing global flag)

Acceptance Criteria

  • kapitan list --targets returns all targets, one per line
  • -o json outputs valid JSON with {"targets": [...]} structure
  • -o yaml outputs valid YAML with targets: list
  • Execution completes in < 100ms for inventories with 100+ targets
  • No inventory compilation or class resolution performed
  • Exit code 0 on success, non-zero on errors
  • Works with --inventory-path override

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions