Skip to content

Add hooks:pre_stop_k8s_container_handler syntax for CI config

What does this MR do and why?

Related issue: gitlab-runner!4055 (closed)

👋 This MR is the first one of a little series to add hooks:pre_stop_k8s_container_handler syntax to the CI Config.

This hook will only be used by a Kubernetes executor type runner.

It is already possible to configure this hook inside the configuration of the runner (Kubernetes executor). See documentation.

The goal of those PRs is to make it possible to define those hook per jobs instead of a static configuration inside the runner. The job's hook should override the runner's.

There are 3 types of handlers possibly usable (only one of them can be used):

  • Exec handler
  • TCP Socket handler
  • HTTP Get handler

Exemple of usage:

  • Terragrunt job crashed, pre_stop_k8s_container_handler will make sure to remove any locks without the need to have another job.
  • Job connected itself to a DB or any external system, pre_stop_k8s_container_handler can make sure to gracefully close connections.
  • etc...

New syntax

Exec Handler:

job:
  hooks:
    pre_get_sources_script:
      - echo 'hello job pre_get_sources'
    pre_stop_k8s_container_handler:
      exec:
        - echo 'goodbye container !'
  script: echo 'hello job script'

TCP Socket Handler:

job:
  hooks:
    pre_get_sources_script:
      - echo 'hello job pre_get_sources'
    pre_stop_k8s_container_handler:
      tcp_socket:
        port: 8080
        host: "127.0.0.1"
  script: echo 'hello job script'

HTTP GET Handler:

job:
  hooks:
    pre_get_sources_script:
      - echo 'hello job pre_get_sources'
    pre_stop_k8s_container_handler:
      http_get:
        port: 8080
        host: "127.0.0.1"
        path: "/var/www/html"
        scheme: "http"
        http_headers:
          - name: "Connection"
            value: "Keep-Alive"
          - name: "Keep-Alive"
            value: "timeout=5, max=1000"
  script: echo 'hello job script'

Implementation table

Runner counterpart: gitlab-runner!4055 (closed) (To update after step 3.)

Step Status
1. Add the hook inside hooks <-- This MR
2. Create merge strategy to have inheritance per hook TODO
3. Add to JobRequest::Response for Runner. TODO
4. Add documentation and CI schema TODO

MR acceptance checklist

This checklist encourages us to confirm any changes have been analysed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Baptiste Lalanne

Merge request reports

Loading