Defining scripts for container starts and stops

Operational Decision Manager (ODM) containers support Kubernetes lifecycle hooks, which allow users to inject scripts that are executed at key points in the lifecycle of a container.

About this task

The lifecycleHooksSecretRef parameter references a secret that contains the scripts. This feature can be useful for custom initialization tasks, such as preloading rulesets into the Decision Server runtime.

Kubernetes lifecycle hooks provide a mechanism that triggers custom behavior during two specific phases of the lifecycle of a container:

  • PostStart Hook: This hook is triggered immediately after a container is created. It allows you to run scripts or commands right after the container starts, before it begins serving requests.
  • PreStop Hook: This hook is triggered before a container is terminated. It is commonly used for cleanup tasks, like closing connections, saving state, or performing any final actions before the container shuts down.

These hooks trigger a script or custom logic to run during startup or before shutdown. For example, you might want to preload configuration data or run initialization tasks at startup, or clean up resources before the container is stopped.

For more information on lifecycle hooks, see Container Lifecycle Hooks External link opens a new window or tab.

Procedure

  1. Create a Kubernetes secret that contains the scripts to be run during the lifecycle of a container.

    You can include one or both of the following scripts:

    • start.sh: The script that runs at container startup (PostStart).
    • stop.sh: The script that runs before container shutdown (PreStop).

    The following example demonstrates how to create a Kubernetes secret that contains both scripts:

    kubectl create secret generic odm-lifecycle-hooks \
     --from-file=start.sh=<PATH_TO_LOCAL_SCRIPT>/start.sh \
     --from-file=stop.sh=<PATH_TO_LOCAL_SCRIPT>/stop.sh
    Example start.sh script

    This script preloads two rulesets into the Decision Server runtime cache:

    #!/bin/bash 
    retry=1
    
    while [ $retry -lt 60 ]; do
        echo "$(date +%T) - checking Decision Service is up and running"
    
        responseCode=$(curl -o /tmp/responseCreateGroup.txt -w "%{response_code}" "https://localhost:9443/odm/DecisionService/" -k)
    
        if [[ "$responseCode" == "200" ]]; then
            echo "$(date +%T) - [$retry/60] [response_code=$responseCode - OK] Decision Service is up and running"
            break
        else
            echo "$(date +%T) - [$retry/60] [response_code=$responseCode - KO] Decision Service is not yet up and running"
            ((retry++))
            sleep 5
        fi
    done
    
    echo "$(date +%T) - rulesets pre-loading started"
    curl -u odmAdmin:odmAdmin https://localhost:9443/odm/DecisionService/rest/v1/mydeployment/Miniloan_ServiceRuleset/WADL -k > /tmp/miniloan.wadl
    curl -u odmAdmin:odmAdmin https://localhost:9443/odm/DecisionService/rest/v1/production_deployment/loan_validation_production/WADL -k > /tmp/loanvalidation.wadl
    echo "$(date +%T) - rulesets pre-loading completed"
  2. Configure the custom resource parameter.

    To use the lifecycle hook scripts, set the lifecycleHooksSecretRef parameter to reference the secret that you created.

    The lifecycleHooksSecretRef parameter can be applied to multiple ODM components. You can configure the lifecycle hook secret for one or more of the following components in your CP4BA custom resource:

    • decisionCenter
    • decisionServerRuntime
    • decisionServerConsole
    • decisionRunner
    For example, if you configure lifecycle hooks for the decisionServerRuntime component, your configuration might look like this:
    odm_configuration:
      decisionServerRuntime:
         lifecycleHooksSecretRef: odm-lifecycle-hooks
  3. To update the existing containers, run the oc apply command on the custom resource .yaml file.
    oc apply -f custom_resources.yaml

Results

This configuration ensures that the specified scripts are executed during the container’s lifecycle for the selected ODM components, and the logs for these lifecycle events are appropriately stored in the specified files:

  • The output of the start.sh script (PostStart hook) is stored in the container /logs/poststart.log file.
  • The output of the stop.sh script (PreStop hook) is stored in the container /logs/prestop.log file.