kuberneteskubernetes-jobs

Kubernetes 1.28 Jobs sidecar events


I want to use the new "sidecar" feature to monitor a container started in a job. The goal is to monitor the main container stdout and react to its completion.

For the last part I want to use something like a hook to send a event to the sidecar when the main container have completed (send an email with the logs, move some files, etc.).

Do you guys have some ideas to do that?

My idea was to do something like:

apiVersion: batch/v1
kind: Job
metadata:
  name: job-wrapper
spec:
  template:
    spec:
      initContainers:
        - name: wrapper
          image: busybox
          restartPolicy: Always
          command:
           - "/bin/sh"
           - "-c"
           - |
             while true; do
               [ -e "start" ] && echo "started" || ([ -e "stop" ] && echo "stopped" || echo "init")
               sleep 1
             done
          lifecycle:
            postStart:
              exec:
                command: [ "sh", "-c", "echo Do something before main container && sleep 10 && touch start"]
            preStop:
              exec:
                command: [ "sh", "-c", "echo Do something after main container && rm start && touch stop && sleep 10"]
          startupProbe:
             exec:
               command: [ "sh", "-c", "test -f start"]
      containers:
      - name: application
        image: busybox
        command: ["sh", "-c", "echo Processing && sleep 10 && echo Processing end!"]

I can launch the job on a k8s 1.28 on RedHat9 by adding the featureGate on each k8s system pod, but I'm still failling because of Kubelet that doesn't recognize the restartPolicy of the initCOntainer. I don't find a way to activate the feature gate on kubelet side.


Solution

  • It does work natively on 1.29 because the sidecar feature gates in on by default.