Adding a blueprint target using execution hooks from the Policy API

By default, hooks sets the namespace as the target for the blueprint unless specified otherwise. This article explains how to set resources other than namespaces as the hooks for blueprint subjects.

K10 supports kanister execution hooks through Policy API. Kanister Blueprints can be used to execute arbitrary functionality before or after K10 Actions.

It can be used to run pre/post hook actions for backup as well as restore(incase of import policies).

There are three types of hooks that K10 policy API supports.

  • Before
  • After - On Success
  • After - On Failure

Why is it important to set the blueprint subject?

One of the blueprint examples in K10 documentation has the application consistent backup blueprint for MongoDB. This blueprint uses template parameters like


{{. deployment.Namespace }} and {{ index .Deployment.Pods 0 }}

to render the namespace name and name of the pods created by the deployment respectively.

However, while re-using these blueprints for pre/post hook action in the Policy API, these templates fail to render with the below error.

executing "config" at <.Deployment.Name>: nil pointer evaluating
*param.DeploymentParams.Name'

This is because the deployment cannot be referenced with template parameters when the namespace is the subject of the blueprint.

In this case, it is important to specify the deployment as the blueprint subject.

Setting the blueprint subject in Policy API

The blueprint subject can be manually patched or added to the YAML manifest by adding the  type of the hook, actionName, blueprint and then specify the subject under the hooks field, as shown in the code block below.

hooks:
        preHook:
          actionName: backupPrehook
          blueprint: mongo-hooks
          subject:
            name: mongodb-app-consistent
            namespace: mongodb-app-consistent
          type: deployment

After adding the blueprint subjects, the policy should look like the following code block.

apiVersion: config.kio.kasten.io/v1alpha1
kind: Policy
metadata:
  name: mongodb-app-consistent-backup
  namespace: kasten-io
spec:
  actions:
  - action: backup
    backupParameters:
      hooks:
        preHook:
          actionName: backupPrehook
          blueprint: mongo-hooks
          subject:
            name: mongodb-app-consistent
            namespace: mongodb-app-consistent
            type: deployment
        onFailure:
          actionName: backupPosthook
          blueprint: mongo-hooks
          subject:
            name: mongodb-app-consistent
            namespace: mongodb-app-consistent
            type: deployment
      profile:
        name: test-jai1
        namespace: kasten-io
  frequency: '@hourly'
  retention:
    daily: 7
    hourly: 24
    monthly: 12
    weekly: 4
    yearly: 7
  selector:
    matchExpressions:
    - key: k10.kasten.io/appNamespace
      operator: In
      values:
      - mongodb-app-consistent

 

Note:

The target resource (blueprint subject) can be set in the import+restore policy from the UI itself. However, K10 doesn’t have this option in the backup/export policy form in the UI yet. This is considered will be added to the product in a future release. Until then, Subjects have to be added to the policy manifest manually as explained above.