Hook Execution using Weight


Hook Execution using Weight

In this tutorial, we are going to discuss about hook execution using weight when I’m having multiple hooks for the same stage. I’ll be creating multiple hooks with preinstalled stage where it will be having different weights.

Based on the weights It’s going to get triggered. So that it will not have any confusion on which hook should get triggered first and which one to get triggered in the last.

Example

So let me go ahead and create a couple of yaml files. Let me get into the folder templates and remove the earlier yaml files.

ashok@waytoeasylearn:~/hooktest/templates$ rm preinstalljob-hook-job.yaml postinstalljob-hook-job.yaml 

Now I’m going to create three yaml files all of type preinstalled hook. Let me add the content.

ashok@waytoeasylearn:~/hooktest/templates$ cat preinstalljob-hook-1.yaml 
apiVersion: batch/v1
kind: Job
metadata:
  name: preinstalljob-hook-1
  annotations:
    "helm.sh/hook": "pre-install"
    "helm.sh/hook-weight": "-2"
spec:
  template:
    spec:      
      containers:
      - name: pre-install
        image: busybox
        imagePullPolicy: IfNotPresent
        command: ['sh', '-c', 'echo pre-install Job Pod is Running Weight -2 and sleep 2 ; sleep 2']   
      restartPolicy: OnFailure  
      terminationGracePeriodSeconds: 0
   backoffLimit: 3
   completions: 1
   parallelism: 1

So it’s of kind job where it is going to have annotation it is a hook. It will get triggered in preinstalled state and hook weight is -2. Now I’m going to add another yaml file. Let me add content to the same.

ashok@waytoeasylearn:~/hooktest/templates$ cat preinstalljob-hook-2.yaml 
apiVersion: batch/v1
kind: Job
metadata:
  name: preinstalljob-hook-2
  annotations:
    "helm.sh/hook": "pre-install"
    "helm.sh/hook-weight": "3"
spec:
  template:
    spec:      
      containers:
      - name: pre-install
        image: busybox
        imagePullPolicy: IfNotPresent
        command: ['sh', '-c', 'echo pre-install Job Pod is Running Weight 3 and sleep 3 ; sleep 3']   
      restartPolicy: OnFailure  
      terminationGracePeriodSeconds: 0
   backoffLimit: 3
   completions: 1
   parallelism: 1

It’s the same content. Only the echo statement, weight are different and the sleep seconds is different. Now, let me add another yaml file. I’m adding the content to it.

ashok@waytoeasylearn:~/hooktest/templates$ cat preinstalljob-hook-3.yaml 
apiVersion: batch/v1
kind: Job
metadata:
  name: preinstalljob-hook-3
  annotations:
    "helm.sh/hook": "pre-install"
    "helm.sh/hook-weight": "5"
spec:
  template:
    spec:      
      containers:
      - name: pre-install
        image: busybox
        imagePullPolicy: IfNotPresent
        command: ['sh', '-c', 'echo pre-install Job Pod is Running Weight 5 and sleep 5 ; sleep 5']   
      restartPolicy: OnFailure  
      terminationGracePeriodSeconds: 0
   backoffLimit: 3
   completions: 1
   parallelism: 1

So here the weight is 5, and the stage when hook will get triggered is preinstalled. So I do have 3 hooks, all of type preinstalled having different weights.

Helm install

Now, let me go ahead and install this particular chart. Let me check is there any deployment.

ashok@waytoeasylearn:~$ helm list
NAME    NAMESPACE       REVISION        UPDATED STATUS  CHART   APP VERSION

I do not have any deployment. Now, let me go ahead and do the installation of this specific chart.

ashok@waytoeasylearn:~$ helm install hookweightdemo ./hooktest
NAME: hookweightdemo
LAST DEPLOYED: Mon Jun  7 12:28:18 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=hooktest,app.kubernetes.io/instance=hookweightdemo" -o jsonpath="{.items[0].metadata.name}")
  export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT 

Now let me go ahead and get the status about the pods

ashok@waytoeasylearn:~$ kubectl get pods
NAME                                       READY   STATUS      RESTARTS   AGE
preinstalljob-hook-1-q7cwp                 0/1     Completed   0          2m59s
preinstalljob-hook-2-hp766                 0/1     Completed   0          2m55s
preinstalljob-hook-3-g75v2                 0/1     Completed   0          2m50s
hookweightdemo-hooktest-749686bbfd-zwflx   1/1     Running     0          2m43s

So I do have three pods in the completed state. That is hook 1, 2 and 3. And let me go ahead and check the execution time of these pods, rather than seeing the pods individually I can go ahead and get the description about all the pods and grep only the name, annotation, started and finished time.

ashok@waytoeasylearn:~$ kubectl describe pod | grep -E 'Name:|Anno|Started:|Finished:'
Name:         preinstalljob-hook-1-q7cwp
Annotations:  cni.projectcalico.org/podIP: 10.1.224.62/32
      Started:      Mon, 07 Jun 2021 12:28:19 +0530
      Finished:     Mon, 07 Jun 2021 12:28:21 +0530
    SecretName:  default-token-bxrvs
Name:         preinstalljob-hook-2-hp766
Annotations:  cni.projectcalico.org/podIP: 10.1.224.20/32
      Started:      Mon, 07 Jun 2021 12:28:23 +0530
      Finished:     Mon, 07 Jun 2021 12:28:26 +0530
    SecretName:  default-token-bxrvs
Name:         preinstalljob-hook-3-g75v2
Annotations:  cni.projectcalico.org/podIP: 10.1.224.40/32
      Started:      Mon, 07 Jun 2021 12:28:28 +0530
      Finished:     Mon, 07 Jun 2021 12:28:33 +0530
    SecretName:  default-token-bxrvs
Name:         hookweightdemo-hooktest-749686bbfd-zwflx
Annotations:  cni.projectcalico.org/podIP: 10.1.224.61/32
      Started:      Mon, 07 Jun 2021 12:28:35 +0530
    SecretName:  hookweightdemo-hooktest-token-kq7wh

If you observe very carefully Hook 1 started in 19th second and got over in 21st second. Hook 2 started afterwards that is 23 and got over at 26. Then hook 3 got started and it started at 28 and got over at 33rd second.

So that means the weight-age with less number going to get executed first and the one with the weight-age of high number will get executed later.

This is a good example on how to use the weightage when I do have multiple hooks so that the system will not have confusion on which hook to pick when it needs to execute multiple hooks for the same stage.

So in a quick summary we discussed about how to use the hook weightage when we do have multiple hooks for the same stage.

Hook Execution using Weight
Scroll to top