# Add custom host preflight checks

This topic describes how to add application-specific host requirements to an Embedded Cluster release and choose when the checks run.

## Define custom checks {#custom-host-preflight-checks}

To add your own host preflight checks, include a HostPreflight custom resource (`kind: HostPreflight`, `apiVersion: troubleshoot.sh/v1beta2`) in the release. Your checks run in addition to the default checks, during installation, during upgrades, and when a node joins the cluster.

Custom host preflight checks behave the same as the default checks. A failure stops the installation and displays a message describing the failure, and the user can bypass it the same way they bypass a default check.

To make a check that users cannot bypass, set `strict: true` on the analyzer, alongside `checkName`. Users cannot continue past a failed strict check from the UI or with `--ignore-host-preflights`, and the installation cannot proceed until the host meets the requirement.

Embedded Cluster honors strict checks in version 3.13.0 and later. In earlier versions, users can bypass a failed strict check.

Use custom host preflight checks to enforce requirements that are more restrictive than the defaults. You can also use them to verify application-specific host requirements, such as a required kernel module or connectivity to an external endpoint.

The following example adds a check for a host with at least six CPU cores:

```yaml
apiVersion: troubleshoot.sh/v1beta2
kind: HostPreflight
metadata:
  name: ec-host-preflights
spec:
  collectors:
    - cpu: {}
  analyzers:
    - cpu:
        checkName: Number of CPU cores
        outcomes:
          - fail:
              when: "count < 6"
              message: This server has less than 6 CPU cores
          - pass:
              message: This server has at least 6 CPU cores
```

Put all of your custom checks in a single HostPreflight resource. If a release contains more than one, Embedded Cluster uses the first and ignores the rest without reporting an error.

If your spec fails to parse, Embedded Cluster does not stop the installation. It runs the default checks, warns that it skipped your checks, and continues.

Embedded Cluster processes your HostPreflight as a template before it runs the checks. If your spec uses Troubleshoot template expressions, such as `{{ .Name }}` in an analyzer message, escape them so that they reach Troubleshoot unchanged. For example, write `{{ .Name }}` as `{{ "{{ .Name }}" }}`.

For more information about writing collectors and analyzers, see [Host Collectors](https://troubleshoot.sh/docs/collect/host-collectors/) and [Host Analyzers](https://troubleshoot.sh/docs/analyze/host-analyzers/) in the Troubleshoot documentation.

### Limitations

Embedded Cluster host preflight checks have the following limitations:

* The default host preflight checks for Embedded Cluster cannot be modified or removed. Custom host preflight checks run in addition to the defaults.
* Host preflight checks run against the host, not the application. To check application requirements in the cluster, see [Add preflight checks](embedded-using#preflights) in _Configure Embedded Cluster_.

## Choose when custom host checks run {#host-preflight-template-values}

The `.IsInstall` and `.IsUpgrade` values require Embedded Cluster 3.22.0 or later.

Embedded Cluster renders Go templates in your HostPreflight resource before running checks. Use the following values to choose which checks run for an operation:

| Value | Description |
| --- | --- |
| `.IsInstall` | `true` when checking the initial installation host, including a host being restored from a backup. `false` for joins and upgrades. |
| `.IsUpgrade` | `true` when checking an upgrade, including validation before converting an Embedded Cluster v2 installation to v3. |
| `.IsJoin` | `true` when checking a joining controller or worker. |
| `.IsStandalone` | `true` when checks run through `install run-preflights` or `node join run-preflights`. `false` during installation, join, or upgrade. |
| `.IsAirgap` | `true` when the operation uses air gap assets. |
| `.DataDir` | The configured Embedded Cluster data directory. |
| `.K0sDataDir` | The Kubernetes data directory within the Embedded Cluster data directory. |

Exactly one of `.IsInstall`, `.IsUpgrade`, and `.IsJoin` is `true`. `.IsStandalone` is independent of these values. For example, `install run-preflights` on an Embedded Cluster v2 host sets both `.IsUpgrade` and `.IsStandalone` to `true`.

To skip a check during upgrades, add `exclude: '{{ .IsUpgrade }}'` to both its collector and its analyzer. To run a check only on the initial installation host, use `exclude: '{{ not .IsInstall }}'` instead. Keep the quotes so the spec is valid YAML before rendering.

For example, this check requires an application port to be available during installation and joins. It skips upgrades, when the application might already use the port:

```yaml
apiVersion: troubleshoot.sh/v1beta2
kind: HostPreflight
metadata:
  name: application-port
spec:
  collectors:
    - tcpPortStatus:
        collectorName: application-port
        port: 8080
        exclude: '{{ .IsUpgrade }}'
  analyzers:
    - tcpPortStatus:
        collectorName: application-port
        checkName: Application port availability
        exclude: '{{ .IsUpgrade }}'
        outcomes:
          - fail:
              when: "address-in-use"
              message: Port 8080 is already in use. Free the port before continuing.
          - pass:
              when: "connected"
              message: Port 8080 is available.
          - fail:
              message: Cannot check port 8080. Review the collector output.
```

Vendor check failures still fail in standalone runs. `.IsStandalone` does not make a check advisory.

:::note
Upgrades include converting an Embedded Cluster v2 installation to v3. Custom host preflight checks that you wrote for a fresh install also run during the upgrade, so scope any install-only checks with `.IsInstall` or `.IsUpgrade` to keep them from blocking an upgrade. For how host preflight checks carry over from v2, see [Upgrade from Embedded Cluster v2 to v3](embedded-v3-migrate).
:::

## Prepare custom host checks from a kURL-based release {#kurl-release-host-preflights}

Replacing `{{kurl .IsUpgrade}}` requires Embedded Cluster 3.22.0 or later. See [Choose when custom host checks run](#host-preflight-template-values).

Embedded Cluster v3 does not read custom checks from `spec.kurl.hostPreflights` in a kURL Installer. Move those checks into your release's [HostPreflight resource](#custom-host-preflight-checks), saved as a separate YAML file at the root of the release. Embedded Cluster does not read HostPreflight specs inside a Helm chart, ConfigMap, Secret, or subdirectory.

Review the checks before moving them:

| kURL behavior | Change for Embedded Cluster v3 |
| --- | --- |
| Custom CPU, memory, disk, or other application host requirements | Copy the collectors and analyzers into the release's HostPreflight resource. Review paths and thresholds for the target environment. |
| `exclude: '{{kurl .IsUpgrade}}'` | Replace it with `exclude: '{{ .IsUpgrade }}'` on both the collector and analyzer. This skips upgrades and still checks joining nodes. |
| Checks for ports used by kURL or its add-ons | Remove checks that do not apply to Embedded Cluster. Keep checks for ports your application requires. |
| `excludeBuiltinHostPreflights` | No equivalent. See [Limitations](#limitations). |

For application preflight checks, Embedded Cluster v3 requires `troubleshoot.sh/v1beta3` application Preflight resources and uses Helm template syntax instead of Replicated template syntax. See [Application preflight specs must use v1beta3](embedded-v3-migrate#preflight-specs-must-use-v1beta3) for the supported locations, rendering changes, connectivity checks, and air gap image requirements.