Resource Models
This section outlines the way Kubernetes resource models are implemented in the C# Kubernetes Client library.
Resource Model Representation
The client library utilizes C# classes to represent Kubernetes resources, including Pods, Deployments, Services, etc. These classes are generated from the Kubernetes API specifications, ensuring adherence to the Kubernetes schema.
Model Generation
Resource model classes are generated by the LibKubernetesGenerator
project. This project leverages Swagger specifications of the Kubernetes API to automatically generate C# classes that reflect the structure of Kubernetes resources.
Model Extensions
The generated models are extended with additional attributes and methods for easier usage in the C# client. These extensions include:
KubernetesEntity
attribute: This attribute is applied to each resource model class and specifies the resource’s group, kind, API version, and plural name. This information is used by the client library to interact with the Kubernetes API.JsonSerializable
attribute (for .NET 8 and above): This attribute, along with theSourceGenerationContext
class, facilitates serialization and deserialization of resource models using the System.Text.Json library.Additional properties and methods: The generated classes may include additional properties and methods specific to the resource type, simplifying common interactions with the Kubernetes API.
Custom Resource Support
The C# Kubernetes Client library supports custom resources (CRDs) through the CustomResourceDefinition
model. This model allows developers to define custom resources, their schema, and how they are managed by Kubernetes.
Custom Resource Example:
The examples/customResource
directory demonstrates how to create, get, and list custom resources using the C# client library.
The example showcases the following steps:
CRD Creation: Defining and creating a CRD using
kubectl
.CRD Instance Creation: Creating an instance of the custom resource.
Custom Resource Access: Using the client library to interact with the custom resource, such as getting and listing instances.
Important Notes:
The generated models may include additional properties and methods, depending on the specific resource. For more detailed information, refer to the generated model documentation within the library.
The
LibKubernetesGenerator
project is responsible for generating the resource models. Ensure that the project is included in your development environment to use the generated models effectively.
Top-Level Directory Explanations
examples/ - This directory contains example projects and usage scenarios for the Kubernetes client library.
examples/customResource/ - This subdirectory contains examples of creating custom Kubernetes resources.
examples/customResource/config/ - This subdirectory contains configuration files for custom resources.
src/ - This directory contains the source code for the project.
src/KubernetesClient/ - This subdirectory contains the main Kubernetes client library source code.
src/KubernetesClient/Models/ - This subdirectory contains model classes for various Kubernetes resources and objects.