Files
node-problem-detector/cmd/nodeproblemdetector/node_problem_detector_unix.go
T
Andrew Halaney 31d036e27f fix: handle SIGTERM gracefully on unix
We weren't catching SIGTERM and cancelling our context, so the go
runtime would have us exit with a non-zero code when the signal was
received. Let's catch it and try to exit gracefully.

i.e. before:

    (...)
    I0508 11:08:00.426917 1787442 log_monitor.go:237] Initialize condition generated: []
    ^C
    $ echo $?
    130

and now:

    (...)
    I0508 11:07:42.150562 1787146 log_monitor.go:237] Initialize condition generated: []
    ^C
    I0508 11:07:43.372093 1787146 log_monitor.go:123] Stop log monitor /tmp/npd-test.json
    I0508 11:07:43.652876 1787146 log_watcher.go:104] Stop watching filelog
    I0508 11:07:43.652923 1787146 log_monitor.go:144] Log monitor stopped: /tmp/npd-test.json
    $ echo $?
    0
2026-05-08 11:14:38 -05:00

66 lines
1.9 KiB
Go

//go:build unix
/*
Copyright 2021 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"flag"
"os"
"os/signal"
"syscall"
"github.com/spf13/pflag"
"k8s.io/klog/v2"
"k8s.io/node-problem-detector/cmd/options"
)
func main() {
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
klog.InitFlags(klogFlags)
// Opt into fixed stderrthreshold behavior (kubernetes/klog#212).
_ = klogFlags.Set("legacy_stderr_threshold_behavior", "false")
_ = klogFlags.Set("stderrthreshold", "INFO")
klogFlags.VisitAll(func(f *flag.Flag) {
switch f.Name {
case "v", "vmodule", "logtostderr":
flag.CommandLine.Var(f.Value, f.Name, f.Usage)
}
})
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
if err := pflag.CommandLine.MarkHidden("vmodule"); err != nil {
klog.Fatalf("Failed to mark hidden flag 'vmodule': %v", err)
}
if err := pflag.CommandLine.MarkHidden("logtostderr"); err != nil {
klog.Fatalf("Failed to mark hidden flag 'logtostderr': %v", err)
}
npdo := options.NewNodeProblemDetectorOptions()
if err := npdo.AddFlags(pflag.CommandLine); err != nil {
klog.Fatalf("Failed to add flags: %v", err)
}
pflag.Parse()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := npdMain(ctx, npdo); err != nil {
klog.Fatalf("Problem detector failed with error: %v", err)
}
}