brokkr-agent::k8s::objects
Rust
Kubernetes Objects Module
Handles the creation and manipulation of Kubernetes objects from YAML definitions.
Constants
pub static STACK_LABEL: &str = "k8s.brokkr.io/stack";pub static CHECKSUM_ANNOTATION: &str = "k8s.brokkr.io/deployment-checksum";pub static LAST_CONFIG_ANNOTATION: &str = "k8s.brokkr.io/last-config-applied";pub static DEPLOYMENT_OBJECT_ID_LABEL: &str = "brokkr.io/deployment-object-id";Object Creation
pub fn create_k8s_objects( deployment_object: DeploymentObject,) -> Result<Vec<DynamicObject>, Box<dyn std::error::Error>>Creates Kubernetes objects from YAML content with proper annotations and labels.
Features
- Multi-document YAML support
- Automatic resource ordering
- Metadata injection
- Object validation
Functions
Section titled “Functions”fn create_k8s_objects
Section titled “fn create_k8s_objects”pub
fn create_k8s_objects( deployment_object: DeploymentObject, agent_id: Uuid,) -> Result<Vec<DynamicObject>, Box<dyn std::error::Error>>Creates Kubernetes objects from a brokkr deployment object’s YAML content.
Parameters:
| Name | Type | Description |
|---|---|---|
deployment_object |
DeploymentObject |
The deployment object containing YAML content |
Returns:
Result<Vec<DynamicObject>, Box<dyn std::error::Error>>- List of created K8s objects or error
Source
pub fn create_k8s_objects( deployment_object: DeploymentObject, agent_id: Uuid,) -> Result<Vec<DynamicObject>, Box<dyn std::error::Error>> { let mut k8s_objects = Vec::new();
let yaml_docs = utils::multidoc_deserialize(&deployment_object.yaml_content)?;
for yaml_doc in yaml_docs { // Skip null documents if yaml_doc.is_null() { continue; }
let mut obj: DynamicObject = serde_yaml::from_value(yaml_doc)?; // Merge Brokkr's tracking keys into the manifest's own annotations // rather than replacing the map — user-declared annotations must // survive apply (BROKKR-T-0191). let mut annotations = obj.metadata.annotations.take().unwrap_or_default(); annotations.insert( STACK_LABEL.to_string(), deployment_object.stack_id.to_string(), ); annotations.insert( CHECKSUM_ANNOTATION.to_string(), deployment_object.yaml_checksum.to_string(), ); annotations.insert( DEPLOYMENT_OBJECT_ID_LABEL.to_string(), deployment_object.id.to_string(), ); // Store the bounded bundle checksum, not a Debug dump of the whole // object: `format!("{:?}", obj)` is unbounded and a large manifest can // push total annotations past the 256 KiB limit and fail apply. The // value is only ever checked for presence, never parsed. annotations.insert( LAST_CONFIG_ANNOTATION.to_string(), deployment_object.yaml_checksum.clone(), );
annotations.insert( BROKKR_AGENT_OWNER_ANNOTATION.to_string(), agent_id.to_string(), );
obj.metadata.annotations = Some(annotations);
let kind = obj .types .as_ref() .map(|t| t.kind.clone()) .unwrap_or_default();
// Move namespace and CRDs to the front of objects list for apply if kind == "Namespace" || kind == "CustomResourceDefinition" { k8s_objects.insert(0, obj); } else { k8s_objects.push(obj); } }
Ok(k8s_objects)}fn verify_object_ownership
Section titled “fn verify_object_ownership”pub
fn verify_object_ownership( object: &DynamicObject, agent_id: &Uuid,) -> boolSource
pub fn verify_object_ownership(object: &DynamicObject, agent_id: &Uuid) -> bool { object .metadata .annotations .as_ref() .and_then(|annotations| annotations.get(BROKKR_AGENT_OWNER_ANNOTATION)) .map(|value| value == &agent_id.to_string()) .unwrap_or(false)}