Kanister Execution Hooks

Kanister Blueprints can be used to execute arbitrary functionality before or after K10 Actions.

To use a Blueprint to define an execution hook, create the Blueprint in the K10 namespace and add a reference to one of the Blueprint's actions in a Policy or Action.

These execution hooks operate on namespaces and can be set independently for snapshot, export, and restore actions. Policies that apply to multiple namespaces will invoke hooks on each namespace. For snapshot and export actions, the namespace will be the source namespace. For restore actions, the namespace will be the target namespace. A hook Blueprint can use this namespace via template parameters like {{ .Namespace.Name }}.

Execution hooks do not require location profiles and hook Blueprint actions cannot use template parameters and helpers such as {{ .Profile.Location.Bucket }} or kando location.

For example, the following Blueprint defines a hook which updates a label on the namespace that was snapshotted.

apiVersion: cr.kanister.io/v1alpha1
kind: Blueprint
metadata:
  name: hook-blueprint
  namespace: kasten-io
actions:
  post-export:
    kind: Namespace
    phases:
    - func: KubeTask
      name: hookPhase
      args:
        podOverride:
          serviceAccountName: "k10-k10"
        image: bitnami/kubectl
        command:
        - /bin/sh
        - -c
        - |
          kubectl patch namespace "{{ .Namespace.Name }}" --type json -p='[{"op": "remove", "path": "/metadata/labels/migrate"}]'

A hook reference may include preHook, onSuccess, or onFailure:

  • A preHook action is executed before the K10 Action (after any K10 setup steps have succeeded).

  • An onSuccess action is executed after the K10 Action has succeeded.

  • An onFailure action is executed when there is a failure in an earlier step and K10 has reached its retry limit.

Once successful, hook actions are not retried. If a preHook or onSuccess action fails, it may be retried by K10. If an onFailure action fails, K10 will not retry. Execution hooks may or may not be invoked when a K10 Action is cancelled asynchronously.

Kanister artifacts returned as outputArtifacts by the hook Blueprint action for preHook are passed as inputArtifacts to any hook Blueprint action for onSuccess or onFailure.

For example, the following hook reference specifies an execution hook for before an ExportAction and the error and non-error cases:

...
hooks:
  preHook:
    blueprint: hook-blueprint
    actionName: pre-export
  onSuccess:
    blueprint: hook-blueprint
    actionName: post-export
  onFailure:
    blueprint: hook-blueprint
    actionName: post-export-failed
...

Look here to see how to embed hook references in API objects.