Resource Management

The Kubernetes client library provides functionality for managing Kubernetes resources such as Pods, Deployments, Services, and more. These resources are managed through the use of ResourceQuotas, which define limits on resource usage within a given namespace.

ResourceQuota Objects

Resource quotas are Kubernetes objects that enforce limits on the use of resources within a namespace. They help to prevent resource exhaustion and ensure that resources are used efficiently.

The V1ResourceQuota object defines a set of hard limits for each named resource. It also tracks the current usage of each resource in the namespace.

Example V1ResourceQuota Object:

from kubernetes.client import V1ResourceQuota
          from kubernetes.client.api import core_v1_api
          
          api = core_v1_api.CoreV1Api()
          
          resource_quota = V1ResourceQuota(
            api_version="v1",
            kind="ResourceQuota",
            metadata={
              "name": "my-resource-quota"
            },
            spec={
              "hard": {
                "limits.cpu": "10",
                "limits.memory": "2Gi",
                "requests.cpu": "1",
                "requests.memory": "512Mi"
              }
            }
          )
          
          api.create_namespaced_resource_quota(namespace="default", body=resource_quota)
          

Resource Quota API

The Kubernetes client library provides a CoreV1Api object that allows you to interact with the ResourceQuota API. The API provides the following methods:

  • list_namespaced_resource_quota(namespace, **kwargs): Lists all ResourceQuotas in a namespace.
  • list_resource_quota_for_all_namespaces(**kwargs): Lists all ResourceQuotas across all namespaces.

Example Usage:

from kubernetes.client.api import core_v1_api
          
          api = core_v1_api.CoreV1Api()
          
          # List all ResourceQuotas in the 'default' namespace
          resource_quotas = api.list_namespaced_resource_quota(namespace='default')
          
          # List all ResourceQuotas across all namespaces
          all_resource_quotas = api.list_resource_quota_for_all_namespaces()
          

Resource Requirements

The V1ResourceRequirements object defines the compute resource requirements for a container. It includes both requests (minimum resources required) and limits (maximum resources allowed).

Example V1ResourceRequirements Object:

from kubernetes.client import V1ResourceRequirements
          
          resource_requirements = V1ResourceRequirements(
            limits={
              "cpu": "2",
              "memory": "1Gi"
            },
            requests={
              "cpu": "1",
              "memory": "512Mi"
            }
          )
          

Resource Slice

Resource slices are used to represent a group of similar resources managed by a common driver. Each device within a pool must have a unique name. The specific slice in which a device is published may change over time. The unique identifier for a device is its tuple of , , .

Whenever a driver needs to update a pool, it increments the pool.Spec.Pool.Generation number and updates all ResourceSlices with that new number. Consumers of ResourceSlices should only use those with the highest generation number.

ResourceClaim

Resource claims are used to request a specific resource from a pool. They track whether the resource has been allocated and the result of the allocation.

Example V1alpha3ResourceClaimStatus Object:

from kubernetes.client import V1alpha3ResourceClaimStatus
          
          resource_claim_status = V1alpha3ResourceClaimStatus(
            reserved_for=[
              {
                "kind": "Pod",
                "name": "my-pod"
              }
            ]
          )
          

The reserved_for field indicates which entities are currently allowed to use the claim. A Pod that references a ResourceClaim that is not reserved for it will not be started.

Top-Level Directory Explanations

doc/ - This directory contains documentation files for the project.

doc/source/ - This directory contains the source files for the documentation.

examples/ - This directory contains example usage of the Kubernetes client library.

examples/dynamic-client/ - This directory contains examples of using the dynamic client to interact with Kubernetes.

kubernetes/ - This directory contains the main Kubernetes client library.

kubernetes/base/ - This directory contains the base Kubernetes client library.

kubernetes/base/config/ - This directory contains configuration files for the base library.

kubernetes/base/dynamic/ - This directory contains the dynamic client implementation for the base library.

kubernetes/base/leaderelection/ - This directory contains the leader election implementation for the base library.

kubernetes/client/ - This directory contains the top-level client for the Kubernetes client library.

kubernetes/client/api/ - This directory contains the API definitions for the client library.

kubernetes/client/models/ - This directory contains the data models used by the client library.

kubernetes/e2e_test/ - This directory contains end-to-end tests for the Kubernetes client library.

kubernetes/e2e_test/test_yaml/ - This directory contains YAML files used in the end-to-end tests.

kubernetes/utils/ - This directory contains utility functions for the Kubernetes client library.

scripts/ - This directory contains scripts used in the development and build process.