kubernetes_asyncio

repository·main·Indexed 19 days ago

https://github.com/tomplus/kubernetes_asyncio

An asynchronous (AsyncIO) Python client library for the Kubernetes API, designed as a drop-in asynchronous replacement for the official Kubernetes Python client. It provides access to various API groups including CoreV1Api, AppsV1Api, BatchV1Api, and CustomObjectsApi for managing Kubernetes resources such as Pods, Deployments, and CRDs.

Tokens
230.1K
Snippets
296
Records
353
Agent score
63%

What's inside kubernetes_asyncio

  1. Configure authentication and authorization for kubernetes_asyncio

    main

    The kubernetes_asyncio.client requires authentication and authorization parameters configured to match your API server's security policy.

    Configuration can be handled in two ways:

    1. Directly in the Configuration class: Set parameters on an instance of the configuration class.
    2. Using the utils helper: Use provided utility functions to load configuration.

    If no arguments are provided to the configuration loader, it defaults to loading the configuration from the standard default location (e.g., ~/.kube/config).

    Note on Host Configuration: Defining the host is optional; it defaults to http://localhost.

  2. Use V1ConfigMapVolumeSource to adapt a ConfigMap into a volume

    main

    The V1ConfigMapVolumeSource class is used to project the contents of a Kubernetes ConfigMap into a volume as files.

    By default, every key-value pair in the ConfigMap's Data field is projected as a file where the key is the filename and the value is the file content. If you use the items property, you can specify exact mappings of keys to specific file paths; in this mode, only the listed keys are projected.

    Key behaviors:

    • File Permissions: Use default_mode to set permissions (octal 0000-0777 or decimal 0-511). Defaults to 0644.
    • Error Handling: If a requested key is missing from the ConfigMap, the volume setup will error unless the optional flag is set to True.
    • Path Constraints: Paths in the items list must be relative and cannot contain or start with ...
  3. How shard_selector works for sharded lists

    main

    The shard_selector is an alpha field that restricts returned objects using a CEL-based expression. It uses the shardRange() function combined with logical OR (||) to specify hash ranges based on object.metadata.uid or object.metadata.namespace.

    Supported paths:

    • object.metadata.uid
    • object.metadata.namespace

    Example 2-shard split:

    • Shard 0: shardRange(object.metadata.uid, '0x0000000000000000', '0x8000000000000000')
    • Shard 1: shardRange(object.metadata.uid, '0x8000000000000000', '0x10000000000000000')

    Note: This requires the ShardedListAndWatch feature gate to be enabled on the server.

    shardRange(object.metadata.uid, '0x0', '0x8000000000000000') || shardRange(object.metadata.uid, '0x8000000000000000', '0x10000000000000000')
  4. Understand kubernetes_asyncio versioning

    main

    The library follows a specific versioning schema where the first two numbers represent the Kubernetes version, and the third number represents changes in the library not directly connected to Kubernetes.

    Example: Version v18.20.0 corresponds to Kubernetes v1.18.20 plus library-specific changes.

  5. How to use shard_selector for sharded lists and watches

    main

    The shard_selector is an alpha feature (requires the ShardedListAndWatch feature gate) that allows restricting the list of returned objects using a CEL-based expression. It uses the shardRange() function combined with logical OR (||) to specify hash ranges based on object.metadata.uid or object.metadata.namespace.

    Example 2-shard split:

    • Shard 0: shardRange(object.metadata.uid, '0x0000000000000000', '0x8000000000000000')
    • Shard 1: shardRange(object.metadata.uid, '0x8000000000000000', '0x10000000000000000')
  6. Configure authentication and authorization

    main

    The kubernetes_asyncio.client requires authentication and authorization parameters configured in accordance with your API server's security policy.

    Key configuration details:

    • Host: Defining the host is optional and defaults to http://localhost.
    • Parameters: For a full list of supported configuration parameters, refer to the configuration.py file.
    • Auth Methods: You must choose an authentication method that satisfies your specific use case (e.g., token-based, certificate-based, etc.).
  7. Use shard_selector for sharded list and watch

    main

    The shard_selector is an alpha field that allows restricting the list of returned objects using a CEL-based shard selector expression. This is useful for splitting large lists across multiple clients.

    Format: Uses the shardRange() function combined with || (logical OR) to specify hash ranges. Supported Paths: object.metadata.uid, object.metadata.namespace. Requirement: Requires enabling the ShardedListAndWatch feature gate.

    Example 2-shard split:

    • Shard 0: shardRange(object.metadata.uid, '0x0000000000000000', '0x8000000000000000')
    • Shard 1: shardRange(object.metadata.uid, '0x8000000000000000', '0x10000000000000000')
  8. Authenticate using BearerToken API Key

    main

    To use BearerToken authentication, configure the api_key dictionary in the Configuration object. If your API server requires a prefix (such as Bearer), you must also set the corresponding entry in api_key_prefix.

    Note: The kubernetes_asyncio.client must be configured in accordance with your API server's security policy.

    import kubernetes_asyncio.client
    
    configuration = kubernetes_asyncio.client.Configuration(
        host = "http://localhost"
    )
    
    # Configure API key authorization: BearerToken
    configuration.api_key['BearerToken'] = 'YOUR_API_KEY'
    
    # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
    # configuration.api_key_prefix['BearerToken'] = 'Bearer'
    
    with kubernetes_asyncio.client.ApiClient(configuration) as api_client:
        api_instance = kubernetes_asyncio.client.AppsV1Api(api_client)
        # ... call API methods ...