Skip to content

Support of kubernetes lifecycle hooks

Romuald Atchadé requested to merge prestop-k8s-lifecycle-hook into main

What does this MR do?

Add support of kubernetes lifecycle hook

  • preStop
  • postStart

Why was this MR needed?

This MR is needed to allow execution of script at the start of the pod or before the stop of the pod

What's the best way to test this MR?

.gitlab-ci.yml
job:
  script:
  - ls -l /builds

Invalid hook configuration

config.toml
[[runners]]
  name = "kubernetes"
  url = "https://gitlab.com/"
  token = "TOKEN_HERE"
  executor = "kubernetes"
  [runners.kubernetes]
    image = "alpine:3.11"
    privileged = true
    namespace = "default"
    [runners.kubernetes.container_lifecycle.pre_stop.exec]
      command = []
    [runners.kubernetes.container_lifecycle.pre_stop.tcp_socket]
      port = 8080
      host = "localhost"
  1. Run a job
  2. The job fails with the following error
ERROR: Job failed (system failure): prepare environment: setting up build pod: Pod "runner-lr33aybb-project-24422682-concurrent-0dw4b6" is invalid: [spec.containers[0].lifecycle.preStop.exec.command: Required value, spec.containers[0].lifecycle.preStop.tcpSocket: Forbidden: may not specify more than 1 handler type, spec.containers[1].lifecycle.preStop.exec.command: Required value, spec.containers[1].lifecycle.preStop.tcpSocket: Forbidden: may not specify more than 1 handler type]. Check https://docs.gitlab.com/runner/shells/index.html#shell-profile-loading for more information
Log File

Log

Screen_Shot_2021-07-28_at_1.31.52_PM

Test postStart hook

config.toml
[[runners]]
  name = "kubernetes"
  url = "https://gitlab.com/"
  token = "TOKEN_HERE"
  executor = "kubernetes"
  [runners.kubernetes]
    image = "alpine:3.11"
    privileged = true
    namespace = "default"
    [runners.kubernetes.container_lifecycle.post_start.exec]
      command = ["touch", "/builds/postStart.txt"]
  1. Run a job
  2. The job succeeds and the file postStart.txt is listed as inside the /builds repository
Log File

Log

Screen_Shot_2021-07-28_at_1.34.08_PM

What are the relevant issue numbers?

closes: #3630 (closed)

Edited by Romuald Atchadé

Merge request reports