11 Topics On The Klok Broker

Topics On The Klok Broker

The Klok broker never creates a topic by itself. A topic exists when it is listed in its tenant's Pulsar file in Git and that file has been applied to the broker. A service that publishes or consumes on a topic that is not there, or that it holds no grant for, does not start, and its deploy is blocked before that. This page walks through what that means for each role: what exists without anyone asking, how a developer sees and requests what a project needs, how an owner approves it, and how platform staff keep Git and the broker the same.

Subscriptions are different: a consumer still creates its own subscription. A subscription is the reader's identity, named after its route id, and the consume grant already decides who may read.

Roles At A Glance

Role What they do with topics
Domain Developer Designs and builds the routes of the domain's projects; lists what a project needs with klokTopics; requests purpose topics and access in klok.dev, or proposes the line in a merge request; follows the error message when a check or a start fails.
Tenant Developer The same as a domain developer, for every domain of the tenant.
Domain Admin Approves or rejects requests for topics and grants in the domain's namespace (<tenant>/<domain>); can request like a developer.
Tenant Admin Approves for every namespace of the tenant, and is the only one who approves in common and public; approves another tenant's consume on the tenant's own topics.
Klok Platform Admin Runs Ensure, which keeps the broker equal to Git; runs the Ansible playbooks for what klok.dev does not do (tenants, namespaces, broker clients, revoking a grant); may approve any request.

Who may publish where never changes with the role: a tenant publishes only in its own namespaces, and reading another tenant's topic is consume on that one topic, approved by its owner.

What Exists Without Anyone Asking

Companions come with every topic. Nobody requests, lists or grants them on their own:

Topic Created with it
persistent://acme/finance/InvoiceV1 …/InvoiceV1_DLQ
persistent://acme/finance/InvoiceV1_created …/InvoiceV1_created_DLQ
persistent://acme/finance/ReqRes_OrderQueryV1_OrderV1_Request …_Request_DLQ, …_Response and …_Response_DLQ
a …_DLQ topic nothing

Every topic gets its dead letter topic because any consumer of it may need one, and a request/reply pair is created whole because one half is no use without the other.

klok.dev creates the generic topic of every datamodel, persistent://<tenant>/<namespace>/<Model> with no purpose, and its _DLQ:

  • when New model creates the datamodel in klok.dev;
  • when a build on main reports that one of its routes needs it;
  • when Ensure missing finds a datamodel that was made by hand in GitLab.

The namespace follows the datamodel's class:

Datamodel class Namespace of its generic topic
domain (C), in domain finance <tenant>/finance
common (B) <tenant>/common
public (A) <tenant>/public
system (X) <tenant>/common

The generic topic is created for production. Another environment gets it when that environment's namespace (<tenant>_<env>/<domain>) is already listed in the tenant's Pulsar file.

klok.dev creates the ODL's change event topics for the tenant whose ODL it reaches (klok): KlokOdlObjectEventV1_<Type> of every type the ODL stores, in common and in the domain a write names. See Using Datamodels In The ODL.

Each of these is a merge request against the tenant's Pulsar file that klok.dev merges itself, because it needs no approval, and then applies to the broker.

Everything else is requested: a topic with a purpose (InvoiceV1_created, see Topic Purpose), a request/reply pair, consume on a topic of another domain or tenant, and publish for a new service in its own tenant's namespace.

From Datamodel To Running Service

The whole way, once, in the order it happens. The sections after this one go into each step per role.

1. The datamodel is created. A developer creates it with New model in klok.dev, which adds the generic topic <Model> to the tenant's Pulsar file in a change klok.dev merges at once and applies the file to the broker. A repository made by hand from the datamodel template under <tenant>/datamodel/... (Creating A Datamodel) gets the same the next time Ensure runs for the tenant.

2. The purpose is decided. Whether a flow uses the generic topic or a purpose topic is a design decision of the solution, made by the publisher's side: one flow uses <Model>; when one datamodel carries several unrelated flows, each flow gets <Model>_<purpose> and its consumers read only that (Topic Purpose). It is written in the solution document in <tenant>/integration-solutions, leg by leg, before the routes are built (From Requirement To Design).

3. The routes are built. The developer sets the purpose on the publishing and the consuming route (topicPurpose() in code, or <RouteId>.topic.purpose, .topic.outgoing-purpose and .topic.result-purpose in application.properties), then runs ./gradlew klokTopics to see every topic the service will use, companions and framework topics included.

4. What is missing is requested. When the build on main publishes the project's documentation, klok.dev asks the owners itself for the purpose topics, request/reply pairs and grants the routes need and the grant rules give (step 2 under For Developers). A developer asks for the rest in klok.dev with the request link, or by hand as a merge request to the tenant's Pulsar file. The generic topic from step 1 and every companion are never requested.

5. The owner approves. A Domain Admin for the domain's namespace, a Tenant Admin for any namespace of the tenant. In klok.dev, approving writes the change into Git, merges it and applies it to the broker in one step. A merge request merged in GitLab instead reaches the broker as described below.

6. The service is checked and deployed. klok_topic_check in the pipeline and the framework's check at startup both compare what the service needs with the broker. Anything still missing is reported with the fix (Check Before You Deploy and When A Service Does Not Start below).

When The Change Is Made In Git Instead Of klok.dev

Git is the truth, so everything klok.dev does can also be done in GitLab alone:

  1. Add the topic under topics:, or the grant under topic_grants: or namespace_grants:, in ansible-master/playbooks/group_vars/all/pulsar_tenant_<tenant>.yml in klok/environment/klok-environment-setup, in a merge request. A datamodel is created as its own repository under <tenant>/datamodel/....
  2. The owner of the namespace reviews and merges it. For a reader in another tenant that is the owning tenant, since the grant lives in the owner's file.
  3. The change reaches the broker in one of two ways:
  • Ensure. klok.dev reads the tenant files from main, finds what the broker lacks and applies it: the topic with its companions, or the grant. For a datamodel made in GitLab it first adds the generic topic to Git. A request in klok.dev that the merge fulfils is marked applied. Today Ensure runs when a Platform Admin starts it; it has no schedule yet.
  • Ansible. A Platform Admin runs playbooks/pulsar.yml for the cluster, which applies the same file.
  1. klok.dev shows the state as soon as the merge is on main, because it reads the files from Git and the broker directly. In Run → Broker each topic and grant is in Git and on the broker, in Git only until it is applied, or on the broker only. The last one is drift: a topic or grant someone made outside Git. It is reported and never removed.

For Developers

1. See What Your Project Needs

Run the Gradle plugin's task after you add or change a route, a purpose or an override:

./gradlew klokTopics
./gradlew klokTopics --environment test

It prints a table of every topic the service needs in that environment (prod by default), with publish or consume, the subscription, the route and the reason, and writes the same list to build/klok-topics/topics.json. The list includes the framework's own topics: the dead letter topic of every topic a route consumes, CloudEvents, log events, and the ODL and task topics when the project uses them. A purpose the scan cannot read (computed in code, or a ${...} without a default) is printed as a warning and left out of the list, so a run with warnings is not proof that the list is complete. Fix each warning, or name the topic yourself in the merge request.

klok.dev shows the same answer on the project's page: open the project from Build → Integration projects, and Broker setup on its Overview lists every topic the routes need and where it stands: ready, requested and waiting for its owner, approved and waiting for the broker, rejected with the owner's reason, or not asked for and why, with TOPIC_MISSING, TOPIC_ACCESS_DENIED or NOT_APPLIED beside it. The panel is built from the project's last documentation publish on main. While the service does not run yet, the Overview ends with Before it runs: its first route, its topics and grants, its deploy manifest, and its Vault role and broker client, each read from where it is true. A datamodel's Topics tab (Build → Datamodels, then the model) shows every topic of that datamodel with the routes that publish and consume it.

2. Request What Is Missing

You usually ask for nothing. When your merge reaches main, the build publishes the project's documentation, and klok.dev asks the owners for what the routes need: in the project's own domain and common, a missing topic and the namespace grant, publish and consume; anywhere else, consume on the one topic. The request names klok.dev and the project, the routes that need it are in its note, and the owner decides it like any other. klok.dev does not ask for publish outside your own namespaces, which the grant rules do not give, and never asks again for what an owner rejected; Broker setup says so for each. For those, before your first build, or to ask with a reason of your own:

  1. Open the request link: from the Broker setup panel, from the error message, from the output of klokTopicCheck, or from Request topic or access in the header of Run → Broker (/monitoring/broker/request?topic=…&access=…). The datamodel's Topics tab has Request a purpose topic.
  2. Check the topic and the access: create for a topic that does not exist, consume or publish for a grant. Name the topic itself, never its _DLQ or _Response: a link to a companion asks for the topic it comes with.
  3. For consume or publish, enter the Keycloak client id of your service: klok.broker.auth.client-id, which is the project name by default. Its broker role is service-account-<client id>.
  4. Say why, and Send request. The owner gets an e-mail.
  5. You get an e-mail when the request is applied or rejected. Broker → Requests shows your tenant's requests and their state: requested, approved, rejected or applied.
  6. Run ./gradlew klokTopicCheck again, or restart the service.

What you may ask for:

Request Who may ask
Create a topic a developer or admin of the tenant that will own it, or of one of its domains
Consume a developer or admin of the reading tenant, for one of its own services
Publish a developer or admin of the tenant, for one of its own services, in its own namespaces only

Write narrow, consume wide: klok.dev refuses a request to publish in another tenant's namespace before it reaches anyone. To read another tenant's topic, ask for consume on that one topic.

The same change can be made by hand: add the topic under topics:, or the grant under topic_grants: or namespace_grants:, in the owning tenant's file ansible-master/playbooks/group_vars/all/pulsar_tenant_<tenant>.yml in klok/environment/klok-environment-setup, in a merge request. Every error message prints the exact line to add. The merge is the owner's approval.

3. Check Before You Deploy

./gradlew klokTopicCheck
./gradlew klokTopicCheck --environment test
./gradlew klokTopicCheck --report

The task builds the same list as klokTopics and asks klok.dev whether every topic is in Git, on the broker and granted to your service's broker client. It needs KLOK_INTEGRATION_CONTEXT_TOKEN (and KLOK_DEV_URL for another klok.dev than https://klok.dev). On your own machine it warns and skips without a token; in CI it fails. --report prints the answer and never fails. The answer is also written to build/klok-topics/topic-check.json.

The integration template runs it as the job klok_topic_check, after the build and before the deploy manifest is updated. In a merge request pipeline it may fail without stopping the pipeline (the job turns orange), so you see the problem early. On main it depends on the template variable KLOK_TOPIC_CHECK:

KLOK_TOPIC_CHECK On main
report (the template default today) a failed check turns the job orange and the deploy goes on; the service's own startup check then holds the routes that need the topic (from klok-quarkus-extension 3.14.0) or stops the start (an older extension version)
enforce a failed check stops the pipeline before update_manifest, so the service is never deployed against a topic it cannot use

A group or a project can set KLOK_TOPIC_CHECK=enforce as a CI/CD variable before the template default changes. The job's output and the saved build/klok-topics/topic-check.json are the same in both modes.

Each problem is one numbered block:

Code Means Fix
TOPIC_MISSING the topic is not in the tenant's Pulsar file request it with the link, or add the line in a merge request
TOPIC_ACCESS_DENIED the service's broker client may not publish or consume on it request the access with the link, or add the grant in a merge request
NOT_APPLIED Git has it and the broker does not yet nothing to request; the next Ensure run, which a Platform Admin starts, applies it. Tell the owner or a Platform Admin if it stays

4. When A Topic Is Missing At Start

At every start the framework checks every topic the service uses, before the first route takes a message: the topics the routes publish and consume, their dead letter topics, response topics, CloudEvents, log events, and the ODL and task topics when they are used. From klok-quarkus-extension 3.14.0 (the default, klok.broker.verify-topics.fail=false), a missing or ungranted topic does not stop the service: the routes that need it are held (not started), and everything else starts and runs. The check logs one report at WARNING level, from dev.klok.framework.broker.KlokTopicCheck:

2 broker topics are missing or not granted; the routes that need them are held and the rest of the service starts (klok.broker.verify-topics.fail=false).

[1] publish persistent://acme/sales/OrderV1_DLQ, needed by ACM001E1_OrderOfframp (dead letter queue)
    TOPIC_MISSING: the topic persistent://acme/sales/OrderV1_DLQ does not exist, so service-account-acme-orders cannot publish on it.
    ...
    Request the topic in klok.dev: https://klok.dev/monitoring/broker/request?topic=...&access=create
    It is approved by a TenantAdmin of acme or a DomainAdmin of acme/sales.
    By hand: add `- persistent://acme/sales/OrderV1_DLQ` under `topics:` in ...pulsar_tenant_acme.yml ...

[2] consume persistent://northwind/logistics/DeliveryNoticeV1, needed by ACM003E1_DeliveryNoticeOfframp (consumes, subscription ACM003E1_DeliveryNoticeOfframp)
    TOPIC_ACCESS_DENIED: service-account-acme-orders may not consume on the topic persistent://northwind/logistics/DeliveryNoticeV1.
    ...

Next to the report, the service logs one console line per problem at ERROR, starting with the marker KLOK_TOPIC_PROBLEM, so an OpenSearch monitor on that text alerts while a topic stays missing:

KLOK_TOPIC_PROBLEM TOPIC_MISSING: route ACM001E1_OrderOfframp is not started; publish persistent://acme/sales/OrderV1_DLQ is needed by ACM001E1_OrderOfframp (dead letter queue). TOPIC_MISSING: ... Request the topic in klok.dev: ... By hand: ... Checked again every 60 s.

Read either from the top. Each entry says what the service tried, on which topic, which routes need it, and how to fix it: the request link, who approves, and the line to add by hand. Do what it says; a held route is not restarted, it starts by itself. Every klok.broker.verify-topics.retry-seconds (60 seconds by default) the service checks the held routes' topics again, and once a topic exists and is granted the route starts and the service logs Route <id> started: its broker topics exist and are granted now. /q/health lists the held routes under klok-broker-topics, which stays UP: the pod keeps running and stays in the Kubernetes service while part of it waits. A problem of something that is not a route, such as the log event publisher, cannot be held: the service runs without what that part does until the topic exists.

A service with klok.broker.verify-topics.fail=true keeps the strict start of the versions before 3.14.0: it logs the same report at ERROR and does not start, the pod restarts, and it logs the report again until Git is fixed.

When every topic is in place the service logs Broker topics checked: N uses of M topics, all exist and are granted. A broker that cannot be reached is not reported as missing topics: the start goes on with a WARNING.

At run time the same failures are not retried as if the broker were down. A publish to a missing topic, or without a grant, fails at once with TOPIC_MISSING or TOPIC_ACCESS_DENIED and the same message.

Local development. A plain pulsar standalone creates topics on its own, so a project that runs locally can still miss topics on the platform broker: run klokTopics and klokTopicCheck before the first deploy. The check can be turned off for such a local broker with klok.broker.verify-topics=false in a local profile. It never belongs in a deployed service: the check is what turns a missing topic into a held route and an alert instead of lost or endlessly redelivered messages.

An AI agent working in a project follows the same steps: it runs ./gradlew klokTopics, reads build/klok-topics/topics.json and the warnings, and hands each missing topic or grant to a person as a request link or a merge request to the tenant's Pulsar file. It never asks for automatic topic creation and never creates a topic through the broker's admin API.

Dead Letters Of Another Tenant's Topic

A route that consumes another tenant's topic may not publish in that tenant's namespace, so it cannot write the topic's _DLQ. The startup check skips that _DLQ and the route logs a WARNING that says so. A message whose retries are exhausted is not quarantined: it is handled by <RouteId>.failure.unresolved, which redelivers it by default. Where such dead letters should go is an open question, klok/framework/klok-common#11.

For Tenant And Domain Admins

Your tenant's topics and grants are declared in its Pulsar file in klok/environment/klok-environment-setup. You approve what goes into it.

A request comes in. You get an e-mail, and the request waits in Broker → Requests (/monitoring/broker/requests) under To approve. A TenantAdmin sees the requests for every namespace of the tenant, common and public included; a DomainAdmin sees those for the domain's namespace.

  1. Read the request: what (create a topic, consume, publish), the topic, the service's client id and why.
  2. Approve and apply. klok.dev writes the change into your Pulsar file in a merge request, merges it and applies the file to the broker. The request becomes applied when the broker has it. approved means it still waits: the merge request link says why (GitLab refused the merge, or the broker could not be reached), and the next Ensure run applies it after the merge.
  3. Or Reject… with a reason. The requester gets it by e-mail.
Request Written into your file
Create a topic the topic under topics
Consume <role>: [consume] under the topic in topic_grants
Publish <role>: [publish, consume] under the namespace in namespace_grants

A reader in another tenant is approved in your file, because you own the topic. It keeps the topic's name and is known by its subscription, named after its route id.

The e-mails you get:

  • a new request for your namespaces;
  • after a project's documentation publish finds topics it needs that are missing or not granted, an e-mail to the project's developers and its domain owners. The same problems are not mailed again on the next build.

Without klok.dev, merge the line into the file yourself in GitLab. The next Ensure run applies it, and marks a request that the merge fulfils as applied.

klok.dev's own guide for this role is Topics and access for tenant admins.

For Platform Admins

Ensure (Tenant → All tenants: Ensure missing in a tenant's row menu, or Ensure all tenants in the header) keeps the broker equal to Git:

  • it declares the generic topic of every datamodel of the tenant in Git, those made in klok.dev and those found in GitLab, and for tenant klok the ODL change event topics;
  • it applies the tenant's Pulsar file on main to the broker: topics with their companions, and grants;
  • it reports each item as created, missing, failed or drifted. A topic or grant on the broker that Git does not have is drift. Ensure reports it and never removes it: put it in the file, or revoke it with Ansible.

Git stays the way to do everything by hand. Ansible applies the same files, and it is the only way to revoke a grant or create a namespace:

ansible-playbook playbooks/pulsar.yml --check --diff

The dry run lists the topics it would create, companions included; run it again without --check to apply.

klok.dev's identity. klok.dev applies Git as the Keycloak client klok-dev-provisioning. Its broker role is a Pulsar tenant admin of each tenant it provisions, never a superuser, and its code can only create a topic and grant on a topic or a namespace. It never deletes a topic, revokes a grant or creates a namespace. Reading the broker for the views in Run → Broker is a separate client. Without the provisioning keys klok.dev still writes every change into Git, and Ensure reports what the broker lacks as missing for a person to apply with Ansible. The setup is Broker for platform admins, and the access model is Broker Access And Tenant Isolation.

The broker runs with allowAutoTopicCreation=false, so a topic that no file lists does not exist.

Why Topics Are Not Created Automatically

A topic created by its first producer has no owner and no review. A misspelt purpose, a wrong namespace or a forgotten environment suffix makes a new topic that its reader never sees. Its dead letter topic did not exist either, so a failing message was negatively acknowledged forever instead of reaching _DLQ, and a grant could not be made in advance, because Pulsar grants only on a topic that exists. With every topic in Git, a topic is a decision someone made and reviewed, it survives a rebuild of the broker, and a missing one is an error at the service, with the fix, instead of a message in a topic nobody reads.

The decisions are The broker is provisioned from Git in klok.dev, and Topics are provisioned, never created automatically in Naming conventions, which also holds the messages word for word. The Gradle tasks are Broker topics of a project, and the startup check is Topics and CloudEvents.

Checklist

  • [ ] Is every purpose decided in the solution document, leg by leg?
  • [ ] Does ./gradlew klokTopics list every topic the service uses, without warnings?
  • [ ] Is every purpose topic and request/reply pair requested, and approved by its owner?
  • [ ] Does every cross-domain or cross-tenant read have a consume grant on that one topic?
  • [ ] Does ./gradlew klokTopicCheck pass for the environment you deploy to?
  • [ ] Is klok.broker.verify-topics=false only in a local profile?