Skip to content

feat: add ndjson output format with field selection to list commands - #3068

Open
vikashkumar2020 wants to merge 1 commit into
tektoncd:mainfrom
vikashkumar2020:feat/field-selection
Open

feat: add ndjson output format with field selection to list commands#3068
vikashkumar2020 wants to merge 1 commit into
tektoncd:mainfrom
vikashkumar2020:feat/field-selection

Conversation

@vikashkumar2020

Copy link
Copy Markdown

Changes

Add --output ndjson and --fields to every tkn * list command so that
automation scripts and AI agents can consume resource lists incrementally and
request only the fields they need.

--output ndjson — newline-delimited JSON streaming

Instead of a single JSON array, each resource is emitted as one JSON object
per line (NDJSON / JSON Lines). Consumers can begin processing the first record
before the last one arrives, and never need to buffer an entire response in
memory.

tkn pipelinerun list --output ndjson
tkn taskrun list    --output ndjson -n staging
tkn pipeline list   --output ndjson -A

--fields — dot-path field projection

Restricts each NDJSON object to the requested fields, specified as a
comma-separated list of dot-separated paths. Shared path prefixes are merged
into a single nested object; unknown paths are silently ignored.

# Only name + status condition for every PipelineRun
tkn pipelinerun list --output ndjson \
  --fields metadata.name,status.conditions

# Combine with --all-namespaces
tkn taskrun list --output ndjson -A \
  --fields metadata.name,metadata.namespace,status.startTime

Implementation

New helper in pkg/formatted/ndjson.go:

Symbol Purpose
PrintNDJSON(w, obj, fields) Converts any runtime.Object list to NDJSON via runtime.DefaultUnstructuredConverter, then streams one line per item
pickFields(src, fields) Dot-path projection — builds a new map with only the requested paths
getNestedField / setNestedField Recursive helpers for reading/writing dot-separated paths

Each of the nine list commands gains:

  • Fields []string in its ListOptions / listOptions struct
  • --fields flag wired to that slice
  • A case output == "ndjson": branch (in switch {} form to satisfy gocritic) that calls formatted.PrintNDJSON

The --output flag is the existing cliopts.NewPrintFlags flag — no new top-level flag required.

Commands covered

Command Package
tkn pipeline list pkg/cmd/pipeline
tkn pipelinerun list pkg/cmd/pipelinerun
tkn task list pkg/cmd/task
tkn taskrun list pkg/cmd/taskrun
tkn customrun list pkg/cmd/customrun
tkn eventlistener list pkg/cmd/eventlistener
tkn triggertemplate list pkg/cmd/triggertemplate
tkn triggerbinding list pkg/cmd/triggerbinding
tkn clustertriggerbinding list pkg/cmd/clustertriggerbinding

(tkn bundle list reads from an OCI registry, not a Kubernetes API, and is
intentionally excluded.)

Tests

pkg/formatted/ndjson_test.go covers:

  • All-fields output produces one valid JSON object per list item
  • Multi-field dot-path projection includes only the requested keys
  • Top-level field selection (no dot) works
  • Unknown paths are silently ignored; known paths are still present
  • Empty list produces empty output

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you
review them:

  • Includes tests (if functionality changed/added)
  • Run the code checkers with make check
  • Regenerate the manpages, docs and go formatting with make generated
  • Commit messages follow commit message best practices

See the contribution guide
for more details.

Release Notes

All `tkn * list` commands now support `--output ndjson` (one JSON object per
line, streamable) and `--fields` (comma-separated dot-path projection, e.g.
`metadata.name,status.conditions`) to reduce token usage for automation and
AI-agent workflows.

@tekton-robot tekton-robot added the release-note Denotes a PR that will be considered when it comes time to generate release notes. label Jul 27, 2026
@tekton-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
To complete the pull request process, please assign divyansh42 after the PR has been reviewed.
You can assign the PR to them by writing /assign @divyansh42 in a comment when ready.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tekton-robot tekton-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jul 27, 2026
@divyansh42
divyansh42 requested a review from Copilot July 27, 2026 16:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new NDJSON (--output ndjson) streaming output mode plus optional field projection (--fields) to Tekton CLI tkn * list commands, enabling incremental consumption of Kubernetes list results and reducing payload size for automation workflows.

Changes:

  • Introduces pkg/formatted.PrintNDJSON to stream Kubernetes list items as one JSON object per line, with optional dot-path field projection.
  • Adds a --fields flag and an ndjson output branch to the supported list commands.
  • Adds unit tests for NDJSON output and field projection behavior.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pkg/formatted/ndjson.go Adds NDJSON list streaming + dot-path field projection helpers.
pkg/formatted/ndjson_test.go Adds tests covering NDJSON output and field selection semantics.
pkg/cmd/pipeline/list.go Adds --fields and --output ndjson handling for tkn pipeline list.
pkg/cmd/pipelinerun/list.go Adds --fields and --output ndjson handling for tkn pipelinerun list.
pkg/cmd/task/list.go Adds --fields and --output ndjson handling for tkn task list.
pkg/cmd/taskrun/list.go Adds --fields and --output ndjson handling for tkn taskrun list.
pkg/cmd/customrun/list.go Adds --fields and --output ndjson handling for tkn customrun list.
pkg/cmd/eventlistener/list.go Adds --fields and --output ndjson handling for tkn eventlistener list.
pkg/cmd/triggertemplate/list.go Adds --fields and --output ndjson handling for tkn triggertemplate list.
pkg/cmd/triggerbinding/list.go Adds --fields and --output ndjson handling for tkn triggerbinding list.
pkg/cmd/clustertriggerbinding/list.go Adds --fields and --output ndjson handling for tkn clustertriggerbinding list.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/formatted/ndjson.go
Comment on lines +29 to +43
func PrintNDJSON(w io.Writer, obj runtime.Object, fields []string) error {
// Convert the list to unstructured so we can work with raw map[string]any.
raw, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return fmt.Errorf("failed to convert object to unstructured: %w", err)
}

itemsVal, ok := raw["items"]
if !ok {
return nil
}
items, ok := itemsVal.([]any)
if !ok {
return nil
}
Comment thread pkg/cmd/task/list.go
Comment on lines +86 to +88
if err := actions.ListV1(taskGroupResource, cs, metav1.ListOptions{}, ns, &tl); err != nil {
return fmt.Errorf("failed to list Tasks from namespace %s: %v", ns, err)
}
Comment thread pkg/cmd/pipeline/list.go
Comment on lines +97 to +99
if err := actions.ListV1(pipelineGroupResource, cs, metav1.ListOptions{}, ns, &pl); err != nil {
return fmt.Errorf("failed to list Pipelines from namespace %s: %v", ns, err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants