Add short output format to kubectl version command#1
Conversation
This change adds a 'short' output format option to the kubectl version command that displays just the version numbers without labels, making it more suitable for scripting scenarios. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Greptile Summary
This PR adds a new 'short' output format to the kubectl version command, enhancing its scripting capabilities. The feature allows users to output only version numbers (e.g., 'v1.28.0') without descriptive labels like 'Client Version:' or 'Server Version:', making it ideal for automated scripts and CI/CD pipelines that need to parse version information.
The implementation modifies the validation logic in version.go to accept 'short' alongside existing 'yaml' and 'json' formats. The short format logic outputs only the GitVersion field without any formatting labels. The change integrates seamlessly with the existing command structure, following the same pattern as other output formats.
A comprehensive test suite was added to validate the new functionality, ensuring that short format produces clean, single-line output for client-only scenarios and excludes any descriptive labels. The test covers edge cases and maintains consistency with existing test patterns in the codebase.
Important Files Changed
Files Modified
| Filename | Score | Overview |
|---|---|---|
| staging/src/k8s.io/kubectl/pkg/cmd/version/version.go | 4/5 | Added 'short' output format support with validation and output logic |
| staging/src/k8s.io/kubectl/pkg/cmd/version/version_test.go | 5/5 | Added comprehensive test coverage for new short output format functionality |
Confidence score: 4/5
- This PR is safe to merge with low risk of introducing production issues
- Score reflects well-structured implementation following existing patterns with comprehensive testing
- Pay close attention to the validation logic in version.go to ensure it properly handles edge cases
Sequence Diagram
sequenceDiagram
participant User
participant CLI as "kubectl CLI"
participant Options as "Version Options"
participant Factory as "cmdutil.Factory"
participant DiscoveryClient as "Discovery Client"
participant VersionInfo as "Version Info"
participant Output as "Output Stream"
User->>CLI: "kubectl version --output=short"
CLI->>Options: "NewOptions(ioStreams)"
CLI->>Options: "Complete(factory, cmd, args)"
alt ClientOnly is false
Options->>Factory: "ToDiscoveryClient()"
Factory-->>Options: "discoveryClient"
end
CLI->>Options: "Validate()"
Options->>Options: "Check output format (yaml|json|short)"
CLI->>Options: "Run()"
Options->>VersionInfo: "Get client version info"
Options->>VersionInfo: "Get kustomize version"
alt Not client-only and discoveryClient exists
Options->>DiscoveryClient: "Invalidate()"
Options->>DiscoveryClient: "ServerVersion()"
DiscoveryClient-->>Options: "serverVersion"
end
alt Output format is "short"
Options->>Output: "Print client version only"
alt Server version exists
Options->>Output: "Print server version only"
end
else Output format is "" (default)
Options->>Output: "Print Client Version: <version>"
Options->>Output: "Print Kustomize Version: <version>"
alt Server version exists
Options->>Output: "Print Server Version: <version>"
end
else Output format is "yaml" or "json"
Options->>Output: "Marshal and print structured output"
end
alt Server version exists
Options->>Options: "Check version skew warning"
alt Warning exists
Options->>Output: "Print warning to stderr"
end
end
Output-->>User: "Version output"
2 files reviewed, no comments
Summary
kubectl versioncommandChanges
Test plan
TestNewCmdVersionShortOutputto verify short format behavior🤖 Generated with Claude Code