feat: add multi-format output support#1094
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1094 +/- ##
===========================================
- Coverage 62.36% 30.71% -31.65%
===========================================
Files 197 239 +42
Lines 4854 6534 +1680
===========================================
- Hits 3027 2007 -1020
- Misses 1439 4351 +2912
+ Partials 388 176 -212
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
6eb491f to
d144e2b
Compare
| deployments/ | ||
| ``` | ||
|
|
||
| **Example 2: All formats to stdout (less common)** |
There was a problem hiding this comment.
I think we should prevent that, what's the purpose of mixing multiple format in a single stream?
There was a problem hiding this comment.
You are absolutely right. I'll add validation to reject this case.
The fix will require users to either:
- Use a single format to stdout:
--format json pod.yaml - Or specify explicit output files:
--format json --output a.json --format sarif --output b.sarif pod.yaml
WDYT?
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" |
There was a problem hiding this comment.
In other tests we are using testify for assertions
There was a problem hiding this comment.
Let me use the same approach.
janisz
left a comment
There was a problem hiding this comment.
It needs some tune up to follow our current testing standards e.g. use testify, add e2e bat test
I think the idea is good and I share the sentiment that sometimes in CI it will be good to have human redable output + junit for machines.
d144e2b to
a41d70c
Compare
Enable generation of multiple output formats (JSON, SARIF, plain text) in a single
kube-linter run, eliminating duplicate executions.
Usage:
kube-linter lint \
--format sarif --output kube-linter.sarif \
--format json --output kube-linter.json \
pod.yaml
Key Features:
- Multiple --format and --output flag pairs supported
- Backward compatible: single format to stdout still works
- Partial failure handling: successful outputs preserved on error
- Clear error messages showing success/failure counts
Implementation:
- OutputDestination abstraction for stdout vs file writing
- FormatOutputPair for format-output pairing and validation
- Explicit resource cleanup (no defer in loops)
- Comprehensive integration tests for multi-format scenarios
- 100% test coverage on all new code
All linter checks and tests pass with full coverage.
a41d70c to
9a289af
Compare
|
@ugiordan I'm sorry it took so long 😅 To not make it longer I added bats myself |
Summary
Adds support for generating multiple output formats (JSON, SARIF, plain text) in a single kube-linter run, eliminating the need for duplicate executions.
Before:
After:
# Single run - faster and more convenient kube-linter lint \ --format sarif --output kube-linter.sarif \ --format json --output kube-linter.json \ --config .kube-linter.yaml \ pod.yamlMotivation
In CI/CD pipelines, it's common to need multiple output formats:
Currently, this requires running kube-linter multiple times, which:
Key Features
--formatand--outputflagsImplementation Details
Architecture
Three new abstractions keep the code clean and testable:
OutputDestination- Abstracts stdout vs file writingFormatOutputPair- Represents format+output combinationsMulti-format output loop - Processes each format sequentially
Resource Management
Critical fix: The implementation explicitly closes files immediately after writing instead of using
deferin a loop, preventing file descriptor exhaustion with many outputs.vs.
Testing
Test Coverage
Unit Tests (9 test cases):
Integration Tests (3 comprehensive scenarios):
TestCommand_MultiFormatOutput- End-to-end multi-format writingTestCommand_MultiFormatResourceCleanup- Stress test with 10 filesTestCommand_PartialFailureHandling- Verifies partial success behaviorAll tests passing:
Manual Testing
Examples
Basic Usage
Single format to file:
Multiple formats to files:
CI/CD Integration
GitHub Actions example:
Error Handling
If one format fails, successful outputs are preserved:
Backward Compatibility
All existing commands continue to work exactly as before:
plain--formatto stdout works unchangedDocumentation
Updated documentation with examples:
README.md- Quick examples of multi-format usagedocs/using-kubelinter.md- Detailed guide with CI/CD examplesdocs/plans/2026-01-27-multi-format-output-review.md- Design reviewChecklist
go test ./pkg/command/lint/...)Notes
This PR is not linked to a specific issue. The feature addresses a common workflow inefficiency (running kube-linter multiple times for different output formats) and maintains full backward compatibility. Happy to discuss or adjust the approach based on maintainer feedback!