Skip to content
This repository has been archived by the owner on Mar 31, 2023. It is now read-only.

Sleep before context cancellation #37

Merged
merged 7 commits into from
Jan 20, 2020
Merged

Conversation

tapih
Copy link

@tapih tapih commented Jan 17, 2020

To shutdown a Kubernetes Pod gracefully, the container process needs to continue accepting connections until the Pod gets removed from the service Endpoints by kube-controller-manager.

This is typically done by /bin/sleep in preStop hook, but doing so is tedious and requires /bin/sleep in the root filesystem.

This PR removes the necessity of preStop hook and /bin/sleep with the following changes:

  • The signal handler has been changed to sleep some seconds(default: 5s) after receiving SIGTERM to make containers shutdown gracefully.
  • Graceful restart servers inherit environment variables to child processes.

@tapih tapih self-assigned this Jan 17, 2020
@tapih tapih changed the title Add CANCELLATION_DELAY_SECONDS to README Sleep before context cancellation Jan 17, 2020
signal.go Outdated
delay, err := strconv.Atoi(delayStr)
if err != nil {
log.Warn("well: set default cancellation delay seconds", map[string]interface{}{
"env": delayStr,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should err be added?

    log.FnError: err

signal.go Outdated
@@ -28,6 +34,23 @@ func handleSignal(env *Environment) {
log.Warn("well: got signal", map[string]interface{}{
"signal": s.String(),
})
delayStr := os.Getenv(cancellationDelaySecondsEnv)
delay, err := strconv.Atoi(delayStr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done only when if len(delayStr) > 0.

@ymmt2005
Copy link
Member

@tapih Please elaborate the message body of this pull request.

signal.go Outdated

func getDelaySecondsFromEnv() int {
delayStr := os.Getenv(cancellationDelaySecondsEnv)
if len(delayStr) <= 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len never returns < 0, so this should be ==.

signal.go Outdated
log.Warn("well: round up negative cancellation delay seconds to 0s", map[string]interface{}{
"env": delayStr,
"delay": 0,
log.FnError: err,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, err is nil, so do not add it.

@tapih tapih requested a review from ymmt2005 January 20, 2020 05:23
Copy link
Member

@ymmt2005 ymmt2005 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ymmt2005 ymmt2005 merged commit d3007a9 into master Jan 20, 2020
@ymmt2005 ymmt2005 deleted the sleep-after-receive-signal branch January 20, 2020 05:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants