Skip to content

Commit

Permalink
Merge pull request openshift#25073 from sttts/sttts-verbose-termination
Browse files Browse the repository at this point in the history
apiserver: verbosity 3 on termination and watch-termination binary
  • Loading branch information
openshift-merge-robot authored Jun 9, 2020
2 parents 8e763d5 + 5772e72 commit eda6943
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 22 deletions.
140 changes: 140 additions & 0 deletions cmd/watch-termination/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package main

import (
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"sync"
"syscall"
"time"
)

func main() {
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")

flag.Parse()
args := flag.CommandLine.Args()

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

// use special tee-like writer when termination log is set
termCh := make(chan struct{})
var stderr io.Writer = os.Stderr
if len(*terminationLog) > 0 {
stderr = &terminationFileWriter{
Writer: os.Stderr,
fn: *terminationLog,
startFileLoggingCh: termCh,
}
}

cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = stderr

// forward SIGTERM and SIGINT to child
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for s := range sigCh {
fmt.Fprintf(stderr, "Received signal %s\n", s)

if len(*terminationLock) > 0 {
fmt.Fprintln(stderr, "Touching", *terminationLock)
if err := touch(*terminationLock); err != nil {
fmt.Fprintln(stderr, fmt.Errorf("error touching %s: %v", *terminationLock, err))
// keep going
}
}

select {
case <-termCh:
default:
close(termCh)
}
cmd.Process.Signal(s)
}
}()

fmt.Printf("Launching %v\n", cmd)
rc := 0
if err := cmd.Run(); err != nil {
if exitError, ok := err.(*exec.ExitError); !ok {
rc = exitError.ExitCode()
} else {
fmt.Fprintf(stderr, "Failed to launch %s: %v\n", args[0], err)
os.Exit(255)
}
}

// remove signal handling
signal.Stop(sigCh)
close(sigCh)
wg.Wait()

if len(*terminationLock) > 0 {
os.Remove(*terminationLock)
}

fmt.Fprintf(stderr, "Exit code %d\n", rc)
os.Exit(rc)
}

// terminationFileWriter forwards everything to the embedded writer. When
// startFileLoggingCh is closed, everything is appended to the given file name
// in addition.
type terminationFileWriter struct {
io.Writer
fn string
startFileLoggingCh <-chan struct{}

f *os.File
}

func (w *terminationFileWriter) Write(bs []byte) (int, error) {
select {
case <-w.startFileLoggingCh:
if w.f == nil {
f, err := os.OpenFile(w.fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
w.f = f
fmt.Println("Starting logging to", w.fn)
}
if n, err := w.f.Write(bs); err != nil {
return n, err
} else if n != len(bs) {
return n, io.ErrShortWrite
}
default:
}

return w.Writer.Write(bs)
}

func touch(fn string) error {
_, err := os.Stat(fn)
if os.IsNotExist(err) {
file, err := os.Create(fn)
if err != nil {
return err
}
defer file.Close()
return nil
}

currentTime := time.Now().Local()
return os.Chtimes(fn, currentTime, currentTime)
}
4 changes: 2 additions & 2 deletions images/hyperkube/Dockerfile.rhel
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ FROM registry.svc.ci.openshift.org/ocp/builder:golang-1.13 AS builder
WORKDIR /go/src/github.com/openshift/origin
COPY . .
RUN for p in vendor/k8s.io/kubernetes/cmd/kube-apiserver vendor/k8s.io/kubernetes/cmd/kube-controller-manager \
vendor/k8s.io/kubernetes/cmd/kube-scheduler vendor/k8s.io/kubernetes/cmd/kubelet; do make build WHAT=$p; done && \
vendor/k8s.io/kubernetes/cmd/kube-scheduler vendor/k8s.io/kubernetes/cmd/kubelet cmd/watch-termination; do make build WHAT=$p; done && \
mkdir -p /tmp/build && \
cp images/hyperkube/hyperkube /tmp/build && \
cp /go/src/github.com/openshift/origin/_output/local/bin/linux/$(go env GOARCH)/{kube-apiserver,kube-controller-manager,kube-scheduler,kubelet} \
cp /go/src/github.com/openshift/origin/_output/local/bin/linux/$(go env GOARCH)/{kube-apiserver,kube-controller-manager,kube-scheduler,kubelet,watch-termination} \
/tmp/build

FROM registry.svc.ci.openshift.org/ocp/4.2:base
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions vendor/k8s.io/kubernetes/cmd/kube-apiserver/app/server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/k8s.io/kubernetes/cmd/kube-scheduler/app/server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/k8s.io/kubernetes/pkg/kubelet/server/server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eda6943

Please sign in to comment.