feat: add ndjson output format with field selection to list commands - #3068
feat: add ndjson output format with field selection to list commands#3068vikashkumar2020 wants to merge 1 commit into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
There was a problem hiding this comment.
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.PrintNDJSONto stream Kubernetes listitemsas one JSON object per line, with optional dot-path field projection. - Adds a
--fieldsflag and anndjsonoutput branch to the supportedlistcommands. - 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.
| 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 | ||
| } |
| 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) | ||
| } |
| 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) | ||
| } |
Changes
Add
--output ndjsonand--fieldsto everytkn * listcommand so thatautomation scripts and AI agents can consume resource lists incrementally and
request only the fields they need.
--output ndjson— newline-delimited JSON streamingInstead 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.
--fields— dot-path field projectionRestricts 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.
Implementation
New helper in
pkg/formatted/ndjson.go:PrintNDJSON(w, obj, fields)runtime.Objectlist to NDJSON viaruntime.DefaultUnstructuredConverter, then streams one line per itempickFields(src, fields)getNestedField/setNestedFieldEach of the nine
listcommands gains:Fields []stringin itsListOptions/listOptionsstruct--fieldsflag wired to that slicecase output == "ndjson":branch (inswitch {}form to satisfygocritic) that callsformatted.PrintNDJSONThe
--outputflag is the existingcliopts.NewPrintFlagsflag — no new top-level flag required.Commands covered
tkn pipeline listpkg/cmd/pipelinetkn pipelinerun listpkg/cmd/pipelineruntkn task listpkg/cmd/tasktkn taskrun listpkg/cmd/taskruntkn customrun listpkg/cmd/customruntkn eventlistener listpkg/cmd/eventlistenertkn triggertemplate listpkg/cmd/triggertemplatetkn triggerbinding listpkg/cmd/triggerbindingtkn clustertriggerbinding listpkg/cmd/clustertriggerbinding(
tkn bundle listreads from an OCI registry, not a Kubernetes API, and isintentionally excluded.)
Tests
pkg/formatted/ndjson_test.gocovers:Submitter Checklist
These are the criteria that every PR should meet, please check them off as you
review them:
make checkmake generatedSee the contribution guide
for more details.
Release Notes