Development and Operator Utilities
Serialization Helper
When working with custom entities, you may need to serialize or deserialize them to and from JSON, especially when interacting with the Kubernetes API or for testing purposes. KubeOps
provides a helper class, KubernetesJsonSerializer
, to make this process straightforward and consistent with Kubernetes conventions.
Example Usage
Get the Kubernetes specific JsonSerializerOptions
var options = KubernetesJsonSerializer.SerializerOptions;
Serialize an Entity
var entity = new V1DemoEntity { /* ... initialize ... */ };
string json = KubernetesJsonSerializer.Serialize(entity);
Deserialize an Entity
string json = /* JSON string from Kubernetes */;
var entity = KubernetesJsonSerializer.Deserialize<V1DemoEntity>(json);
With Custom JsonSerializerOptions
var options = new JsonSerializerOptions { WriteIndented = true };
string json = KubernetesJsonSerializer.Serialize(entity, options);
API Overview
Serialize(object value, JsonSerializerOptions? options = null)
: Serializes an object to a JSON string.Deserialize<T>(...)
: Deserializes JSON (from string, stream,JsonDocument
,JsonElement
, orJsonNode
) to a strongly-typed object.
This helper ensures your custom entities are always serialized and deserialized in a way that's compatible with Kubernetes expectations.
CRD Installer Utility
The CRD Installer is a powerful utility intended only for development environments. Depending on its settings, it can overwrite or delete existing CRDs, which may lead to data loss or cluster instability. Never use this in production!
When developing operators, you may want to quickly install or update CustomResourceDefinitions (CRDs) in your cluster. The CrdInstaller
service automates this process, making it easier to iterate on CRD changes during development.
How to Add the CRD Installer
To enable the CRD installer, add the following to your operator's Program.cs
:
builder.Services
.AddKubernetesOperator()
#if DEBUG
.AddCrdInstaller(c =>
{
c.OverwriteExisting = true;
c.DeleteOnShutdown = true;
})
#endif
.RegisterComponents();
OverwriteExisting
: Iftrue
, existing CRDs with the same name will be overwritten. This is useful for development but can be destructive if used in production, as it may cause data loss.DeleteOnShutdown
: Iftrue
, all CRDs installed by the operator will be deleted when the operator shuts down. This is extremely destructive and should only be used in disposable development environments.