# How installers choose and install components

> The rules an installer follows to choose components from the catalog API, order them and check them once installed, with cases to test an installer against.

An installer reads the [API](https://oss.zeabur.com/docs/catalog/reference/releases#the-api) of a catalog release and decides which components to install. This page gives the rules. Two installers that follow them choose the same components for the same configuration. Each rule has a case in [`conformance/select/`](https://oss.zeabur.com/docs/catalog/reference/installers#test-cases) to test against.

## What an installer starts from

- The API of one catalog release: `catalog.json` and each component's file.
- The profile, `single-node` or `multi-node`.
- The distribution, `k3s`, `rke2` or `kubesolo`.
- The platform of the cluster's nodes, such as `linux/amd64`.
- The `enable` and `disable` lists that the person writes. Each item is a component id, such as `victoria-logs`, or a capability, such as `logs`.

## What the installer works out

The API holds facts. An installer works out two lists for each component from them, and the rules below use both.

### Tested on

`testedOn` is where a component passed its test, as distributions, each with the platforms it passed on. For each distribution in the `distributions` of `catalog.json`, take the component's measurements with that `distribution` and its pinned `distributionVersion`. A platform is in `testedOn` when the newest passing run on it has the component's `sourceDigest` and is no older than the newest failed run on it. A pass on the same day as a failure wins. A distribution without such a platform isn't in `testedOn`, so `testedOn` can be empty.

### Install order

`installOrder` is what installing the component installs on a profile: the components it needs, then the component. Go through its `requires` in order. A `{component: <id>}` entry names that component. A `{capability: <capability>}` entry names the first component in id order whose `provides.capability` matches and is `recommended` on the profile, or else the first that is `optional`. Put the order of each named component first, without repeats, then the component. A profile where the component is `unsupported` has no `installOrder`.

## Names

A name that is a component id names that component. Any other name is a capability, from `provides.capability`, and names one of the components that offer it and aren't `unsupported` on the profile. The installer sorts those components and takes the first:

1. Components whose `testedOn` lists the distribution with the platform come first.
2. Then components `recommended` on the profile.
3. Then id order.

A name that matches no capability or component id is an error, in `enable` and in `disable`.

## Choose the components

The installer takes these steps in order.

1. **Defaults.** Start from every component `recommended` on the profile. An installer may add components it needs on top of the defaults, by id or capability. They count as defaults.
2. **Enable.** Add each component that `enable` names. It counts as enabled.
3. **Disable.** Take out each chosen component that a `disable` item names. An id takes out that component, and a capability takes out every chosen component of that capability. A known name that matches nothing chosen does nothing.
4. **Leave out what can't be installed here.** Take out the components below and list each with its reason. The test cases name a reason by its code, and the installer words it for the person.

   | When | Code | Left out |
   | - | - | - |
   | `distributions` doesn't list the distribution | `distributions` | Always. An enabled component also gets a `left-out` warning |
   | `defaults` is `unsupported` on the profile | `unsupported` | Always. An enabled component also gets a `left-out` warning |

   A chosen component that `testedOn` doesn't list with the distribution and the platform stays chosen, with an `untested` warning. This covers a component never tested there, one whose latest run there failed and one changed since its last passing run.
5. **Order.** Go through the chosen components in id order. For each one, take its `installOrder` for the profile, which lists its dependencies and then the component. Add each id that isn't in the result yet. A component in the result that wasn't chosen is there as a dependency.

   A component in `installOrder` may be disabled, left out or untested:

   | The needed component | Result |
   | - | - |
   | Is disabled, or left out for `distributions` or `unsupported` | Error `needs-disabled` |
   | Has a `testedOn` without the distribution and the platform | The needed component is installed, with an `untested` warning |

   For example, say VictoriaLogs needs the VictoriaMetrics operator, which is tested only on k3s. On rke2, the installer installs the operator with VictoriaLogs and warns that the operator is untested there, whether VictoriaLogs is a default or enabled.

The result has the components in install order, each with why it's installed (`default`, `enabled` or `dependency`), the components left out with their reasons and the warnings. Because each `installOrder` already places a component after its dependencies, the result does too. It doesn't depend on the order of `enable` or `disable`.

## Errors

The installer stops and installs nothing on these errors. The `kind` names the error in the test cases.

| `kind` | When | Tell the person |
| - | - | - |
| `unknown` | A name matches no capability or component id | The name |
| `needs-disabled` | A chosen component needs one that is disabled, or left out for `distributions` or `unsupported` | Both components |

## Install and check

Install the components in the order of the result, and remove the installed components that the result doesn't have. [Component file format](https://oss.zeabur.com/docs/catalog/reference/component-format) describes the releases of each one, the digests that pin them and the other fields an installer reads.

Once a component is installed, run each of its `health` checks. A check with a `condition` is one `kubectl wait`: `--for=condition=<condition>`, then the objects of `resource`, then `-n <namespace>` when it has one, and `--timeout=<timeout>`. Give the objects as `<resource>/<name>`, as `<resource> -l <key>=<value>,…` for a `selector`, or as `<resource> --all` for `all: true`. Wait until the objects exist before you wait on the condition, because `kubectl wait` fails at once when there is none. A check of a `Deployment`, `DaemonSet` or `StatefulSet` has no `condition`. It is ready when the rollout is complete, so run `kubectl rollout status <resource>/<name>`, or `kubectl rollout status <resource> -l <key>=<value>,…` for a `selector`, with `-n <namespace>` and `--timeout=<timeout>`. kubectl compares `observedGeneration` with `generation`, so the pods of an earlier rollout never make a new one ready. `<resource>` comes from the kind:

| `kind` | `<resource>` |
| - | - |
| `APIService` | `apiservices.apiregistration.k8s.io` |
| `DaemonSet` | `daemonsets.apps` |
| `Deployment` | `deployments.apps` |
| `GatewayClass` | `gatewayclasses.gateway.networking.k8s.io` |
| `Node.longhorn.io` | `nodes.longhorn.io` |
| `Pod` | `pods` |
| `StatefulSet` | `statefulsets.apps` |

For example, Longhorn's `nodes-ready` check runs:

```sh
kubectl wait --for=condition=Ready nodes.longhorn.io --all -n longhorn-system --timeout=60s
```

An installer may use its own time limit in place of `timeout`. When a check fails, show the reason and message of the object, as `kubectl describe` does, and then its `advice`. The `advice` says what to do, not what went wrong.

## Test cases

`conformance/select/` has one case for each rule above. The directory is in the repository and in `bundle.tar.gz`, so an installer that pins a catalog commit or release reads the cases of that version. [`schema/v1/select-case.schema.json`](https://github.com/zeabur-oss/catalog/blob/v0.6.3/schema/v1/select-case.schema.json) defines the format.

```yaml
description: A default not tested on the distribution is installed with a warning.
components:            # each component with only the fields selection reads, and the testedOn
                       # and installOrder that the installer works out
  - id: traefik
    provides: {capability: ingress}
    defaults: {single-node: recommended, multi-node: recommended}
    distributions: [k3s, rke2, kubesolo]
    testedOn:
      - {distribution: k3s, platforms: [linux/amd64]}
      - {distribution: rke2, platforms: [linux/amd64]}
      - {distribution: kubesolo, platforms: [linux/amd64]}
  - id: victoria-logs
    provides: {capability: logs}
    defaults: {single-node: recommended, multi-node: recommended}
    distributions: [k3s, rke2, kubesolo]
    testedOn:
      - {distribution: k3s, platforms: [linux/amd64]}
      - {distribution: kubesolo, platforms: [linux/amd64]}
config:
  profile: multi-node
  distribution: rke2
  platform: linux/amd64
  # add: what the installer adds to the defaults
  # enable, disable: the person's lists
want:
  components:          # in install order
    - {id: traefik, why: default}
    - {id: victoria-logs, why: default}
  warnings:            # in any order
    - {kind: untested, component: victoria-logs, distribution: rke2}
  # leftOut: {<id>: {reason: …, …}}
  # error: {kind: unknown, name: …}, in place of the rest
```

To run a case:

1. Take `components` as the catalog, with `testedOn` and `installOrder` as given. The capability of each component is its `provides.capability`. A profile without `installOrder` means the component alone.
2. Choose with `config`.
3. Compare the result with `want`. A list or map that `want` leaves out is empty. Compare each reason, warning and error by its code and fields, not by the words an installer shows.
