Skip to content

Commit

Permalink
Merge pull request openshift#25190 from sttts/sttts-early-kill-lock-file
Browse files Browse the repository at this point in the history
watch-termination: create lock file on launch to catch every hard termination
  • Loading branch information
openshift-merge-robot authored Jun 23, 2020
2 parents c8a9b82 + bf741a8 commit 8871b3d
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions cmd/watch-termination/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
)

func main() {
os.Exit(run())
}

func run() int {
terminationLog := flag.String("termination-log-file", "", "Write logs after SIGTERM to this file (in addition to stderr)")
terminationLock := flag.String("termination-touch-file", "", "Touch this file on SIGTERM and delete on termination")

Expand All @@ -33,7 +37,7 @@ func main() {

if len(args) == 0 {
fmt.Println("Missing command line")
os.Exit(1)
return 1
}

// use special tee-like writer when termination log is set
Expand All @@ -53,6 +57,20 @@ func main() {
klog.SetOutputBySeverity("INFO", stderr)
}

// touch file early. If the file is not removed on termination, we are not
// terminating cleanly via SIGTERM.
if len(*terminationLock) > 0 {
klog.Infof("Touching termination lock file %q", *terminationLock)
if err := touch(*terminationLock); err != nil {
klog.Infof("error touching %s: %v", *terminationLock, err)
// keep going
}
defer func() {
klog.Infof("Deleting termination lock file %q", *terminationLock)
os.Remove(*terminationLock)
}()
}

cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = stderr
Expand All @@ -73,14 +91,6 @@ func main() {

klog.Infof("Received signal %s. Forwarding to sub-process %q.", s, args[0])

if len(*terminationLock) > 0 {
klog.Infof("Touching termination lock file %q", *terminationLock)
if err := touch(*terminationLock); err != nil {
klog.Infof("error touching %s: %v", *terminationLock, err)
// keep going
}
}

cmd.Process.Signal(s)
}
}()
Expand All @@ -92,7 +102,7 @@ func main() {
rc = exitError.ExitCode()
} else {
klog.Infof("Failed to launch %s: %v", args[0], err)
os.Exit(255)
return 255
}
}

Expand All @@ -101,13 +111,8 @@ func main() {
close(sigCh)
wg.Wait()

if len(*terminationLock) > 0 {
klog.Infof("Deleting termination lock file %q", *terminationLock)
os.Remove(*terminationLock)
}

klog.Infof("Termination finished with exit code %d", rc)
os.Exit(rc)
return rc
}

// terminationFileWriter forwards everything to the embedded writer. When
Expand Down

0 comments on commit 8871b3d

Please sign in to comment.