diff --git a/README.md b/README.md index 86a1feb..1398cdb 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,28 @@ # backstage-plugin -A Backstage Plugin for supporting Runtime Conditions Profiles -Adds a "Runtime Conditions" tab to a Component's page. A `RuntimeConditionsProfile` -describes one codebase, independent of where it's deployed, so a single profile -can exist as a resource in more than one cluster (dev, prod, federal, and so -on). The tab shows one card per cluster where a matching profile is found, -each is a distinct deployment of that codebase. +A Backstage plugin for Runtime Conditions Profiles. -Each card shows the profile's workload, its extensions, and its declared -conditions, alongside which resource fulfilled each condition, if a platform -adapter (see `runtimeconditions/rc-demos/tree/main/cilium-policy/adapter` for -an example) has reported one through the fulfillment API below. Fulfillment -is never inferred by this plugin, only reported by an adapter. +A `RuntimeConditionsProfile` is a portable, machine-readable list of what one workload needs at runtime. This plugin adds a **Runtime Conditions** tab to a Component's page. The tab shows one card per cluster where a matching profile is found, with the profile's workload, extensions, declared conditions, and which resources fulfilled each condition, as reported by platform adapters. Fulfillment is never inferred by this plugin. Only an adapter reports it. -## Install +## The mental model -Add the package to your app's `packages/app`: +| Concept | What it is | Where it lives | +| --- | --- | --- | +| **Profile** (`RuntimeConditionsProfile`) | A description of one codebase, independent of where it runs: its `workload.uri`, the `extensions` it uses, and the `conditions` it declares. Each condition has a `kind`, an `interface.type`, and an optional `optional` flag. | A custom resource (`runtimeconditions.io/v1alpha1`) in a cluster. Conceptually it could live anywhere; only the current fetch is Kubernetes. | +| **Deployment** | One cluster where a matching profile exists. The same profile in dev, prod, and federal means three deployments, shown as three cards on the Component page. | Derived: profile found in cluster X. | +| **Condition** | A capability the workload needs (e.g. `available-stock-capability`) with an interface contract. | `profile.conditions[]`. | +| **Fulfillment** | A report from a platform adapter that a condition is satisfied in an environment, and by which concrete resources. Never inferred by the plugin. Only reported. | Stored by the companion backend plugin via `POST /api/runtime-conditions/fulfillments`. | +| **Extension** | A pluggable capability the workload consumes. Extension URLs of the form `https://runtimeconditions.io/extensions///runtimeconditions.extension.yaml` are linked by the UI to their definition in the `runtimeconditions/extensions` repo. | `profile.extensions[]`. | + +**Correlation rule (important):** a profile belongs to a Component when the profile's `workload.uri` equals the Component's `backstage.io/source-location` annotation, with the `url:` prefix stripped. Matching is by identity (repo URL), not by Kubernetes labels. + +Profile YAML doesn't have to live in Kubernetes at all. It can just as well live in a Git repo or a file server. This plugin only reads it from Kubernetes today because that's the only source wired up so far. The correlation and rendering logic doesn't assume Kubernetes. Only the current fetch does. + +## Quickstart + +Prerequisites: the Kubernetes plugin configured in your Backstage app with at least one cluster registered, and the CRD `runtimeconditionsprofiles.runtimeconditions.io` installed in each cluster you want scanned. Profiles are listed from every configured cluster through the Kubernetes plugin's proxy API. No per-entity Kubernetes annotation or `kubernetes.customResources` config is needed. + +Frontend: ``` yarn --cwd packages/app add @runtimeconditions/plugin-runtime-conditions @@ -38,33 +45,7 @@ import { ``` -Requires the Kubernetes plugin already configured in the app, with at least -one cluster registered, since profiles are currently read as -`RuntimeConditionsProfile` custom resources from every configured cluster. -No per-entity Kubernetes annotation or `kubernetes.customResources` config is -needed: this plugin lists profiles directly through the Kubernetes plugin's -proxy API rather than the entity-scoped fetch. - -## Correlating a Profile to a Component - -A profile describes a codebase, not a deployment, so it's matched by -identity, not by Kubernetes labels. A `RuntimeConditionsProfile` is shown on -a Component's page when its `workload.uri` equals that Component's -`backstage.io/source-location` annotation (with the `url:` prefix stripped). -The same profile can be found in more than one cluster; each is shown as its -own deployment. - -Profile YAML doesn't have to live in Kubernetes at all, it can just as well -live in a Git repo or a file server. This plugin only reads it from -Kubernetes today because that's the only source wired up so far; the -correlation and rendering logic here doesn't assume Kubernetes, only the -current fetch does. - -## Fulfillment API - -`backend/` is `@runtimeconditions/plugin-runtime-conditions-backend`, a -companion backend plugin an adapter reports fulfillment to. Add it to your -app's `packages/backend`: +Backend, only if adapters will report fulfillments: ``` yarn --cwd packages/backend add @runtimeconditions/plugin-runtime-conditions-backend @@ -74,66 +55,30 @@ yarn --cwd packages/backend add @runtimeconditions/plugin-runtime-conditions-bac backend.add(import('@runtimeconditions/plugin-runtime-conditions-backend')); ``` -An adapter registers a fulfillment with: +Open any Component with a `backstage.io/source-location` annotation. A "Runtime Conditions" tab appears. -``` -POST /api/runtime-conditions/fulfillments -{ - "profileName": "request-coordinator", - "condition": "available-stock-capability", - "environment": "dev", - "resources": [ - { - "kind": "CiliumNetworkPolicy", - "provider": "kubernetes", - "reference": "rc-cilium/applications/request-coordinator-egress", - "componentRef": "component:default/inventory-service", - "automation": { "tool": "kratix", "reference": "cnp-promise" } - } - ] -} -``` +## Documentation + +- [Usage: critical-path workflows](docs/usage.md) +- [Architecture and API reference](docs/architecture.md) +- [Fulfillment API for adapters](docs/fulfillment-api.md) +- [Releases](docs/releases.md) + +## Ecosystem + +Part of the Runtime Conditions org, https://github.com/runtimeconditions: + +- [spec](https://github.com/runtimeconditions/spec): the Runtime Conditions specification +- [extensions](https://github.com/runtimeconditions/extensions): first-party extension definitions; profile extension URLs link here +- [rc-demos](https://github.com/runtimeconditions/rc-demos): demos, including the `cilium-policy/adapter` fulfillment adapter +- [rc-cli](https://github.com/runtimeconditions/rc-cli): CLI that generates and validates profiles +- [go-rc-profiler](https://github.com/runtimeconditions/go-rc-profiler), [java-rc-profiler](https://github.com/runtimeconditions/java-rc-profiler), [python-rc-profiler](https://github.com/runtimeconditions/python-rc-profiler): language profilers +- https://runtimeconditions.github.io: public reader guide + +## Contributing + +See [CONTRIBUTING.md](CONTRIBUTING.md). + +## License -- A condition is often fulfilled by more than one resource, e.g. a - `CiliumNetworkPolicy` plus a cert-manager `Certificate` for an API - condition, or a database plus a `CiliumNetworkPolicy` plus a `Secret` for a - datastore condition, so `resources` is always an array, never a single - object. -- Each resource's `reference` is whatever uniquely identifies it in its - environment, e.g. `cluster/namespace/kind/name` for Kubernetes or an ARN - for a cloud resource. -- `resource.provider` and `resource.automation.tool` are free-form strings, - not an enum, since the set of platforms and automation tools isn't fixed. -- `resource.componentRef` and `resource.automation` are optional. When a - resource's `provider` is `"kubernetes"` and it carries a `componentRef`, - the frontend links its reference to that component's Kubernetes tab in the - Kubernetes Backstage plugin. -- `automation` lives on each resource, not on the fulfillment as a whole, - because a single POST doesn't have to come from one adapter's work: a - platform composed of several independent adapters (say, separate Kratix - Promises for network policy, certificates, and secrets) can each POST - their own resources for the same condition and environment, and each - resource keeps its own automation provenance. - -Every POST is purely additive, nothing is ever looked up by key and -overwritten, so multiple adapters can post to the same profile, condition, -and even the same environment concurrently without racing or clobbering -each other; each POST's resources simply show up alongside whatever else -has already been reported for that condition. - -The frontend reads `GET /api/runtime-conditions/fulfillments?profileName=...`. -A single condition can be fulfilled in more than one environment (dev, prod, -federal), each independently, so the conditions table shows how many -distinct environments have reported a fulfillment for that condition, even -if multiple adapters posted to the same one; expanding a row shows a table -of every environment, resource, and automation tool involved. A condition -with no reported fulfillment shows none, it is never inferred. - -## Releases - -Merging a `feat`, `fix`, `perf`, `revert`, or breaking change to `main` -publishes both packages to npm at the same version, tags the merge commit -`vX.Y.Z`, and creates a GitHub release. Other changes, such as docs or CI, do -not release. Versions follow semantic versioning and come from the PR titles -described in `CONTRIBUTING.md`. Publishing uses npm trusted publishing, so no -token is stored; `CONTRIBUTING.md` lists the one-time setup. +[MIT](LICENSE) diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..1e10a1f --- /dev/null +++ b/docs/README.md @@ -0,0 +1,8 @@ +# Documentation + +- [Usage: critical-path workflows](./usage.md): publish a profile, correlate it to a Component, report and read fulfillments +- [Architecture and API reference](./architecture.md): data flow, internals, `POST`/`GET /api/runtime-conditions/fulfillments` +- [Fulfillment API for adapters](./fulfillment-api.md): backend install, POST/GET contract, validation rules, storage caveat +- [Releases](./releases.md): how versions and npm publishing work + +Install and ecosystem links live in the [README](../README.md). diff --git a/docs/architecture.md b/docs/architecture.md new file mode 100644 index 0000000..8d6e5e4 --- /dev/null +++ b/docs/architecture.md @@ -0,0 +1,91 @@ +# Architecture and API reference + +## Data flow + +The adapter only writes. The browser only reads. The store is not a separate actor; it is just the backend plugin's memory. + +``` +Adapter ──POST /fulfillments──▶ backend plugin ──▶ in-memory store (append-only) + ▲ +Browser ──▶ frontend plugin ────┘── GET /fulfillments?profileName=... + │ + └──▶ Kubernetes plugin proxy ──GET /apis/runtimeconditions.io/v1alpha1/runtimeconditionsprofiles + │ (once per configured cluster) + ▼ + profiles matched to the current entity (workload.uri == source-location) + │ + ▼ + one card per cluster; conditions table joined with fulfillments by condition name +``` + +## How the pieces work + +### Profile fetch + +- File: `src/profiles.ts`, function `fetchAllProfiles` +- Calls `kubernetesApi.getClusters()` to list configured clusters. +- Per cluster, calls `kubernetesApi.proxy()` with path `/apis/runtimeconditions.io/v1alpha1/runtimeconditionsprofiles` (the `PROFILES_PATH` constant). +- Non-OK responses are skipped, so clusters without the CRD installed simply contribute nothing. +- Returns one `ProfileDeployment` per profile: `{ cluster, profile }`. + +### Correlation + +- File: `src/profiles.ts`, functions `entitySourceLocationUrl` and `deploymentsForEntity` +- The Component's `backstage.io/source-location` annotation is parsed with `parseLocationRef`, which strips the `url:` prefix. The result is compared to `profile.workload.uri`. +- Entities without the annotation match nothing. The tab is hidden for them via `isRuntimeConditionsAvailable`. +- Matching is by identity (repo URL), not by Kubernetes labels. + +### Fulfillment store + +- File: `backend/src/router.ts` +- A plain in-memory array. `POST /fulfillments` validates and appends (returns `201`). `GET /fulfillments` filters by optional `profileName` and `condition` query params. +- Ephemeral: a backend restart loses all fulfillments. Adapters should treat POST as "report current state" and re-post after a restart. +- File: `backend/src/plugin.ts` registers an auth policy with `allow: 'unauthenticated'` for path `/fulfillments`, so adapters can call without a Backstage session. + +### Rendering + +- File: `src/components/RuntimeConditionsContent.tsx` +- Extension links: URLs matching `https://runtimeconditions.io/extensions///runtimeconditions.extension.yaml` are rewritten to `https://github.com/runtimeconditions/extensions/blob/main/catalog/rc//-.yaml`. Other URLs are shown as-is. +- Kubernetes deep links: fulfillments with `provider: "kubernetes"` and a `componentRef` render the reference as a link to `/catalog////kubernetes`. +- Conditions table columns: Name, Kind, Interface, Optional, "Fulfilled in". The count is the number of distinct environments that reported a fulfillment for the condition; `none` when zero. +- Expanding a condition row shows a table of every fulfillment resource: Environment, Kind, Provider, Reference (linked as above), Automation. + +## API reference + +Base path: `/api/runtime-conditions` (backend plugin id `runtime-conditions`). + +### `POST /fulfillments` + +Registers a fulfillment. Body: + +```ts +interface FulfillmentRecord { + profileName: string; // required + profileNamespace?: string; // optional + condition: string; // required; matches profile.conditions[].name + environment: string; // required, e.g. "dev", "prod", "federal" + resources: FulfillingResource[]; // required, non-empty +} +interface FulfillingResource { + kind: string; // required, e.g. "CiliumNetworkPolicy" + provider: string; // required, free-form, e.g. "kubernetes" + reference: string; // required; unique id in its environment + componentRef?: string; // optional, e.g. "component:default/inventory-service" + automation?: { tool: string; reference?: string }; // optional provenance +} +``` + +Responses: + +- `201` with the stored record +- `400` with an error message if validation fails + +### `GET /fulfillments?profileName=...&condition=...` + +Returns the array of stored records matching the filters (both optional). Always `200`. + +## Frontend exports (`src/index.ts`) + +- `runtimeConditionsPlugin`: the plugin object created with `createPlugin`. Id: `runtime-conditions`. It registers one route, `entityContent`, which is the mount point for the entity tab. +- `EntityRuntimeConditionsContent`: the routable extension rendered inside the tab. Fetches profiles via the Kubernetes API, correlates them to the current entity, and renders one card per matching cluster. +- `isRuntimeConditionsAvailable(entity)`: route guard for `EntityLayout.Route`. Returns true when the entity has a `backstage.io/source-location` annotation. diff --git a/docs/fulfillment-api.md b/docs/fulfillment-api.md new file mode 100644 index 0000000..ee747bb --- /dev/null +++ b/docs/fulfillment-api.md @@ -0,0 +1,64 @@ +# Fulfillment API (for adapters) + +`backend/` ships as `@runtimeconditions/plugin-runtime-conditions-backend`, a companion backend plugin that adapters report fulfillment to. Install it in `packages/backend`: + +``` +yarn --cwd packages/backend add @runtimeconditions/plugin-runtime-conditions-backend +``` + +```ts +backend.add(import('@runtimeconditions/plugin-runtime-conditions-backend')); +``` + +The plugin registers an auth policy allowing **unauthenticated** access to `/fulfillments`, because adapters call it without a Backstage user session. Restrict this at your ingress or API gateway if the endpoint should not be public. + +## Register a fulfillment + +``` +POST /api/runtime-conditions/fulfillments +{ + "profileName": "request-coordinator", + "condition": "available-stock-capability", + "environment": "dev", + "resources": [ + { + "kind": "CiliumNetworkPolicy", + "provider": "kubernetes", + "reference": "rc-cilium/applications/request-coordinator-egress", + "componentRef": "component:default/inventory-service", + "automation": { "tool": "kratix", "reference": "cnp-promise" } + } + ] +} +``` + +Validation rules (the backend returns `400` otherwise): + +- Required fields: `profileName`, `condition`, `environment`, and a non-empty `resources` array. +- Each resource requires `kind`, `provider`, and `reference`. +- `resources` is always an array, never a single object. One condition is often fulfilled by several resources, e.g. a `CiliumNetworkPolicy` plus a cert-manager `Certificate` for an API condition, or a database plus a `CiliumNetworkPolicy` plus a `Secret` for a datastore condition. +- `reference` is whatever uniquely identifies the resource in its environment: `cluster/namespace/kind/name` for Kubernetes, an ARN for a cloud resource. +- `provider` and `automation.tool` are free-form strings, not an enum. The set of platforms and automation tools isn't fixed. +- `componentRef` and `automation` are optional. When a resource's `provider` is `"kubernetes"` and it carries a `componentRef`, the frontend links its reference to that component's Kubernetes tab in the Kubernetes Backstage plugin. +- `automation` lives on each resource, not on the fulfillment as a whole. A platform composed of several independent adapters (separate Kratix Promises for network policy, certificates, and secrets) can each POST their own resources for the same condition and environment. Each resource keeps its own automation provenance. + +POSTs are purely additive. Nothing is ever looked up by key and overwritten. Multiple adapters can post to the same profile, condition, and even the same environment concurrently without racing or clobbering each other. Each POST's resources show up alongside whatever else has been reported for that condition. + +## Read fulfillments + +``` +GET /api/runtime-conditions/fulfillments?profileName=request-coordinator +GET /api/runtime-conditions/fulfillments?profileName=request-coordinator&condition=datastore +``` + +Both query params are optional filters. The frontend uses the `profileName` form. + +The conditions table shows how many distinct environments have reported a fulfillment for a condition, even if multiple adapters posted to the same one. Expanding a row shows every environment, resource, and automation tool involved. A condition with no reported fulfillment shows none. It is never inferred. + +## Example adapter + +See `runtimeconditions/rc-demos`, path `cilium-policy/adapter`, for a working adapter that reports CiliumNetworkPolicy fulfillments. + +## Storage caveat + +The store is in-memory and ephemeral. A backend restart loses all fulfillments. Treat POST as "report current state" and re-post after a restart. diff --git a/docs/releases.md b/docs/releases.md new file mode 100644 index 0000000..135a5d3 --- /dev/null +++ b/docs/releases.md @@ -0,0 +1,5 @@ +# Releases + +Merging a `feat`, `fix`, `perf`, `revert`, or breaking change to `main` publishes both packages to npm at the same version, tags the merge commit `vX.Y.Z`, and creates a GitHub release. Other changes, such as docs or CI, do not release. + +Versions follow semantic versioning and come from the PR titles described in [CONTRIBUTING.md](../CONTRIBUTING.md). Publishing uses npm trusted publishing, so no token is stored. `CONTRIBUTING.md` lists the one-time setup. diff --git a/docs/usage.md b/docs/usage.md new file mode 100644 index 0000000..b06b1ea --- /dev/null +++ b/docs/usage.md @@ -0,0 +1,92 @@ +# Usage: critical-path workflows + +## 1. Publish a profile for your codebase + +Create a `RuntimeConditionsProfile` in each cluster where the codebase is deployed (dev, prod, federal, ...): + +```yaml +apiVersion: runtimeconditions.io/v1alpha1 +kind: RuntimeConditionsProfile +metadata: + name: request-coordinator + namespace: default +spec: + workload: + uri: https://github.com/my-org/request-coordinator # must equal the Component's source-location, minus "url:" + version: v1.2.3 # optional + extensions: + - https://runtimeconditions.io/extensions/rc/stock/runtimeconditions.extension.yaml + conditions: + - name: available-stock-capability + kind: Capability + interface: + type: rest-api + - name: datastore + kind: Datastore + optional: true + interface: + type: postgres +``` + +Apply it per cluster: `kubectl apply -f profile.yaml -n default --context `. + +**Correlation check:** the Component's catalog YAML must carry +`metadata.annotations.backstage.io/source-location: url:https://github.com/my-org/request-coordinator`. +The plugin strips `url:` and compares to `workload.uri` exactly. + +## 2. See the deployment on the Component page + +Open the Component in Backstage, then the **Runtime Conditions** tab. You get one card per cluster where a matching profile exists. Each card shows: + +- the workload URI and cluster, +- the extensions, linked to their definition in the `runtimeconditions/extensions` repo when they follow the standard URL shape, +- a conditions table with columns: Name, Kind, Interface, Optional, "Fulfilled in" (N environments, or `none`). + +Expanding a condition row shows every reported fulfillment: environment, resource kind, provider, reference, and automation tool. The reference links to the referenced component's Kubernetes tab when `provider: kubernetes` and a `componentRef` was posted. + +A condition with no reported fulfillment shows `none`. Fulfillment is never inferred. + +## 3. Report a fulfillment from an adapter + +Adapters (platform-side automation) register what fulfills a condition: + +```bash +curl -X POST $BACKSTAGE_BACKEND/api/runtime-conditions/fulfillments \ + -H 'Content-Type: application/json' \ + -d '{ + "profileName": "request-coordinator", + "condition": "available-stock-capability", + "environment": "dev", + "resources": [ + { + "kind": "CiliumNetworkPolicy", + "provider": "kubernetes", + "reference": "rc-cilium/applications/request-coordinator-egress", + "componentRef": "component:default/inventory-service", + "automation": { "tool": "kratix", "reference": "cnp-promise" } + } + ] + }' +``` + +Rules, enforced by the backend (returns `400` otherwise): + +- `profileName`, `condition`, `environment`, and a non-empty `resources` array are required. Each resource needs `kind`, `provider`, `reference`. +- `resources` is always an array. One condition is typically fulfilled by several resources (e.g. `CiliumNetworkPolicy` plus cert-manager `Certificate`). +- `reference` is whatever uniquely identifies the resource in its environment: `cluster/namespace/kind/name` for Kubernetes, an ARN for cloud, and so on. +- `provider` and `automation.tool` are free-form strings, not enums. +- `componentRef` and `automation` are optional. With `provider: "kubernetes"` and `componentRef: component:default/`, the UI links the reference to that component's Kubernetes tab. +- POSTs are purely additive. Nothing is keyed or overwritten. Multiple adapters can POST the same profile, condition, and environment concurrently. Resources accumulate. + +## 4. Read fulfillments back + +``` +GET /api/runtime-conditions/fulfillments?profileName=request-coordinator +GET /api/runtime-conditions/fulfillments?profileName=request-coordinator&condition=datastore +``` + +Both query params are optional filters. The frontend uses the `profileName` form. + +## 5. Example adapter + +See `runtimeconditions/rc-demos`, path `cilium-policy/adapter`, for a working adapter that reports CiliumNetworkPolicy fulfillments.