Skip to main content

Check entitlements in preflights with Replicated template functions

This topic describes how to check custom entitlements before installation or upgrade using preflight checks and Replicated template functions in the License context. The LicenseFieldValue example in this topic applies to applications installed with KOTS, kURL, or Embedded Cluster v2. The v1beta3 example applies to every installation method.

note

The LicenseFieldValue example on this page uses a troubleshoot.sh/v1beta2 spec. A troubleshoot.sh/v1beta3 spec is rendered with Helm, so it can't use repl{{ }} template functions. To check entitlements in a v1beta3 spec, see Check entitlements in a v1beta3 preflight spec.

Overview​

The Replicated installers include default logic to control the user experience based on the customer's built-in license fields. For example, by default, the Replicated installers use the built-in expires_at field to prevent an instance from receiving updates when the customer license expires. You can also add custom logic to your application to control the behavior of your application based on the built-in fields or any of the custom fields that you create.

For more information, see Manage Customer License Fields. For the list of built-in fields in customer licenses, see Built-In License Fields.

Add preflights to check entitlements before installation or upgrade​

To enforce entitlements when your customer installs or updates your application, you can use the Replicated LicenseFieldValue template function in your application to read the value of license fields. The LicenseFieldValue template function accepts the built-in license fields and any custom fields that you configure. For more information, see LicenseFieldValue in License Context.

For example, a license might limit how many nodes are permitted in a customer's cluster. You could define this limit by creating a node_count custom license field:

NameKeyTypeDescription
Node Countnode_countIntegerThe maximum number of nodes permitted

To enforce the node count when a customer installs or updates your application, you can use LicenseFieldValue to create a preflight check that references the custom node_count field:

apiVersion: troubleshoot.sh/v1beta2
kind: Preflight
metadata:
name: example-preflight-checks
spec:
analyzers:
- nodeResources:
checkName: Node Count Check
outcomes:
- fail:
when: 'count() > {{repl LicenseFieldValue "node_count"}}'
message: The cluster has more nodes than the {{repl LicenseFieldValue "node_count"}} you are licensed for.
- pass:
message: The number of nodes matches your license ({{repl LicenseFieldValue "node_count"}})

In the example above, the preflight check uses the nodeResources analyzer and the value of the custom node_count field to determine if the customer has exceeded the maximum number of nodes permitted by their license. If the preflight checks fails, a failure message is displayed to the user and KOTS prevents the installation or upgrade from continuing.

For more information about defining preflight checks, see Define Preflight Checks.

Check entitlements in a v1beta3 preflight spec​

A troubleshoot.sh/v1beta3 preflight spec is a Helm template, so it reads license fields from the Helm values that Replicated injects in the global.replicated.licenseFields field, rather than with LicenseFieldValue. This works for Embedded Cluster, KOTS, and Helm CLI installations. Your chart must include the Replicated SDK. For more information, see Check entitlements in Helm charts before deployment.

The following example is the node count check from the previous section, written as a v1beta3 spec in a Secret in your chart:

apiVersion: v1
kind: Secret
metadata:
labels:
troubleshoot.sh/kind: preflight
name: "{{ .Release.Name }}-preflight-config"
stringData:
preflight.yaml: |
apiVersion: troubleshoot.sh/v1beta3
kind: Preflight
metadata:
name: example-preflight-checks
spec:
analyzers:
- nodeResources:
checkName: Node Count Check
outcomes:
- fail:
when: 'count() > {{ .Values.global.replicated.licenseFields.node_count.value }}'
message: The cluster has more nodes than the {{ .Values.global.replicated.licenseFields.node_count.value }} you are licensed for.
- pass:
message: The number of nodes matches your license ({{ .Values.global.replicated.licenseFields.node_count.value }})