helm-charts icon indicating copy to clipboard operation
helm-charts copied to clipboard

lifecycle hook to wait until all terraform processes are completed

Open nitrocode opened this issue 2 years ago • 5 comments

Lifecycle hooks are supported but it would be nice to add

  • either a default lifecycle to prevent terraform from being killed in the middle of an apply or plan
    • otherwise we could have resources created and the information is not saved into the tfstate
    • otherwise we could have stale dynamodb locks
  • or document the lifecycle hook to be optionally added

cc: @Balsir since you added the lifecycle feature to this repo, have you figured out a good preStop lifecycle ?

References

  • https://github.com/runatlantis/helm-charts/pull/220
  • https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/

nitrocode avatar Jan 27 '23 15:01 nitrocode

hi @nitrocode, unfortunately haven't had the time for it yet.

The initial idea was to trap SIGTERM via preStop and wait for a configurable amount of time to attempt to let Terraform finish. Atlantis could then proceed with graceful termination (https://github.com/runatlantis/atlantis/pull/1051).

A little food for thought: since the server already handles graceful termination to an extent, wouldn't it be better to implement the use case on the server rather than in the Helm chart?

Balsir avatar Jan 30 '23 17:01 Balsir

Yes, graceful termination should be implemented in the server. I was thinking this lifecycle method could be a nice workaround for now. :smile:

nitrocode avatar Jan 31 '23 02:01 nitrocode

@nitrocode Should we close this issue and create one on the Atlantis repo?

GenPage avatar Jan 31 '23 16:01 GenPage

I think we can have both issues. One here for lifecycle method and one for graceful termination in the server.

nitrocode avatar Jan 31 '23 19:01 nitrocode

This is what I came up with, let me know if seems okay and I will create a PR.

statefulSet:
  terminationGracePeriodSeconds: 180

lifecycle:
  preStop:
    exec:
      command:
        - /bin/bash
        - -c
        - while [ "$(pgrep -f terraform)" ]; do sleep 1; done

mubarak-j avatar Aug 22 '23 23:08 mubarak-j