create_or_patch_json

Function create_or_patch_json 

Source
pub async fn create_or_patch_json<T>(
    client: &Client,
    namespace: &str,
    resource: &T,
    patch_json: &Value,
    field_manager: &str,
) -> Result<()>
where T: Resource<DynamicType = (), Scope = NamespaceResourceScope> + ResourceExt + Clone + Debug + Serialize + for<'de> Deserialize<'de>,
Expand description

Create or update a resource using JSON patch on conflict.

This function attempts to create the resource. If it already exists (AlreadyExists error), it patches the resource using server-side apply with a provided JSON patch object.

This strategy is useful when you need full control over the patch structure, such as when updating complex resources with owner references, labels, and annotations.

§Arguments

  • client - Kubernetes API client
  • namespace - Namespace where the resource should be created/updated
  • resource - The resource to create
  • patch_json - JSON value containing the patch to apply if resource exists
  • field_manager - Field manager name for server-side apply (e.g., “bindy-controller”)

§Returns

Returns Ok(()) if the operation succeeded.

§Errors

Returns an error if:

  • The resource has no name in its metadata
  • Create or patch operations fail (other than AlreadyExists on create)

§Example

let patch = json!({
    "apiVersion": "dns.firestoned.io/v1alpha1",
    "kind": "Bind9Instance",
    "metadata": {
        "name": "my-instance",
        "namespace": namespace,
    },
    "spec": instance.spec,
});

create_or_patch_json(
    client,
    namespace,
    &instance,
    &patch,
    "bindy-controller"
).await.unwrap();