k8s-openapi-ext
Extensions for k8s-openapi crate. Collection of fluent builder traits for Kubernetes objects. See crates.io for more details.
Versioning
This project follows versioning of k8s-openapi.
For example, if you are using k8s-openapi version 0.27, you should use k8s-openapi-ext version 0.27 as well.
k8s-openapi features (K8s version selection)
You need to enable corresponding k8s-openapi features according to the Kubernetes version you are targeting.
For example in your Cargo.toml:
[]
= { = "0.27", = ["latest"] }
= "0.27"
Note: All common Kubernetes API modules (e.g., corev1, appsv1, metav1) are conveniently re-exported by k8s-openapi-ext. The original k8s-openapi crate is also re-exported as openapi when you need direct access to its functionality.
API Overview
This crate provides fluent builder traits for creating and working with Kubernetes resources:
- Builder Traits (
*Ext): Over 30 traits for fluent resource construction (e.g.,PodExt,ServiceExt,DeploymentExt) - Getter Traits (
*GetExt): Convenient accessors for extracting data from resources (e.g.,PodGetExt,ContainerGetExt) ResourceBuilder: Common functionality for metadata, labels, annotations, and recommended Kubernetes labels- Convenient Re-exports: All common Kubernetes API modules are re-exported (e.g.,
corev1,appsv1,metav1) for easier imports - Well-known Labels: Constants for standard Kubernetes labels via the
labelmodule (e.g.,label::APP_NAME,label::APP_VERSION)
The original k8s-openapi crate is re-exported as openapi for direct access when needed.
All builder traits support method chaining and follow consistent naming patterns. Getter traits provide ergonomic access to nested fields that would otherwise require verbose option chaining.
Usage Examples
Basic Pod Creation
use ;
// Create a simple pod
let pod = new
.namespace
.labels
.spec;
Advanced Pod with Method Chaining
use ;
let pod = new
.namespace
.app_name
.app_version
.spec;
Service Creation
use ;
// ClusterIP service
let service = cluster_ip
.namespace
.selector
.app_name;
// LoadBalancer service
let lb_service = load_balancer
.namespace
.labels
.selector;
Working with Existing Resources (GetExt traits)
use ;
Deployment Creation
use ;
let deployment = new
.namespace
.app_name
.replicas
.match_labels
.template;
Using Well-known Labels
use ;
// Using standard Kubernetes labels with constants
let pod = new
.namespace
.label
.label
.label
.label
.spec;
// Or use the convenience methods (which use the same labels internally)
let pod_with_convenience = new
.namespace
.app_name // Uses label::APP_NAME
.app_version // Uses label::APP_VERSION
.app_component // Uses label::APP_COMPONENT
.app_managed_by // Uses label::APP_MANAGED_BY
.spec;
Features
time
The time feature enables additional time conversion methods in the TimeExt trait for working with Kubernetes timestamps using the popular time crate.
When enabled, you get additional methods for converting between metav1::Time and time::UtcDateTime:
[]
= { = "0.27", = ["latest"] }
= { = "0.27", = ["time"] }
= "0.3"
use ;
// Convert from time::UtcDateTime to metav1::Time
let time_utc = now_utc;
let k8s_time = try_from_utc_date_time?;
// Convert from metav1::Time to time::UtcDateTime
let k8s_time = now;
let time_utc = k8s_time.to_utc_date_time;
Without the time feature, you can still work with Kubernetes timestamps using std::time::SystemTime and the jiff crate (which is used internally).