Models
The kubernetes/client/models
package contains the Python classes representing Kubernetes resources. Each class maps to a Kubernetes API resource, such as Pods, Deployments, Services, etc. These classes are designed for easy creation, manipulation, and interaction with the Kubernetes API using the Python client.
Model Structure
Each Kubernetes resource model class follows a consistent structure:
- Attributes: The class has attributes corresponding to the fields of the Kubernetes resource, with data types matching the Kubernetes API schema.
- OpenAPI Types: The
openapi_types
dictionary maps each attribute to its corresponding OpenAPI type. - Attribute Map: The
attribute_map
dictionary maps each attribute to its corresponding key in the Kubernetes API response. - Validation: Some models have validation logic implemented using the
validate_*
methods.
Example Model: V1OwnerReference
The V1OwnerReference
model represents the ownerReferences
field of a Kubernetes object. It is used to track ownership relationships between Kubernetes objects.
class V1OwnerReference(Model):
"""
OwnerReference contains a reference to an object that owns this object. If the owner has been deleted, the reference will be nil.
"""
openapi_types = {
'api_version': str,
'block_owner_deletion': bool,
'controller': bool,
'kind': str,
'name': str,
'uid': str,
}
attribute_map = {
'api_version': 'apiVersion',
'block_owner_deletion': 'blockOwnerDeletion',
'controller': 'controller',
'kind': 'kind',
'name': 'name',
'uid': 'uid',
}
def __init__(self, api_version=None, block_owner_deletion=None, controller=None, kind=None, name=None, uid=None, local_vars_configuration=None): # noqa: E501
"""V1OwnerReference - a model defined in OpenAPI""" # noqa: E501
if local_vars_configuration is None:
local_vars_configuration = Configuration()
super(V1OwnerReference, self).__init__(local_vars_configuration=local_vars_configuration)
self._api_version = None
self._block_owner_deletion = None
self._controller = None
self._kind = None
self._name = None
self._uid = None
self.discriminator = None
if api_version is not None:
self.api_version = api_version
if block_owner_deletion is not None:
self.block_owner_deletion = block_owner_deletion
if controller is not None:
self.controller = controller
if kind is not None:
self.kind = kind
if name is not None:
self.name = name
if uid is not None:
self.uid = uid
@property
def api_version(self):
"""Gets the api_version of this V1OwnerReference. # noqa: E501
API version of the referent. # noqa: E501
:return: The api_version of this V1OwnerReference. # noqa: E501
:rtype: str
"""
return self._api_version
@api_version.setter
def api_version(self, api_version):
"""Sets the api_version of this V1OwnerReference.
API version of the referent. # noqa: E501
:param api_version: The api_version of this V1OwnerReference. # noqa: E501
:type: str
"""
self._api_version = api_version
@property
def block_owner_deletion(self):
"""Gets the block_owner_deletion of this V1OwnerReference. # noqa: E501
If true, this reference points to the managing controller. # noqa: E501
:return: The block_owner_deletion of this V1OwnerReference. # noqa: E501
:rtype: bool
"""
return self._block_owner_deletion
@block_owner_deletion.setter
def block_owner_deletion(self, block_owner_deletion):
"""Sets the block_owner_deletion of this V1OwnerReference.
If true, this reference points to the managing controller. # noqa: E501
:param block_owner_deletion: The block_owner_deletion of this V1OwnerReference. # noqa: E501
:type: bool
"""
self._block_owner_deletion = block_owner_deletion
@property
def controller(self):
"""Gets the controller of this V1OwnerReference. # noqa: E501
If true, this reference points to the managing controller. # noqa: E501
:return: The controller of this V1OwnerReference. # noqa: E501
:rtype: bool
"""
return self._controller
@controller.setter
def controller(self, controller):
"""Sets the controller of this V1OwnerReference.
If true, this reference points to the managing controller. # noqa: E501
:param controller: The controller of this V1OwnerReference. # noqa: E501
:type: bool
"""
self._controller = controller
@property
def kind(self):
"""Gets the kind of this V1OwnerReference. # noqa: E501
Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds # noqa: E501
:return: The kind of this V1OwnerReference. # noqa: E501
:rtype: str
"""
return self._kind
@kind.setter
def kind(self, kind):
"""Sets the kind of this V1OwnerReference.
Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds # noqa: E501
:param kind: The kind of this V1OwnerReference. # noqa: E501
:type: str
"""
self._kind = kind
@property
def name(self):
"""Gets the name of this V1OwnerReference. # noqa: E501
Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names#names # noqa: E501
:return: The name of this V1OwnerReference. # noqa: E501
:rtype: str
"""
return self._name
@name.setter
def name(self, name):
"""Sets the name of this V1OwnerReference.
Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names#names # noqa: E501
:param name: The name of this V1OwnerReference. # noqa: E501
:type: str
"""
self._name = name
@property
def uid(self):
"""Gets the uid of this V1OwnerReference. # noqa: E501
UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names#uids # noqa: E501
:return: The uid of this V1OwnerReference. # noqa: E501
:rtype: str
"""
return self._uid
@uid.setter
def uid(self, uid):
"""Sets the uid of this V1OwnerReference.
UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names#uids # noqa: E501
:param uid: The uid of this V1OwnerReference. # noqa: E501
:type: str
"""
self._uid = uid
def to_dict(self):
"""Returns the dictionary representation of the model.
:return: dict representing the model.
:rtype: dict
"""
_dict = {}
if self.api_version is not None:
_dict['apiVersion'] = self.api_version
if self.block_owner_deletion is not None:
_dict['blockOwnerDeletion'] = self.block_owner_deletion
if self.controller is not None:
_dict['controller'] = self.controller
if self.kind is not None:
_dict['kind'] = self.kind
if self.name is not None:
_dict['name'] = self.name
if self.uid is not None:
_dict['uid'] = self.uid
return _dict
def to_str(self):
"""Returns the string representation of the model"""
return pprint.pformat(self.to_dict())
def __repr__(self):
"""For `print` and `pprint`"""
return self.to_str()
def __eq__(self, other):
"""Returns true if both objects are equal"""
if not isinstance(other, V1OwnerReference):
return False
return self.to_dict() == other.to_dict()
def __ne__(self, other):
"""Returns true if both objects are not equal"""
if not isinstance(other, V1OwnerReference):
return True
return self.to_dict() != other.to_dict()
Model Usage
Here’s a basic example of using the V1OwnerReference
model:
from kubernetes.client import V1OwnerReference
# Create a V1OwnerReference object
owner_reference = V1OwnerReference(
api_version="apps/v1",
kind="Deployment",
name="my-deployment",
uid="1234567890abcdef"
)
# Access model attributes
print(owner_reference.api_version) # Output: apps/v1
print(owner_reference.kind) # Output: Deployment
print(owner_reference.name) # Output: my-deployment
print(owner_reference.uid) # Output: 1234567890abcdef
# Convert to a dictionary
owner_reference_dict = owner_reference.to_dict()
print(owner_reference_dict) # Output: {'apiVersion': 'apps/v1', 'kind': 'Deployment', 'name': 'my-deployment', 'uid': '1234567890abcdef'}
Serialization and Deserialization
The Python models are designed to work seamlessly with the Kubernetes API client. The client uses the __deserialize_model
method in kubernetes/client/api_client.py
to automatically deserialize the API responses into corresponding model objects. Similarly, when making requests to the API, the client serializes model objects into the required JSON format.
def __deserialize_model(self, data, klass):
"""Deserializes list or dict to model.
:param data: dict, list.
:param klass: class literal.
:return: model object.
"""
if not klass.openapi_types and not hasattr(klass,
'get_real_child_model'):
return data
kwargs = {}
if (data is not None and
klass.openapi_types is not None and
isinstance(data, (list, dict))):
for attr, attr_type in six.iteritems(klass.openapi_types):
if klass.attribute_map[attr] in data:
value = data[klass.attribute_map[attr]]
kwargs[attr] = self.__deserialize(value, attr_type)
instance = klass(**kwargs)
if hasattr(instance, 'get_real_child_model'):
klass_name = instance.get_real_child_model(data)
if klass_name:
instance = self.__deserialize(data, klass_name)
return instance
Model Documentation
The documentation for each model is available in the kubernetes/docs
directory. Each model has a separate markdown file, which provides a detailed description of its attributes and usage.
Example:
- V1OwnerReference Documentation: kubernetes/docs/V1OwnerReference.md
Model List
- Core Resources:
- V1PersistentVolumeClaimCondition
- V1PersistentVolumeClaimList
- V1PersistentVolumeClaimSpec
- V1PersistentVolumeClaimStatus
- V1PersistentVolumeClaimTemplate
- V1PersistentVolumeClaimVolumeSource
- V1PersistentVolumeList
- V1PersistentVolumeSpec
- V1PersistentVolumeStatus
- V1PhotonPersistentDiskVolumeSource
- V1Pod
- V1PodAffinity
- V1PodAffinityTerm
- V1PodAntiAffinity
- V1PodCondition
- V1PodDNSConfig
- V1PodDNSConfigOption
- V1PodDisruptionBudget
- V1PodDisruptionBudgetList
- V1PodDisruptionBudgetSpec
- V1PodDisruptionBudgetStatus
- V1PodFailurePolicy
- V1PodFailurePolicyOnExitCodesRequirement
- V1PodFailurePolicyOnPodConditionsPattern
- V1PodFailurePolicyRule
- V1PodIP
- V1PodList
- V1PodOS
- V1PodReadinessGate
- V1PodResourceClaim
- V1PodResourceClaimStatus
- V1PodSchedulingGate
- V1ReplicaSetStatus
- V1ReplicationController
- V1ReplicationControllerCondition
- V1ReplicationControllerList
- V1ReplicationControllerSpec
- V1ReplicationControllerStatus
- V1ResourceAttributes
- V1ResourceClaim
- V1ResourceFieldSelector
- V1ResourceHealth
- V1ResourcePolicyRule
- V1ResourceQuota
- V1ResourceQuotaList
- V1ResourceQuotaSpec
- V1ResourceQuotaStatus
- V1ResourceRequirements
- V1ResourceRule
- V1ResourceStatus
- V1Role
- V1RoleBinding
- V1RoleBindingList
- V1RoleList
- V1RoleRef
- V1RollingUpdateDaemonSet
- V1RollingUpdateDeployment
- V1RollingUpdateStatefulSetStrategy
- V1RuleWithOperations
- V1RuntimeClass
- V1RuntimeClassList
- V1SELinuxOptions
- V1Scale
- V1ScaleIOPersistentVolumeSource
- V1ScaleIOVolumeSource
- V1ScaleSpec
- V1ScaleStatus
- V1Scheduling
- V1CustomResourceDefinitionList
- V1CustomResourceDefinitionNames
- V1CustomResourceDefinitionSpec
- V1CustomResourceDefinitionStatus
- V1CustomResourceDefinitionVersion
- V1CustomResourceSubresourceScale
- V1CustomResourceSubresources
- V1CustomResourceValidation
- V1DaemonEndpoint
- V1DaemonSet
- V1DaemonSetCondition
- V1DaemonSetList
- V1DaemonSetSpec
- V1DaemonSetStatus
- V1DaemonSetUpdateStrategy
- V1DeleteOptions
- V1Deployment
- V1DeploymentCondition
- V1DeploymentList
- V1DeploymentSpec
- V1DeploymentStatus
- V1DeploymentStrategy
- V1DownwardAPIProjection
- V1DownwardAPIVolumeFile
- V1DownwardAPIVolumeSource
- V1EmptyDirVolumeSource
- V1Endpoint
- V1EndpointAddress
- V1EndpointConditions
- V1EndpointHints
- V1EndpointSlice
- V1EndpointSliceList
- V1EndpointSubset
- V1Endpoints
- V1NamespaceStatus
- V1NetworkPolicy
- V1NetworkPolicyEgressRule
- V1NetworkPolicyIngressRule
- V1NetworkPolicyList
- V1NetworkPolicyPeer
- V1NetworkPolicyPort
- V1NetworkPolicySpec
- V1Node
- V1NodeAddress
- V1NodeAffinity
- V1NodeCondition
- V1NodeConfigSource
- V1NodeConfigStatus
- V1NodeDaemonEndpoints
- V1NodeFeatures
- V1NodeList
- V1NodeRuntimeHandler
- V1NodeRuntimeHandlerFeatures
- V1NodeSelector
- V1NodeSelectorRequirement
- V1NodeSelectorTerm
- V1NodeSpec
- V1NodeStatus
- V1NodeSystemInfo
- V1NonResourceAttributes
- V1NonResourcePolicyRule
- V1NonResourceRule
- V1ObjectFieldSelector
- V1ObjectMeta
- V1ObjectReference
- V1
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/watch/ - This directory contains the watch 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.
scripts/ - This directory contains scripts used in the development and build process.