Writing a provider
This guide explains how to describe a new resource kind, or a whole new API, with descriptors. The field-by-field reference is in descriptor-reference.md.
1. Study the API
Before writing YAML, answer these questions from the API documentation:
| Question | Where it goes |
|---|---|
| Which call reads one object, and how does it report “not found”? | read steps, absentOn |
| Can that call find the object from spec fields, such as a name, or only from an identifier assigned by the provider? | params, see Identifiers |
| Which call creates the object, and what does it return? | create steps, create.id |
| Which fields can change later, and which call changes each one? | crd.immutable, update steps |
| Which call deletes the object, and is “not found” a success there? | delete steps, accept |
| Which statuses are transient? What rate limit applies? | provider retryable, rateLimit |
2. Write or reuse the Provider
One Provider serves every resource kind of an API. Keep allowedHosts as narrow as the API allows: it stops a crafted spec from sending credentials to another host.
3. Write the Resource
Start from the closest shipped descriptor:
providers/cloudflare/r2-bucket.yamlfor a JSON API with one call per operation;providers/aws/s3-bucket.yamlfor an XML API, a per-region endpoint, or several calls per operation.
Schema
Expose the fields users manage, with the API’s own constraints (pattern, enum, required). The API server then rejects invalid specs before any call. List in crd.immutable every field that the API cannot change after creation.
Read and observe
observe decides what drift means. Return exactly the managed fields, with the same names, shapes and types as forProvider:
observe: >-
{
"jurisdiction": steps.get.body.result.jurisdiction,
"storageClass": steps.get.body.result.storage_class
}
- Leave out a field that the API does not echo reliably. The R2 descriptor never compares
locationHint, which Cloudflare treats as a hint. - Convert representations: S3 returns tags as a list of
Key/Valuepairs, whichtransformMapEntryturns into the map the schema declares. - Supply the API’s implicit default: an S3 bucket that never had versioning returns no status, so
observereportsSuspended. - Use
absentOn: [404]on the step that proves existence, andaccepton steps where an error status means “empty”, like S3’sNoSuchTagSet.
Create
Send only what the API needs to create the object. kbridge reads the object back, then runs the update operation for any field that still differs. Optional fields go in map literals with ?"key": spec.?field.
Update
Write one step per field or group of fields, each guarded by when on changed:
- method: PATCH
path: /accounts/{account}/r2/buckets/{bucket}
when: '"storageClass" in changed'
headers:
cf-r2-storage-class: spec.storageClass
After an update, kbridge reads the object again. If the fields still differ, the resource gets UpdateIneffective. That usually means observe and update disagree on a representation.
Delete
Accept the “not found” status so that deleting an already deleted object succeeds.
Identifiers
kbridge works best when the read can address the object from spec fields, as S3 and R2 buckets do with their name. Then create.id is spec.name, and kbridge can:
- detect that an object already exists before creating it (
AlreadyExists); - recover the identifier when a create is interrupted.
When only the provider assigns the identifier, for example steps.post.body.id, the read path needs {id}. kbridge then cannot look for an existing object. After an uncertain create, it stops with CreatePending rather than risk a duplicate. Many APIs offer a reference field chosen by the client, such as a name or a ref, that can be read back. Prefer it when it exists.
4. Validate
./bin/kbridge validate --descriptors providers --resources cloudflare/r2-bucket
Validation reports every problem as file: field path: message, for example providers/cloudflare/r2-bucket.yaml: spec.operations.update.steps[0].path: unknown placeholder {acount}.
5. Test
- Expression tests:
internal/descriptor/providers_test.goevaluates the shipped expressions against recorded responses. Add the responses of the new API there. - Integration tests:
internal/controller/fakes_test.goholds fake APIs, andcontroller_test.goruns the shipped descriptors against them in envtest. A fake that follows the documented API behaviour catches most descriptor mistakes without a cloud account.
Then regenerate the manifests and run the checks:
make manifests
make check
Pitfalls
Conditional maps need dyn()
CEL type-checks both branches of a conditional. cond ? null : {"a": 1} fails to compile, because null and a map have different types. Wrap the map:
body: >-
spec.region == "us-east-1"
? null
: dyn({"CreateBucketConfiguration": {"LocationConstraint": spec.region}})
Single XML elements are not lists
<TagSet><Tag>…</Tag></TagSet> decodes Tag as a map when there is one tag, and as a list when there are several. Always read repeated elements through toList().
Header names are lowercase
Response headers are exposed with lowercase names: steps.head.headers["x-amz-bucket-region"].
Missing objects that are not 404
Some APIs, such as Proxmox VE, answer 500 for an object that does not exist. absentOn: [500] would take an outage for a deletion and recreate the object. Read the collection instead, make observe return null when the object is not in it, and guard the delete with the same list. See providers/proxmox/storage.yaml.
Lists in an order of their own
kbridge compares lists in order. When the API returns a set in any order, observe the spec items it holds in spec order, then the items the spec lacks. Only a different set then differs:
"content": spec.content.filter(c, c in s.content.split(","))
+ s.content.split(",").filter(c, !(c in spec.content))
Optional spec fields
spec.field fails when the field is absent. Use spec.?field.orValue(default) or has(spec.field) for optional fields.