Skip to content

Conversation

dakshgup
Copy link
Member

@dakshgup dakshgup commented Sep 2, 2025

Summary

  • Adds a new 'short' output format option to the kubectl version command
  • The short format displays only the version numbers without labels
  • This is useful for scripting scenarios where clean version output is needed

Changes

  • Updated version.go to support 'short' as a valid output format
  • Modified validation logic to accept 'short' in addition to 'yaml' and 'json'
  • Added comprehensive test coverage for the new short output format
  • Updated help text to document the new option

Test plan

  • Added unit test TestNewCmdVersionShortOutput to verify short format behavior
  • Test validates that output contains only version numbers without labels
  • Test verifies correct number of output lines for client-only mode
  • Manual testing with actual kubectl binary (requires build environment)

🤖 Generated with Claude Code

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 <[email protected]>
Copy link

@greptile-apps-staging greptile-apps-staging bot left a comment

Choose a reason for hiding this comment

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

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"
Loading

2 files reviewed, no comments

Edit Code Review Bot Settings | Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant