cli-runtime

repository·master·Indexed 43 days ago

https://github.com/kubernetes/cli-runtime

A collection of helper libraries for building Kubernetes CLI tools and plugins that maintain behavioral consistency with kubectl. It provides utilities for creating REST clients for specific GroupVersions, unstructured REST clients, and managing ClientConfigFunc configurations, including stdin unavailability settings.

Tokens
1.1K
Snippets
3
Records
6
Agent score
5%

What's inside cli-runtime

  1. Use cli-runtime to create kubectl commands and plugins

    master
    The cli-runtime library provides a set of helpers designed for developers building Kubernetes CLI tools. Its primary purpose is to allow clients to interact with Kubernetes API infrastructure while maintaining behavior that is compatible with kubectl. The library is a shared dependency used by k8s.io/kubectl to ensure consistent CLI experiences.
  2. Understand compatibility and versioning for cli-runtime

    master
    There are no compatibility guarantees for cli-runtime. Because this library is in direct support of the main Kubernetes project, its branches track Kubernetes development closely. Developers should expect breaking changes as the library evolves alongside Kubernetes. Compatibility guarantees will only be reviewed once the architectural layers are more cleanly separated.
  3. Best practices and restrictions for using cli-runtime

    master

    To maintain the integrity of the library and its relationship with the main Kubernetes repository, follow these restrictions:

    • Do not add API types: This repository is intended for helpers, not for defining API types.
    • Do not add kubectl-specific types: Avoid adding any types that only serve the needs of kubectl specifically.
    • Do not modify pkg files directly: Files under pkg are driven from the source location in the main Kubernetes repository (k8s.io/kubernetes/staging/src/k8s.io/cli-runtime).
    • Do not expect stable APIs: Treat the API as unstable and subject to change without notice.
  4. Create a REST client for a specific GroupVersion

    master

    Use the clientForGroupVersion method on a ClientConfigFunc to generate a RESTClient configured for a specific schema.GroupVersion. This method automatically sets the APIPath to /api if the group is empty, or /apis if a group is specified. You can optionally provide a runtime.NegotiatedSerializer to handle content negotiation.

    Note: This method relies on the underlying ClientConfigFunc to provide a valid *rest.Config.

    // Example usage (conceptual):
    // clientFn is a ClientConfigFunc
    // gv is a schema.GroupVersion
    // serializer is a runtime.NegotiatedSerializer
    
    client, err := clientFn.clientForGroupVersion(gv, serializer)
    if err != nil {
        // handle error
    }
    // Use client to make requests...
  5. Configure stdin unavailability in ClientConfigFunc

    master

    The withStdinUnavailable method returns a new ClientConfigFunc that wraps an existing one. If stdinUnavailable is set to true, it modifies the underlying rest.Config's ExecProvider to mark StdinUnavailable as true and sets a specific StdinUnavailableMessage ("used by stdin resource manifest reader"). This is useful when using an ExecProvider (like for cloud credentials) in contexts where stdin is not available, such as when reading resource manifests from stdin.

    // Example usage:
    // originalFn is a ClientConfigFunc
    
    safeFn := originalFn.withStdinUnavailable(true)
    // safeFn can now be used to generate configs where stdin is restricted.
  6. Create an unstructured REST client for a specific GroupVersion

    master

    Use the unstructuredClientForGroupVersion method on a ClientConfigFunc to generate a RESTClient that uses unstructured data handling. This method configures the client with UnstructuredPlusDefaultContentConfig(), making it suitable for interacting with resources where the specific Go type is not known at compile time. Like the standard client method, it sets the APIPath to /api for core groups or /apis for named groups.

    // Example usage (conceptual):
    // clientFn is a ClientConfigFunc
    // gv is a schema.GroupVersion
    
    client, err := clientFn.unstructuredClientForGroupVersion(gv)
    if err != nil {
        // handle error
    }
    // Use client to make requests with unstructured data...